-
Notifications
You must be signed in to change notification settings - Fork 214
/
PoliciesJournalTestHelper.java
executable file
·105 lines (92 loc) · 4.31 KB
/
PoliciesJournalTestHelper.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
/*
* 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.policies.service.persistence.testhelper;
import static java.util.Objects.requireNonNull;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.bson.BsonDocument;
import org.eclipse.ditto.policies.model.PolicyId;
//TODO InMemoryReadJournal
import org.apache.pekko.NotUsed;
import org.apache.pekko.actor.ActorSystem;
//import org.apache.pekko.persistence.inmemory.query.javadsl.InMemoryReadJournal;
import org.apache.pekko.persistence.query.EventEnvelope;
import org.apache.pekko.persistence.query.PersistenceQuery;
import org.apache.pekko.stream.javadsl.Sink;
import org.apache.pekko.stream.javadsl.Source;
/**
* Helper class which provides functionality for testing with Pekko persistence journal for the policies services.
* Requires akka-persistence-inmemory (by com.github.dnvriend).
*
* @param <J> the domain specific datatype stored in the journal
*/
public final class PoliciesJournalTestHelper<J> {
private static final int WAIT_TIMEOUT = 3;
private final Function<PolicyId, String> domainIdToPersistenceId;
private final BiFunction<BsonDocument, Long, J> journalEntryToDomainObject;
// private final InMemoryReadJournal readJournal;
private final ActorSystem actorSystem;
/**
* Constructor.
*
* @param actorSystem the actor system to be used to find the persistence extension
* @param journalEntryToDomainObject a {@link java.util.function.BiFunction} providing the journal entry and its sequence number and
* expecting a domain object
* @param domainIdToPersistenceId a {@link java.util.function.Function} providing the domain ID and expecting the matching
* persistence ID
*/
public PoliciesJournalTestHelper(final ActorSystem actorSystem,
final BiFunction<BsonDocument, Long, J> journalEntryToDomainObject, final Function<PolicyId, String>
domainIdToPersistenceId) {
this.journalEntryToDomainObject = requireNonNull(journalEntryToDomainObject);
this.domainIdToPersistenceId = requireNonNull(domainIdToPersistenceId);
this.actorSystem = actorSystem;
// readJournal = PersistenceQuery.get(actorSystem).
// getReadJournalFor(InMemoryReadJournal.class, InMemoryReadJournal.Identifier());
}
/**
* Gets all events for the given domain ID.
*
* @param domainId the domain ID
* @return the events
*/
public List<J> getAllEvents(final PolicyId domainId) {
final String persistenceId = domainIdToPersistenceId.apply(domainId);
final List<EventEnvelope> eventEnvelopes = getAllEventEnvelopes(persistenceId);
return Collections.unmodifiableList(
eventEnvelopes.stream().map(this::convertEventEnvelopeToDomainObject).collect(Collectors.toList()));
}
private List<EventEnvelope> getAllEventEnvelopes(final String persistenceId) {
return null;
// return runBlockingWithReturn(readJournal.currentEventsByPersistenceId(persistenceId, 0, Long.MAX_VALUE));
}
private J convertEventEnvelopeToDomainObject(final EventEnvelope eventEnvelope) {
final BsonDocument event = (BsonDocument) eventEnvelope.event();
return journalEntryToDomainObject.apply(event, eventEnvelope.sequenceNr());
}
private <T> List<T> runBlockingWithReturn(final Source<T, NotUsed> publisher) {
final CompletionStage<List<T>> done = publisher.runWith(Sink.seq(), actorSystem);
try {
return done.toCompletableFuture().get(WAIT_TIMEOUT, TimeUnit.SECONDS);
} catch (final InterruptedException | ExecutionException | TimeoutException e) {
throw new IllegalStateException(e);
}
}
}