-
Notifications
You must be signed in to change notification settings - Fork 214
/
AbstractCommandStrategyTest.java
217 lines (174 loc) · 10.1 KB
/
AbstractCommandStrategyTest.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
* 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.things.service.persistence.actors.strategies.commands;
import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.ditto.things.model.TestConstants.Thing.THING_ID;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import java.util.concurrent.CompletionStage;
import java.util.function.Consumer;
import javax.annotation.Nullable;
import org.assertj.core.api.CompletableFutureAssert;
import org.eclipse.ditto.base.model.common.DittoSystemProperties;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.WithDittoHeaders;
import org.eclipse.ditto.base.model.signals.commands.Command;
import org.eclipse.ditto.base.model.signals.commands.CommandResponse;
import org.eclipse.ditto.internal.utils.akka.ActorSystemResource;
import org.eclipse.ditto.internal.utils.akka.logging.DittoDiagnosticLoggingAdapter;
import org.eclipse.ditto.internal.utils.persistentactors.commands.AbstractCommandStrategy;
import org.eclipse.ditto.internal.utils.persistentactors.commands.CommandStrategy;
import org.eclipse.ditto.internal.utils.persistentactors.commands.DefaultContext;
import org.eclipse.ditto.internal.utils.persistentactors.results.Result;
import org.eclipse.ditto.internal.utils.persistentactors.results.ResultVisitor;
import org.eclipse.ditto.things.model.Thing;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.signals.events.ThingEvent;
import org.eclipse.ditto.things.model.signals.events.ThingModifiedEvent;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
/**
* Abstract base implementation for unit tests of implementations of {@link org.eclipse.ditto.internal.utils.persistentactors.commands.AbstractCommandStrategy}.
*/
public abstract class AbstractCommandStrategyTest {
protected static final long NEXT_REVISION = 42L;
protected static final long THING_SIZE_LIMIT_BYTES = Long.parseLong(
System.getProperty(DittoSystemProperties.DITTO_LIMITS_THINGS_MAX_SIZE_BYTES, "-1"));
protected static DittoDiagnosticLoggingAdapter logger;
@ClassRule
public static final ActorSystemResource ACTOR_SYSTEM_RESOURCE = ActorSystemResource.newInstance();
@BeforeClass
public static void initTestConstants() {
logger = Mockito.mock(DittoDiagnosticLoggingAdapter.class);
Mockito.when(logger.withCorrelationId(Mockito.any(DittoHeaders.class))).thenReturn(logger);
Mockito.when(logger.withCorrelationId(Mockito.any(WithDittoHeaders.class))).thenReturn(logger);
Mockito.when(logger.withCorrelationId(Mockito.any(CharSequence.class))).thenReturn(logger);
}
protected static CommandStrategy.Context<ThingId> getDefaultContext() {
return DefaultContext.getInstance(THING_ID, logger, ACTOR_SYSTEM_RESOURCE.getActorSystem());
}
protected static <C extends Command<?>, T extends ThingModifiedEvent<?>> T assertModificationResult(
final CommandStrategy<C, Thing, ThingId, ThingEvent<?>> underTest,
@Nullable final Thing thing,
final C command,
final Class<T> expectedEventClass,
final CommandResponse<?> expectedCommandResponse) {
return assertModificationResult(underTest, thing, command, expectedEventClass, expectedCommandResponse, false);
}
protected static <C extends Command<?>, T extends ThingModifiedEvent<?>> T assertModificationResult(
final CommandStrategy<C, Thing, ThingId, ThingEvent<?>> underTest,
@Nullable final Thing thing,
final C command,
final Class<T> expectedEventClass,
final CommandResponse<?> expectedCommandResponse,
final boolean becomeDeleted) {
final CommandStrategy.Context<ThingId> context = getDefaultContext();
final Result<ThingEvent<?>> result = applyStrategy(underTest, context, thing, command);
return assertModificationResult(result, expectedEventClass, expectedCommandResponse, becomeDeleted);
}
protected static <C extends Command<?>, T extends ThingModifiedEvent<?>> T assertStagedModificationResult(
final CommandStrategy<C, Thing, ThingId, ThingEvent<?>> underTest,
@Nullable final Thing thing,
final C command,
final Class<T> expectedEventClass,
final CommandResponse<?> expectedCommandResponse,
final boolean becomeDeleted) {
final CommandStrategy.Context<ThingId> context = getDefaultContext();
final Result<ThingEvent<?>> result = applyStrategy(underTest, context, thing, command);
return assertStagedModificationResult(result, expectedEventClass, expectedCommandResponse, becomeDeleted);
}
protected static <C extends Command<?>> void assertErrorResult(
final CommandStrategy<C, Thing, ThingId, ThingEvent<?>> underTest,
@Nullable final Thing thing,
final C command,
final DittoRuntimeException expectedException) {
final ResultVisitor<ThingEvent<?>> mock = mock(Dummy.class);
applyStrategy(underTest, getDefaultContext(), thing, command).accept(mock);
verify(mock).onError(eq(expectedException), eq(command));
}
protected static <C extends Command<?>> void assertQueryResult(
final CommandStrategy<C, Thing, ThingId, ThingEvent<?>> underTest,
@Nullable final Thing thing,
final C command,
final CommandResponse<?> expectedCommandResponse) {
assertInfoResult(applyStrategy(underTest, getDefaultContext(), thing, command), expectedCommandResponse);
}
protected static <C extends Command<?>> void assertQueryResult(
final CommandStrategy<C, Thing, ThingId, ThingEvent<?>> underTest,
@Nullable final Thing thing,
final C command,
final Consumer<CommandResponse<?>> commandResponseAssertions) {
final Result<ThingEvent<?>> thingEventResult = applyStrategy(underTest, getDefaultContext(), thing, command);
final ResultVisitor<ThingEvent<?>> mock = mock(Dummy.class);
thingEventResult.accept(mock);
final ArgumentCaptor<CommandResponse<?>> captor = ArgumentCaptor.forClass(CommandResponse.class);
verify(mock).onQuery(any(), captor.capture());
commandResponseAssertions.accept(captor.getValue());
}
protected static <C extends Command<?>> void assertUnhandledResult(
final AbstractCommandStrategy<C, Thing, ThingId, ThingEvent<?>> underTest,
@Nullable final Thing thing,
final C command,
final DittoRuntimeException expectedResponse) {
final ResultVisitor<ThingEvent<?>> mock = mock(Dummy.class);
underTest.unhandled(getDefaultContext(), thing, NEXT_REVISION, command).accept(mock);
verify(mock).onError(eq(expectedResponse), eq(command));
}
private static <T extends ThingModifiedEvent<?>> T assertModificationResult(final Result<ThingEvent<?>> result,
final Class<T> eventClazz,
final WithDittoHeaders expectedResponse,
final boolean becomeDeleted) {
final ArgumentCaptor<T> event = ArgumentCaptor.forClass(eventClazz);
final ResultVisitor<ThingEvent<?>> mock = mock(Dummy.class);
result.accept(mock);
verify(mock).onMutation(any(), event.capture(), eq(expectedResponse), anyBoolean(), eq(becomeDeleted));
assertThat(event.getValue()).isInstanceOf(eventClazz);
return event.getValue();
}
private static <T extends ThingModifiedEvent<?>> T assertStagedModificationResult(final Result<ThingEvent<?>> result,
final Class<T> eventClazz,
final WithDittoHeaders expectedResponse,
final boolean becomeDeleted) {
final ArgumentCaptor<CompletionStage<ThingEvent<?>>> eventStage = ArgumentCaptor.forClass(CompletionStage.class);
final ArgumentCaptor<CompletionStage<WithDittoHeaders>> responseStage = ArgumentCaptor.forClass(CompletionStage.class);
final ResultVisitor<ThingEvent<?>> mock = mock(Dummy.class);
result.accept(mock);
verify(mock).onStagedMutation(any(), eventStage.capture(), responseStage.capture(), anyBoolean(), eq(becomeDeleted));
assertThat(eventStage.getValue()).isInstanceOf(CompletionStage.class);
CompletableFutureAssert.assertThatCompletionStage(eventStage.getValue())
.isCompletedWithValueMatching(t -> eventClazz.isAssignableFrom(t.getClass()));
assertThat(responseStage.getValue()).isInstanceOf(CompletionStage.class);
CompletableFutureAssert.assertThatCompletionStage(responseStage.getValue())
.isCompletedWithValue(expectedResponse);
return (T) eventStage.getValue().toCompletableFuture().join();
}
private static void assertInfoResult(final Result<ThingEvent<?>> result, final WithDittoHeaders infoResponse) {
final ResultVisitor<ThingEvent<?>> mock = mock(Dummy.class);
result.accept(mock);
verify(mock).onQuery(any(), eq(infoResponse));
}
private static <C extends Command<?>> Result<ThingEvent<?>> applyStrategy(
final CommandStrategy<C, Thing, ThingId, ThingEvent<?>> underTest,
final CommandStrategy.Context<ThingId> context,
final @Nullable Thing thing,
final C command) {
return underTest.apply(context, thing, NEXT_REVISION, command);
}
interface Dummy extends ResultVisitor<ThingEvent<?>> {}
}