1
|
|
|
package com.hltech.judged.server.domain.validation; |
2
|
|
|
|
3
|
|
|
import com.hltech.judged.server.domain.contracts.ServiceContracts; |
4
|
|
|
import lombok.AccessLevel; |
5
|
|
|
import lombok.Getter; |
6
|
|
|
import lombok.RequiredArgsConstructor; |
7
|
|
|
|
8
|
|
|
import java.util.Collection; |
9
|
|
|
import java.util.List; |
10
|
|
|
import java.util.Map; |
11
|
|
|
import java.util.Optional; |
12
|
|
|
import java.util.stream.Stream; |
13
|
|
|
|
14
|
|
|
import static com.google.common.collect.Lists.newArrayList; |
15
|
|
|
import static com.hltech.judged.server.domain.validation.InterfaceContractValidator.InteractionValidationStatus.FAILED; |
16
|
|
|
import static com.hltech.judged.server.domain.validation.InterfaceContractValidator.InteractionValidationStatus.OK; |
17
|
|
|
import static java.util.Collections.emptyList; |
18
|
|
|
import static java.util.stream.Collectors.toList; |
19
|
|
|
|
20
|
|
|
@RequiredArgsConstructor |
21
|
|
|
public abstract class InterfaceContractValidator<C, E> { |
22
|
|
|
|
23
|
|
|
private final String communicationInterface; |
24
|
|
|
|
25
|
|
|
public String getCommunicationInterface() { |
26
|
|
|
return this.communicationInterface; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public List<CapabilitiesValidationResult> validateCapabilities(ServiceContracts provider, Collection<ServiceContracts> allConsumers) { |
30
|
|
|
return extractCapabilities(provider) |
31
|
|
|
.map(capabilities -> allConsumers |
32
|
|
|
.stream() |
33
|
|
|
.map(consumer -> validateCapabilities(capabilities, provider.getName(), consumer)) |
34
|
|
|
.filter(validationResult -> !validationResult.getInteractionValidationResults().isEmpty()) |
35
|
|
|
.collect(toList()) |
36
|
|
|
).orElse(emptyList()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
private CapabilitiesValidationResult validateCapabilities(C capabilities, String providerName, ServiceContracts consumer) { |
40
|
|
|
List<InteractionValidationResult> validatedInteractions = consumer |
41
|
|
|
.getMappedExpectations(providerName, this.communicationInterface, this::asExpectations) |
42
|
|
|
.map(expectations -> validate(expectations, capabilities)) |
43
|
|
|
.orElse(emptyList()); |
44
|
|
|
|
45
|
|
|
return new CapabilitiesValidationResult( |
46
|
|
|
consumer.getName(), |
47
|
|
|
consumer.getVersion(), |
48
|
|
|
validatedInteractions |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public List<ExpectationValidationResult> validateExpectations(ServiceContracts consumer, Collection<ServiceContracts> allProviders) { |
53
|
|
|
return extractExpectations(consumer) |
54
|
|
|
.entrySet() |
55
|
|
|
.stream() |
56
|
|
|
.flatMap(pe -> validateExpectations(pe.getValue(), pe.getKey(), allProviders)) |
57
|
|
|
.collect(toList()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private Stream<? extends ExpectationValidationResult> validateExpectations(E expectations, String providerName, Collection<ServiceContracts> allProviders) { |
61
|
|
|
List<ServiceContracts> matchedProviders = allProviders |
62
|
|
|
.stream() |
63
|
|
|
.filter(it -> it.getName().equals(providerName)) |
64
|
|
|
.collect(toList()); |
65
|
|
|
if (matchedProviders.isEmpty()) { |
66
|
|
|
return Stream.of(createProviderNotAvailableResult(providerName)); |
67
|
|
|
} else { |
68
|
|
|
return matchedProviders |
69
|
|
|
.stream() |
70
|
|
|
.map(matchedProvider -> validateExpectations(expectations, matchedProvider)); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private ExpectationValidationResult validateExpectations(E expectations, ServiceContracts provider) { |
75
|
|
|
List<InteractionValidationResult> validatedInteractions = provider |
76
|
|
|
.getMappedCapabilities(this.communicationInterface, this::asCapabilities) |
77
|
|
|
.map(capabilities -> validate(expectations, capabilities)) |
78
|
|
|
.orElseGet(() -> newArrayList( |
79
|
|
|
new InteractionValidationResult( |
80
|
|
|
"any", |
81
|
|
|
FAILED, |
82
|
|
|
newArrayList("provider was registered without any '" + this.communicationInterface + "' capabilities") |
83
|
|
|
) |
84
|
|
|
)); |
85
|
|
|
|
86
|
|
|
return new ExpectationValidationResult( |
87
|
|
|
provider.getName(), |
88
|
|
|
provider.getVersion(), |
89
|
|
|
validatedInteractions |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private ExpectationValidationResult createProviderNotAvailableResult(String providerName) { |
94
|
|
|
return new ExpectationValidationResult( |
95
|
|
|
providerName, |
96
|
|
|
null, |
97
|
|
|
newArrayList(new InteractionValidationResult("any", InteractionValidationStatus.FAILED, newArrayList("provider not available"))) |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public Optional<C> extractCapabilities(ServiceContracts serviceContracts) { |
102
|
|
|
return serviceContracts.getMappedCapabilities(this.communicationInterface, this::asCapabilities); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public Map<String, E> extractExpectations(ServiceContracts testedServiceContracts) { |
106
|
|
|
return testedServiceContracts.getMappedExpectations(this.communicationInterface, this::asExpectations); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public abstract C asCapabilities(String rawCapabilities); |
110
|
|
|
|
111
|
|
|
public abstract E asExpectations(String rawExpectations); |
112
|
|
|
|
113
|
|
|
public abstract List<InteractionValidationResult> validate(E expectations, C capabilities); |
114
|
|
|
|
115
|
|
|
public enum InteractionValidationStatus { |
116
|
|
|
OK, |
117
|
|
|
FAILED |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
@Getter |
121
|
|
|
@RequiredArgsConstructor |
122
|
|
|
public static class CapabilitiesValidationResult { |
123
|
|
|
|
124
|
|
|
private final String consumerName; |
125
|
|
|
private final String consumerVersion; |
126
|
|
|
private final List<InteractionValidationResult> interactionValidationResults; |
127
|
|
|
|
128
|
|
|
public boolean isEmpty() { |
129
|
|
|
return this.interactionValidationResults.isEmpty(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
@Getter |
135
|
|
|
@RequiredArgsConstructor |
136
|
|
|
public static class ExpectationValidationResult { |
137
|
|
|
|
138
|
|
|
private final String providerName; |
139
|
|
|
private final String providerVersion; |
140
|
|
|
|
141
|
|
|
private final List<InteractionValidationResult> interactionValidationResults; |
142
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
@Getter |
146
|
|
|
@RequiredArgsConstructor(access = AccessLevel.PRIVATE) |
147
|
|
|
public static class InteractionValidationResult { |
148
|
|
|
|
149
|
|
|
private final String name; |
150
|
|
|
private final InteractionValidationStatus status; |
151
|
|
|
private final List<String> errors; |
152
|
|
|
|
153
|
|
|
public static InteractionValidationResult success(String name) { |
154
|
|
|
return new InteractionValidationResult(name, OK, newArrayList()); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public static InteractionValidationResult fail(String name, List<String> errors) { |
158
|
|
|
return new InteractionValidationResult(name, FAILED, errors); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|