Skip to content

Commit

Permalink
Information about spaces in an environment exposed in REST endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Felipe444 committed Jul 29, 2021
1 parent ad83433 commit 0f89bca
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.hltech.judged.server.domain.environment.Environment;
import com.hltech.judged.server.domain.environment.EnvironmentRepository;
import com.hltech.judged.server.domain.ServiceId;
import com.hltech.judged.server.domain.environment.Space;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
Expand All @@ -17,11 +18,10 @@
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.hltech.judged.server.domain.environment.Environment.DEFAULT_NAMESPACE;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;

@RestController
Expand All @@ -43,22 +43,16 @@ public Set<String> getEnvironmentNames() {
@GetMapping(value = "environments/{name}", produces = MediaType.APPLICATION_JSON_VALUE)
@ApiOperation(value = "Get services from the environment", nickname = "Get Services")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Success", response = ServiceDto.class, responseContainer = "list"),
@ApiResponse(code = 200, message = "Success", response = EnvironmentDto.SpaceDto.ServiceDto.class, responseContainer = "list"),
@ApiResponse(code = 404, message = "Not found"),
@ApiResponse(code = 500, message = "Failure")})
public ResponseEntity<List<ServiceDto>> getEnvironment(@PathVariable("name") String name) {
public ResponseEntity<EnvironmentDto> getEnvironment(@PathVariable("name") String name) {
return environmentRepository.find(name)
.map(this::getServices)
.map(this::map)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}

private List<ServiceDto> getServices(Environment env) {
return env.getAllServices().stream()
.map(sv -> new ServiceDto(sv.getName(), sv.getVersion()))
.collect(toList());
}

@PutMapping(
value = "environments/{name}",
consumes = MediaType.APPLICATION_JSON_VALUE)
Expand All @@ -78,4 +72,29 @@ public void overwriteEnvironment(

judgeD.overwriteEnvironment(name, agentSpace, serviceIds);
}

private EnvironmentDto map(Environment env) {
return new EnvironmentDto(
env.getName(),
env.getSpaces().stream()
.map(this::map)
.collect(Collectors.toSet())
);
}

private EnvironmentDto.SpaceDto map(Space space) {
return new EnvironmentDto.SpaceDto(
space.getName(),
space.getServiceIds().stream()
.map(this::map)
.collect(Collectors.toSet())
);
}

private EnvironmentDto.SpaceDto.ServiceDto map(ServiceId service) {
return new EnvironmentDto.SpaceDto.ServiceDto(
service.getName(),
service.getVersion()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.hltech.judged.server.interfaces.rest.environment;

import lombok.Value;

import java.util.Set;

@Value
public class EnvironmentDto {
String name;
Set<SpaceDto> spaces;

@Value
public static class SpaceDto {
String name;
Set<ServiceDto> services;

@Value
public static class ServiceDto {
String name;
String version;
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.hltech.judged.server

import com.hltech.judged.server.interfaces.rest.environment.EnvironmentDto
import io.restassured.RestAssured
import org.springframework.boot.web.server.LocalServerPort
import org.springframework.test.context.jdbc.Sql
Expand Down Expand Up @@ -171,41 +172,61 @@ class EnvironmentControllerFT extends PostgresDatabaseSpecification {
given:
def environmentName = 'TEST1'
when:
def response = RestAssured.given()
def environment = RestAssured.given()
.port(serverPort)
.contentType("application/json")
.when()
.get("/environments/${environmentName}")
.then()
.statusCode(200)
.contentType("application/json")
.extract().body().jsonPath().getList('$')
.extract().body().jsonPath().getObject('$', EnvironmentDto)

then:
response.any{
environment.name == environmentName
environment.spaces.size() == 1
environment.spaces[0].name == 'default'
environment.spaces[0].services.size() == 2
environment.spaces[0].services.any{
it['name'] == "test-service-1" &&
it['version'] == "1.0"
}
response.any{
environment.spaces[0].services.any{
it['name'] == "test-service-2" &&
it['version'] == "2.0"
}
}

@Sql("EnvironmentControllerFT.sql")
def "should return empty list when there are no services for selected environment"() {
def "should return no spaces when there are no services for selected environment"() {
given:
def environmentName = 'TEST3'

when:
def response = RestAssured.given()
def environment = RestAssured.given()
.port(serverPort)
.contentType("application/json")
.when()
.get("/environments/TEST3")
.get("/environments/${environmentName}")
.then()
.statusCode(200)
.contentType("application/json")
.extract().body().jsonPath().getList('$')
.extract().body().jsonPath().getObject('$', EnvironmentDto)

then:
response.size() == 0
environment.name == environmentName
environment.spaces.size() == 0
}

@Sql("EnvironmentControllerFT.sql")
def "should return 404 status when there is no such environment as specified in the request"() {
expect:
def response = RestAssured.given()
.port(serverPort)
.contentType("application/json")
.when()
.get("/environments/UNKNOWN")
.then()
.statusCode(404)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.hltech.judged.server.interfaces.rest.environment

import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import com.hltech.judged.server.config.BeanFactory
import com.hltech.judged.server.domain.JudgeDApplicationService
Expand Down Expand Up @@ -38,7 +37,7 @@ class EnvironmentControllerIT extends Specification {
environmentRepository.storage.clear()
}

def "get on not existing environment should end up with status 200"() {
def "get on not existing environment should end up with status 200 and an environment without any space"() {
given:
def environment = new Environment('SIT', new HashSet<Space>())
environmentRepository.persist(environment)
Expand All @@ -51,7 +50,9 @@ class EnvironmentControllerIT extends Specification {
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 responseBody = objectMapper.readValue(response.getContentAsString(), EnvironmentDto)
responseBody.name == 'SIT'
responseBody.spaces.size() == 0
}

def 'update environment hits the url and receives 200'() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ 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
import com.hltech.judged.server.interfaces.rest.environment.EnvironmentDto

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.boot.test.context.TestConfiguration
Expand Down Expand Up @@ -66,7 +67,7 @@ class ValidationControllerIT extends Specification {
then: 'controller returns validation response in json'
response.getStatus() == 200
response.getContentType().contains("application/json")
objectMapper.readValue(response.getContentAsString(), new TypeReference<List<ServiceDto>>() {}) != null
objectMapper.readValue(response.getContentAsString(), new TypeReference<List<EnvironmentDto.SpaceDto.ServiceDto>>() {}) != null
}

def "should return 404 when validate service contracts against env given contracts have not been registered"() {
Expand All @@ -87,7 +88,7 @@ class ValidationControllerIT extends Specification {
then: 'controller returns validation response in json'
response.getStatus() == 200
response.getContentType().contains("application/json")
objectMapper.readValue(response.getContentAsString(), new TypeReference<List<ServiceDto>>() {}) != null
objectMapper.readValue(response.getContentAsString(), new TypeReference<List<EnvironmentDto.SpaceDto.ServiceDto>>() {}) != null
}

def "should return 404 when validate service contracts against not existing environment"() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,21 @@ class EnvironmentControllerUT extends Specification {
def service = new ServiceId("service", "version")
given:
def environment = new Environment(
randomAlphabetic(10),
'abc',
[new Space('def', [service] as Set)] as Set
)
environmentRepository.persist(environment)
when:
def services = environmentController.getEnvironment(environment.name).getBody()
def receivedEnvironment = environmentController.getEnvironment(environment.name).getBody()

then:
services == [new ServiceDto(service.name, service.version)] as List
receivedEnvironment ==
new EnvironmentDto(
'abc',
[new EnvironmentDto.SpaceDto(
'def',
[new EnvironmentDto.SpaceDto.ServiceDto(service.name, service.version)] as Set
)] as Set
)
}
}

0 comments on commit 0f89bca

Please sign in to comment.