-
Notifications
You must be signed in to change notification settings - Fork 215
/
MockClientActorPropsFactory.java
292 lines (255 loc) · 12.8 KB
/
MockClientActorPropsFactory.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
* 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.connectivity.service.messaging;
import javax.annotation.Nullable;
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.connectivity.model.Connection;
import org.eclipse.ditto.connectivity.model.ConnectionIdInvalidException;
import org.eclipse.ditto.connectivity.model.ConnectivityModelFactory;
import org.eclipse.ditto.connectivity.model.ConnectivityStatus;
import org.eclipse.ditto.connectivity.model.RecoveryStatus;
import org.eclipse.ditto.connectivity.model.signals.commands.modify.CheckConnectionLogsActive;
import org.eclipse.ditto.connectivity.model.signals.commands.modify.CloseConnection;
import org.eclipse.ditto.connectivity.model.signals.commands.modify.CreateConnection;
import org.eclipse.ditto.connectivity.model.signals.commands.modify.DeleteConnection;
import org.eclipse.ditto.connectivity.model.signals.commands.modify.EnableConnectionLogs;
import org.eclipse.ditto.connectivity.model.signals.commands.modify.ModifyConnection;
import org.eclipse.ditto.connectivity.model.signals.commands.modify.OpenConnection;
import org.eclipse.ditto.connectivity.model.signals.commands.modify.TestConnection;
import org.eclipse.ditto.connectivity.model.signals.commands.query.RetrieveConnectionLogs;
import org.eclipse.ditto.connectivity.model.signals.commands.query.RetrieveConnectionStatus;
import org.eclipse.ditto.internal.utils.akka.logging.DittoLoggerFactory;
import org.eclipse.ditto.internal.utils.cluster.DistPubSubAccess;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonValue;
import com.typesafe.config.Config;
import akka.actor.AbstractActorWithStash;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.actor.Status;
import akka.cluster.pubsub.DistributedPubSub;
import akka.cluster.pubsub.DistributedPubSubMediator;
import akka.event.DiagnosticLoggingAdapter;
public final class MockClientActorPropsFactory extends ClientActorPropsFactory {
/**
* @param actorSystem the actor system in which to load the extension.
*/
public MockClientActorPropsFactory(final ActorSystem actorSystem) {
super(actorSystem);
}
@Override
public Props getActorPropsForType(final Connection connection, final ActorRef proxyActor,
final ActorRef connectionActor,
final ActorSystem actorSystem, final DittoHeaders dittoHeaders,
final Config connectivityConfigOverwrites) {
return MockClientActor.props();
}
/**
* Mocks a {@link org.eclipse.ditto.connectivity.service.messaging.persistence.ConnectionPersistenceActor} and provides abstraction for a real connection.
*/
public static class MockClientActor extends AbstractActorWithStash {
private final DiagnosticLoggingAdapter log = DittoLoggerFactory.getDiagnosticLoggingAdapter(this);
private final ActorRef mediator = DistributedPubSub.get(getContext().getSystem()).mediator();
@Nullable private ActorRef delegate;
@Nullable private ActorRef gossip;
private MockClientActor() {
}
public static Props props() {
return Props.create(MockClientActor.class);
}
@Override
public void preStart() {
log.info("Mock client actor started.");
if (gossip != null) {
gossip.tell(getSelf(), getSelf());
}
subscribeForSnapshotPubSubTopic(mediator);
}
private void subscribeForSnapshotPubSubTopic(final ActorRef pubSubMediator) {
final var self = getSelf();
final var subscriptionMessage =
DistPubSubAccess.subscribe("mockClientActor:change", self);
pubSubMediator.tell(subscriptionMessage, self);
}
@Override
public void postStop() {
log.info("Mock client actor was stopped.");
}
@Override
public Receive createReceive() {
return receiveBuilder()
.match(DistributedPubSubMediator.SubscribeAck.class, subscribeAck ->
mediator.tell(DistPubSubAccess.publish("mockClientActor:subscribed", new Subscribed()),
getSelf()))
.match(ChangeActorRef.class, s -> {
delegate =
s.delegate != null ? getContext().getSystem().provider().resolveActorRef(s.delegate) :
null;
gossip =
s.gossip != null ? getContext().getSystem().provider().resolveActorRef(s.gossip) : null;
if (gossip != null) {
gossip.tell(getSelf(), getSelf());
}
getSender().tell(new ActorRefChanged(), getSelf());
log.info("Switching state.");
getContext().become(initializedBehavior(), false);
unstashAll();
})
.matchAny(any -> stash())
.build();
}
private Receive initializedBehavior() {
return receiveBuilder()
.match(CreateConnection.class, cc -> {
log.info("Creating connection...");
forward(cc);
sender().tell(new Status.Success("mock"), getSelf());
})
.match(OpenConnection.class, oc -> {
log.info("Opening connection...");
forward(oc);
sender().tell(new Status.Success("mock"), getSelf());
})
.match(ModifyConnection.class, mc -> {
log.info("Modifying connection...");
forward(mc);
sender().tell(new Status.Success("mock"), getSelf());
})
.match(CloseConnection.class, cc -> {
log.info("Closing connection...");
forward(cc);
sender().tell(new Status.Success("mock"), getSelf());
})
.match(DeleteConnection.class, dc -> {
log.info("Deleting connection...");
forward(dc);
sender().tell(new Status.Success("mock"), getSelf());
})
.match(RetrieveConnectionStatus.class, rcs -> {
log.info("Retrieve connection status...");
sender().tell(ConnectivityModelFactory.newClientStatus("client1",
ConnectivityStatus.OPEN, RecoveryStatus.SUCCEEDED, "connection is open",
TestConstants.INSTANT),
getSelf());
// simulate consumer and pusblisher actor response
sender().tell(ConnectivityModelFactory.newSourceStatus("client1",
ConnectivityStatus.OPEN, "source1", "consumer started"),
getSelf());
sender().tell(ConnectivityModelFactory.newSourceStatus("client1",
ConnectivityStatus.OPEN, "source2", "consumer started"),
getSelf());
sender().tell(ConnectivityModelFactory.newTargetStatus("client1",
ConnectivityStatus.OPEN, "target1", "publisher started"),
getSelf());
sender().tell(ConnectivityModelFactory.newTargetStatus("client1",
ConnectivityStatus.OPEN, "target2", "publisher started"),
getSelf());
sender().tell(ConnectivityModelFactory.newTargetStatus("client1",
ConnectivityStatus.OPEN, "target3", "publisher started"),
getSelf());
})
.match(RetrieveConnectionLogs.class, rcl -> {
log.info("Retrieve connection logs...");
// forwarding to delegate so it can respond to correct sender
if (null != delegate) {
delegate.forward(rcl, getContext());
} else {
log.error(
"No delegate found in MockClientActor. RetrieveConnectionLogs needs a delegate which" +
" needs to respond with a RetrieveConnectionLogsResponse to the sender of the command");
}
})
.match(EnableConnectionLogs.class, ecl -> {
log.info("Enable connection logs...");
forward(ecl);
})
.match(TestConnection.class, testConnection -> {
log.info("Testing connection");
final DittoRuntimeException exception =
ConnectionIdInvalidException.newBuilder("invalid").build();
if (testConnection.getDittoHeaders().getOrDefault("error", "").equals("true")) {
sender().tell(exception, getSelf());
}
if (testConnection.getDittoHeaders().getOrDefault("fail", "").equals("true")) {
sender().tell(new Status.Failure(exception), getSelf());
}
sender().tell(new Status.Success("mock"), getSelf());
})
.match(CheckConnectionLogsActive.class, ccla -> {
log.info("Check connection logs active...");
forward(ccla);
})
.match(ActorRef.class, actorRef -> {})
.matchAny(unhandled -> {
log.info("Received unhandled message: {}", unhandled.getClass().getName());
forward(unhandled);
})
.build();
}
private void forward(final Object obj) {
if (delegate != null) {
delegate.tell(obj, getSelf());
} else {
log.info("Not forwarding, since no delegate is present.");
}
}
public static class ChangeActorRef implements Jsonifiable<JsonObject> {
final String delegate;
@Nullable final String gossip;
public ChangeActorRef(@Nullable final String delegate, @Nullable final String gossip) {
this.delegate = delegate;
this.gossip = gossip;
}
@Override
public JsonObject toJson() {
final var jsonBuilder = JsonObject.newBuilder();
if (null != delegate) {
jsonBuilder.set("delegate", delegate);
}
if (null != gossip) {
jsonBuilder.set("gossip", gossip);
}
return jsonBuilder.build();
}
public static ChangeActorRef fromJson(final JsonObject jsonObject) {
return new ChangeActorRef(jsonObject.getValue("delegate").map(JsonValue::asString).orElse(null),
jsonObject.getValue("gossip").map(JsonValue::asString).orElse(null));
}
}
public static class ActorRefChanged implements Jsonifiable<JsonObject> {
public ActorRefChanged() {
}
@Override
public JsonObject toJson() {
return JsonObject.empty();
}
public static ActorRefChanged fromJson(final JsonObject jsonObject) {
return new ActorRefChanged();
}
}
public static class Subscribed implements Jsonifiable<JsonObject> {
public Subscribed() {
}
@Override
public JsonObject toJson() {
return JsonObject.empty();
}
public static Subscribed fromJson(final JsonObject jsonObject) {
return new Subscribed();
}
}
}
}