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.Expectation; |
5
|
|
|
import com.hltech.judged.server.domain.contracts.ServiceContracts; |
6
|
|
|
import lombok.Data; |
7
|
|
|
|
8
|
|
|
import java.io.Serializable; |
9
|
|
|
import java.time.Instant; |
10
|
|
|
import java.util.HashMap; |
11
|
|
|
import java.util.List; |
12
|
|
|
import java.util.Map; |
13
|
|
|
import java.util.stream.Collectors; |
14
|
|
|
|
15
|
|
|
import static com.google.common.collect.Maps.newHashMap; |
16
|
|
|
|
17
|
|
|
@Data |
18
|
|
|
public class ServiceContractsDto { |
19
|
|
|
|
20
|
|
|
private final String name; |
21
|
|
|
private final String version; |
22
|
|
|
|
23
|
|
|
private final Map<String, ContractDto> capabilities; |
24
|
|
|
private final Map<String, Map<String, ContractDto>> expectations; |
25
|
|
|
|
26
|
|
|
private final Instant publicationTime; |
27
|
|
|
|
28
|
|
|
public static ServiceContractsDto fromDomain(ServiceContracts serviceContracts) { |
29
|
|
|
return new ServiceContractsDto( |
30
|
|
|
serviceContracts.getName(), |
31
|
|
|
serviceContracts.getVersion(), |
32
|
|
|
mapCapabilitiesToDto(serviceContracts.getCapabilities()), |
33
|
|
|
mapExpectationsToDto(serviceContracts.getExpectations()), |
34
|
|
|
serviceContracts.getPublicationTime() |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
private static Map<String, ServiceContractsDto.ContractDto> mapCapabilitiesToDto(List<Capability> capabilities) { |
39
|
|
|
return capabilities.stream() |
40
|
|
|
.collect(Collectors.toMap( |
41
|
|
|
Capability::getProtocol, |
42
|
|
|
capability -> new ServiceContractsDto.ContractDto( |
43
|
|
|
capability.getContract().getValue(), |
44
|
|
|
capability.getContract().getMimeType()) |
45
|
|
|
)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
private static Map<String, Map<String, ServiceContractsDto.ContractDto>> mapExpectationsToDto(List<Expectation> expectations) { |
49
|
|
|
HashMap<String, Map<String, 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
|
|
|
@Data |
66
|
|
|
static class ContractDto implements Serializable { |
67
|
|
|
private final String value; |
68
|
|
|
private final String mimeType; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|