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

[plugin-rest-api] Introduce variableName parameter for FROM_JSON transformer #3718

Merged
merged 1 commit into from
Mar 17, 2023
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
6 changes: 4 additions & 2 deletions docs/modules/plugins/pages/plugin-rest-api.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,12 @@ Then `${response-code}` is equal to `200`
|Description

|`variable`
|The name of the variable containing source JSON, only variables of scopes `global` and `next_batches` are allowed.
a| WARNING: This parameter is deprecated and will be removed in VIVIDUS 0.6.0. `variableName` parameter must be used instead.

The name of the variable containing source JSON, only variables of scopes `global` and `next_batches` are allowed.

|`url`
a| WARNING: This parameter is deprecated and will be removed in VIVIDUS 0.6.0. `variable` parameter must be used instead.
a| WARNING: This parameter is deprecated and will be removed in VIVIDUS 0.6.0. `variableName` parameter must be used instead.

The URL of the JSON resource that can be retrieved using the https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET[HTTP GET] call.

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 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";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no docs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

proper warning is added into docs on the same way as for url


@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().containsKey(VARIABLE_PROPERTY);
boolean namePropertyPresent = properties.getProperties().containsKey(VARIABLE_NAME_PROPERTY);

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));
}
}