Passed
Push — master ( d9dbdd...a550db )
by Tomasz
03:52
created

dev.hltech.dredd.domain.JudgeD   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 24
c 2
b 0
f 0
dl 0
loc 32
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A JudgeD(EnvironmentRepository,ServiceContractsRepository) 0 4 1
A getValidatorResult(ServiceContracts,List,InterfaceContractValidator) 0 9 1
A validateServiceAgainstEnvironments(ServiceContracts,List,InterfaceContractValidator) 0 9 1
1
package dev.hltech.dredd.domain;
2
3
import dev.hltech.dredd.domain.contracts.ServiceContracts;
4
import dev.hltech.dredd.domain.contracts.ServiceContractsRepository;
5
import dev.hltech.dredd.domain.environment.EnvironmentRepository;
6
import dev.hltech.dredd.domain.validation.EnvironmentValidatorResult;
7
import dev.hltech.dredd.domain.validation.InterfaceContractValidator;
8
import org.springframework.beans.factory.annotation.Autowired;
9
import org.springframework.stereotype.Component;
10
11
import java.util.List;
12
import java.util.Optional;
13
14
import static java.util.stream.Collectors.toList;
15
16
@Component
17
public class JudgeD {
18
19
    private EnvironmentRepository environmentRepository;
20
    private ServiceContractsRepository serviceContractsRepository;
21
22
    @Autowired
23
    public JudgeD(EnvironmentRepository environmentRepository, ServiceContractsRepository serviceContractsRepository) {
24
        this.environmentRepository = environmentRepository;
25
        this.serviceContractsRepository = serviceContractsRepository;
26
    }
27
28
    public <C, E> EnvironmentValidatorResult validateServiceAgainstEnvironments(ServiceContracts validatedService, List<String> environments, InterfaceContractValidator<C, E> validator) {
29
        List<ServiceContracts> environmentContracts = environments.stream()
30
            .flatMap(env -> this.environmentRepository.get(env).getAllServices().stream())
31
            .map(sv -> this.serviceContractsRepository.find(sv.getName(), sv.getVersion()))
32
            .filter(Optional::isPresent)
33
            .map(Optional::get)
34
            .collect(toList());
35
36
        return getValidatorResult(validatedService, environmentContracts, validator);
37
    }
38
39
    public <C, E> EnvironmentValidatorResult getValidatorResult(
40
        ServiceContracts validatedService,
41
        List<ServiceContracts> environmentContracts,
42
        InterfaceContractValidator<C, E> validator
43
    ) {
44
        return new EnvironmentValidatorResult(
45
            validator.getCommunicationInterface(),
46
            validator.validateCapabilities(validatedService, environmentContracts),
47
            validator.validateExpectations(validatedService, environmentContracts)
48
        );
49
    }
50
}
51