Passed
Push — master ( 931bc2...fdbf8c )
by Tomasz
06:06
created

validateServiceAgainstEnv(ServiceContracts,List,InterfaceContractValidator)   A

Complexity

Conditions 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
dl 0
loc 13
rs 9.85
c 0
b 0
f 0
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 validateServiceAgainstEnv(ServiceContracts validatedService, String environment, InterfaceContractValidator<C, E> validator) {
29
        List<ServiceContracts> environmentContracts = this.environmentRepository.get(environment)
30
            .getAllServices()
31
            .stream()
32
            .map(sv -> this.serviceContractsRepository.find(sv.getName(), sv.getVersion()))
33
            .filter(it -> it.isPresent())
34
            .map(it -> it.get())
35
            .collect(toList());
36
37
        return new EnvironmentValidatorResult(
38
            validator.getCommunicationInterface(),
39
            validator.validateCapabilities(validatedService, environmentContracts),
40
            validator.validateExpectations(validatedService, environmentContracts)
41
        );
42
    }
43
44
    public <C, E> EnvironmentValidatorResult validateServiceAgainstEnv(ServiceContracts validatedService, List<String> environments, InterfaceContractValidator<C, E> validator) {
45
46
        List<ServiceContracts> environmentContracts = environments.stream()
47
            .flatMap(env -> this.environmentRepository.get(env).getAllServices().stream())
48
            .map(sv -> this.serviceContractsRepository.find(sv.getName(), sv.getVersion()))
49
            .filter(Optional::isPresent)
50
            .map(Optional::get)
51
            .collect(toList());
52
53
        return new EnvironmentValidatorResult(
54
            validator.getCommunicationInterface(),
55
            validator.validateCapabilities(validatedService, environmentContracts),
56
            validator.validateExpectations(validatedService, environmentContracts)
57
        );
58
    }
59
}
60