1
|
|
|
package com.hltech.judged.server.interfaces.rest.validation; |
2
|
|
|
|
3
|
|
|
import com.hltech.judged.server.domain.JudgeDApplicationService; |
4
|
|
|
import com.hltech.judged.server.domain.ServiceId; |
5
|
|
|
import com.hltech.judged.server.interfaces.rest.RequestValidationException; |
6
|
|
|
import io.swagger.annotations.ApiOperation; |
7
|
|
|
import io.swagger.annotations.ApiResponse; |
8
|
|
|
import io.swagger.annotations.ApiResponses; |
9
|
|
|
import lombok.RequiredArgsConstructor; |
10
|
|
|
import org.springframework.http.MediaType; |
11
|
|
|
import org.springframework.web.bind.annotation.GetMapping; |
12
|
|
|
import org.springframework.web.bind.annotation.PathVariable; |
13
|
|
|
import org.springframework.web.bind.annotation.RequestParam; |
14
|
|
|
import org.springframework.web.bind.annotation.RestController; |
15
|
|
|
|
16
|
|
|
import java.util.List; |
17
|
|
|
import java.util.stream.Collectors; |
18
|
|
|
|
19
|
|
|
import static com.hltech.judged.server.interfaces.rest.validation.Converters.toDtos; |
20
|
|
|
import static java.util.stream.Collectors.toList; |
21
|
|
|
|
22
|
|
|
@RestController |
23
|
|
|
@RequiredArgsConstructor |
24
|
|
|
public class ValidationController { |
25
|
|
|
|
26
|
|
|
private final JudgeDApplicationService judgeD; |
27
|
|
|
|
28
|
|
|
@GetMapping(value = "/environment-compatibility-report", produces = MediaType.APPLICATION_JSON_VALUE) |
29
|
|
|
@ApiOperation(value = "Get validation report for contract between set of services and given environment as if those services were first deployed", nickname = "Validate services against environment") |
30
|
|
|
@ApiResponses(value = { |
31
|
|
|
@ApiResponse(code = 200, message = "Success", response = BatchValidationReportDto.class, responseContainer = "list"), |
32
|
|
|
@ApiResponse(code = 500, message = "Failure"), |
33
|
|
|
@ApiResponse(code = 404, message = "Service or environment not found") |
34
|
|
|
}) |
35
|
|
|
public List<BatchValidationReportDto> validateAgainstEnvironments( |
36
|
|
|
@RequestParam("services") List<String> services, |
37
|
|
|
@RequestParam("environment") String environment |
38
|
|
|
) { |
39
|
|
|
List<ServiceId> serviceIds = services.stream() |
40
|
|
|
.map(service -> { |
41
|
|
|
if (service.contains(":") && service.indexOf(":") == service.lastIndexOf(":")) { |
42
|
|
|
String[] serviceNameAndVersion = service.split(":"); |
43
|
|
|
return new ServiceId(serviceNameAndVersion[0], serviceNameAndVersion[1]); |
44
|
|
|
} else { |
45
|
|
|
throw new RequestValidationException(); |
46
|
|
|
} |
47
|
|
|
}) |
48
|
|
|
.collect(Collectors.toUnmodifiableList()); |
49
|
|
|
|
50
|
|
|
return judgeD.validatedServicesAgainstEnvironment(serviceIds, environment).asMap() |
51
|
|
|
.entrySet() |
52
|
|
|
.stream() |
53
|
|
|
.map(e -> BatchValidationReportDto.builder() |
54
|
|
|
.service(ServiceDto.builder().name(e.getKey().getName()).version(e.getKey().getVersion()).build()) |
55
|
|
|
.validationReports(toDtos(e.getKey(), e.getValue())) |
56
|
|
|
.build()) |
57
|
|
|
.collect(toList()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
@GetMapping(value = "/environment-compatibility-report/{serviceName}:{serviceVersion:.+}", produces = MediaType.APPLICATION_JSON_VALUE) |
61
|
|
|
@ApiOperation(value = "Get validation report for contract between given service and given environment", nickname = "Validate service against environment") |
62
|
|
|
@ApiResponses(value = { |
63
|
|
|
@ApiResponse(code = 200, message = "Success", response = ContractValidationReportDto.class, responseContainer = "list"), |
64
|
|
|
@ApiResponse(code = 500, message = "Failure"), |
65
|
|
|
@ApiResponse(code = 404, message = "Service not found") |
66
|
|
|
}) |
67
|
|
|
public List<ContractValidationReportDto> validateAgainstEnvironments( |
68
|
|
|
@PathVariable("serviceName") String name, |
69
|
|
|
@PathVariable("serviceVersion") String version, |
70
|
|
|
@RequestParam("environment") List<String> environments |
71
|
|
|
) { |
72
|
|
|
ServiceId serviceId = new ServiceId(name, version); |
73
|
|
|
|
74
|
|
|
return toDtos(serviceId, judgeD.validateServiceAgainstEnvironments(serviceId, environments)); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|