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.RequiredArgsConstructor; |
11
|
|
|
import lombok.extern.slf4j.Slf4j; |
12
|
|
|
|
13
|
|
|
import java.util.Collection; |
14
|
|
|
import java.util.Iterator; |
15
|
|
|
import java.util.List; |
16
|
|
|
import java.util.Map; |
17
|
|
|
import java.util.Optional; |
18
|
|
|
|
19
|
|
|
import static java.util.stream.Collectors.toList; |
20
|
|
|
|
21
|
|
|
@Slf4j |
22
|
|
|
@RequiredArgsConstructor |
23
|
|
|
public class JudgeDApplicationService { |
24
|
|
|
|
25
|
|
|
private final EnvironmentRepository environmentRepository; |
26
|
|
|
private final ServiceContractsRepository serviceContractsRepository; |
27
|
|
|
|
28
|
|
|
public <C, E> EnvironmentValidatorResult validateServiceAgainstEnvironments( |
29
|
|
|
ServiceContracts contractsToValidate, |
30
|
|
|
List<String> environments, |
31
|
|
|
InterfaceContractValidator<C, E> validator |
32
|
|
|
) { |
33
|
|
|
List<ServiceContracts> environmentContracts = environments.stream() |
34
|
|
|
.flatMap(env -> this.environmentRepository.get(env).getAllServices().stream()) |
35
|
|
|
.map(sv -> this.serviceContractsRepository.findOne(sv)) |
36
|
|
|
.filter(Optional::isPresent) |
37
|
|
|
.map(Optional::get) |
38
|
|
|
.collect(toList()); |
39
|
|
|
|
40
|
|
|
return getValidatorResult(contractsToValidate, environmentContracts, validator); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public <C, E> Map<ServiceVersion, EnvironmentValidatorResult> validatedServicesAgainstEnvironment( |
44
|
|
|
List<ServiceContracts> contractToValidate, |
45
|
|
|
String env, |
46
|
|
|
InterfaceContractValidator<C, E> validator |
47
|
|
|
){ |
48
|
|
|
// find contracts on given env |
49
|
|
|
List<ServiceContracts> environmentContracts = this.environmentRepository.get(env).getAllServices() |
50
|
|
|
.stream() |
51
|
|
|
.map(sv -> this.serviceContractsRepository.findOne(sv)) |
52
|
|
|
.filter(Optional::isPresent) |
53
|
|
|
.map(Optional::get) |
54
|
|
|
.collect(toList()); |
55
|
|
|
|
56
|
|
|
log.info("checking how " + |
57
|
|
|
"["+ Joiner.on(", ").join(contractToValidate.stream().map(sc -> sc.getId().toString()).collect(toList()))+"] will impact the env " + |
58
|
|
|
"["+ Joiner.on(", ").join(environmentContracts.stream().map(sc -> sc.getId().toString()).collect(toList()))+"]"); |
59
|
|
|
|
60
|
|
|
// replace environment contracts with validated ones |
61
|
|
|
contractToValidate.forEach(validatedContract -> { |
62
|
|
|
Iterator<ServiceContracts> iterator = environmentContracts.iterator(); |
63
|
|
|
while (iterator.hasNext()) { |
64
|
|
|
if (iterator.next().getName().equals(validatedContract.getId().getName())) { |
65
|
|
|
iterator.remove(); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
environmentContracts.add(validatedContract); |
69
|
|
|
}); |
70
|
|
|
|
71
|
|
|
log.info("after the deployment env will contain: ["+Joiner.on(", ").join(environmentContracts.stream().map(sc -> sc.getId().toString()).collect(toList()))+"]"); |
72
|
|
|
|
73
|
|
|
Map<ServiceVersion, EnvironmentValidatorResult> hashMap = Maps.newHashMap(); |
74
|
|
|
for (ServiceContracts sc : contractToValidate) { |
75
|
|
|
hashMap.put(sc.getId(), getValidatorResult(sc, environmentContracts, validator)); |
76
|
|
|
} |
77
|
|
|
return hashMap; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public <C, E> EnvironmentValidatorResult getValidatorResult( |
81
|
|
|
ServiceContracts validatedService, |
82
|
|
|
Collection<ServiceContracts> environmentContracts, |
83
|
|
|
InterfaceContractValidator<C, E> validator |
84
|
|
|
) { |
85
|
|
|
return new EnvironmentValidatorResult( |
86
|
|
|
validator.getCommunicationInterface(), |
87
|
|
|
validator.validateCapabilities(validatedService, environmentContracts), |
88
|
|
|
validator.validateExpectations(validatedService, environmentContracts) |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|