-
Notifications
You must be signed in to change notification settings - Fork 214
/
DefaultPersistenceStreamingActorTest.java
146 lines (119 loc) · 5.64 KB
/
DefaultPersistenceStreamingActorTest.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
/*
* 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.internal.utils.persistence.mongo;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.Collections;
import java.util.UUID;
import org.eclipse.ditto.base.model.entity.id.EntityId;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.things.model.ThingConstants;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.internal.models.streaming.AbstractEntityIdWithRevision;
import org.eclipse.ditto.internal.models.streaming.BatchedEntityIdWithRevisions;
import org.eclipse.ditto.internal.models.streaming.EntityIdWithRevision;
import org.eclipse.ditto.internal.models.streaming.SudoStreamPids;
import org.eclipse.ditto.internal.utils.persistence.mongo.streaming.MongoReadJournal;
import org.eclipse.ditto.internal.utils.persistence.mongo.streaming.PidWithSeqNr;
import org.eclipse.ditto.base.model.signals.commands.Command;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import org.apache.pekko.NotUsed;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.actor.Props;
import org.apache.pekko.stream.SourceRef;
import org.apache.pekko.stream.javadsl.Source;
import org.apache.pekko.stream.testkit.javadsl.TestSink;
import org.apache.pekko.testkit.javadsl.TestKit;
/**
* Test for {@link DefaultPersistenceStreamingActor}.
*/
public final class DefaultPersistenceStreamingActorTest {
private static ActorSystem actorSystem;
private static final ThingId ID = ThingId.of("ns:knownId");
@BeforeClass
public static void initActorSystem() {
final Config config = ConfigFactory.load("test");
actorSystem = ActorSystem.create("AkkaTestSystem", config);
}
@AfterClass
public static void shutdownActorSystem() {
TestKit.shutdownActorSystem(actorSystem);
}
@Test
public void retrieveEmptyStream() {
new TestKit(actorSystem) {{
final Source<String, NotUsed> mockedSource = Source.empty();
final ActorRef underTest = createPersistenceQueriesActor(mockedSource);
final Command<?> command = createStreamingRequest();
sendCommand(this, underTest, command);
final SourceRef<?> sourceRef = expectMsgClass(SourceRef.class);
sourceRef.getSource()
.runWith(TestSink.probe(actorSystem), actorSystem)
.request(1000L)
.expectComplete();
}};
}
@Test
@SuppressWarnings("unchecked")
public void retrieveNonEmptyStream() {
new TestKit(actorSystem) {{
final Source<String, NotUsed> mockedSource = Source.single(ID.toString());
final ActorRef underTest = createPersistenceQueriesActor(mockedSource);
final Command<?> command = createStreamingRequest();
sendCommand(this, underTest, command);
final SourceRef<Object> sourceRef = expectMsgClass(SourceRef.class);
final Object expectedMessage =
BatchedEntityIdWithRevisions.of(SimpleEntityIdWithRevision.class,
Collections.singletonList(new SimpleEntityIdWithRevision(ID, 0L)));
sourceRef.getSource()
.runWith(TestSink.probe(actorSystem), actorSystem)
.request(1000L)
.expectNext(expectedMessage)
.expectComplete();
}};
}
private static Command<?> createStreamingRequest() {
return SudoStreamPids.of(1, 10_000L, DittoHeaders.empty(), ThingConstants.ENTITY_TYPE);
}
private static ActorRef createPersistenceQueriesActor(final Source<String, NotUsed> mockedSource) {
final MongoReadJournal mockJournal = mock(MongoReadJournal.class);
when(mockJournal.getJournalPids(anyInt(), any(), any())).thenReturn(mockedSource);
final Props props = DefaultPersistenceStreamingActor.propsForTests(SimpleEntityIdWithRevision.class,
DefaultPersistenceStreamingActorTest::mapEntity,
DefaultPersistenceStreamingActorTest::unmapEntity,
mockJournal);
return actorSystem.actorOf(props, "persistenceQueriesActor-" + UUID.randomUUID());
}
private static void sendCommand(final TestKit testKit, final ActorRef actorRef, final Command<?> command) {
actorRef.tell(command, testKit.getRef());
}
private static SimpleEntityIdWithRevision mapEntity(final PidWithSeqNr pidWithSeqNr) {
return new SimpleEntityIdWithRevision(ThingId.of(pidWithSeqNr.getPersistenceId()),
pidWithSeqNr.getSequenceNr());
}
private static PidWithSeqNr unmapEntity(final EntityIdWithRevision<?> entityIdWithRevision) {
return new PidWithSeqNr(entityIdWithRevision.getEntityId().toString(), entityIdWithRevision.getRevision());
}
private static final class SimpleEntityIdWithRevision extends AbstractEntityIdWithRevision<EntityId> {
private SimpleEntityIdWithRevision(final EntityId id, final long revision) {
super(id, revision);
}
}
}