Skip to content

Commit

Permalink
[plugin-rest-api] Introduce variableName parameter for FROM_JSON tran…
Browse files Browse the repository at this point in the history
…sformer
  • Loading branch information
EDbarvinsky committed Mar 15, 2023
1 parent 4dc7ac3 commit 34733d2
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 19 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 @@ -23,12 +23,14 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import javax.inject.Inject;

import com.google.common.base.Splitter;

import org.apache.commons.lang3.Validate;
import org.jbehave.core.model.ExamplesTable.TableProperties;
import org.jbehave.core.model.TableParsers;
import org.slf4j.Logger;
Expand All @@ -45,9 +47,12 @@ public class JsonRestApiTableTransformer implements ExtendedTableTransformer
private static final Logger LOGGER = LoggerFactory.getLogger(JsonRestApiTableTransformer.class);

private static final String URL_PROPERTY = "url";
@Deprecated(since = "0.5.6", forRemoval = true)
private static final String VARIABLE_PROPERTY = "variable";
private static final String VARIABLE_NAME_PROPERTY = "variableName";

@Inject private VariableContext variableContext;
@Inject
private VariableContext variableContext;
private IHttpClient httpClient;

@SuppressWarnings("unchecked")
Expand All @@ -58,9 +63,23 @@ public String transform(String tableAsString, TableParsers tableParsers, TablePr

String columns = properties.getMandatoryNonBlankProperty("columns", String.class);

boolean isDeprecatedPropertyPresent = properties.getProperties().getProperty(VARIABLE_PROPERTY) != null;
boolean isPropertyNamePresent = properties.getProperties().getProperty(VARIABLE_NAME_PROPERTY) != null;

if (isDeprecatedPropertyPresent)
{
warnDeprecateProperty(VARIABLE_PROPERTY, VARIABLE_NAME_PROPERTY);
}
Validate.isTrue(!(isPropertyNamePresent && isDeprecatedPropertyPresent),
"Only one property can be used, but found `%s` and `%s`. Please use `%s` instead",
VARIABLE_PROPERTY, VARIABLE_NAME_PROPERTY, VARIABLE_NAME_PROPERTY);

Map.Entry<String, Function<String, String>> variableProperty = isDeprecatedPropertyPresent
? entry(VARIABLE_PROPERTY, variableContext::getVariable)
: entry(VARIABLE_NAME_PROPERTY, variableContext::getVariable);
String jsonData = processCompetingMandatoryProperties(properties,
entry(URL_PROPERTY, this::getJsonByUrl),
entry(VARIABLE_PROPERTY, variableContext::getVariable));
variableProperty);

Map<String, String> columnsPerJsonPaths = Splitter.on(';').withKeyValueSeparator(Splitter.on('=').limit(2))
.split(columns);
Expand All @@ -84,10 +103,7 @@ public String transform(String tableAsString, TableParsers tableParsers, TablePr
@Deprecated(since = "0.5.3", forRemoval = true)
private String getJsonByUrl(String url)
{
LOGGER.warn(
"`{}` parameter of `FROM_JSON` table transformer is deprecated and will be removed in VIVIDUS 0.6.0. "
+ "`{}` parameter must be used instead.",
URL_PROPERTY, VARIABLE_PROPERTY);
warnDeprecateProperty(URL_PROPERTY, VARIABLE_NAME_PROPERTY);
try
{
return httpClient.doHttpGet(UriUtils.createUri(url)).getResponseBodyAsString();
Expand All @@ -102,4 +118,12 @@ public void setHttpClient(IHttpClient httpClient)
{
this.httpClient = httpClient;
}

private void warnDeprecateProperty(String deprecated, String replacement)
{
LOGGER.warn(
"`{}` parameter of `FROM_JSON` table transformer is deprecated and will be removed in VIVIDUS 0.6.0. "
+ "`{}` parameter must be used instead.",
deprecated, replacement);
}
}
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 @@ -27,17 +27,23 @@

import java.io.IOException;
import java.util.List;
import java.util.stream.Stream;

import com.github.valfirst.slf4jtest.LoggingEvent;
import com.github.valfirst.slf4jtest.TestLogger;
import com.github.valfirst.slf4jtest.TestLoggerFactory;
import com.github.valfirst.slf4jtest.TestLoggerFactoryExtension;

import org.apache.commons.lang3.StringUtils;
import org.hamcrest.Matcher;
import org.jbehave.core.configuration.Keywords;
import org.jbehave.core.model.ExamplesTable.TableProperties;
import org.jbehave.core.steps.ParameterConverters;
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.MethodSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
Expand All @@ -50,12 +56,19 @@
@ExtendWith({ MockitoExtension.class, TestLoggerFactoryExtension.class })
class JsonRestApiTableTransformerTests
{
private static final String COMMA = ",";

private static final String JSON_DATA = ResourceUtils.loadResource(JsonRestApiTableTransformerTests.class,
"data.json");
private static final String URL_VALUE = "https://example.com/";
private static final String URL_PROPERTY = "url";
private static final String VAR_NAME = "varName";

private static final String VARIABLE_PROPERTY = "variable";
private static final String VARIABLE_NAME_PROPERTY = "variableName";

private static final String EQUAL = "=";
private static final String VARIABLE_NAME_PARAMETER = VARIABLE_NAME_PROPERTY + EQUAL;
private static final String VARIABLE_PARAMETER = VARIABLE_PROPERTY + EQUAL;

@Mock private VariableContext variableContext;
Expand All @@ -74,19 +87,40 @@ void testTransformFromUrl() throws IOException
when(httpClient.doHttpGet(UriUtils.createUri(URL_VALUE))).thenReturn(httpResponse);
when(httpResponse.getResponseBodyAsString()).thenReturn(JSON_DATA);
testTransform(URL_PROPERTY + EQUAL + URL_VALUE);
assertThat(logger.getLoggingEvents(), is(List.of(warn(
"`{}` parameter of `FROM_JSON` table transformer is deprecated and will be removed in VIVIDUS 0.6.0. "
+ "`{}` parameter must be used instead.", URL_PROPERTY, VARIABLE_PROPERTY)
assertThat(logger.getLoggingEvents(), is(List.of(getDeprecatedEvent(URL_PROPERTY, VARIABLE_NAME_PROPERTY)
)));
}

static Stream<Arguments> parameters()
{
return Stream.of(
Arguments.of(VARIABLE_PARAMETER, is(List.of(getDeprecatedEvent(VARIABLE_PROPERTY, VARIABLE_NAME_PROPERTY)))
),
Arguments.of(VARIABLE_NAME_PARAMETER, empty())
);
}

@ParameterizedTest
@MethodSource("parameters")
void testTransformFromVariable(String parameter, Matcher expectedLogEvent)
{
when(variableContext.getVariable(VAR_NAME)).thenReturn(JSON_DATA);
testTransform(parameter + VAR_NAME);
assertThat(logger.getLoggingEvents(), expectedLogEvent);
}

@Test
void testTransformFromVariable()
void testTransformFromVariableValidationFails()
{
var variableName = "varName";
when(variableContext.getVariable(variableName)).thenReturn(JSON_DATA);
testTransform(VARIABLE_PARAMETER + variableName);
assertThat(logger.getLoggingEvents(), is(empty()));
var exception = assertThrows(IllegalArgumentException.class,
() -> testTransform(VARIABLE_NAME_PARAMETER + VAR_NAME + COMMA + VARIABLE_PARAMETER + VAR_NAME)
);
assertEquals(
"Only one property can be used, but found `variable` and `variableName`. Please use `variableName` instead",
exception.getMessage());

assertThat(logger.getLoggingEvents(),
is(List.of(getDeprecatedEvent(VARIABLE_PROPERTY, VARIABLE_NAME_PROPERTY))));
}

private void testTransform(String source)
Expand All @@ -112,7 +146,7 @@ void testTransformFromVariableWithComplexJsonPath()
var variableName = "varWithJson";
when(variableContext.getVariable(variableName)).thenReturn(JSON_DATA);
var columns = "columns=type=$..[?(@.codes[0].code==\"107214\")].type";
var tableProperties = createProperties(columns + "," + VARIABLE_PARAMETER + variableName);
var tableProperties = createProperties(columns + COMMA + VARIABLE_NAME_PARAMETER + variableName);
var table = jsonTableGenerator.transform(StringUtils.EMPTY, null, tableProperties);
var expectedTable = "|type|\n|A|";
assertEquals(expectedTable, table);
Expand All @@ -125,13 +159,14 @@ void testSourceTransformPropertyIsNotSpecifiedException()
var properties = createProperties("columns=key=value\\,url");
var exception = assertThrows(IllegalArgumentException.class,
() -> jsonTableGenerator.transform(StringUtils.EMPTY, null, properties));
assertEquals("One of ExamplesTable properties must be set: either 'url' or 'variable'", exception.getMessage());
assertEquals("One of ExamplesTable properties must be set: either 'url' or 'variableName'",
exception.getMessage());
}

@Test
void testColumnsTransformPropertyIsNotSpecifiedException()
{
var properties = createProperties(VARIABLE_PROPERTY + EQUAL + "any");
var properties = createProperties(VARIABLE_NAME_PROPERTY + EQUAL + "any");
var exception = assertThrows(IllegalArgumentException.class,
() -> jsonTableGenerator.transform(StringUtils.EMPTY, null, properties));
assertEquals("'columns' is not set in ExamplesTable properties", exception.getMessage());
Expand All @@ -141,4 +176,11 @@ private TableProperties createProperties(String propertiesAsString)
{
return new TableProperties(propertiesAsString, keywords, parameterConverters);
}

private static LoggingEvent getDeprecatedEvent(String current, String replacement)
{
return warn(
"`{}` parameter of `FROM_JSON` table transformer is deprecated and will be removed in VIVIDUS 0.6.0. "
+ "`{}` parameter must be used instead.", current, replacement);
}
}

0 comments on commit 34733d2

Please sign in to comment.