|
1
|
|
|
package dev.hltech.dredd.interfaces.rest.validation.rest; |
|
2
|
|
|
|
|
3
|
|
|
import au.com.dius.pact.model.RequestResponsePact; |
|
4
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException; |
|
5
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
6
|
|
|
import com.fasterxml.jackson.databind.node.ObjectNode; |
|
7
|
|
|
import dev.hltech.dredd.domain.validation.InterfaceContractValidator; |
|
8
|
|
|
import dev.hltech.dredd.domain.validation.JudgeD; |
|
9
|
|
|
import dev.hltech.dredd.domain.validation.ProviderNotAvailableException; |
|
10
|
|
|
import dev.hltech.dredd.domain.validation.rest.RestContractValidator; |
|
11
|
|
|
import dev.hltech.dredd.interfaces.rest.validation.AggregatedValidationReportDto; |
|
12
|
|
|
import dev.hltech.dredd.interfaces.rest.validation.ContractValidationReportDto; |
|
13
|
|
|
import dev.hltech.dredd.interfaces.rest.validation.ContractValidationStatus; |
|
14
|
|
|
import dev.hltech.dredd.interfaces.rest.validation.InteractionValidationReportDto; |
|
15
|
|
|
import io.swagger.annotations.ApiOperation; |
|
16
|
|
|
import io.swagger.annotations.ApiResponse; |
|
17
|
|
|
import io.swagger.annotations.ApiResponses; |
|
18
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
19
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
20
|
|
|
import org.springframework.http.MediaType; |
|
21
|
|
|
import org.springframework.validation.annotation.Validated; |
|
22
|
|
|
import org.springframework.web.bind.annotation.PathVariable; |
|
23
|
|
|
import org.springframework.web.bind.annotation.PostMapping; |
|
24
|
|
|
import org.springframework.web.bind.annotation.RequestBody; |
|
25
|
|
|
import org.springframework.web.bind.annotation.RestController; |
|
26
|
|
|
|
|
27
|
|
|
import java.util.List; |
|
28
|
|
|
|
|
29
|
|
|
import static com.google.common.collect.Lists.newArrayList; |
|
30
|
|
|
import static dev.hltech.dredd.interfaces.rest.validation.ContractValidationStatus.FAILED_NO_SUCH_PROVIDER_ON_ENVIRONMENT; |
|
31
|
|
|
import static dev.hltech.dredd.interfaces.rest.validation.ContractValidationStatus.PERFORMED; |
|
32
|
|
|
import static java.util.stream.Collectors.toList; |
|
33
|
|
|
|
|
34
|
|
|
@Slf4j |
|
35
|
|
|
@RestController |
|
36
|
|
|
public class RestValidationController { |
|
37
|
|
|
|
|
38
|
|
|
private final ObjectMapper objectMapper; |
|
39
|
|
|
private final JudgeD judgeD; |
|
40
|
|
|
private final RestContractValidator restCommunicationInterface; |
|
41
|
|
|
|
|
42
|
|
|
@Autowired |
|
43
|
|
|
public RestValidationController( |
|
44
|
|
|
ObjectMapper objectMapper, |
|
45
|
|
|
JudgeD judgeD, |
|
46
|
|
|
RestContractValidator restCommunicationInterface |
|
47
|
|
|
) { |
|
48
|
|
|
this.objectMapper = objectMapper; |
|
49
|
|
|
this.judgeD = judgeD; |
|
50
|
|
|
this.restCommunicationInterface = restCommunicationInterface; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
@PostMapping(value = "environments/{environment}/expectations-validation/rest", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) |
|
54
|
|
|
@ApiOperation(value = "Verify pacts against providers from the environment", nickname = "Verify pats") |
|
55
|
|
|
@ApiResponses(value = { |
|
56
|
|
|
@ApiResponse(code = 200, message = "Success", response = AggregatedValidationReportDto.class), |
|
57
|
|
|
@ApiResponse(code = 400, message = "Bad Request"), |
|
58
|
|
|
@ApiResponse(code = 500, message = "Failure")}) |
|
59
|
|
|
public AggregatedValidationReportDto validatePacts( |
|
60
|
|
|
@PathVariable("environment") String environment, |
|
61
|
|
|
@RequestBody @Validated PactValidationForm pactValidationForm |
|
62
|
|
|
) { |
|
63
|
|
|
log.info("Received request {}", pactValidationForm); |
|
64
|
|
|
List<ObjectNode> pacts = pactValidationForm.getPacts(); |
|
65
|
|
|
|
|
66
|
|
|
List<ContractValidationReportDto> validationResults = pacts |
|
67
|
|
|
.stream() |
|
68
|
|
|
.map(pact -> { |
|
69
|
|
|
try { |
|
70
|
|
|
return restCommunicationInterface.asExpectations(objectMapper.writeValueAsString(pact)); |
|
71
|
|
|
} catch (JsonProcessingException e) { |
|
72
|
|
|
throw new RuntimeException("unable to parse pact", e); |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
}) |
|
75
|
|
|
.flatMap(pact -> validatePact(environment, pact).stream()) |
|
76
|
|
|
.collect(toList()); |
|
77
|
|
|
|
|
78
|
|
|
return new AggregatedValidationReportDto(validationResults); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
private List<ContractValidationReportDto> validatePact(String environment, RequestResponsePact pact) { |
|
82
|
|
|
try { |
|
83
|
|
|
return judgeD |
|
84
|
|
|
.createContractValidator(environment, restCommunicationInterface) |
|
85
|
|
|
.validateExpectations(pact.getProvider().getName(), pact) |
|
86
|
|
|
.stream() |
|
87
|
|
|
.map(evr -> toDto(pact, evr)) |
|
88
|
|
|
.collect(toList()); |
|
89
|
|
|
} catch (ProviderNotAvailableException e) { |
|
90
|
|
|
return newArrayList(ContractValidationReportDto.builder() |
|
91
|
|
|
.consumerName(pact.getConsumer().getName()) |
|
92
|
|
|
.providerName(pact.getProvider().getName()) |
|
93
|
|
|
.validationStatus(FAILED_NO_SUCH_PROVIDER_ON_ENVIRONMENT) |
|
94
|
|
|
.build()); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
@PostMapping(value = "environments/{environment}/capabilities-validation/rest", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) |
|
99
|
|
|
@ApiOperation(value = "Verify swagger against consumers from the environment", nickname = "Verify swagger") |
|
100
|
|
|
@ApiResponses(value = { |
|
101
|
|
|
@ApiResponse(code = 200, message = "Success", response = AggregatedValidationReportDto.class), |
|
102
|
|
|
@ApiResponse(code = 400, message = "Bad Request"), |
|
103
|
|
|
@ApiResponse(code = 500, message = "Failure")}) |
|
104
|
|
|
public AggregatedValidationReportDto validateSwagger( |
|
105
|
|
|
@PathVariable("environment") String environment, |
|
106
|
|
|
@RequestBody @Validated SwaggerValidationForm swaggerValidationForm |
|
107
|
|
|
) throws JsonProcessingException { |
|
108
|
|
|
log.info("Received request {}", swaggerValidationForm); |
|
109
|
|
|
String providerName = swaggerValidationForm.getProviderName(); |
|
110
|
|
|
ObjectNode swagger = swaggerValidationForm.getSwagger(); |
|
111
|
|
|
|
|
112
|
|
|
List<ContractValidationReportDto> collect = judgeD |
|
113
|
|
|
.createContractValidator(environment, restCommunicationInterface) |
|
114
|
|
|
.validateCapabilities( |
|
115
|
|
|
providerName, |
|
116
|
|
|
objectMapper.writeValueAsString(swagger) |
|
117
|
|
|
) |
|
118
|
|
|
.stream() |
|
119
|
|
|
.map(cvr -> toDto(providerName, cvr)) |
|
120
|
|
|
.collect(toList()); |
|
121
|
|
|
return new AggregatedValidationReportDto(collect); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
private ContractValidationReportDto toDto(RequestResponsePact pact, InterfaceContractValidator.ExpectationValidationResult evr) { |
|
126
|
|
|
return new ContractValidationReportDto( |
|
127
|
|
|
pact.getConsumer().getName(), |
|
128
|
|
|
null, |
|
129
|
|
|
evr.getProviderName(), |
|
130
|
|
|
evr.getProviderVersion(), |
|
131
|
|
|
PERFORMED, |
|
132
|
|
|
evr.getInteractionValidationResults().stream().map(RestValidationController::toDto).collect(toList()) |
|
133
|
|
|
); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
private ContractValidationReportDto toDto(String providerName, InterfaceContractValidator.CapabilitiesValidationResult cvr) { |
|
137
|
|
|
return new ContractValidationReportDto( |
|
138
|
|
|
cvr.getConsumerName(), |
|
139
|
|
|
cvr.getConsumerVersion(), |
|
140
|
|
|
providerName, |
|
141
|
|
|
null, |
|
142
|
|
|
ContractValidationStatus.PERFORMED, |
|
143
|
|
|
cvr.getInteractionValidationResults().stream().map(RestValidationController::toDto).collect(toList()) |
|
144
|
|
|
); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public static InteractionValidationReportDto toDto(InterfaceContractValidator.InteractionValidationResult input) { |
|
148
|
|
|
return InteractionValidationReportDto.builder() |
|
149
|
|
|
.interactionName(input.getName()) |
|
150
|
|
|
.validationResult(input.getStatus()) |
|
151
|
|
|
.errors(input.getErrors()) |
|
152
|
|
|
.build(); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
} |
|
156
|
|
|
|