-
Notifications
You must be signed in to change notification settings - Fork 215
/
MockClientActorPropsFactory.java
86 lines (73 loc) · 3.38 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
/*
* Copyright (c) 2022 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 java.text.MessageFormat;
import java.util.Optional;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.WithDittoHeaders;
import org.eclipse.ditto.connectivity.model.Connection;
import org.eclipse.ditto.connectivity.model.ConnectionConfigurationInvalidException;
import org.eclipse.ditto.internal.utils.akka.controlflow.WithSender;
import com.typesafe.config.Config;
import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.japi.pf.ReceiveBuilder;
import akka.testkit.TestProbe;
public final class MockClientActorPropsFactory implements ClientActorPropsFactory {
public static TestProbe mockClientActorProbe;
public static TestProbe gossipProbe;
public MockClientActorPropsFactory(final ActorSystem actorSystem, final Config config) {}
@Override
public Props getActorPropsForType(final Connection connection, final ActorRef commandForwarderActor,
final ActorRef connectionActor, final ActorSystem actorSystem, final DittoHeaders dittoHeaders,
final Config connectivityConfigOverwrites) {
final Boolean shouldThrowException = Optional.ofNullable(dittoHeaders.get("should-throw-exception"))
.map(Boolean::parseBoolean)
.orElse(false);
if (shouldThrowException) {
throw ConnectionConfigurationInvalidException.newBuilder("validation failed...").build();
}
return Props.create(MockClientActor.class, dittoHeaders, connectionActor);
}
public static class MockClientActor extends AbstractActor {
private int current = 0;
private final ActorRef connectionActor;
public MockClientActor(final DittoHeaders dittoHeaders, final ActorRef connectionActor) {
this.connectionActor = connectionActor;
final int numberOfFailures = Optional.ofNullable(dittoHeaders.get("number-of-instantiation-failures"))
.map(Integer::parseInt)
.orElse(0);
if (current++ < numberOfFailures) {
final var message =
MessageFormat.format("''FailingActor'' intentionally failing for {0} of {1} times",
current, numberOfFailures);
throw new IllegalStateException(message);
}
}
@Override
public void preStart() {
connectionActor.tell(self(), self());
gossipProbe.ref().tell(self(), self());
}
@Override
public Receive createReceive() {
return ReceiveBuilder.create()
.match(WithDittoHeaders.class,
message -> mockClientActorProbe.ref().tell(WithSender.of(message, getSelf()), getSender()))
.matchAny(any -> mockClientActorProbe.ref().forward(any, getContext()))
.build();
}
}
}