1
|
|
|
package dev.hltech.dredd.domain.validation.rest; |
2
|
|
|
|
3
|
|
|
import au.com.dius.pact.model.RequestResponseInteraction; |
4
|
|
|
import au.com.dius.pact.model.RequestResponsePact; |
5
|
|
|
import com.atlassian.oai.validator.SwaggerRequestResponseValidator; |
6
|
|
|
import com.atlassian.oai.validator.pact.PactResponse; |
7
|
|
|
import com.atlassian.oai.validator.report.ValidationReport; |
8
|
|
|
import dev.hltech.dredd.domain.validation.InterfaceContractValidator; |
9
|
|
|
import org.springframework.stereotype.Component; |
10
|
|
|
|
11
|
|
|
import java.util.List; |
12
|
|
|
import java.util.Map; |
13
|
|
|
import java.util.stream.Collectors; |
14
|
|
|
|
15
|
|
|
import static au.com.dius.pact.model.PactReader.loadPact; |
16
|
|
|
import static com.atlassian.oai.validator.pact.PactRequest.of; |
17
|
|
|
import static dev.hltech.dredd.domain.validation.InterfaceContractValidator.InteractionValidationStatus.FAILED; |
18
|
|
|
import static dev.hltech.dredd.domain.validation.InterfaceContractValidator.InteractionValidationStatus.OK; |
19
|
|
|
import static java.util.function.Function.identity; |
20
|
|
|
|
21
|
|
|
@Component |
22
|
|
|
public class RestContractValidator extends InterfaceContractValidator<String, RequestResponsePact> { |
23
|
|
|
|
24
|
|
|
public static final String COMMUNICATION_INTERFACE = "rest"; |
25
|
|
|
|
26
|
|
|
public RestContractValidator() { |
27
|
|
|
super(COMMUNICATION_INTERFACE); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public List<InteractionValidationResult> validate(RequestResponsePact pact, String capabilities) { |
31
|
|
|
SwaggerRequestResponseValidator swaggerValidator = SwaggerRequestResponseValidator.createFor(capabilities).build(); |
32
|
|
|
|
33
|
|
|
Map<RequestResponseInteraction, ValidationReport> validationReports = pact.getInteractions() |
34
|
|
|
.stream() |
35
|
|
|
.collect(Collectors.toMap( |
36
|
|
|
identity(), |
37
|
|
|
interaction -> swaggerValidator.validate(of(interaction.getRequest()), PactResponse.of(interaction.getResponse())) |
38
|
|
|
)); |
39
|
|
|
|
40
|
|
|
List<InteractionValidationResult> collect = validationReports |
41
|
|
|
.entrySet() |
42
|
|
|
.stream() |
43
|
|
|
.map(e -> { |
44
|
|
|
ValidationReport validationReport = e.getValue(); |
45
|
|
|
return new InteractionValidationResult( |
46
|
|
|
e.getKey().getDescription(), |
47
|
|
|
validationReport.hasErrors() ? FAILED : OK, |
48
|
|
|
validationReport.getMessages().stream().map( |
49
|
|
|
message -> message.getMessage() |
50
|
|
|
).collect(Collectors.toList()) |
51
|
|
|
); |
52
|
|
|
}) |
53
|
|
|
.collect(Collectors.toList()); |
54
|
|
|
|
55
|
|
|
return collect; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
@Override |
59
|
|
|
public String asCapabilities(String rawCapabilities) { |
60
|
|
|
return rawCapabilities; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
@Override |
64
|
|
|
public RequestResponsePact asExpectations(String rawExpectations) { |
65
|
|
|
return (RequestResponsePact) loadPact(rawExpectations); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
} |
69
|
|
|
|