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 16, 2023
1 parent 4dc7ac3 commit c49e6c5
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 23 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,7 +47,9 @@ 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.7", forRemoval = true)
private static final String VARIABLE_PROPERTY = "variable";
private static final String VARIABLE_NAME_PROPERTY = "variableName";

@Inject private VariableContext variableContext;
private IHttpClient httpClient;
Expand All @@ -58,9 +62,23 @@ public String transform(String tableAsString, TableParsers tableParsers, TablePr

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

boolean deprecatedNamePropertyPresent = properties.getProperties().getProperty(VARIABLE_PROPERTY) != null;
boolean namePropertyPresent = properties.getProperties().getProperty(VARIABLE_NAME_PROPERTY) != null;

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

Map.Entry<String, Function<String, String>> variableProperty =
entry(deprecatedNamePropertyPresent ? VARIABLE_PROPERTY : 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 +102,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);
warnOnDeprecatedParameter(URL_PROPERTY, VARIABLE_NAME_PROPERTY);
try
{
return httpClient.doHttpGet(UriUtils.createUri(url)).getResponseBodyAsString();
Expand All @@ -102,4 +117,12 @@ public void setHttpClient(IHttpClient httpClient)
{
this.httpClient = httpClient;
}

private void warnOnDeprecatedParameter(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 All @@ -17,17 +17,18 @@
package org.vividus.http.transformer;

import static com.github.valfirst.slf4jtest.LoggingEvent.warn;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.params.provider.Arguments.arguments;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.util.Collections;
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;
Expand All @@ -38,6 +39,9 @@
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 +54,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 +85,37 @@ 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)
)));
assertEquals(getDeprecatedEvent(URL_PROPERTY, VARIABLE_NAME_PROPERTY), logger.getLoggingEvents());
}

static Stream<Arguments> parameters()
{
return Stream.of(
arguments(VARIABLE_PARAMETER, getDeprecatedEvent(VARIABLE_PROPERTY, VARIABLE_NAME_PROPERTY)),
arguments(VARIABLE_NAME_PARAMETER, Collections.EMPTY_LIST)
);
}

@ParameterizedTest
@MethodSource("parameters")
void testTransformFromVariable(String parameter, List<LoggingEvent> expectedLogEvent)
{
when(variableContext.getVariable(VAR_NAME)).thenReturn(JSON_DATA);
testTransform(parameter + VAR_NAME);
assertEquals(expectedLogEvent, logger.getLoggingEvents());
}

@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`",
exception.getMessage());

assertEquals(getDeprecatedEvent(VARIABLE_PROPERTY, VARIABLE_NAME_PROPERTY), logger.getLoggingEvents());
}

private void testTransform(String source)
Expand All @@ -112,11 +141,11 @@ 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);
assertThat(logger.getLoggingEvents(), is(empty()));
assertEquals(Collections.EMPTY_LIST, logger.getLoggingEvents());
}

@Test
Expand All @@ -125,13 +154,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 +171,11 @@ private TableProperties createProperties(String propertiesAsString)
{
return new TableProperties(propertiesAsString, keywords, parameterConverters);
}

private static List<LoggingEvent> getDeprecatedEvent(String current, String replacement)
{
return 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.", current, replacement));
}
}

0 comments on commit c49e6c5

Please sign in to comment.