-
Notifications
You must be signed in to change notification settings - Fork 214
/
ThingsAggregatorProxyActorTest.java
171 lines (142 loc) · 7.1 KB
/
ThingsAggregatorProxyActorTest.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
/*
* Copyright (c) 2017 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.edge.service.dispatching;
import java.util.List;
import java.util.UUID;
import org.eclipse.ditto.base.model.correlationid.TestNameCorrelationId;
import org.eclipse.ditto.base.model.exceptions.DittoInternalErrorException;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.json.Jsonifiable;
import org.eclipse.ditto.internal.utils.pekko.ActorSystemResource;
import org.eclipse.ditto.internal.utils.tracing.DittoTracingInitResource;
import org.eclipse.ditto.things.model.Thing;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.ThingIdInvalidException;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveThingResponse;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveThings;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveThingsResponse;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.apache.pekko.Done;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.actor.Props;
import org.apache.pekko.cluster.pubsub.DistributedPubSubMediator;
import org.apache.pekko.stream.Materializer;
import org.apache.pekko.stream.SourceRef;
import org.apache.pekko.stream.javadsl.Source;
import org.apache.pekko.stream.javadsl.StreamRefs;
import org.apache.pekko.testkit.TestActor.AutoPilot;
import org.apache.pekko.testkit.TestProbe;
import org.apache.pekko.testkit.javadsl.TestKit;
/**
* Tests {@link ThingsAggregatorProxyActor}.
*/
public final class ThingsAggregatorProxyActorTest {
@ClassRule
public static final DittoTracingInitResource DITTO_TRACING_INIT_RESOURCE =
DittoTracingInitResource.disableDittoTracing();
@ClassRule
public static final ActorSystemResource ACTOR_SYSTEM_RESOURCE = ActorSystemResource.newInstance();
@Rule
public final TestNameCorrelationId testNameCorrelationId = TestNameCorrelationId.newInstance();
private static final String NAMESPACE = "ditto";
private static final ThingId THING_ID = ThingId.of(NAMESPACE, "thing");
private static final DittoHeaders DITTO_HEADERS =
DittoHeaders.newBuilder().correlationId(UUID.randomUUID().toString()).build();
private static final DittoRuntimeException DITTO_RUNTIME_EXCEPTION =
ThingIdInvalidException.newBuilder("invalidThingId")
.dittoHeaders(DITTO_HEADERS)
.build();
private static final DittoInternalErrorException INTERNAL_ERROR_EXCEPTION =
DittoInternalErrorException.newBuilder().dittoHeaders(DITTO_HEADERS).build();
private static final RetrieveThings RETRIEVE_THINGS_COMMAND =
RetrieveThings.getBuilder(THING_ID).namespace(NAMESPACE)
.dittoHeaders(DITTO_HEADERS)
.build();
private static final RetrieveThingResponse RETRIEVE_THING_RESPONSE =
RetrieveThingResponse.of(THING_ID, Thing.newBuilder().setId(THING_ID).build().toJsonString(),
DITTO_HEADERS);
private static final RetrieveThingsResponse RETRIEVE_THINGS_RESPONSE =
RetrieveThingsResponse.of(List.of(Thing.newBuilder().setId(THING_ID).build().toJsonString()),
NAMESPACE,
DITTO_HEADERS);
private static SourceRef<Jsonifiable<?>> getSourceRef(final Iterable<Jsonifiable<?>> jsonValues) {
final var source = Source.from(jsonValues);
return source.runWith(StreamRefs.sourceRef(), Materializer.apply(ACTOR_SYSTEM_RESOURCE.getActorSystem()));
}
@Test
public void testHandleDittoRuntimeException() {
final ActorSystem actorSystem = ACTOR_SYSTEM_RESOURCE.getActorSystem();
new TestKit(actorSystem) {{
final TestProbe targetActor = new TestProbe(actorSystem);
targetActor.setAutoPilot(new AutoPilotAnsweringWithException(DITTO_RUNTIME_EXCEPTION));
final Props props = ThingsAggregatorProxyActor.props(targetActor.ref());
final ActorRef proxyActor = actorSystem.actorOf(props);
proxyActor.tell(RETRIEVE_THINGS_COMMAND, getRef());
expectMsg(DITTO_RUNTIME_EXCEPTION);
}};
}
@Test
public void testHandleGenericException() {
final ActorSystem actorSystem = ACTOR_SYSTEM_RESOURCE.getActorSystem();
new TestKit(actorSystem) {{
final TestProbe targetActor = new TestProbe(actorSystem);
targetActor.setAutoPilot(new AutoPilotAnsweringWithException(INTERNAL_ERROR_EXCEPTION));
final Props props = ThingsAggregatorProxyActor.props(targetActor.ref());
final ActorRef proxyActor = actorSystem.actorOf(props);
proxyActor.tell(RETRIEVE_THINGS_COMMAND, getRef());
expectMsg(INTERNAL_ERROR_EXCEPTION);
}};
}
@Test
public void shutdownWithoutTask() {
final ActorSystem actorSystem = ACTOR_SYSTEM_RESOURCE.getActorSystem();
new TestKit(actorSystem) {{
final TestProbe pubSubMediator = new TestProbe(actorSystem);
final Props props = ThingsAggregatorProxyActor.props(pubSubMediator.ref());
final ActorRef underTest = actorSystem.actorOf(props);
underTest.tell(ThingsAggregatorProxyActor.Control.SERVICE_REQUESTS_DONE, getRef());
expectMsg(Done.getInstance());
}};
}
@Test
public void shutdownWithTask() {
final ActorSystem actorSystem = ACTOR_SYSTEM_RESOURCE.getActorSystem();
new TestKit(actorSystem) {{
final TestProbe pubSubMediator = new TestProbe(actorSystem);
final Props props = ThingsAggregatorProxyActor.props(pubSubMediator.ref());
final ActorRef underTest = actorSystem.actorOf(props);
underTest.tell(RETRIEVE_THINGS_COMMAND, getRef());
pubSubMediator.expectMsgClass(DistributedPubSubMediator.Publish.class);
underTest.tell(ThingsAggregatorProxyActor.Control.SERVICE_REQUESTS_DONE, getRef());
expectNoMsg();
pubSubMediator.reply(getSourceRef(List.of(RETRIEVE_THING_RESPONSE)));
expectMsg(RETRIEVE_THINGS_RESPONSE);
expectMsg(Done.getInstance());
}};
}
private static final class AutoPilotAnsweringWithException extends AutoPilot {
private final Exception exceptionToRespond;
private AutoPilotAnsweringWithException(final Exception exception) {
exceptionToRespond = exception;
}
@Override
public AutoPilot run(final ActorRef sender, final Object msg) {
sender.tell(exceptionToRespond, ActorRef.noSender());
return keepRunning();
}
}
}