Skip to content

Commit 8a716ce

Browse files
committed
feat: add hasVariable and hasVariableWithValue to MessageAssert
adds assertions for message variables similar to `ProcessInstanceAssert`
1 parent 7ed7474 commit 8a716ce

File tree

2 files changed

+120
-2
lines changed

2 files changed

+120
-2
lines changed

assertions/src/main/java/io/camunda/zeebe/process/test/assertions/MessageAssert.java

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2021 camunda services GmbH (info@camunda.com)
2+
* Copyright © 2024 camunda services GmbH (info@camunda.com)
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616
package io.camunda.zeebe.process.test.assertions;
1717

1818
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
19+
import static org.assertj.core.api.MapAssert.assertThatMap;
1920

2021
import io.camunda.zeebe.client.api.response.PublishMessageResponse;
2122
import io.camunda.zeebe.process.test.filters.RecordStream;
@@ -27,7 +28,9 @@
2728
import io.camunda.zeebe.protocol.record.intent.ProcessMessageSubscriptionIntent;
2829
import io.camunda.zeebe.protocol.record.value.MessageStartEventSubscriptionRecordValue;
2930
import io.camunda.zeebe.protocol.record.value.ProcessMessageSubscriptionRecordValue;
31+
import java.util.Collections;
3032
import java.util.List;
33+
import java.util.Map;
3134
import java.util.Optional;
3235
import java.util.stream.Collectors;
3336
import org.assertj.core.api.AbstractAssert;
@@ -36,7 +39,7 @@
3639
/** Assertions for {@link PublishMessageResponse} instances */
3740
public class MessageAssert extends AbstractAssert<MessageAssert, PublishMessageResponse> {
3841

39-
private RecordStream recordStream;
42+
private final RecordStream recordStream;
4043

4144
protected MessageAssert(final PublishMessageResponse actual, final RecordStream recordStream) {
4245
super(actual, MessageAssert.class);
@@ -234,4 +237,51 @@ private List<Long> getProcessInstanceKeysForCorrelatedMessageStartEvent() {
234237
.map(record -> record.getValue().getProcessInstanceKey())
235238
.collect(Collectors.toList());
236239
}
240+
241+
private Map<String, Object> getVariables() {
242+
return StreamFilter.processMessageSubscription(recordStream)
243+
.withMessageKey(actual.getMessageKey())
244+
.withRejectionType(RejectionType.NULL_VAL)
245+
.withIntent(ProcessMessageSubscriptionIntent.CORRELATED)
246+
.stream()
247+
.map(record -> record.getValue().getVariables())
248+
.findFirst().orElse(Collections.emptyMap());
249+
}
250+
251+
/**
252+
* Verifies the message has the given variable.
253+
*
254+
* @param name The name of the variable
255+
* @return this ${@link MessageAssert}
256+
*/
257+
public MessageAssert hasVariable(final String name) {
258+
final Map<String, Object> variables = getVariables();
259+
assertThatMap(variables)
260+
.withFailMessage(
261+
"Unable to find variable with name '%s'. Available variables are: %s",
262+
name, variables.keySet())
263+
.containsKeys(name);
264+
return this;
265+
}
266+
267+
/**
268+
* Verifies the message has the given variable with the specified value.
269+
*
270+
* @param name The name of the variable
271+
* @param value The value of the variable
272+
* @return this ${@link MessageAssert}
273+
*/
274+
public MessageAssert hasVariableWithValue(final String name, final Object value) {
275+
hasVariable(name);
276+
277+
final Object actualValue = getVariables().get(name);
278+
assertThat(actualValue)
279+
.withFailMessage(
280+
"The variable '%s' does not have the expected value.%n"
281+
+ "expected: %s%n"
282+
+ "but was: %s",
283+
name, value, actualValue)
284+
.isEqualTo(value);
285+
return this;
286+
}
237287
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright © 2024 camunda services GmbH (info@camunda.com)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.camunda.zeebe.process.test.assertions;
18+
19+
import static org.assertj.core.api.Assertions.assertThatCode;
20+
import static org.mockito.Mockito.doReturn;
21+
import static org.mockito.Mockito.mock;
22+
23+
import io.camunda.zeebe.protocol.record.Record;
24+
import io.camunda.zeebe.client.api.response.PublishMessageResponse;
25+
import io.camunda.zeebe.process.test.filters.RecordStream;
26+
import io.camunda.zeebe.protocol.record.RejectionType;
27+
import io.camunda.zeebe.protocol.record.ValueType;
28+
import io.camunda.zeebe.protocol.record.intent.ProcessMessageSubscriptionIntent;
29+
import io.camunda.zeebe.protocol.record.value.ProcessMessageSubscriptionRecordValue;
30+
import java.util.Collections;
31+
import org.junit.jupiter.api.AfterEach;
32+
import org.junit.jupiter.api.Test;
33+
34+
class MessageAssertTest {
35+
36+
@AfterEach
37+
void afterEach() {
38+
BpmnAssert.resetRecordStream();
39+
}
40+
41+
@Test
42+
void hasVariableWithValueTest() {
43+
PublishMessageResponse result = mock(PublishMessageResponse.class);
44+
doReturn(123L).when(result).getMessageKey();
45+
46+
ProcessMessageSubscriptionRecordValue recordValue = mock(ProcessMessageSubscriptionRecordValue.class);
47+
doReturn(Collections.singletonMap("key", "value")).when(recordValue).getVariables();
48+
doReturn(123L).when(recordValue).getMessageKey();
49+
50+
Record<ProcessMessageSubscriptionRecordValue> record = mock(Record.class);
51+
doReturn(recordValue).when(record).getValue();
52+
doReturn(ValueType.PROCESS_MESSAGE_SUBSCRIPTION).when(record).getValueType();
53+
doReturn(RejectionType.NULL_VAL).when(record).getRejectionType();
54+
doReturn(ProcessMessageSubscriptionIntent.CORRELATED).when(record).getIntent();
55+
56+
BpmnAssert.initRecordStream(RecordStream.of(() -> Collections.singleton(record)));
57+
58+
BpmnAssert.assertThat(result).hasVariableWithValue("key", "value");
59+
60+
assertThatCode(() -> BpmnAssert.assertThat(result).hasVariableWithValue("foo", "value"))
61+
.isInstanceOf(AssertionError.class)
62+
.hasMessageStartingWith("Unable to find variable with name 'foo'.");
63+
64+
assertThatCode(() -> BpmnAssert.assertThat(result).hasVariableWithValue("key", "value2"))
65+
.isInstanceOf(AssertionError.class)
66+
.hasMessageStartingWith("The variable 'key' does not have the expected value.");
67+
}
68+
}

0 commit comments

Comments
 (0)