Skip to content

Commit

Permalink
Merge ca65990 into 8623b8a
Browse files Browse the repository at this point in the history
  • Loading branch information
Felipe444 committed Jul 26, 2021
2 parents 8623b8a + ca65990 commit 109bbd8
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ public Collection<EnvironmentValidatorResult> validateServiceAgainstEnvironments
ServiceContracts validatedServiceContracts = this.serviceContractsRepository.findOne(serviceId)
.orElseThrow(ResourceNotFoundException::new);



return this.validators.stream()
.map(validator ->
validateServiceAgainstEnvironments(
Expand Down Expand Up @@ -149,8 +147,9 @@ private <C, E> Map<ServiceId, EnvironmentValidatorResult> validatedServicesAgain
}

private Set<ServiceId> getServicesIds(String environmentName) {
return this.environmentRepository.find(environmentName)
.map(Environment::getAllServices)
.orElse(new HashSet<>());
var environment = this.environmentRepository.find(environmentName)
.orElseThrow(ResourceNotFoundException::new);

return environment.getAllServices();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
import io.swagger.annotations.ApiResponses;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

Expand Down Expand Up @@ -44,11 +44,13 @@ public Set<String> getEnvironmentNames() {
@ApiOperation(value = "Get services from the environment", nickname = "Get Services")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Success", response = ServiceDto.class, responseContainer = "list"),
@ApiResponse(code = 404, message = "Not found"),
@ApiResponse(code = 500, message = "Failure")})
public List<ServiceDto> getEnvironment(@PathVariable("name") String name) {
public ResponseEntity<List<ServiceDto>> getEnvironment(@PathVariable("name") String name) {
return environmentRepository.find(name)
.map(this::getServices)
.orElse(new ArrayList<>());
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}

private List<ServiceDto> getServices(Environment env) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.hltech.judged.server.domain.ServiceId;
import com.hltech.judged.server.domain.environment.Environment;
import com.hltech.judged.server.interfaces.rest.ResourceNotFoundException;
import com.hltech.judged.server.interfaces.rest.contracts.ServiceContractsDto;
import com.hltech.judged.server.domain.contracts.ServiceContracts;
import com.hltech.judged.server.domain.contracts.ServiceContractsRepository;
Expand All @@ -17,7 +18,6 @@
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

Expand All @@ -33,9 +33,10 @@ public class InterrelationshipController {
@ApiOperation(value = "Get interrelationship between services in given environment", nickname = "Validate against environment")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Success", response = InterrelationshipDto.class),
@ApiResponse(code = 404, message = "Not found"),
@ApiResponse(code = 500, message = "Failure")
})
public InterrelationshipDto getInterrelationship (@PathVariable("environment") String env) {
public InterrelationshipDto getInterrelationship(@PathVariable("environment") String env) {
Set<ServiceContractsDto> serviceContractsSet = getServicesIds(env).stream()
.map(this::getServiceContracts)
.map(ServiceContractsDto::fromDomain)
Expand All @@ -56,6 +57,6 @@ private ServiceContracts getServiceContracts(ServiceId serviceId) {
private Set<ServiceId> getServicesIds(String environmentName) {
return this.environmentRepository.find(environmentName)
.map(Environment::getAllServices)
.orElse(new HashSet<>());
.orElseThrow(ResourceNotFoundException::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class ValidationController {
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Success", response = BatchValidationReportDto.class, responseContainer = "list"),
@ApiResponse(code = 500, message = "Failure"),
@ApiResponse(code = 404, message = "Service not found")
@ApiResponse(code = 404, message = "Service or environment not found")
})
public List<BatchValidationReportDto> validateAgainstEnvironments(
@RequestParam("services") List<String> services,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ class InterrelationshipControllerFT extends PostgresDatabaseSpecification {
.when()
.get("/interrelationship/NOT_EXISTS")
.then()
.statusCode(200)
.contentType("application/json")
.extract().body().jsonPath().getMap('$')
.statusCode(404)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import com.fasterxml.jackson.databind.ObjectMapper
import com.hltech.judged.server.config.BeanFactory
import com.hltech.judged.server.domain.JudgeDApplicationService
import com.hltech.judged.server.domain.contracts.InMemoryServiceContractsRepository
import com.hltech.judged.server.domain.environment.Environment
import com.hltech.judged.server.domain.environment.EnvironmentRepository
import com.hltech.judged.server.domain.environment.InMemoryEnvironmentRepository
import com.hltech.judged.server.domain.environment.Space
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.boot.test.context.TestConfiguration
Expand All @@ -23,13 +25,24 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
@ActiveProfiles("test")
class EnvironmentControllerIT extends Specification {

@Autowired
InMemoryEnvironmentRepository environmentRepository

@Autowired
ObjectMapper objectMapper

@Autowired
MockMvc mockMvc

def "getServices test hits the URL and parses JSON output"() {
def cleanup() {
environmentRepository.storage.clear()
}

def "get on not existing environment should end up with status 200"() {
given:
def environment = new Environment('SIT', new HashSet<Space>())
environmentRepository.persist(environment)

when: 'rest validatePacts url is hit'
def response = mockMvc.perform(
get('/environments/SIT')
Expand All @@ -54,6 +67,16 @@ class EnvironmentControllerIT extends Specification {
response.status == 200
}

def 'get on not existing environment should end up with status 404'() {
when:
def response = mockMvc.perform(
get('/environments/unknown')
.accept("application/json")
).andReturn().getResponse()
then:
response.status == 404
}

private ServiceForm randomServiceForm() {
new ServiceForm()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ class InterrelationshipControllerIT extends Specification {
.andExpect(MockMvcResultMatchers.jsonPath('$.serviceContracts[0].publicationTime').exists())
}

def "should return 404 when getting interrelationship for not existing environment"() {
expect: 'rest validatePacts url is hit'
def response = mockMvc.perform(
get(new URI('/interrelationship/unknown'))
.accept("application/json")
)
.andExpect(status().isNotFound())
}

def createServiceContracts(def name, def version) {
new ServiceContracts(
new ServiceId(name, version),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import com.hltech.judged.server.domain.contracts.Expectation
import com.hltech.judged.server.domain.contracts.InMemoryServiceContractsRepository
import com.hltech.judged.server.domain.contracts.ServiceContracts
import com.hltech.judged.server.domain.contracts.ServiceContractsRepository
import com.hltech.judged.server.domain.environment.Environment
import com.hltech.judged.server.domain.environment.EnvironmentRepository
import com.hltech.judged.server.domain.environment.InMemoryEnvironmentRepository
import com.hltech.judged.server.domain.environment.Space
import com.hltech.judged.server.domain.validation.InterfaceContractValidator
import com.hltech.judged.server.domain.validation.ping.PingContractValidator
import com.hltech.judged.server.interfaces.rest.environment.ServiceDto
Expand All @@ -35,18 +37,48 @@ class ValidationControllerIT extends Specification {
ObjectMapper objectMapper

@Autowired
ServiceContractsRepository serviceContractsRepository
InMemoryServiceContractsRepository serviceContractsRepository

@Autowired
InMemoryEnvironmentRepository environmentRepository

@Autowired
MockMvc mockMvc

def cleanup() {
environmentRepository.storage.clear()
serviceContractsRepository.storage.clear()
}

def "should return 200 when validate given all went fine"() {
given:
def serviceId = new ServiceId('service-name', '1.0')
def serviceContracts = new ServiceContracts(serviceId, [], [])
serviceContractsRepository.persist(serviceContracts)
and:
environmentRepository.persist(new Environment('SIT', [new Space(Environment.DEFAULT_NAMESPACE, [serviceId] as Set)] as Set))
environmentRepository.persist(new Environment('UAT', [new Space(Environment.DEFAULT_NAMESPACE, [serviceId] as Set)] as Set))
when: 'rest validatePacts url is hit'
def response = mockMvc.perform(
get(new URI('/environment-compatibility-report/service-name:1.0?environment=SIT&environment=UAT'))
.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<List<ServiceDto>>() {}) != null
}

def "should return 404 when validate service contracts against env given contracts have not been registered"() {
given:
serviceContractsRepository.persist(new ServiceContracts(
new ServiceId('service-name', '1.0'),
[],
[]
))
and:
environmentRepository.persist(new Environment('SIT', [new Space(Environment.DEFAULT_NAMESPACE, [] as Set)] as Set))
environmentRepository.persist(new Environment('UAT', [new Space(Environment.DEFAULT_NAMESPACE, [] as Set)] as Set))
when: 'rest validatePacts url is hit'
def response = mockMvc.perform(
get(new URI('/environment-compatibility-report/service-name:1.0?environment=SIT&environment=UAT'))
Expand All @@ -58,7 +90,13 @@ class ValidationControllerIT extends Specification {
objectMapper.readValue(response.getContentAsString(), new TypeReference<List<ServiceDto>>() {}) != null
}

def "should return 404 when validate service contracts against env given contracts have not been registered"() {
def "should return 404 when validate service contracts against not existing environment"() {
given:
serviceContractsRepository.persist(new ServiceContracts(
new ServiceId('service-name', '1.0'),
[],
[]
))
when: 'rest validatePacts url is hit'
def response = mockMvc.perform(
get(new URI('/environment-compatibility-report/other-service:1.0?environment=SIT&environment=UAT'))
Expand Down Expand Up @@ -90,6 +128,9 @@ class ValidationControllerIT extends Specification {
[],
[new Expectation('provider', 'ping', new Contract('123456', MediaType.APPLICATION_JSON_VALUE))]
))
and:
environmentRepository.persist(new Environment('SIT', [new Space(Environment.DEFAULT_NAMESPACE, [] as Set)] as Set))
environmentRepository.persist(new Environment('UAT', [new Space(Environment.DEFAULT_NAMESPACE, [] as Set)] as Set))
when: 'rest validatePacts url is hit'
def response = mockMvc.perform(
get(new URI('/environment-compatibility-report?services=provider:2.0&services=consumer:2.0&environment=SIT'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ INSERT INTO "public".service_versions (name, version, space, environment_name) V
INSERT INTO "public".service_versions (name, version, space, environment_name) VALUES ('test-service-3', '1.0', 'default' ,'TEST2');
INSERT INTO "public".environments (name) VALUES ('TEST1');
INSERT INTO "public".environments (name) VALUES ('TEST2');
INSERT INTO "public".environments (name) VALUES ('TEST3');
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ class EnvironmentControllerUT extends Specification {

def 'should return empty list of services when no such environment was saved before'() {
when:
def services = environmentController.getEnvironment("some-environment")
def services = environmentController.getEnvironment("some-environment").getBody()
then:
services.isEmpty()
!services
}

def 'should return list of services from and environment given it was saved before'() {
Expand All @@ -44,7 +44,8 @@ class EnvironmentControllerUT extends Specification {
)
environmentRepository.persist(environment)
when:
def services = environmentController.getEnvironment(environment.name)
def services = environmentController.getEnvironment(environment.name).getBody()

then:
services == [new ServiceDto(service.name, service.version)] as List
}
Expand Down

0 comments on commit 109bbd8

Please sign in to comment.