1
|
|
|
package dev.hltech.dredd.domain.validation; |
2
|
|
|
|
3
|
|
|
import dev.hltech.dredd.domain.contracts.ServiceContracts; |
4
|
|
|
import dev.hltech.dredd.domain.contracts.ServiceContractsRepository; |
5
|
|
|
import dev.hltech.dredd.domain.environment.EnvironmentAggregate; |
6
|
|
|
import dev.hltech.dredd.domain.environment.EnvironmentRepository; |
7
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
8
|
|
|
import org.springframework.stereotype.Component; |
9
|
|
|
|
10
|
|
|
import java.util.Collection; |
11
|
|
|
import java.util.List; |
12
|
|
|
import java.util.Optional; |
13
|
|
|
import java.util.Set; |
14
|
|
|
import java.util.stream.Collectors; |
15
|
|
|
|
16
|
|
|
import static com.google.common.collect.Lists.newArrayList; |
17
|
|
|
import static dev.hltech.dredd.domain.validation.InterfaceContractValidator.InteractionValidationStatus.FAILED; |
18
|
|
|
import static java.util.stream.Collectors.toList; |
19
|
|
|
|
20
|
|
|
@Component |
21
|
|
|
public class JudgeD { |
22
|
|
|
|
23
|
|
|
private EnvironmentRepository environmentRepository; |
24
|
|
|
private ServiceContractsRepository serviceContractsRepository; |
25
|
|
|
|
26
|
|
|
@Autowired |
27
|
|
|
public JudgeD(EnvironmentRepository environmentRepository, ServiceContractsRepository serviceContractsRepository) { |
28
|
|
|
this.environmentRepository = environmentRepository; |
29
|
|
|
this.serviceContractsRepository = serviceContractsRepository; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public <C, E> ContractValidator<C, E> createContractValidator(String environment, InterfaceContractValidator<C, E> interfaceContractValidator) { |
33
|
|
|
return new ContractValidator(environment, interfaceContractValidator); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public class ContractValidator<C, E> { |
37
|
|
|
|
38
|
|
|
public final InterfaceContractValidator.InteractionValidationResult INTERACTION_VALIDATION_REPORT_4_NO_PROVIDER = new InterfaceContractValidator.InteractionValidationResult( |
39
|
|
|
"any", |
40
|
|
|
FAILED, |
41
|
|
|
newArrayList("provider not registered") |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
private String environment; |
45
|
|
|
private InterfaceContractValidator<C, E> interfaceContractValidator; |
46
|
|
|
|
47
|
|
|
public ContractValidator(String environment, InterfaceContractValidator<C, E> interfaceContractValidator) { |
48
|
|
|
this.environment = environment; |
49
|
|
|
this.interfaceContractValidator = interfaceContractValidator; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public List<InterfaceContractValidator.CapabilitiesValidationResult> validateCapabilities(String providerName, C capabilities) { |
53
|
|
|
Set<ServiceContracts> allRegisteredServices = environmentRepository.get(environment).getAllServices() |
54
|
|
|
.stream() |
55
|
|
|
.map(sv -> serviceContractsRepository.find(sv.getName(), sv.getVersion())) |
56
|
|
|
.filter(Optional::isPresent) |
57
|
|
|
.map(Optional::get) |
58
|
|
|
.collect(Collectors.toSet()); |
59
|
|
|
|
60
|
|
|
return allRegisteredServices.stream() |
61
|
|
|
.map(consumer -> interfaceContractValidator.validate(consumer, providerName, capabilities)) |
62
|
|
|
.filter(result -> !result.isEmpty()) |
63
|
|
|
.collect(toList()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public List<InterfaceContractValidator.ExpectationValidationResult> validateExpectations(String providerName, E expectations) throws ProviderNotAvailableException { |
67
|
|
|
Collection<EnvironmentAggregate.ServiceVersion> providersOnEnv = environmentRepository |
68
|
|
|
.get(environment) |
69
|
|
|
.findServices(providerName); |
70
|
|
|
|
71
|
|
|
if (providersOnEnv.isEmpty()) |
72
|
|
|
throw new ProviderNotAvailableException(); |
73
|
|
|
|
74
|
|
|
return providersOnEnv |
75
|
|
|
.stream() |
76
|
|
|
.map(provider -> { |
77
|
|
|
Optional<ServiceContracts> osc = serviceContractsRepository.find(provider.getName(), provider.getVersion()); |
78
|
|
|
if (osc.isPresent()) { |
79
|
|
|
return interfaceContractValidator.validate(osc.get(), expectations); |
80
|
|
|
} else { |
81
|
|
|
return new InterfaceContractValidator.ExpectationValidationResult( |
82
|
|
|
provider.getName(), |
83
|
|
|
provider.getVersion(), |
84
|
|
|
newArrayList(INTERACTION_VALIDATION_REPORT_4_NO_PROVIDER) |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
}) |
88
|
|
|
.collect(toList()); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|