1
|
|
|
package dev.hltech.dredd.domain.validation; |
2
|
|
|
|
3
|
|
|
import dev.hltech.dredd.domain.contracts.ServiceContracts; |
4
|
|
|
import lombok.AllArgsConstructor; |
5
|
|
|
import lombok.Getter; |
6
|
|
|
|
7
|
|
|
import java.util.List; |
8
|
|
|
|
9
|
|
|
import static com.google.common.collect.Lists.newArrayList; |
10
|
|
|
import static dev.hltech.dredd.domain.validation.InterfaceContractValidator.InteractionValidationStatus.FAILED; |
11
|
|
|
import static java.util.Collections.emptyList; |
12
|
|
|
|
13
|
|
|
public abstract class InterfaceContractValidator<C, E> { |
14
|
|
|
|
15
|
|
|
private String communicationInterface; |
16
|
|
|
|
17
|
|
|
public InterfaceContractValidator(String communicationInterface) { |
18
|
|
|
this.communicationInterface = communicationInterface; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public CapabilitiesValidationResult validate(ServiceContracts consumer, String providerName, C capabilities) { |
22
|
|
|
List<InteractionValidationResult> validatedInteractions = consumer.getExpectations(providerName, communicationInterface) |
23
|
|
|
.map(expectations -> validate(asExpectations(expectations), capabilities)) |
24
|
|
|
.orElse(emptyList()); |
25
|
|
|
|
26
|
|
|
return new CapabilitiesValidationResult( |
27
|
|
|
consumer.getName(), |
28
|
|
|
consumer.getVersion(), |
29
|
|
|
validatedInteractions |
30
|
|
|
); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public ExpectationValidationResult validate(ServiceContracts provider, E expectations) { |
34
|
|
|
List<InteractionValidationResult> validatedInteractions = provider.getCapabilities(communicationInterface) |
35
|
|
|
.map(capabilities -> validate(expectations, asCapabilities(capabilities))) |
36
|
|
|
.orElseGet(() -> newArrayList( |
37
|
|
|
new InteractionValidationResult( |
38
|
|
|
"any", |
39
|
|
|
FAILED, |
40
|
|
|
newArrayList("provider was registered without any '" + communicationInterface + "' capabilities") |
41
|
|
|
) |
42
|
|
|
)); |
43
|
|
|
|
44
|
|
|
return new ExpectationValidationResult( |
45
|
|
|
provider.getName(), |
46
|
|
|
provider.getVersion(), |
47
|
|
|
validatedInteractions |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public abstract C asCapabilities(String rawCapabilities); |
52
|
|
|
|
53
|
|
|
public abstract E asExpectations(String rawExpectations); |
54
|
|
|
|
55
|
|
|
public abstract List<InteractionValidationResult> validate(E expectations, C capabilities); |
56
|
|
|
|
57
|
|
|
public enum InteractionValidationStatus { |
58
|
|
|
|
59
|
|
|
OK, |
60
|
|
|
FAILED |
61
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
@Getter |
65
|
|
|
@AllArgsConstructor |
66
|
|
|
public static class CapabilitiesValidationResult { |
67
|
|
|
|
68
|
|
|
private String consumerName; |
69
|
|
|
private String consumerVersion; |
70
|
|
|
private List<InteractionValidationResult> interactionValidationResults; |
71
|
|
|
|
72
|
|
|
public boolean isEmpty() { |
73
|
|
|
return interactionValidationResults.isEmpty(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
@Getter |
79
|
|
|
@AllArgsConstructor |
80
|
|
|
public static class ExpectationValidationResult { |
81
|
|
|
|
82
|
|
|
private String providerName; |
83
|
|
|
private String providerVersion; |
84
|
|
|
|
85
|
|
|
private List<InteractionValidationResult> interactionValidationResults; |
86
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
@Getter |
90
|
|
|
@AllArgsConstructor |
91
|
|
|
public static class InteractionValidationResult { |
92
|
|
|
|
93
|
|
|
private final String name; |
94
|
|
|
private final InteractionValidationStatus status; |
95
|
|
|
private final List<String> errors; |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|