|
1
|
|
|
package dev.hltech.dredd.domain; |
|
2
|
|
|
|
|
3
|
|
|
import com.google.common.collect.Maps; |
|
4
|
|
|
import dev.hltech.dredd.domain.contracts.ServiceContracts; |
|
5
|
|
|
import dev.hltech.dredd.domain.contracts.ServiceContractsRepository; |
|
6
|
|
|
import dev.hltech.dredd.domain.environment.EnvironmentRepository; |
|
7
|
|
|
import dev.hltech.dredd.domain.validation.EnvironmentValidatorResult; |
|
8
|
|
|
import dev.hltech.dredd.domain.validation.InterfaceContractValidator; |
|
9
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
10
|
|
|
import org.springframework.stereotype.Component; |
|
11
|
|
|
|
|
12
|
|
|
import java.util.Collection; |
|
13
|
|
|
import java.util.Iterator; |
|
14
|
|
|
import java.util.List; |
|
15
|
|
|
import java.util.Map; |
|
16
|
|
|
import java.util.Optional; |
|
17
|
|
|
import java.util.function.Function; |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
import static java.util.stream.Collectors.toList; |
|
20
|
|
|
import static java.util.stream.Collectors.toMap; |
|
21
|
|
|
|
|
22
|
|
|
@Component |
|
23
|
|
|
public class JudgeD { |
|
24
|
|
|
|
|
25
|
|
|
private EnvironmentRepository environmentRepository; |
|
26
|
|
|
private ServiceContractsRepository serviceContractsRepository; |
|
27
|
|
|
|
|
28
|
|
|
@Autowired |
|
29
|
|
|
public JudgeD(EnvironmentRepository environmentRepository, ServiceContractsRepository serviceContractsRepository) { |
|
30
|
|
|
this.environmentRepository = environmentRepository; |
|
31
|
|
|
this.serviceContractsRepository = serviceContractsRepository; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public <C, E> EnvironmentValidatorResult validateServiceAgainstEnvironments( |
|
35
|
|
|
ServiceContracts contractsToValidate, |
|
36
|
|
|
List<String> environments, |
|
37
|
|
|
InterfaceContractValidator<C, E> validator |
|
38
|
|
|
) { |
|
39
|
|
|
List<ServiceContracts> environmentContracts = environments.stream() |
|
40
|
|
|
.flatMap(env -> this.environmentRepository.get(env).getAllServices().stream()) |
|
41
|
|
|
.map(sv -> this.serviceContractsRepository.findOne(sv)) |
|
42
|
|
|
.filter(Optional::isPresent) |
|
43
|
|
|
.map(Optional::get) |
|
44
|
|
|
.collect(toList()); |
|
45
|
|
|
|
|
46
|
|
|
return getValidatorResult(contractsToValidate, environmentContracts, validator); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public <C, E> Map<ServiceVersion, EnvironmentValidatorResult> validatedServicesAgainstEnvironment( |
|
50
|
|
|
List<ServiceContracts> contractToValidate, |
|
51
|
|
|
String env, |
|
52
|
|
|
InterfaceContractValidator<C, E> validator |
|
53
|
|
|
){ |
|
54
|
|
|
// find contracts on given env |
|
55
|
|
|
List<ServiceContracts> environmentContracts = this.environmentRepository.get(env).getAllServices() |
|
56
|
|
|
.stream() |
|
57
|
|
|
.map(sv -> this.serviceContractsRepository.findOne(sv)) |
|
58
|
|
|
.filter(Optional::isPresent) |
|
59
|
|
|
.map(Optional::get) |
|
60
|
|
|
.collect(toList()); |
|
61
|
|
|
|
|
62
|
|
|
// replace environment contracts with validated ones |
|
63
|
|
|
contractToValidate.forEach(validatedContract -> { |
|
64
|
|
|
Iterator<ServiceContracts> iterator = environmentContracts.iterator(); |
|
65
|
|
|
while (iterator.hasNext()) { |
|
66
|
|
|
if (iterator.next().getName().equals(validatedContract.getId().getName())) { |
|
67
|
|
|
iterator.remove(); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
environmentContracts.add(validatedContract); |
|
71
|
|
|
}); |
|
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
|
|
|
|