1
|
|
|
package com.hltech.vaunt.validator; |
2
|
|
|
|
3
|
|
|
import com.fasterxml.jackson.module.jsonSchema.JsonSchema; |
4
|
|
|
import com.google.common.collect.Lists; |
5
|
|
|
import com.hltech.vaunt.core.domain.model.Contract; |
6
|
|
|
import com.hltech.vaunt.core.domain.model.DestinationType; |
7
|
|
|
import com.hltech.vaunt.core.domain.model.Service; |
8
|
|
|
import com.hltech.vaunt.validator.schema.SchemaValidator; |
9
|
|
|
|
10
|
|
|
import java.util.List; |
11
|
|
|
import java.util.stream.Collectors; |
12
|
|
|
|
13
|
|
|
public class VauntValidator { |
14
|
|
|
private static final String MISSING_ENDPOINT = "Missing endpoint required by consumer"; |
15
|
|
|
private static final String MISSING_MESSAGE_WITH_ID = "Missing message with given id required by consumer"; |
16
|
|
|
private static final String MULTIPLE_MATCH = "More than one message with the same id for given endpoint"; |
17
|
|
|
|
18
|
|
|
public List<ValidationResult> validate(Service consumer, Service provider) { |
19
|
|
|
return consumer.getExpectations().getProviderNameToContracts().get(provider.getName()).stream() |
20
|
|
|
.map(consumerContract -> validateWithMatchingProviderContract( |
21
|
|
|
consumerContract, provider.getCapabilities().getContracts())) |
22
|
|
|
.collect(Collectors.toList()); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public List<ValidationResult> validate(List<Contract> expectations, List<Contract> capabilities) { |
26
|
|
|
return expectations.stream() |
27
|
|
|
.map(consumerContract -> validateWithMatchingProviderContract(consumerContract, capabilities)) |
28
|
|
|
.collect(Collectors.toList()); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
private ValidationResult validateWithMatchingProviderContract(Contract consumerContract, |
32
|
|
|
List<Contract> providerContracts) { |
33
|
|
|
List<Contract> endpointMatchingContracts = providerContracts.stream() |
34
|
|
|
.filter(providerContract -> isEndpointMatching(consumerContract, providerContract)) |
35
|
|
|
.collect(Collectors.toList()); |
36
|
|
|
|
37
|
|
|
if (endpointMatchingContracts.isEmpty()) { |
38
|
|
|
return ValidationResult.failure( |
39
|
|
|
consumerContract.toString(), Lists.newArrayList(MISSING_ENDPOINT)); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
List<Contract> idMatchingContracts = endpointMatchingContracts.stream() |
43
|
|
|
.filter(providerContract -> isIdMatching(consumerContract.getMessage(), providerContract.getMessage())) |
44
|
|
|
.collect(Collectors.toList()); |
45
|
|
|
|
46
|
|
|
if (idMatchingContracts.isEmpty()) { |
47
|
|
|
return ValidationResult.failure( |
48
|
|
|
consumerContract.toString(), |
49
|
|
|
Lists.newArrayList(MISSING_MESSAGE_WITH_ID)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (idMatchingContracts.size() > 1) { |
53
|
|
|
return ValidationResult.failure( |
54
|
|
|
consumerContract.toString(), |
55
|
|
|
Lists.newArrayList(MULTIPLE_MATCH)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
List<String> validationErrors = |
59
|
|
|
validateSchema(consumerContract.getMessage(), idMatchingContracts.get(0).getMessage()); |
60
|
|
|
|
61
|
|
|
return validationErrors.size() == 0 |
62
|
|
|
? ValidationResult.success(consumerContract.toString()) |
63
|
|
|
: ValidationResult.failure(consumerContract.toString(), validationErrors); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private boolean isEndpointMatching(Contract firstContract, Contract secondContract) { |
67
|
|
|
return isTmpQueueMatching(firstContract, secondContract) |
68
|
|
|
|| (isDstTypeMatching(firstContract, secondContract) |
69
|
|
|
&& isDstNameMatching(firstContract, secondContract)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private boolean isTmpQueueMatching(Contract firstContract, Contract secondContract) { |
73
|
|
|
return firstContract.getDestinationType() == DestinationType.TEMPORARY_QUEUE |
74
|
|
|
&& isDstTypeMatching(firstContract, secondContract); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private boolean isDstTypeMatching(Contract firstContract, Contract secondContract) { |
78
|
|
|
return firstContract.getDestinationType() == secondContract.getDestinationType(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private boolean isDstNameMatching(Contract firstContract, Contract secondContract) { |
82
|
|
|
return firstContract.getDestinationName().equals(secondContract.getDestinationName()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private boolean isIdMatching(JsonSchema consumerBody, JsonSchema providerBody) { |
86
|
|
|
return consumerBody.getId().equals(providerBody.getId()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private List<String> validateSchema(JsonSchema consumerBody, JsonSchema providerBody) { |
90
|
|
|
return SchemaValidator.validate(consumerBody, providerBody); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|