Passed
Pull Request — master (#45)
by Filip
03:02
created

isDstNameMatching(Contract,Contract)   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
package com.hltech.vaunt.validator;
2
3
import com.hltech.vaunt.core.domain.model.Contract;
4
import com.hltech.vaunt.core.domain.model.DestinationType;
5
import com.hltech.vaunt.core.domain.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
        List<Contract> contracts = providerContracts.stream()
29
                .filter(providerContract -> isEndpointMatching(consumerContract, providerContract))
30
                .collect(Collectors.toList());
31
32
        if (contracts.isEmpty()) {
33
            return ValidationResult.failure(consumerContract, ValidationError.MISSING_ENDPOINT);
34
        }
35
36
        Optional<Contract> matchingProviderContract = contracts.stream()
37
                .filter(providerContract -> isSchemaMatching(consumerContract, providerContract))
38
                .findFirst();
39
40
        return matchingProviderContract
41
                .map(contract -> ValidationResult.success(consumerContract, contract))
42
                .orElseGet(() -> ValidationResult.failure(consumerContract, contracts, ValidationError.WRONG_SCHEMA));
43
44
    }
45
46
    private boolean isEndpointMatching(Contract firstContract, Contract secondContract) {
47
        return isTmpQueueMatching(firstContract, secondContract)
48
                || (isDstTypeMatching(firstContract, secondContract)
49
                && isDstNameMatching(firstContract, secondContract));
50
    }
51
52
    private boolean isTmpQueueMatching(Contract firstContract, Contract secondContract) {
53
        return firstContract.getDestinationType() == DestinationType.TEMPORARY_QUEUE
54
                && isDstTypeMatching(firstContract, secondContract);
55
    }
56
57
    private boolean isDstTypeMatching(Contract firstContract, Contract secondContract) {
58
        return firstContract.getDestinationType() == secondContract.getDestinationType();
59
    }
60
61
    private boolean isDstNameMatching(Contract firstContract, Contract secondContract) {
62
        return firstContract.getDestinationName().equals(secondContract.getDestinationName());
63
    }
64
65
    private boolean isSchemaMatching(Contract firstContract, Contract secondContract) {
66
        return firstContract.getBody().equals(secondContract.getBody());
67
    }
68
69
}
70