1
|
|
|
package com.hltech.judged.server.interfaces.rest.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 lombok.Data; |
9
|
|
|
|
10
|
|
|
import java.io.Serializable; |
11
|
|
|
import java.util.List; |
12
|
|
|
import java.util.Map; |
13
|
|
|
import java.util.stream.Collectors; |
14
|
|
|
|
15
|
|
|
@Data |
16
|
|
|
class ServiceContractsForm { |
17
|
|
|
|
18
|
|
|
//<protocol, contract> |
19
|
|
|
private final Map<String, ContractForm> capabilities; |
20
|
|
|
//<provider, protocol, contract> |
21
|
|
|
private final Map<String, Map<String, ContractForm>> expectations; |
22
|
|
|
|
23
|
|
|
ServiceContracts toDomain(String serviceName, String version) { |
24
|
|
|
return new ServiceContracts( |
25
|
|
|
new ServiceId(serviceName, version), |
26
|
|
|
mapCapabilitiesForm(this.capabilities), |
27
|
|
|
mapExpectationsForm(this.expectations) |
28
|
|
|
); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
private List<Capability> mapCapabilitiesForm(Map<String, ServiceContractsForm.ContractForm> capabilities) { |
32
|
|
|
return capabilities.keySet().stream() |
33
|
|
|
.map(protocol -> new Capability(protocol, new Contract(capabilities.get(protocol).getValue(), capabilities.get(protocol).getMimeType()))) |
34
|
|
|
.collect(Collectors.toList()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
private List<Expectation> mapExpectationsForm(Map<String, Map<String, ServiceContractsForm.ContractForm>> expectations) { |
38
|
|
|
return expectations.keySet().stream() |
39
|
|
|
.flatMap(provider -> expectations.get(provider).keySet().stream() |
40
|
|
|
.map(protocol -> new Expectation( |
41
|
|
|
provider, protocol, |
42
|
|
|
new Contract( |
43
|
|
|
expectations.get(provider).get(protocol).getValue(), |
44
|
|
|
expectations.get(provider).get(protocol).getMimeType() |
45
|
|
|
) |
46
|
|
|
)) |
47
|
|
|
) |
48
|
|
|
.collect(Collectors.toList()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
@Data |
52
|
|
|
static class ContractForm implements Serializable { |
53
|
|
|
private final String value; |
54
|
|
|
private final String mimeType; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|