-
Notifications
You must be signed in to change notification settings - Fork 214
/
MockJournalPlugin.java
91 lines (79 loc) · 3.83 KB
/
MockJournalPlugin.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
/*
* Copyright (c) 2019 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.persistentactors;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.stream.Stream;
import org.mockito.Mockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.pekko.persistence.AtomicWrite;
import org.apache.pekko.persistence.PersistentRepr;
import org.apache.pekko.persistence.journal.japi.AsyncWriteJournal;
import scala.compat.java8.FutureConverters;
import scala.concurrent.Future;
/**
* Mock implementation of {@link AsyncWriteJournal} that logs invocations and forwards invocations to delete* methods
* to another implementation e.g. a Mockito mock to verify invocations.
*/
final class MockJournalPlugin extends AsyncWriteJournal {
private static final Logger LOGGER = LoggerFactory.getLogger(MockJournalPlugin.class);
static final String FAIL_DELETE_MESSAGE = "thing:namespace:failDeleteMessage";
static final String SLOW_DELETE = "thing:namespace:slowDelete";
private static final AsyncWriteJournal journalMock = Mockito.mock(AsyncWriteJournal.class);
@Override
public Future<Void> doAsyncReplayMessages(final String persistenceId, final long fromSequenceNr,
final long toSequenceNr,
final long max, final Consumer<PersistentRepr> replayCallback) {
LOGGER.debug("[doAsyncReplayMessages]: persistenceId {}, fromSequenceNr {}, toSequenceNr {}, max {}",
persistenceId,
fromSequenceNr, toSequenceNr, max);
return Future.successful(null);
}
@Override
public Future<Long> doAsyncReadHighestSequenceNr(final String persistenceId, final long fromSequenceNr) {
LOGGER.debug("[doAsyncReadHighestSequenceNr]: persistenceId {}, fromSequenceNr {}", persistenceId,
fromSequenceNr);
return Future.successful(0L);
}
@Override
public Future<Iterable<Optional<Exception>>> doAsyncWriteMessages(final Iterable<AtomicWrite> messages) {
LOGGER.debug("[doAsyncWriteMessages]: messages {}", messages);
final Iterable<Optional<Exception>> collect =
Stream.of(messages).map(m -> Optional.<Exception>empty()).toList();
return Future.successful(collect);
}
@Override
public Future<Void> doAsyncDeleteMessagesTo(final String persistenceId, final long toSequenceNr) {
LOGGER.debug("[doAsyncDeleteMessagesTo]: persistenceId {}, toSequenceNr {}", persistenceId, toSequenceNr);
journalMock.doAsyncDeleteMessagesTo(persistenceId, toSequenceNr);
if (FAIL_DELETE_MESSAGE.equals(persistenceId)) {
final CompletableFuture<Void> failDeleteMessage = new CompletableFuture<>();
failDeleteMessage.completeExceptionally(new IllegalStateException(FAIL_DELETE_MESSAGE));
return FutureConverters.toScala(failDeleteMessage);
} else if (SLOW_DELETE.equals(persistenceId)) {
final CompletableFuture<Void> completableFuture = new CompletableFuture<>();
// do not complete future to simulate long running delete
return FutureConverters.toScala(completableFuture);
}else {
return Future.successful(null);
}
}
static void verify(final String persistenceId, final int toSequenceNr) {
Mockito.verify(journalMock).doAsyncDeleteMessagesTo(persistenceId, toSequenceNr);
}
static void reset() {
Mockito.reset(journalMock);
}
}