Skip to content

Commit

Permalink
Interrelationship endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Felipe444 committed Aug 14, 2019
1 parent 3ca348e commit 43e12c0
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private Map<String, ServiceContracts.Contract> mapContractsForm(Map<String, Serv
));
}

ServiceContractsDto toDto(ServiceContracts serviceContracts) {
public ServiceContractsDto toDto(ServiceContracts serviceContracts) {
return new ServiceContractsDto(
serviceContracts.getName(),
serviceContracts.getVersion(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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.EnvironmentRepository;
import dev.hltech.dredd.interfaces.rest.contracts.ContractsMapper;
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<ServiceContracts> serviceContractsSet = environmentRepository.get(env).getAllServices().stream()
.map(service ->
serviceContractsRepository.find(service.getName(), service.getVersion()).orElseGet(
() -> new ServiceContracts(service.getName(), service.getVersion(), new HashMap<>(), new HashMap<>())))
.collect(Collectors.toSet());

return new InterrelationshipDto(env, serviceContractsSet.stream()
.map(contractsMapper::toDto)
.collect(Collectors.toSet()));
}
}
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;
}
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()
}
}
}
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')])
}
}

0 comments on commit 43e12c0

Please sign in to comment.