-
Notifications
You must be signed in to change notification settings - Fork 214
/
ConnectionIdsRetrievalActorTest.java
141 lines (119 loc) · 6.02 KB
/
ConnectionIdsRetrievalActorTest.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
/*
* Copyright (c) 2021 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 static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import org.bson.Document;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.connectivity.model.ConnectivityInternalErrorException;
import org.eclipse.ditto.connectivity.service.config.ConnectionIdsRetrievalConfig;
import org.eclipse.ditto.connectivity.service.messaging.persistence.ConnectionPersistenceActor;
import org.eclipse.ditto.internal.utils.persistence.mongo.streaming.MongoReadJournal;
import org.eclipse.ditto.connectivity.model.signals.commands.ConnectivityErrorResponse;
import org.eclipse.ditto.connectivity.model.signals.commands.query.RetrieveAllConnectionIds;
import org.eclipse.ditto.connectivity.model.signals.commands.query.RetrieveAllConnectionIdsResponse;
import org.eclipse.ditto.connectivity.model.signals.events.ConnectionCreated;
import org.eclipse.ditto.connectivity.model.signals.events.ConnectionDeleted;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.actor.Props;
import org.apache.pekko.stream.javadsl.Source;
import org.apache.pekko.testkit.javadsl.TestKit;
/**
* Tests {@link ConnectionIdsRetrievalActor}.
*/
public final class ConnectionIdsRetrievalActorTest {
private static final List<Document> JOURNAL_ENTRIES =
List.of(document(pid("connection-1"), ConnectionCreated.TYPE),
document(pid("connection-2"), ConnectionCreated.TYPE),
document(pid("connection-3"), ConnectionDeleted.TYPE),
document(pid("connection-4"), ConnectionCreated.TYPE));
private static final List<Document> SNAPSHOT_IDS =
// connection-3 and connection-4 is a duplicate by intention
List.of(doc("connection-3"), doc("connection-4"), doc("connection-5"));
private static final DittoHeaders DITTO_HEADERS =
DittoHeaders.newBuilder().correlationId(UUID.randomUUID().toString()).build();
private static ActorSystem actorSystem;
private static final int BATCH_SIZE = 10;
private MongoReadJournal mongoReadJournal;
private ConnectionIdsRetrievalConfig connectionIdsRetrievalConfig;
@BeforeClass
public static void setUp() {
actorSystem = ActorSystem.create("AkkaTestSystem", TestConstants.CONFIG);
}
@AfterClass
public static void tearDown() {
TestKit.shutdownActorSystem(actorSystem, scala.concurrent.duration.Duration.apply(5, TimeUnit.SECONDS), false);
}
@Before
public void setup() {
mongoReadJournal = mock(MongoReadJournal.class);
connectionIdsRetrievalConfig = mock(ConnectionIdsRetrievalConfig.class);
when(connectionIdsRetrievalConfig.getReadJournalBatchSize()).thenReturn(BATCH_SIZE);
when(connectionIdsRetrievalConfig.getReadSnapshotBatchSize()).thenReturn(BATCH_SIZE);
}
@Test
public void testRetrieveAllConnectionIds() {
new TestKit(actorSystem) {{
when(mongoReadJournal.getLatestJournalEntries(eq(BATCH_SIZE), any(), any()))
.thenReturn(Source.from(JOURNAL_ENTRIES));
when(mongoReadJournal.getNewestSnapshotsAbove(anyString(), eq(BATCH_SIZE), any()))
.thenReturn(Source.from(SNAPSHOT_IDS));
final Props props =
ConnectionIdsRetrievalActor.props(mongoReadJournal, connectionIdsRetrievalConfig);
final ActorRef reconnectActor = actorSystem.actorOf(props);
reconnectActor.tell(RetrieveAllConnectionIds.of(DITTO_HEADERS), getRef());
final RetrieveAllConnectionIdsResponse response = expectMsgClass(RetrieveAllConnectionIdsResponse.class);
assertThat(response.getAllConnectionIds()).isEqualTo(
Set.of("connection-1", "connection-2", "connection-4", "connection-5"));
}};
}
@Test
public void internalErrorShouldResultInErrorResponse() {
new TestKit(actorSystem) {{
when(mongoReadJournal.getLatestJournalEntries(eq(BATCH_SIZE), any(), any()))
.thenThrow(new NullPointerException("expected"));
when(mongoReadJournal.getNewestSnapshotsAbove(anyString(), eq(BATCH_SIZE), any()))
.thenThrow(new NullPointerException("expected"));
final Props props =
ConnectionIdsRetrievalActor.props(mongoReadJournal, connectionIdsRetrievalConfig);
final ActorRef reconnectActor = actorSystem.actorOf(props);
reconnectActor.tell(RetrieveAllConnectionIds.of(DITTO_HEADERS), getRef());
final ConnectivityErrorResponse error = expectMsgClass(ConnectivityErrorResponse.class);
assertThat(error.getDittoRuntimeException()).isInstanceOf(ConnectivityInternalErrorException.class);
}};
}
private static Document doc(final String id) {
return new Document("_id", pid(id));
}
private static String pid(final String id) {
return ConnectionPersistenceActor.PERSISTENCE_ID_PREFIX + id;
}
private static Document document(final String pid, final String manifest) {
return new Document()
.append("pid", pid)
.append("manifest", manifest);
}
}