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