|
1 | 1 | /* |
2 | | - * Copyright © 2021 camunda services GmbH (info@camunda.com) |
| 2 | + * Copyright © 2024 camunda services GmbH (info@camunda.com) |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
16 | 16 | package io.camunda.zeebe.process.test.assertions; |
17 | 17 |
|
18 | 18 | import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 19 | +import static org.assertj.core.api.MapAssert.assertThatMap; |
19 | 20 |
|
20 | 21 | import io.camunda.zeebe.client.api.response.PublishMessageResponse; |
21 | 22 | import io.camunda.zeebe.process.test.filters.RecordStream; |
|
27 | 28 | import io.camunda.zeebe.protocol.record.intent.ProcessMessageSubscriptionIntent; |
28 | 29 | import io.camunda.zeebe.protocol.record.value.MessageStartEventSubscriptionRecordValue; |
29 | 30 | import io.camunda.zeebe.protocol.record.value.ProcessMessageSubscriptionRecordValue; |
| 31 | +import java.util.Collections; |
30 | 32 | import java.util.List; |
| 33 | +import java.util.Map; |
31 | 34 | import java.util.Optional; |
32 | 35 | import java.util.stream.Collectors; |
33 | 36 | import org.assertj.core.api.AbstractAssert; |
|
36 | 39 | /** Assertions for {@link PublishMessageResponse} instances */ |
37 | 40 | public class MessageAssert extends AbstractAssert<MessageAssert, PublishMessageResponse> { |
38 | 41 |
|
39 | | - private RecordStream recordStream; |
| 42 | + private final RecordStream recordStream; |
40 | 43 |
|
41 | 44 | protected MessageAssert(final PublishMessageResponse actual, final RecordStream recordStream) { |
42 | 45 | super(actual, MessageAssert.class); |
@@ -234,4 +237,51 @@ private List<Long> getProcessInstanceKeysForCorrelatedMessageStartEvent() { |
234 | 237 | .map(record -> record.getValue().getProcessInstanceKey()) |
235 | 238 | .collect(Collectors.toList()); |
236 | 239 | } |
| 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 | + } |
237 | 287 | } |
0 commit comments