|
1
|
|
|
package dev.hltech.dredd.domain.validation.jms; |
|
2
|
|
|
|
|
3
|
|
|
import java.util.List; |
|
4
|
|
|
import java.util.Optional; |
|
5
|
|
|
import java.util.stream.Collectors; |
|
6
|
|
|
|
|
7
|
|
|
public class VauntValidator { |
|
8
|
|
|
|
|
9
|
|
|
public List<ValidationResult> validate(Service consumer, Service provider) { |
|
10
|
|
|
return consumer.getExpectations().getProviderNameToContracts().get(provider.getName()).stream() |
|
11
|
|
|
.map(consumerContract -> validateWithMatchingProviderContract( |
|
12
|
|
|
consumerContract, provider.getCapabilities().getContracts())) |
|
13
|
|
|
.collect(Collectors.toList()); |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
public List<ValidationResult> validate(List<Contract> expectations, List<Contract> capabilities) { |
|
17
|
|
|
return expectations.stream() |
|
18
|
|
|
.map(consumerContract -> validateWithMatchingProviderContract(consumerContract, capabilities)) |
|
19
|
|
|
.collect(Collectors.toList()); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
private ValidationResult validateWithMatchingProviderContract( |
|
23
|
|
|
Contract consumerContract, List<Contract> providerContracts) { |
|
24
|
|
|
List<Contract> contracts = providerContracts.stream() |
|
25
|
|
|
.filter(providerContract -> isEndpointMatching(consumerContract, providerContract)) |
|
26
|
|
|
.collect(Collectors.toList()); |
|
27
|
|
|
|
|
28
|
|
|
if (contracts.isEmpty()) { |
|
29
|
|
|
return ValidationResult.failure(consumerContract, providerContracts, ValidationError.MISSING_ENDPOINT); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
Optional<Contract> matchingProviderContract = contracts.stream() |
|
33
|
|
|
.filter(providerContract -> isSchemaMatching(consumerContract, providerContract)) |
|
34
|
|
|
.findFirst(); |
|
35
|
|
|
|
|
36
|
|
|
if (!matchingProviderContract.isPresent()) { |
|
37
|
|
|
return ValidationResult.failure(consumerContract, providerContracts, ValidationError.WRONG_SCHEMA); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return ValidationResult.success(consumerContract, providerContracts); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
private boolean isEndpointMatching(Contract firstContract, Contract secondContract) { |
|
44
|
|
|
return firstContract.getDestinationType().equals(secondContract.getDestinationType()) |
|
45
|
|
|
&& firstContract.getDestinationName().equals(secondContract.getDestinationName()); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
private boolean isSchemaMatching(Contract firstContract, Contract secondContract) { |
|
49
|
|
|
return firstContract.getBody().equals(secondContract.getBody()); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|