1
|
|
|
package com.hltech.judged.server.interfaces.rest.contracts; |
2
|
|
|
|
3
|
|
|
import com.hltech.judged.server.domain.contracts.Capability; |
4
|
|
|
import com.hltech.judged.server.domain.contracts.Contract; |
5
|
|
|
import com.hltech.judged.server.domain.contracts.Expectation; |
6
|
|
|
import com.hltech.judged.server.domain.contracts.ServiceContracts; |
7
|
|
|
import org.springframework.stereotype.Service; |
8
|
|
|
|
9
|
|
|
import java.util.HashMap; |
10
|
|
|
import java.util.List; |
11
|
|
|
import java.util.Map; |
12
|
|
|
import java.util.stream.Collectors; |
13
|
|
|
|
14
|
|
|
import static com.google.common.collect.Maps.newHashMap; |
15
|
|
|
|
16
|
|
|
@Service |
17
|
|
|
public class ContractsMapper { |
18
|
|
|
|
19
|
|
|
List<Capability> mapCapabilitiesForm(Map<String, ServiceContractsForm.ContractForm> capabilities) { |
20
|
|
|
return capabilities.keySet().stream() |
21
|
|
|
.map(protocol -> new Capability(protocol, new Contract(capabilities.get(protocol).getValue(), capabilities.get(protocol).getMimeType()))) |
22
|
|
|
.collect(Collectors.toList()); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
List<Expectation> mapExpectationsForm(Map<String, Map<String, ServiceContractsForm.ContractForm>> expectations) { |
26
|
|
|
return expectations.keySet().stream() |
27
|
|
|
.flatMap(provider -> expectations.get(provider).keySet().stream() |
28
|
|
|
.map(protocol -> new Expectation( |
29
|
|
|
provider, protocol, |
30
|
|
|
new Contract( |
31
|
|
|
expectations.get(provider).get(protocol).getValue(), |
32
|
|
|
expectations.get(provider).get(protocol).getMimeType() |
33
|
|
|
) |
34
|
|
|
)) |
35
|
|
|
) |
36
|
|
|
.collect(Collectors.toList()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public ServiceContractsDto toDto(ServiceContracts serviceContracts) { |
40
|
|
|
return new ServiceContractsDto( |
41
|
|
|
serviceContracts.getName(), |
42
|
|
|
serviceContracts.getVersion(), |
43
|
|
|
mapCapabilitiesToDto(serviceContracts.getCapabilities()), |
44
|
|
|
mapExpectationsToDto(serviceContracts.getExpectations()) |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
private Map<String, Map<String, ServiceContractsDto.ContractDto>> mapExpectationsToDto(List<Expectation> expectations) { |
49
|
|
|
HashMap<String, Map<String, ServiceContractsDto.ContractDto>> result = newHashMap(); |
50
|
|
|
for (Expectation expectation : expectations) { |
51
|
|
|
if (!result.containsKey(expectation.getProvider())) { |
52
|
|
|
result.put(expectation.getProvider(), newHashMap()); |
53
|
|
|
} |
54
|
|
|
result.get(expectation.getProvider()).put( |
55
|
|
|
expectation.getProtocol(), |
56
|
|
|
new ServiceContractsDto.ContractDto( |
57
|
|
|
expectation.getContract().getValue(), |
58
|
|
|
expectation.getContract().getMimeType() |
59
|
|
|
) |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
return result; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private Map<String, ServiceContractsDto.ContractDto> mapCapabilitiesToDto(List<Capability> capabilities) { |
66
|
|
|
return capabilities.stream() |
67
|
|
|
.collect(Collectors.toMap( |
68
|
|
|
Capability::getProtocol, |
69
|
|
|
capability -> new ServiceContractsDto.ContractDto( |
70
|
|
|
capability.getContract().getValue(), |
71
|
|
|
capability.getContract().getMimeType()) |
72
|
|
|
)); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|