|
1
|
|
|
package com.hltech.judged.server.interfaces.rest.validation; |
|
2
|
|
|
|
|
3
|
|
|
import com.google.common.collect.HashMultimap; |
|
4
|
|
|
import com.google.common.collect.Multimap; |
|
5
|
|
|
import com.hltech.judged.server.domain.JudgeD; |
|
6
|
|
|
import com.hltech.judged.server.domain.contracts.ServiceContracts; |
|
7
|
|
|
import com.hltech.judged.server.domain.contracts.ServiceContractsRepository; |
|
8
|
|
|
import com.hltech.judged.server.domain.ServiceVersion; |
|
9
|
|
|
import com.hltech.judged.server.domain.validation.EnvironmentValidatorResult; |
|
10
|
|
|
import com.hltech.judged.server.domain.validation.InterfaceContractValidator; |
|
11
|
|
|
import com.hltech.judged.server.interfaces.rest.RequestValidationException; |
|
12
|
|
|
import com.hltech.judged.server.interfaces.rest.ResourceNotFoundException; |
|
13
|
|
|
import com.hltech.judged.server.interfaces.rest.environment.ServiceDto; |
|
14
|
|
|
import io.swagger.annotations.ApiOperation; |
|
15
|
|
|
import io.swagger.annotations.ApiResponse; |
|
16
|
|
|
import io.swagger.annotations.ApiResponses; |
|
17
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
18
|
|
|
import org.springframework.http.MediaType; |
|
19
|
|
|
import org.springframework.web.bind.annotation.GetMapping; |
|
20
|
|
|
import org.springframework.web.bind.annotation.PathVariable; |
|
21
|
|
|
import org.springframework.web.bind.annotation.RequestParam; |
|
22
|
|
|
import org.springframework.web.bind.annotation.RestController; |
|
23
|
|
|
|
|
24
|
|
|
import java.util.Collection; |
|
25
|
|
|
import java.util.List; |
|
26
|
|
|
|
|
27
|
|
|
import static com.hltech.judged.server.interfaces.rest.validation.Converters.toDtos; |
|
28
|
|
|
import static java.util.stream.Collectors.toList; |
|
29
|
|
|
|
|
30
|
|
|
@RestController |
|
31
|
|
|
public class ValidationController { |
|
32
|
|
|
|
|
33
|
|
|
private final JudgeD judgeD; |
|
34
|
|
|
private final ServiceContractsRepository serviceContractsRepository; |
|
35
|
|
|
private final List<InterfaceContractValidator<?, ?>> validators; |
|
36
|
|
|
|
|
37
|
|
|
@Autowired |
|
38
|
|
|
public ValidationController( |
|
39
|
|
|
JudgeD judgeD, |
|
40
|
|
|
ServiceContractsRepository serviceContractsRepository, |
|
41
|
|
|
List<InterfaceContractValidator<?, ?>> validators |
|
42
|
|
|
) { |
|
43
|
|
|
this.judgeD = judgeD; |
|
44
|
|
|
this.serviceContractsRepository = serviceContractsRepository; |
|
45
|
|
|
this.validators = validators; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
@GetMapping(value = "/environment-compatibility-report", produces = MediaType.APPLICATION_JSON_VALUE) |
|
49
|
|
|
@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") |
|
50
|
|
|
@ApiResponses(value = { |
|
51
|
|
|
@ApiResponse(code = 200, message = "Success", response = BatchValidationReportDto.class, responseContainer = "list"), |
|
52
|
|
|
@ApiResponse(code = 500, message = "Failure"), |
|
53
|
|
|
@ApiResponse(code = 404, message = "Service not found") |
|
54
|
|
|
}) |
|
55
|
|
|
public List<BatchValidationReportDto> validateAgainstEnvironments( |
|
56
|
|
|
@RequestParam("services") List<String> services, |
|
57
|
|
|
@RequestParam("environment") String environment |
|
58
|
|
|
) { |
|
59
|
|
|
List<ServiceContracts> validatedServiceContracts = services.stream() |
|
60
|
|
|
.map(service -> { |
|
61
|
|
|
if (service.contains(":") && service.indexOf(":") == service.lastIndexOf(":")) { |
|
62
|
|
|
String[] serviceNameAndVersion = service.split(":"); |
|
63
|
|
|
return new ServiceVersion(serviceNameAndVersion[0], serviceNameAndVersion[1]); |
|
64
|
|
|
} else { |
|
65
|
|
|
throw new RequestValidationException(); |
|
66
|
|
|
} |
|
67
|
|
|
}) |
|
68
|
|
|
.map(serviceContractsRepository::findOne) |
|
69
|
|
|
.map(o -> o.orElseThrow(RequestValidationException::new)) |
|
70
|
|
|
.collect(toList()); |
|
71
|
|
|
|
|
72
|
|
|
Multimap<ServiceVersion, EnvironmentValidatorResult> validationResults = HashMultimap.create(); |
|
73
|
|
|
this.validators.stream() |
|
74
|
|
|
.forEach(validator -> |
|
75
|
|
|
judgeD.validatedServicesAgainstEnvironment( |
|
76
|
|
|
validatedServiceContracts, |
|
77
|
|
|
environment, |
|
78
|
|
|
validator |
|
79
|
|
|
) |
|
80
|
|
|
.entrySet() |
|
81
|
|
|
.stream() |
|
82
|
|
|
.forEach(e -> validationResults.put(e.getKey(), e.getValue())) |
|
83
|
|
|
); |
|
84
|
|
|
|
|
85
|
|
|
return validationResults.asMap() |
|
86
|
|
|
.entrySet() |
|
87
|
|
|
.stream() |
|
88
|
|
|
.map(e -> { |
|
89
|
|
|
return BatchValidationReportDto.builder() |
|
90
|
|
|
.service(ServiceDto.builder().name(e.getKey().getName()).version(e.getKey().getVersion()).build()) |
|
91
|
|
|
.validationReports(toDtos(e.getKey(), e.getValue())) |
|
92
|
|
|
.build(); |
|
93
|
|
|
}) |
|
94
|
|
|
.collect(toList()); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
@GetMapping(value = "/environment-compatibility-report/{serviceName}:{serviceVersion:.+}", produces = MediaType.APPLICATION_JSON_VALUE) |
|
98
|
|
|
@ApiOperation(value = "Get validation report for contract between given service and given environment", nickname = "Validate service against environment") |
|
99
|
|
|
@ApiResponses(value = { |
|
100
|
|
|
@ApiResponse(code = 200, message = "Success", response = ContractValidationReportDto.class, responseContainer = "list"), |
|
101
|
|
|
@ApiResponse(code = 500, message = "Failure"), |
|
102
|
|
|
@ApiResponse(code = 404, message = "Service not found") |
|
103
|
|
|
}) |
|
104
|
|
|
public List<ContractValidationReportDto> validateAgainstEnvironments( |
|
105
|
|
|
@PathVariable("serviceName") String name, |
|
106
|
|
|
@PathVariable("serviceVersion") String version, |
|
107
|
|
|
@RequestParam("environment") List<String> environments |
|
108
|
|
|
) { |
|
109
|
|
|
ServiceVersion serviceVersion = new ServiceVersion(name, version); |
|
110
|
|
|
ServiceContracts validatedServiceContracts = this.serviceContractsRepository.findOne(serviceVersion) |
|
111
|
|
|
.orElseThrow(ResourceNotFoundException::new); |
|
112
|
|
|
|
|
113
|
|
|
Collection<EnvironmentValidatorResult> collect = this.validators.stream() |
|
114
|
|
|
.map(validator -> |
|
115
|
|
|
this.judgeD.validateServiceAgainstEnvironments( |
|
116
|
|
|
validatedServiceContracts, |
|
117
|
|
|
environments, |
|
118
|
|
|
validator |
|
119
|
|
|
)) |
|
120
|
|
|
.collect(toList()); |
|
121
|
|
|
return toDtos(serviceVersion, collect); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|