Skip to content

Commit

Permalink
[engine] Use pretty logging when saving maps and lists
Browse files Browse the repository at this point in the history
  • Loading branch information
uarlouski committed Apr 4, 2023
1 parent 7911bef commit ad57eca
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 the original author or authors.
* Copyright 2019-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,13 +16,15 @@

package org.vividus.context;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vividus.testcontext.TestContext;
import org.vividus.util.EnumUtils;
import org.vividus.util.json.JsonUtils;
import org.vividus.variable.IVariablesFactory;
import org.vividus.variable.VariableScope;
import org.vividus.variable.Variables;
Expand All @@ -35,11 +37,13 @@ public class VariableTestContext implements VariableContext

private final TestContext testContext;
private final IVariablesFactory variablesFactory;
private final JsonUtils jsonUtils;

public VariableTestContext(TestContext testContext, IVariablesFactory variablesFactory)
public VariableTestContext(TestContext testContext, IVariablesFactory variablesFactory, JsonUtils jsonUtils)
{
this.testContext = testContext;
this.variablesFactory = variablesFactory;
this.jsonUtils = jsonUtils;
}

@Override
Expand Down Expand Up @@ -69,7 +73,9 @@ public void putVariable(VariableScope variableScope, String variableKey, Object
else
{
LOGGER.atInfo()
.addArgument(variableValue)
.addArgument(() -> variableValue instanceof Map || variableValue instanceof Collection
? jsonUtils.toPrettyJson(variableValue)
: variableValue)
.addArgument(() -> EnumUtils.toHumanReadableForm(variableScope))
.addArgument(variableKey)
.log("Saving a value '{}' into the {} variable '{}'");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 the original author or authors.
* Copyright 2019-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,6 +31,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

import com.github.valfirst.slf4jtest.LoggingEvent;
import com.github.valfirst.slf4jtest.TestLogger;
Expand All @@ -40,15 +41,18 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.EnumSource.Mode;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.vividus.testcontext.SimpleTestContext;
import org.vividus.testcontext.TestContext;
import org.vividus.util.EnumUtils;
import org.vividus.util.json.JsonUtils;
import org.vividus.variable.IVariablesFactory;
import org.vividus.variable.VariableScope;
import org.vividus.variable.Variables;
Expand All @@ -60,11 +64,13 @@ class VariableTestContextTests
private static final String VARIABLE_KEY = "variableKey";
private static final String VALUE = "value";
private static final String STEP = "step";
private static final String STORY = "story";

private final TestLogger logger = TestLoggerFactory.getTestLogger(VariableTestContext.class);

@Mock private IVariablesFactory variablesFactory;
@Spy private final TestContext testContext = new SimpleTestContext();
@Spy private final JsonUtils jsonUtils = new JsonUtils();
@InjectMocks private VariableTestContext variableTestContext;

@Test
Expand Down Expand Up @@ -140,7 +146,7 @@ void shouldPutVariablesIntoAllPassedScopes()
assertThat(loggingEvents, hasItems(
info(SAVE_MESSAGE_TEMPLATE, VALUE, STEP, VARIABLE_KEY),
info(SAVE_MESSAGE_TEMPLATE, VALUE, "scenario", VARIABLE_KEY),
info(SAVE_MESSAGE_TEMPLATE, VALUE, "story", VARIABLE_KEY),
info(SAVE_MESSAGE_TEMPLATE, VALUE, STORY, VARIABLE_KEY),
info(SAVE_MESSAGE_TEMPLATE, VALUE, "next batches", VARIABLE_KEY)));
verify(variablesFactory).addNextBatchesVariable(VARIABLE_KEY, VALUE);
}
Expand Down Expand Up @@ -193,4 +199,24 @@ void shoulReturnVariables()
variableTestContext.getVariables();
verify(variables).getVariables();
}

private static Stream<Arguments> containers()
{
return Stream.of(
Arguments.of(Map.of("stars", 323)),
Arguments.of(List.of(Map.of("name", "Ulad")))
);
}

@ParameterizedTest
@MethodSource("containers")
void shouldPutContainerToVariable(Object container)
{
Variables variables = new Variables(Map.of());
when(variablesFactory.createVariables()).thenReturn(variables);
variableTestContext.putVariable(VariableScope.STORY, VARIABLE_KEY, container);
assertEquals(container, variables.getVariable(VARIABLE_KEY));
assertThat(logger.getLoggingEvents(),
equalTo(List.of(info(SAVE_MESSAGE_TEMPLATE, jsonUtils.toPrettyJson(container), STORY, VARIABLE_KEY))));
}
}

0 comments on commit ad57eca

Please sign in to comment.