Passed
Push — master ( 68cf67...975f45 )
by Filip
03:08
created

com.hltech.vaunt.validator.VauntValidator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 36
dl 0
loc 56
rs 10
c 3
b 0
f 0
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A validate(Service,Service) 0 5 1
A validate(List,List) 0 4 1
A isDstNameMatching(Contract,Contract) 0 2 1
A validateWithMatchingProviderContract(Contract,List) 0 17 2
A isSchemaMatching(Contract,Contract) 0 2 1
A isTmpQueueMatching(Contract,Contract) 0 3 1
A isEndpointMatching(Contract,Contract) 0 4 1
A isDstTypeMatching(Contract,Contract) 0 2 1
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