/
FaultyClientActor.java
79 lines (70 loc) · 3.42 KB
/
FaultyClientActor.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
/*
* 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 org.eclipse.ditto.internal.utils.akka.logging.DittoLoggerFactory;
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.OpenConnection;
import akka.actor.AbstractActor;
import akka.actor.Props;
import akka.actor.Status;
import akka.event.DiagnosticLoggingAdapter;
/**
* A ClientActor implementation that fails for every command received and answers with an exception. If {@code
* allowCreate} is {@code true} the first create command will return success (required to test open/close/delete).
*/
public class FaultyClientActor extends AbstractActor {
static final ClientActorPropsFactory faultyClientActorPropsFactory =
(connection, connectionActor, proxyActor) -> FaultyClientActor.props(true, false);
private final DiagnosticLoggingAdapter log = DittoLoggerFactory.getDiagnosticLoggingAdapter(this);
private final boolean allowClose;
private boolean allowCreate;
private FaultyClientActor(final boolean allowCreate, final boolean allowClose) {
this.allowCreate = allowCreate;
this.allowClose = allowClose;
}
public static Props props(final boolean allowFirstCreateCommand, final boolean allowCloseCommands) {
return Props.create(FaultyClientActor.class, allowFirstCreateCommand, allowCloseCommands);
}
@Override
public Receive createReceive() {
return receiveBuilder()
.match(CreateConnection.class, cc -> {
if (allowCreate) {
log.info("connection created");
this.allowCreate = false;
sender().tell(new Status.Success("mock"), getSelf());
} else {
sender().tell(new Status.Failure(new IllegalStateException("error message")),
getSelf());
}
})
.match(OpenConnection.class,
oc -> sender().tell(new Status.Failure(new IllegalStateException("error message")),
getSelf()))
.match(CloseConnection.class,
cc -> {
if (allowClose) {
sender().tell(new Status.Success("mock"), getSelf());
} else {
sender().tell(new Status.Success(new IllegalStateException("error message")),
getSelf());
}
})
.match(DeleteConnection.class,
dc -> sender().tell(new Status.Failure(new IllegalStateException("error message")),
getSelf()))
.build();
}
}