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
|
|
|
|
15
|
|
|
return consumer.getExpectations().getProviderNameToContracts().get(provider.getName()).stream() |
16
|
|
|
.map(consumerContract -> validateWithMatchingProviderContract( |
17
|
|
|
consumerContract, provider.getCapabilities().getContracts())) |
18
|
|
|
.collect(Collectors.toList()); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public List<ValidationResult> validate(List<Contract> expectations, List<Contract> capabilities) { |
22
|
|
|
|
23
|
|
|
return expectations.stream() |
24
|
|
|
.map(consumerContract -> validateWithMatchingProviderContract(consumerContract, capabilities)) |
25
|
|
|
.collect(Collectors.toList()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
private ValidationResult validateWithMatchingProviderContract( |
29
|
|
|
Contract consumerContract, List<Contract> providerContracts) { |
30
|
|
|
|
31
|
|
|
Optional<Contract> matchingProviderContract = providerContracts.stream() |
32
|
|
|
.filter(providerContract -> isEndpointMatching(consumerContract, providerContract)) |
33
|
|
|
.findFirst(); |
34
|
|
|
|
35
|
|
|
if (!matchingProviderContract.isPresent()) { |
36
|
|
|
return new ValidationResult(false, Lists.newArrayList("No endpoint required by consumer")); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if (!consumerContract.getBody().equals(matchingProviderContract.get().getBody())) { |
40
|
|
|
return new ValidationResult(false, Lists.newArrayList("Unmatching schema of the message")); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return new ValidationResult(true, Lists.newArrayList()); |
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
|
|
|
} |
52
|
|
|
|