-
Notifications
You must be signed in to change notification settings - Fork 214
/
AbstractSignalEnrichmentFacadeTest.java
192 lines (166 loc) · 9.44 KB
/
AbstractSignalEnrichmentFacadeTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.internal.models.signalenrichment;
import static org.assertj.core.api.Assertions.assertThat;
import java.time.Duration;
import java.time.Instant;
import java.util.UUID;
import java.util.concurrent.CompletionStage;
import org.eclipse.ditto.base.model.entity.metadata.MetadataModelFactory;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.signals.DittoTestSystem;
import org.eclipse.ditto.json.JsonFieldSelector;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.signals.commands.exceptions.ThingNotAccessibleException;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveThing;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveThingResponse;
import org.eclipse.ditto.things.model.signals.events.AttributeModified;
import org.eclipse.ditto.things.model.signals.events.ThingDeleted;
import org.junit.Test;
import org.apache.pekko.pattern.AskTimeoutException;
import org.apache.pekko.testkit.javadsl.TestKit;
/**
* Abstract base test for different {@link SignalEnrichmentFacade} implementations.
*/
abstract class AbstractSignalEnrichmentFacadeTest {
protected static final JsonFieldSelector SELECTOR =
JsonFieldSelector.newInstance("policyId", "attributes/x", "features/y/properties/z", "_metadata");
protected static final String RESULT_POLICY_ID = "policy:id";
protected static final AttributeModified THING_EVENT = AttributeModified.of(ThingId.generateRandom(),
JsonPointer.of("x"),
JsonValue.of(5),
3L,
Instant.EPOCH,
DittoHeaders.empty(),
MetadataModelFactory.newMetadataBuilder()
.set("type", "x attribute")
.build());
@Test
public void success() {
DittoTestSystem.run(this, kit -> {
// GIVEN: SignalEnrichmentFacade.retrievePartialThing()
final SignalEnrichmentFacade underTest =
createSignalEnrichmentFacadeUnderTest(kit, Duration.ofSeconds(10L));
final ThingId thingId = ThingId.of("test:thing-id");
final DittoHeaders headers = DittoHeaders.newBuilder().correlationId(UUID.randomUUID().toString()).build();
final CompletionStage<JsonObject> askResult =
underTest.retrievePartialThing(thingId, SELECTOR, headers, THING_EVENT);
// WHEN: Command handler receives expected RetrieveThing and responds with RetrieveThingResponse
final RetrieveThing retrieveThing = kit.expectMsgClass(RetrieveThing.class);
assertThat(retrieveThing.getSelectedFields()).contains(actualSelectedFields(SELECTOR));
kit.reply(RetrieveThingResponse.of(thingId, getThingResponseThingJson(), headers));
// THEN: The result future completes with the entity of the RetrieveThingResponse
askResult.toCompletableFuture().join();
assertThat(askResult).isCompletedWithValue(getExpectedThingJson());
});
}
protected JsonFieldSelector actualSelectedFields(final JsonFieldSelector selector) {
return selector;
}
protected JsonObject getThingResponseThingJson() {
return JsonObject.of("{\n" +
" \"policyId\": \"" + RESULT_POLICY_ID + "\",\n" +
" \"attributes\": {\"x\": 5},\n" +
" \"features\": {\"y\": {\"properties\": {\"z\": true}}}\n" +
"}");
}
protected JsonObject getExpectedThingJson() {
return getThingResponseThingJson();
}
@Test
public void thingNotAccessible() {
DittoTestSystem.run(this, kit -> {
// GIVEN: SignalEnrichmentFacade.retrievePartialThing()
final SignalEnrichmentFacade underTest =
createSignalEnrichmentFacadeUnderTest(kit, Duration.ofSeconds(10L));
final ThingId thingId = ThingId.generateRandom();
final DittoHeaders headers = DittoHeaders.newBuilder().correlationId(UUID.randomUUID().toString()).build();
final CompletionStage<JsonObject> askResult =
underTest.retrievePartialThing(thingId, SELECTOR, headers, THING_EVENT);
// WHEN: Command handler receives expected RetrieveThing and responds with ThingNotAccessibleException
final RetrieveThing retrieveThing = kit.expectMsgClass(RetrieveThing.class);
assertThat(retrieveThing.getSelectedFields()).contains(actualSelectedFields(SELECTOR));
final ThingNotAccessibleException thingNotAccessibleException =
ThingNotAccessibleException.newBuilder(thingId).dittoHeaders(headers).build();
kit.reply(thingNotAccessibleException);
// THEN: The result future fails with ThingNotAccessibleException
askResult.toCompletableFuture().exceptionally(e -> null).join();
assertThat(askResult).hasFailedWithThrowableThat().isEqualTo(thingNotAccessibleException);
});
}
@Test
public void unexpectedMessage() {
DittoTestSystem.run(this, kit -> {
// GIVEN: SignalEnrichmentFacade.retrievePartialThing()
final SignalEnrichmentFacade underTest =
createSignalEnrichmentFacadeUnderTest(kit, Duration.ofSeconds(10L));
final ThingId thingId = ThingId.generateRandom();
final DittoHeaders headers = DittoHeaders.newBuilder().correlationId(UUID.randomUUID().toString()).build();
final CompletionStage<JsonObject> askResult =
underTest.retrievePartialThing(thingId, SELECTOR, headers, THING_EVENT);
// WHEN: Command handler receives expected RetrieveThing and responds with a random object
final RetrieveThing retrieveThing = kit.expectMsgClass(RetrieveThing.class);
assertThat(retrieveThing.getSelectedFields()).contains(actualSelectedFields(SELECTOR));
final Object randomObject = new Object();
kit.reply(randomObject);
// THEN: The result future fails with a runtime exception containing a description of the random object
askResult.toCompletableFuture().exceptionally(e -> null).join();
assertThat(askResult).hasFailedWithThrowableThat()
.hasMessageContaining(randomObject.toString())
.isInstanceOf(RuntimeException.class);
});
}
@Test
public void timeout() {
DittoTestSystem.run(this, kit -> {
// GIVEN: SignalEnrichmentFacade.retrievePartialThing() with a short timeout
final SignalEnrichmentFacade underTest =
createSignalEnrichmentFacadeUnderTest(kit, Duration.ofMillis(1L));
final ThingId thingId = ThingId.generateRandom();
final DittoHeaders headers = DittoHeaders.newBuilder().correlationId(UUID.randomUUID().toString()).build();
final CompletionStage<JsonObject> askResult =
underTest.retrievePartialThing(thingId, SELECTOR, headers, THING_EVENT);
// WHEN: Command handler does not respond
final RetrieveThing retrieveThing = kit.expectMsgClass(RetrieveThing.class);
assertThat(retrieveThing.getSelectedFields()).contains(actualSelectedFields(SELECTOR));
// THEN: The result future fails with an AskTimeoutException.
askResult.toCompletableFuture().exceptionally(e -> null).join();
assertThat(askResult).hasFailedWithThrowableThat()
.isInstanceOf(AskTimeoutException.class);
});
}
@Test
public void enrichThingDeleted() {
DittoTestSystem.run(this, kit -> {
// GIVEN: SignalEnrichmentFacade.retrievePartialThing()
final SignalEnrichmentFacade underTest =
createSignalEnrichmentFacadeUnderTest(kit, Duration.ofSeconds(10L));
final ThingId thingId = ThingId.generateRandom();
final DittoHeaders headers = DittoHeaders.newBuilder().correlationId(UUID.randomUUID().toString()).build();
final ThingDeleted thingDeleted = ThingDeleted.of(thingId, 2L, Instant.EPOCH, headers, null);
// WHEN: ThingDeleted event is about to be enriched by facade
final CompletionStage<JsonObject> askResult =
underTest.retrievePartialThing(thingId, SELECTOR, headers, thingDeleted);
final RetrieveThing retrieveThing = kit.expectMsgClass(RetrieveThing.class);
assertThat(retrieveThing.getSelectedFields()).contains(actualSelectedFields(SELECTOR));
kit.reply(RetrieveThingResponse.of(thingId, getThingResponseThingJson(), headers));
// THEN: The result future completes with the entity of the RetrieveThingResponse
askResult.toCompletableFuture().join();
assertThat(askResult).isCompletedWithValue(getExpectedThingJson());
});
}
protected abstract SignalEnrichmentFacade createSignalEnrichmentFacadeUnderTest(TestKit kit, Duration duration);
}