-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
197 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
.../java/dev/hltech/dredd/interfaces/rest/interrelationship/InterrelationshipController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package dev.hltech.dredd.interfaces.rest.interrelationship; | ||
|
||
import dev.hltech.dredd.domain.contracts.ServiceContracts; | ||
import dev.hltech.dredd.domain.contracts.ServiceContractsRepository; | ||
import dev.hltech.dredd.domain.environment.EnvironmentAggregate; | ||
import dev.hltech.dredd.domain.environment.EnvironmentRepository; | ||
import dev.hltech.dredd.interfaces.rest.contracts.ContractsMapper; | ||
import dev.hltech.dredd.interfaces.rest.contracts.ServiceContractsDto; | ||
import io.swagger.annotations.ApiOperation; | ||
import io.swagger.annotations.ApiResponse; | ||
import io.swagger.annotations.ApiResponses; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.HashMap; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
@RestController | ||
public class InterrelationshipController { | ||
|
||
private final EnvironmentRepository environmentRepository; | ||
private final ServiceContractsRepository serviceContractsRepository; | ||
private final ContractsMapper contractsMapper; | ||
|
||
@Autowired | ||
public InterrelationshipController(EnvironmentRepository environmentRepository, | ||
ServiceContractsRepository serviceContractsRepository, | ||
ContractsMapper contractsMapper) { | ||
this.environmentRepository = environmentRepository; | ||
this.serviceContractsRepository = serviceContractsRepository; | ||
this.contractsMapper = contractsMapper; | ||
} | ||
|
||
@GetMapping(value = "/interrelationship/{environment}", produces = MediaType.APPLICATION_JSON_VALUE) | ||
@ApiOperation(value = "Get interrelationship between services in given environment", nickname = "Validate against environment") | ||
@ApiResponses(value = { | ||
@ApiResponse(code = 200, message = "Success", response = String.class, responseContainer = "list"), | ||
@ApiResponse(code = 500, message = "Failure") | ||
}) | ||
public InterrelationshipDto getInterrelationship (@PathVariable("environment") String env) { | ||
|
||
Set<ServiceContractsDto> serviceContractsSet = environmentRepository.get(env).getAllServices().stream() | ||
.map(this::getServiceContracts) | ||
.map(contractsMapper::toDto) | ||
.collect(Collectors.toSet()); | ||
|
||
return new InterrelationshipDto(env, serviceContractsSet); | ||
} | ||
|
||
private ServiceContracts getServiceContracts(EnvironmentAggregate.ServiceVersion service) { | ||
return serviceContractsRepository.find(service.getName(), service.getVersion()) | ||
.orElseGet(() -> | ||
new ServiceContracts(service.getName(), service.getVersion(), new HashMap<>(), new HashMap<>())); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...rc/main/java/dev/hltech/dredd/interfaces/rest/interrelationship/InterrelationshipDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package dev.hltech.dredd.interfaces.rest.interrelationship; | ||
|
||
import dev.hltech.dredd.interfaces.rest.contracts.ServiceContractsDto; | ||
import lombok.Data; | ||
|
||
import java.util.Set; | ||
|
||
@Data | ||
public class InterrelationshipDto { | ||
|
||
private final String environment; | ||
private final Set<ServiceContractsDto> serviceContracts; | ||
} |
65 changes: 65 additions & 0 deletions
65
...y/dev/hltech/dredd/interfaces/rest/interrelationship/InterrelationshipControllerIT.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package dev.hltech.dredd.interfaces.rest.interrelationship | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import dev.hltech.dredd.config.BeanFactory | ||
import dev.hltech.dredd.domain.contracts.InMemoryServiceContractsRepository | ||
import dev.hltech.dredd.domain.contracts.ServiceContractsRepository | ||
import dev.hltech.dredd.domain.environment.EnvironmentRepository | ||
import dev.hltech.dredd.domain.environment.InMemoryEnvironmentRepository | ||
import dev.hltech.dredd.interfaces.rest.contracts.ContractsMapper | ||
import dev.hltech.dredd.interfaces.rest.environment.ServiceDto | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest | ||
import org.springframework.boot.test.context.TestConfiguration | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.test.context.ActiveProfiles | ||
import org.springframework.test.web.servlet.MockMvc | ||
import spock.lang.Specification | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get | ||
|
||
@WebMvcTest(InterrelationshipController.class) | ||
@ActiveProfiles("test-integration") | ||
class InterrelationshipControllerIT extends Specification { | ||
|
||
@Autowired | ||
ObjectMapper objectMapper | ||
|
||
@Autowired | ||
EnvironmentRepository environmentRepository | ||
|
||
@Autowired | ||
MockMvc mockMvc | ||
|
||
def "should return 200 when getting interrelationship for any environment"() { | ||
when: 'rest validatePacts url is hit' | ||
def response = mockMvc.perform( | ||
get(new URI('/interrelationship/SIT')) | ||
.accept("application/json") | ||
).andReturn().getResponse() | ||
then: 'controller returns validation response in json' | ||
response.getStatus() == 200 | ||
response.getContentType().contains("application/json") | ||
objectMapper.readValue(response.getContentAsString(), new TypeReference<InterrelationshipDto>() {}) != null | ||
} | ||
|
||
@TestConfiguration | ||
static class TestConfig extends BeanFactory { | ||
|
||
@Bean | ||
EnvironmentRepository environmentRepository() { | ||
return new InMemoryEnvironmentRepository() | ||
} | ||
|
||
@Bean | ||
ServiceContractsRepository serviceContractsRepository() { | ||
return new InMemoryServiceContractsRepository() | ||
} | ||
|
||
@Bean | ||
ContractsMapper contractsMapper() { | ||
return new ContractsMapper() | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...y/dev/hltech/dredd/interfaces/rest/interrelationship/InterrelationshipControllerUT.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package dev.hltech.dredd.interfaces.rest.interrelationship | ||
|
||
import dev.hltech.dredd.domain.contracts.ServiceContracts | ||
import dev.hltech.dredd.domain.contracts.ServiceContractsRepository | ||
import dev.hltech.dredd.domain.environment.EnvironmentAggregate | ||
import dev.hltech.dredd.domain.environment.EnvironmentRepository | ||
import dev.hltech.dredd.interfaces.rest.contracts.ContractsMapper | ||
import dev.hltech.dredd.interfaces.rest.contracts.ServiceContractsDto | ||
import spock.lang.Specification | ||
import spock.lang.Subject | ||
|
||
class InterrelationshipControllerUT extends Specification { | ||
|
||
def environmentRepository = Mock(EnvironmentRepository) | ||
|
||
def serviceContractsRepository = Mock(ServiceContractsRepository) | ||
|
||
def contractsMapper = new ContractsMapper() | ||
|
||
@Subject | ||
def controller = new InterrelationshipController(environmentRepository, serviceContractsRepository, contractsMapper) | ||
|
||
def "should return 200 when getting interrelationship for any environment"() { | ||
given: | ||
def env = 'SIT' | ||
def services = [new EnvironmentAggregate.ServiceVersion('1', '1'), new EnvironmentAggregate.ServiceVersion('2', '2')] | ||
|
||
1 * environmentRepository.get(env) >> new EnvironmentAggregate('name': env, 'serviceVersions': services) | ||
1 * serviceContractsRepository.find('1', '1') >> Optional.of(createServiceContracts('1', '1')) | ||
1 * serviceContractsRepository.find('2', '2') >> Optional.of(createServiceContracts('2', '2')) | ||
|
||
when: | ||
def result = controller.getInterrelationship(env) | ||
|
||
then: | ||
result.getEnvironment() == env | ||
result.getServiceContracts().size() == 2 | ||
result.getServiceContracts().any({ it -> | ||
it.name == '1' | ||
it.version == '1' | ||
it.expectations == ['prov': ['rest': new ServiceContractsDto.ContractDto('contract-rest', 'json')]] | ||
it.capabilities == ['jms': new ServiceContractsDto.ContractDto('contract-jms', 'json')] | ||
}) | ||
result.getServiceContracts().any({ | ||
it.name == '2' | ||
it.version == '2' | ||
it.expectations == ['prov': ['rest': new ServiceContractsDto.ContractDto('contract-rest', 'json')]] | ||
it.capabilities == ['jms': new ServiceContractsDto.ContractDto('contract-jms', 'json')] | ||
}) | ||
} | ||
|
||
def createServiceContracts(def name, def version) { | ||
new ServiceContracts( | ||
'id': new ServiceContracts.ServiceContractsId(name, version), | ||
'capabilitiesPerProtocol': ['jms': new ServiceContracts.Contract('contract-jms', 'json')], | ||
'expectations': [(new ServiceContracts.ProviderProtocol('prov', 'rest')): new ServiceContracts.Contract('contract-rest', 'json')]) | ||
} | ||
} |