|
1
|
|
|
package com.hltech.judged.server.infrastructure.persistence.contracts; |
|
2
|
|
|
|
|
3
|
|
|
import com.hltech.judged.server.domain.ServiceId; |
|
4
|
|
|
import com.hltech.judged.server.domain.contracts.Capability; |
|
5
|
|
|
import com.hltech.judged.server.domain.contracts.Contract; |
|
6
|
|
|
import com.hltech.judged.server.domain.contracts.Expectation; |
|
7
|
|
|
import com.hltech.judged.server.domain.contracts.ServiceContracts; |
|
8
|
|
|
import com.hltech.judged.server.domain.contracts.ServiceContractsRepository; |
|
9
|
|
|
import lombok.RequiredArgsConstructor; |
|
10
|
|
|
import org.springframework.stereotype.Component; |
|
11
|
|
|
|
|
12
|
|
|
import javax.persistence.NoResultException; |
|
13
|
|
|
import java.util.List; |
|
14
|
|
|
import java.util.Map; |
|
15
|
|
|
import java.util.Optional; |
|
16
|
|
|
import java.util.stream.Collectors; |
|
17
|
|
|
|
|
18
|
|
|
@Component |
|
19
|
|
|
@RequiredArgsConstructor |
|
20
|
|
|
public class ServiceContractsRepositoryImpl implements ServiceContractsRepository { |
|
21
|
|
|
|
|
22
|
|
|
private final ServiceContractsTupleRepository serviceContractsTupleRepository; |
|
23
|
|
|
|
|
24
|
|
|
@Override |
|
25
|
|
|
public ServiceContracts persist(ServiceContracts serviceContracts) { |
|
26
|
|
|
return toServiceContracts(serviceContractsTupleRepository.save(toServiceContractsTuple(serviceContracts))); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
@Override |
|
30
|
|
|
public Optional<ServiceContracts> findOne(ServiceId serviceVersion) { |
|
31
|
|
|
return serviceContractsTupleRepository |
|
32
|
|
|
.findById_NameAndId_Version(serviceVersion.getName(), serviceVersion.getVersion()) |
|
33
|
|
|
.map(this::toServiceContracts); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
@Override |
|
37
|
|
|
public Optional<Contract> findCapabilityByServiceIdProtocol(ServiceId serviceId, String protocol) { |
|
38
|
|
|
return serviceContractsTupleRepository |
|
39
|
|
|
.findById_NameAndId_Version(serviceId.getName(), serviceId.getVersion()) |
|
40
|
|
|
.map(sc -> sc.getCapabilitiesPerProtocol().get(protocol)) |
|
41
|
|
|
.map(tuple -> new Contract(tuple.getValue(), tuple.getMimeType())); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
@Override |
|
45
|
|
|
public String getService(String name) { |
|
46
|
|
|
return serviceContractsTupleRepository.findById_Name(name).stream() |
|
47
|
|
|
.map(ServiceContractsTuple::getName) |
|
48
|
|
|
.findFirst() |
|
49
|
|
|
.orElseThrow(() -> new NoResultException("No service for specified name")); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
@Override |
|
53
|
|
|
public List<ServiceContracts> findAllByServiceName(String name) { |
|
54
|
|
|
return serviceContractsTupleRepository.findById_Name(name).stream() |
|
55
|
|
|
.map(this::toServiceContracts) |
|
56
|
|
|
.collect(Collectors.toList()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
@Override |
|
60
|
|
|
public List<String> getServiceNames() { |
|
61
|
|
|
return serviceContractsTupleRepository.findId_NameDistinct(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
private ServiceContractsTuple toServiceContractsTuple(ServiceContracts serviceContracts) { |
|
65
|
|
|
return new ServiceContractsTuple( |
|
66
|
|
|
new ServiceVersion(serviceContracts.getId().getName(), serviceContracts.getId().getVersion()), |
|
67
|
|
|
toCapabilities(serviceContracts), |
|
68
|
|
|
toExpectations(serviceContracts) |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
private Map<ServiceContractsTuple.ProviderProtocolTuple, ServiceContractsTuple.ContractTuple> |
|
73
|
|
|
toExpectations(ServiceContracts serviceContracts) { |
|
74
|
|
|
return serviceContracts.getExpectations().stream() |
|
75
|
|
|
.collect(Collectors.toMap( |
|
76
|
|
|
expectation -> new ServiceContractsTuple.ProviderProtocolTuple( |
|
77
|
|
|
expectation.getProvider(), |
|
78
|
|
|
expectation.getProtocol() |
|
79
|
|
|
), |
|
80
|
|
|
expectation -> new ServiceContractsTuple.ContractTuple( |
|
81
|
|
|
expectation.getContract().getValue(), |
|
82
|
|
|
expectation.getContract().getMimeType() |
|
83
|
|
|
) |
|
84
|
|
|
)); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
private Map<String, ServiceContractsTuple.ContractTuple> toCapabilities(ServiceContracts serviceContracts) { |
|
88
|
|
|
return serviceContracts.getCapabilities().stream() |
|
89
|
|
|
.collect(Collectors.toMap( |
|
90
|
|
|
Capability::getProtocol, |
|
91
|
|
|
capability -> new ServiceContractsTuple.ContractTuple( |
|
92
|
|
|
capability.getContract().getValue(), |
|
93
|
|
|
capability.getContract().getMimeType() |
|
94
|
|
|
) |
|
95
|
|
|
)); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
private ServiceContracts toServiceContracts(ServiceContractsTuple serviceContractsTuple) { |
|
99
|
|
|
return new ServiceContracts( |
|
100
|
|
|
new ServiceId(serviceContractsTuple.getId().getName(), serviceContractsTuple.getId().getVersion()), |
|
101
|
|
|
toCapabilities(serviceContractsTuple), |
|
102
|
|
|
toExpectations(serviceContractsTuple) |
|
103
|
|
|
); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
private List<Expectation> toExpectations(ServiceContractsTuple serviceContractsTuple) { |
|
107
|
|
|
return serviceContractsTuple.getExpectations().keySet().stream() |
|
108
|
|
|
.map(providerProtocol -> new Expectation( |
|
109
|
|
|
providerProtocol.getProvider(), |
|
110
|
|
|
providerProtocol.getProtocol(), |
|
111
|
|
|
new Contract( |
|
112
|
|
|
serviceContractsTuple.getExpectations().get(providerProtocol).getValue(), |
|
113
|
|
|
serviceContractsTuple.getExpectations().get(providerProtocol).getMimeType()))) |
|
114
|
|
|
.collect(Collectors.toList()); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
private List<Capability> toCapabilities(ServiceContractsTuple serviceContractsTuple) { |
|
118
|
|
|
return serviceContractsTuple.getCapabilitiesPerProtocol().keySet().stream() |
|
119
|
|
|
.map(protocol -> new Capability( |
|
120
|
|
|
protocol, |
|
121
|
|
|
new Contract( |
|
122
|
|
|
serviceContractsTuple.getCapabilitiesPerProtocol().get(protocol).getValue(), |
|
123
|
|
|
serviceContractsTuple.getCapabilitiesPerProtocol().get(protocol).getMimeType()))) |
|
124
|
|
|
.collect(Collectors.toList()); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|