-
Notifications
You must be signed in to change notification settings - Fork 214
/
ResultFactoryTest.java
112 lines (93 loc) · 4.71 KB
/
ResultFactoryTest.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
/*
* 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.things.service.persistence.actors.strategies.commands;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mutabilitydetector.unittesting.MutabilityAssert.assertInstancesOf;
import static org.mutabilitydetector.unittesting.MutabilityMatchers.areImmutable;
import java.util.concurrent.CompletionStage;
import org.assertj.core.api.CompletableFutureAssert;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.headers.WithDittoHeaders;
import org.eclipse.ditto.base.model.signals.commands.Command;
import org.eclipse.ditto.internal.utils.persistentactors.results.Result;
import org.eclipse.ditto.internal.utils.persistentactors.results.ResultFactory;
import org.eclipse.ditto.internal.utils.persistentactors.results.ResultVisitor;
import org.eclipse.ditto.things.model.signals.commands.ThingCommandResponse;
import org.eclipse.ditto.things.model.signals.commands.modify.ThingModifyCommand;
import org.eclipse.ditto.things.model.signals.commands.query.ThingQueryCommand;
import org.eclipse.ditto.things.model.signals.events.ThingEvent;
import org.eclipse.ditto.things.model.signals.events.ThingModifiedEvent;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
/**
* Unit tests for {@link org.eclipse.ditto.internal.utils.persistentactors.results.ResultFactory}.
*/
public final class ResultFactoryTest {
private final ResultVisitor<ThingEvent<?>> mock = mock(Dummy.class);
private final ThingQueryCommand thingQueryCommand = mock(ThingQueryCommand.class);
private final ThingModifyCommand thingModifyCommand = mock(ThingModifyCommand.class);
private final ThingModifiedEvent thingModifiedEvent = mock(ThingModifiedEvent.class);
private final ThingCommandResponse response = mock(ThingCommandResponse.class);
private final DittoRuntimeException exception = mock(DittoRuntimeException.class);
@Test
public void assertImmutability() {
assertInstancesOf(ResultFactory.class, areImmutable());
}
@Test
public void notifyQueryResponse() {
final Result<ThingEvent<?>> result = ResultFactory.newQueryResult(thingQueryCommand, response);
result.accept(mock);
verify(mock).onQuery(eq(thingQueryCommand), eq(response));
}
@Test
public void notifyException() {
final Command command = mock(Command.class);
final Result<ThingEvent<?>> result = ResultFactory.newErrorResult(exception, command);
result.accept(this.mock);
verify(this.mock).onError(eq(exception), eq(command));
}
@Test
public void notifyMutationResponseWithNeitherBecomingDeletedNorCreated() {
assertNotifyMutationResponse(false, false);
}
@Test
public void notifyMutationResponseWithBecomingCreated() {
assertNotifyMutationResponse(true, false);
}
@Test
public void notifyMutationResponseWithBecomingDeleted() {
assertNotifyMutationResponse(false, true);
}
private void assertNotifyMutationResponse(final boolean becomeCreated, final boolean becomeDeleted) {
final Result<ThingEvent<?>> result =
ResultFactory.newMutationResult(thingModifyCommand, thingModifiedEvent, response,
becomeCreated,
becomeDeleted);
result.accept(mock);
final ArgumentCaptor<CompletionStage<ThingEvent<?>>> eventStage = ArgumentCaptor.forClass(CompletionStage.class);
final ArgumentCaptor<CompletionStage<WithDittoHeaders>> responseStage = ArgumentCaptor.forClass(CompletionStage.class);
verify(mock).onMutation(eq(thingModifyCommand), eventStage.capture(),
responseStage.capture(), eq(becomeCreated), eq(becomeDeleted));
assertThat(eventStage.getValue()).isInstanceOf(CompletionStage.class);
CompletableFutureAssert.assertThatCompletionStage(eventStage.getValue())
.isCompletedWithValue(thingModifiedEvent);
assertThat(responseStage.getValue()).isInstanceOf(CompletionStage.class);
CompletableFutureAssert.assertThatCompletionStage(responseStage.getValue())
.isCompletedWithValue(response);
}
interface Dummy extends ResultVisitor<ThingEvent<?>> {
}
}