|
1
|
|
|
package com.hltech.judged.server.domain.contracts; |
|
2
|
|
|
|
|
3
|
|
|
import com.hltech.judged.server.domain.ServiceId; |
|
4
|
|
|
import lombok.EqualsAndHashCode; |
|
5
|
|
|
import lombok.Getter; |
|
6
|
|
|
import lombok.ToString; |
|
7
|
|
|
import lombok.experimental.Delegate; |
|
8
|
|
|
|
|
9
|
|
|
import java.time.Instant; |
|
10
|
|
|
import java.util.ArrayList; |
|
11
|
|
|
import java.util.List; |
|
12
|
|
|
import java.util.Map; |
|
13
|
|
|
import java.util.Optional; |
|
14
|
|
|
import java.util.function.Function; |
|
15
|
|
|
|
|
16
|
|
|
import static java.util.stream.Collectors.toMap; |
|
17
|
|
|
|
|
18
|
|
|
@Getter |
|
19
|
|
|
@ToString |
|
20
|
|
|
@EqualsAndHashCode(exclude = "publicationTime") |
|
21
|
|
|
public class ServiceContracts { |
|
22
|
|
|
|
|
23
|
|
|
@Delegate |
|
24
|
|
|
private final ServiceId id; |
|
25
|
|
|
private final List<Capability> capabilities; |
|
26
|
|
|
private final List<Expectation> expectations; |
|
27
|
|
|
private final Instant publicationTime; |
|
28
|
|
|
|
|
29
|
|
|
public ServiceContracts(ServiceId id, List<Capability> capabilities, List<Expectation> expectations) { |
|
30
|
|
|
this.id = id; |
|
31
|
|
|
this.capabilities = capabilities; |
|
32
|
|
|
this.expectations = expectations; |
|
33
|
|
|
this.publicationTime = Instant.now(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public ServiceContracts(ServiceId id, List<Capability> capabilities, List<Expectation> expectations, Instant publicationTime) { |
|
37
|
|
|
this.id = id; |
|
38
|
|
|
this.capabilities = capabilities; |
|
39
|
|
|
this.expectations = expectations; |
|
40
|
|
|
this.publicationTime = publicationTime; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public static ServiceContracts withEmptyContracts(ServiceId id) { |
|
44
|
|
|
return new ServiceContracts( |
|
45
|
|
|
id, |
|
46
|
|
|
new ArrayList<>(), |
|
47
|
|
|
new ArrayList<>(), |
|
48
|
|
|
null); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public <C> Optional<C> getMappedCapabilities(String communicationInterface, Function<String, C> deserializer) { |
|
52
|
|
|
return this.capabilities.stream() |
|
53
|
|
|
.filter(capability -> capability.getProtocol().equals(communicationInterface)) |
|
54
|
|
|
.findAny() |
|
55
|
|
|
.map(capability -> capability.getContract().getValue()) |
|
56
|
|
|
.map(deserializer); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public <E> Optional<E> getMappedExpectations(String providerName, String communicationInterface, Function<String, E> deserializer) { |
|
60
|
|
|
return this.expectations.stream() |
|
61
|
|
|
.filter(expectation -> expectation.getProvider().equals(providerName)) |
|
62
|
|
|
.filter(expectation -> expectation.getProtocol().equals(communicationInterface)) |
|
63
|
|
|
.findAny() |
|
64
|
|
|
.map(expectation -> expectation.getContract().getValue()) |
|
65
|
|
|
.map(deserializer); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public <E> Map<String, E> getMappedExpectations(String communicationInterface, Function<String, E> deserializer) { |
|
69
|
|
|
return this.expectations.stream() |
|
70
|
|
|
.filter(expectation -> expectation.getProtocol().equals(communicationInterface)) |
|
71
|
|
|
.collect(toMap( |
|
72
|
|
|
Expectation::getProvider, |
|
73
|
|
|
expectation -> deserializer.apply(expectation.getContract().getValue()) |
|
74
|
|
|
)); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|