1
|
|
|
package com.hltech.judged.server.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.OpenApiInteractionValidator; |
6
|
|
|
import com.atlassian.oai.validator.pact.PactResponse; |
7
|
|
|
import com.atlassian.oai.validator.report.ValidationReport; |
8
|
|
|
import com.hltech.judged.server.domain.validation.InterfaceContractValidator; |
9
|
|
|
|
10
|
|
|
import java.util.List; |
11
|
|
|
import java.util.Map; |
12
|
|
|
import java.util.stream.Collectors; |
13
|
|
|
|
14
|
|
|
import static au.com.dius.pact.model.PactReader.loadPact; |
15
|
|
|
import static com.atlassian.oai.validator.pact.PactRequest.of; |
16
|
|
|
import static com.hltech.judged.server.domain.validation.InterfaceContractValidator.InteractionValidationResult.fail; |
17
|
|
|
import static com.hltech.judged.server.domain.validation.InterfaceContractValidator.InteractionValidationResult.success; |
18
|
|
|
import static java.util.function.Function.identity; |
19
|
|
|
|
20
|
|
|
public class RestContractValidator extends InterfaceContractValidator<String, RequestResponsePact> { |
21
|
|
|
|
22
|
|
|
private static final String COMMUNICATION_INTERFACE = "rest"; |
23
|
|
|
|
24
|
|
|
public RestContractValidator() { |
25
|
|
|
super(COMMUNICATION_INTERFACE); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
@Override |
29
|
|
|
public List<InteractionValidationResult> validate(RequestResponsePact pact, String capabilities) { |
30
|
|
|
OpenApiInteractionValidator swaggerValidator = OpenApiInteractionValidator.createForInlineApiSpecification(capabilities).build(); |
31
|
|
|
|
32
|
|
|
Map<RequestResponseInteraction, ValidationReport> validationReports = pact.getInteractions() |
33
|
|
|
.stream() |
34
|
|
|
.collect(Collectors.toMap( |
35
|
|
|
identity(), |
36
|
|
|
interaction -> swaggerValidator.validate(of(interaction.getRequest()), PactResponse.of(interaction.getResponse())) |
37
|
|
|
)); |
38
|
|
|
|
39
|
|
|
return validationReports |
40
|
|
|
.entrySet() |
41
|
|
|
.stream() |
42
|
|
|
.map(e -> { |
43
|
|
|
ValidationReport validationReport = e.getValue(); |
44
|
|
|
if (validationReport.hasErrors()) { |
45
|
|
|
return fail( |
46
|
|
|
e.getKey().getDescription(), |
47
|
|
|
validationReport.getMessages().stream() |
48
|
|
|
.map(ValidationReport.Message::getMessage) |
49
|
|
|
.collect(Collectors.toList()) |
50
|
|
|
); |
51
|
|
|
} else { |
52
|
|
|
return success(e.getKey().getDescription()); |
53
|
|
|
} |
54
|
|
|
}) |
55
|
|
|
.collect(Collectors.toList()); |
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
|
|
|
|