isTmpQueueMatching(Contract,Contract)   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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