Completed
Branch master (034e5d)
by Tomasz
03:17 queued 20s
created

validateServiceAgainstEnvironments(ServiceContracts,List,InterfaceContractValidator)   A

Complexity

Conditions 1

Size

Total Lines 13
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 13
c 0
b 0
f 0
cc 1
rs 9.8
1
package com.hltech.judged.server.domain;
2
3
import com.google.common.base.Joiner;
4
import com.google.common.collect.Maps;
5
import com.hltech.judged.server.domain.environment.EnvironmentRepository;
6
import com.hltech.judged.server.domain.contracts.ServiceContracts;
7
import com.hltech.judged.server.domain.contracts.ServiceContractsRepository;
8
import com.hltech.judged.server.domain.validation.EnvironmentValidatorResult;
9
import com.hltech.judged.server.domain.validation.InterfaceContractValidator;
10
import lombok.extern.slf4j.Slf4j;
11
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.stereotype.Component;
13
14
import java.util.Collection;
15
import java.util.Iterator;
16
import java.util.List;
17
import java.util.Map;
18
import java.util.Optional;
19
20
import static java.util.stream.Collectors.toList;
21
import static java.util.stream.Collectors.toMap;
22
23
@Slf4j
24
@Component
25
public class JudgeD {
26
27
    private EnvironmentRepository environmentRepository;
28
    private ServiceContractsRepository serviceContractsRepository;
29
30
    @Autowired
31
    public JudgeD(EnvironmentRepository environmentRepository, ServiceContractsRepository serviceContractsRepository) {
32
        this.environmentRepository = environmentRepository;
33
        this.serviceContractsRepository = serviceContractsRepository;
34
    }
35
36
    public <C, E> EnvironmentValidatorResult validateServiceAgainstEnvironments(
37
        ServiceContracts contractsToValidate,
38
        List<String> environments,
39
        InterfaceContractValidator<C, E> validator
40
    ) {
41
        List<ServiceContracts> environmentContracts = environments.stream()
42
            .flatMap(env -> this.environmentRepository.get(env).getAllServices().stream())
43
            .map(sv -> this.serviceContractsRepository.findOne(sv))
44
            .filter(Optional::isPresent)
45
            .map(Optional::get)
46
            .collect(toList());
47
48
        return getValidatorResult(contractsToValidate, environmentContracts, validator);
49
    }
50
51
    public <C, E> Map<ServiceVersion, EnvironmentValidatorResult> validatedServicesAgainstEnvironment(
52
        List<ServiceContracts> contractToValidate,
53
        String env,
54
        InterfaceContractValidator<C, E> validator
55
    ){
56
        // find contracts on given env
57
        List<ServiceContracts> environmentContracts = this.environmentRepository.get(env).getAllServices()
58
            .stream()
59
            .map(sv -> this.serviceContractsRepository.findOne(sv))
60
            .filter(Optional::isPresent)
61
            .map(Optional::get)
62
            .collect(toList());
63
64
        log.info("checking how " +
65
            "["+ Joiner.on(", ").join(contractToValidate.stream().map(sc -> sc.getId().toString()).collect(toList()))+"] will impact the env " +
66
            "["+ Joiner.on(", ").join(environmentContracts.stream().map(sc -> sc.getId().toString()).collect(toList()))+"]");
67
68
        // replace environment contracts with validated ones
69
        contractToValidate.forEach(validatedContract -> {
70
            Iterator<ServiceContracts> iterator = environmentContracts.iterator();
71
            while (iterator.hasNext()) {
72
                if (iterator.next().getName().equals(validatedContract.getId().getName())) {
73
                    iterator.remove();
74
                }
75
            }
76
            environmentContracts.add(validatedContract);
77
        });
78
79
        log.info("after the deployment env will contain: ["+Joiner.on(", ").join(environmentContracts.stream().map(sc -> sc.getId().toString()).collect(toList()))+"]");
80
81
        Map<ServiceVersion, EnvironmentValidatorResult> hashMap = Maps.newHashMap();
82
        for (ServiceContracts sc : contractToValidate) {
83
            hashMap.put(sc.getId(), getValidatorResult(sc, environmentContracts, validator));
84
        }
85
        return hashMap;
86
    }
87
88
    public <C, E> EnvironmentValidatorResult getValidatorResult(
89
        ServiceContracts validatedService,
90
        Collection<ServiceContracts> environmentContracts,
91
        InterfaceContractValidator<C, E> validator
92
    ) {
93
        return new EnvironmentValidatorResult(
94
            validator.getCommunicationInterface(),
95
            validator.validateCapabilities(validatedService, environmentContracts),
96
            validator.validateExpectations(validatedService, environmentContracts)
97
        );
98
    }
99
}
100