Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NPE during logging #307

Merged
merged 2 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions filters/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class RecordStreamLogger {
private final Map<ValueType, Function<Record<?>, String>> valueTypeLoggers = new HashMap<>();

public RecordStreamLogger(final RecordStreamSource recordStreamSource) {
this.recordStream = RecordStream.of(recordStreamSource);
recordStream = RecordStream.of(recordStreamSource);
valueTypeLoggers.put(ValueType.JOB, this::logJobRecordValue);
valueTypeLoggers.put(ValueType.DEPLOYMENT, this::logDeploymentRecordValue);
valueTypeLoggers.put(ValueType.PROCESS_INSTANCE, this::logProcessInstanceRecordValue);
Expand Down Expand Up @@ -266,13 +266,13 @@ private String logProcessEventRecordValue(final Record<?> record) {
return joiner.toString();
}

private String logVariables(final Map<String, Object> variables) {
protected String logVariables(final Map<String, Object> variables) {
if (variables.isEmpty()) {
return "";
}

final StringJoiner joiner = new StringJoiner(", ", "[", "]");
variables.forEach((key, value) -> joiner.add(key + " -> " + value.toString()));
variables.forEach((key, value) -> joiner.add(key + " -> " + value));
return String.format("(Variables: %s)", joiner);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,34 @@
*/
package io.camunda.zeebe.process.test.filters.logger;

import static org.assertj.core.api.Assertions.assertThat;

import io.camunda.zeebe.protocol.record.Record;
import io.camunda.zeebe.protocol.record.ValueType;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Stream;
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

class RecordStreamLoggerTest {

private static final Map<String, Object> TYPED_TEST_VARIABLES = new HashMap<>();
pihme marked this conversation as resolved.
Show resolved Hide resolved

static {
TYPED_TEST_VARIABLES.put("stringProperty", "stringValue");
TYPED_TEST_VARIABLES.put("numberProperty", 123);
TYPED_TEST_VARIABLES.put("booleanProperty", true);
TYPED_TEST_VARIABLES.put("complexProperty", Arrays.asList("Element 1", "Element 2"));
TYPED_TEST_VARIABLES.put("nullProperty", null);
}

@Test
void testAllValueTypesAreMapped() {
final Map<ValueType, Function<Record<?>, String>> valueTypeLoggers =
Expand All @@ -41,4 +59,34 @@ void testAllValueTypesAreMapped() {

softly.assertAll();
}

@ParameterizedTest(name = "{0}")
@MethodSource("provideVariables")
void testLogVariable(final String key, final Object value) {
final RecordStreamLogger logger = new RecordStreamLogger(null);

final String result = logger.logVariables(Collections.singletonMap(key, value));

assertThat(result).isEqualTo(String.format("(Variables: [%s -> %s])", key, value));
}

@Test
void testLogMultipleVariables() {
final RecordStreamLogger logger = new RecordStreamLogger(null);

final String result = logger.logVariables(TYPED_TEST_VARIABLES);

assertThat(result)
.contains("Variables: ")
.contains("stringProperty -> stringValue")
.contains("numberProperty -> 123")
.contains("booleanProperty -> true")
.contains("complexProperty -> [Element 1, Element 2]")
.contains("nullProperty -> null");
}

private static Stream<Arguments> provideVariables() {
return TYPED_TEST_VARIABLES.entrySet().stream()
.map(entry -> Arguments.of(entry.getKey(), entry.getValue()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public abstract class AbstractProcessInstanceAssertTest {
TYPED_TEST_VARIABLES.put("numberProperty", 123);
TYPED_TEST_VARIABLES.put("booleanProperty", true);
TYPED_TEST_VARIABLES.put("complexProperty", Arrays.asList("Element 1", "Element 2"));
TYPED_TEST_VARIABLES.put("nullProperty", null);
}

// These tests are for testing assertions as well as examples for users
Expand Down