1
|
|
|
package dev.hltech.dredd.domain.contracts; |
2
|
|
|
|
3
|
|
|
import lombok.AllArgsConstructor; |
4
|
|
|
import lombok.EqualsAndHashCode; |
5
|
|
|
import lombok.Getter; |
6
|
|
|
import lombok.NoArgsConstructor; |
7
|
|
|
|
8
|
|
|
import javax.persistence.*; |
9
|
|
|
import java.io.Serializable; |
10
|
|
|
import java.util.HashMap; |
11
|
|
|
import java.util.Map; |
12
|
|
|
import java.util.Map.Entry; |
13
|
|
|
import java.util.Optional; |
14
|
|
|
import java.util.function.Function; |
15
|
|
|
|
16
|
|
|
import static com.google.common.collect.Maps.newHashMap; |
17
|
|
|
import static java.util.Optional.ofNullable; |
18
|
|
|
import static java.util.stream.Collectors.toMap; |
19
|
|
|
|
20
|
|
|
@Entity |
21
|
|
|
@Getter |
22
|
|
|
@Access(AccessType.FIELD) |
23
|
|
|
public class ServiceContracts { |
24
|
|
|
|
25
|
|
|
@EmbeddedId |
26
|
|
|
private ServiceContractsId id; |
27
|
|
|
|
28
|
|
|
@ElementCollection(fetch = FetchType.EAGER) |
29
|
|
|
@MapKeyColumn(name = "protocol") |
30
|
|
|
@JoinTable(name = "capabilities", joinColumns = { |
31
|
|
|
@JoinColumn(name = "service_name", referencedColumnName = "name"), |
32
|
|
|
@JoinColumn(name = "service_version", referencedColumnName = "version") |
33
|
|
|
}) |
34
|
|
|
private Map<String, Contract> capabilitiesPerProtocol; |
35
|
|
|
|
36
|
|
|
@ElementCollection(fetch = FetchType.EAGER) |
37
|
|
|
@JoinTable(name = "expectations", joinColumns = { |
38
|
|
|
@JoinColumn(name = "service_name", referencedColumnName = "name"), |
39
|
|
|
@JoinColumn(name = "service_version", referencedColumnName = "version") |
40
|
|
|
}) |
41
|
|
|
private Map<ProviderProtocol, Contract> expectations; |
42
|
|
|
|
43
|
|
|
protected ServiceContracts() { |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public ServiceContracts(String name, String version, Map<String, Contract> capabilitiesPerProtocol, Map<String, Map<String, Contract>> expectationsPerProvider) { |
47
|
|
|
this.id = new ServiceContractsId(name, version); |
48
|
|
|
this.capabilitiesPerProtocol = capabilitiesPerProtocol; |
49
|
|
|
this.expectations = newHashMap(); |
50
|
|
|
for (Entry<String, Map<String, Contract>> expectationsPerProviderEntry : expectationsPerProvider.entrySet()) { |
51
|
|
|
String provider = expectationsPerProviderEntry.getKey(); |
52
|
|
|
Map<String, Contract> expectationsPerProtocol = expectationsPerProviderEntry.getValue(); |
53
|
|
|
|
54
|
|
|
for (Entry<String, Contract> expectationsPerProtocolEntry : expectationsPerProtocol.entrySet()) { |
55
|
|
|
String protocol = expectationsPerProtocolEntry.getKey(); |
56
|
|
|
this.expectations.put(new ProviderProtocol(provider, protocol), expectationsPerProtocolEntry.getValue()); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public String getName() { |
62
|
|
|
return this.id.getName(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public String getVersion() { |
66
|
|
|
return this.id.getVersion(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
public <C> Optional<C> getMappedCapabilities(String communicationInterface, Function<String, C> deserializer) { |
71
|
|
|
return ofNullable(this.capabilitiesPerProtocol.get(communicationInterface)) |
72
|
|
|
.map(Contract::getValue) |
73
|
|
|
.map(deserializer); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public <E> Optional<E> getMappedExpectations(String providerName, String communicationInterface, Function<String, E> deserializer) { |
77
|
|
|
return ofNullable(this.expectations.get(new ProviderProtocol(providerName, communicationInterface))) |
78
|
|
|
.map(Contract::getValue) |
79
|
|
|
.map(deserializer); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public <E> Map<String, E> getMappedExpectations(String communicationInterface, Function<String, E> deserializer) { |
83
|
|
|
return this.expectations.entrySet() |
84
|
|
|
.stream() |
85
|
|
|
.filter(expectationsEntry -> expectationsEntry.getKey().getProtocol().equals(communicationInterface)) |
86
|
|
|
.collect(toMap( |
87
|
|
|
e -> e.getKey().getProvider(), |
88
|
|
|
e -> deserializer.apply(e.getValue().getValue()) |
89
|
|
|
)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public Map<String, String> getMappedCapabilities() { |
93
|
|
|
return this.capabilitiesPerProtocol.entrySet().stream().collect(toMap( |
94
|
|
|
e -> e.getKey(), |
95
|
|
|
e -> e.getValue().getValue() |
96
|
|
|
)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public Map<String, Map<String, String>> getMappedExpectations() { |
100
|
|
|
HashMap<String, Map<String, String>> result = newHashMap(); |
101
|
|
|
for (Entry<ProviderProtocol, Contract> e : this.expectations.entrySet()) { |
102
|
|
|
ProviderProtocol pp = e.getKey(); |
103
|
|
|
Contract contract = e.getValue(); |
104
|
|
|
if (!result.containsKey(pp.provider)) { |
105
|
|
|
result.put(pp.provider, newHashMap()); |
106
|
|
|
} |
107
|
|
|
result.get(pp.getProvider()).put(pp.getProtocol(), contract.getValue()); |
108
|
|
|
} |
109
|
|
|
return result; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
@Getter |
113
|
|
|
@Embeddable |
114
|
|
|
@AllArgsConstructor |
115
|
|
|
@NoArgsConstructor |
116
|
|
|
@Access(AccessType.FIELD) |
117
|
|
|
static class ServiceContractsId implements Serializable { |
118
|
|
|
private String name; |
119
|
|
|
private String version; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
@Getter |
123
|
|
|
@Embeddable |
124
|
|
|
@AllArgsConstructor |
125
|
|
|
@NoArgsConstructor |
126
|
|
|
@Access(AccessType.FIELD) |
127
|
|
|
@EqualsAndHashCode |
128
|
|
|
public static class ProviderProtocol { |
129
|
|
|
private String provider; |
130
|
|
|
private String protocol; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
@Getter |
134
|
|
|
@Embeddable |
135
|
|
|
@AllArgsConstructor |
136
|
|
|
@NoArgsConstructor |
137
|
|
|
@Access(AccessType.FIELD) |
138
|
|
|
public static class Contract implements Serializable { |
139
|
|
|
private String value; |
140
|
|
|
private String mimeType; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|