1
|
|
|
package com.hltech.vaunt.validator; |
2
|
|
|
|
3
|
|
|
import com.google.common.collect.Lists; |
4
|
|
|
import com.hltech.vaunt.validator.domain.representation.model.Contract; |
5
|
|
|
import com.hltech.vaunt.validator.domain.representation.model.Service; |
6
|
|
|
|
7
|
|
|
import java.util.List; |
8
|
|
|
import java.util.Optional; |
9
|
|
|
import java.util.stream.Collectors; |
10
|
|
|
|
11
|
|
|
public class VauntValidator { |
12
|
|
|
|
13
|
|
|
public List<ValidationResult> validate(Service consumer, Service provider) { |
14
|
|
|
return consumer.getExpectations().getProviderNameToContracts().get(provider.getName()).stream() |
15
|
|
|
.map(consumerContract -> validateWithMatchingProviderContract( |
16
|
|
|
consumerContract, provider.getCapabilities().getContracts())) |
17
|
|
|
.collect(Collectors.toList()); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public List<ValidationResult> validate(List<Contract> expectations, List<Contract> capabilities) { |
21
|
|
|
return expectations.stream() |
22
|
|
|
.map(consumerContract -> validateWithMatchingProviderContract(consumerContract, capabilities)) |
23
|
|
|
.collect(Collectors.toList()); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
private ValidationResult validateWithMatchingProviderContract( |
27
|
|
|
Contract consumerContract, List<Contract> providerContracts) { |
28
|
|
|
Optional<Contract> matchingProviderContract = providerContracts.stream() |
29
|
|
|
.filter(providerContract -> isEndpointMatching(consumerContract, providerContract)) |
30
|
|
|
.findFirst(); |
31
|
|
|
|
32
|
|
|
if (!matchingProviderContract.isPresent()) { |
33
|
|
|
return ValidationResult.failure(Lists.newArrayList(ValidationError.MISSING_ENDPOINT)); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if (!consumerContract.getBody().equals(matchingProviderContract.get().getBody())) { |
37
|
|
|
return ValidationResult.failure(Lists.newArrayList(ValidationError.WRONG_SCHEMA)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return ValidationResult.success(); |
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
|
|
|
} |
49
|
|
|
|