From c49e6c5c50d61919c14fafc9692f8a59ae446047 Mon Sep 17 00:00:00 2001 From: Yauheni Barvinski Date: Tue, 14 Mar 2023 16:51:24 +0100 Subject: [PATCH] [plugin-rest-api] Introduce variableName parameter for FROM_JSON transformer --- .../JsonRestApiTableTransformer.java | 35 +++++++-- .../JsonRestApiTableTransformerTests.java | 71 ++++++++++++++----- 2 files changed, 83 insertions(+), 23 deletions(-) diff --git a/vividus-plugin-rest-api/src/main/java/org/vividus/http/transformer/JsonRestApiTableTransformer.java b/vividus-plugin-rest-api/src/main/java/org/vividus/http/transformer/JsonRestApiTableTransformer.java index d2c2b5f78b..c81104c764 100644 --- a/vividus-plugin-rest-api/src/main/java/org/vividus/http/transformer/JsonRestApiTableTransformer.java +++ b/vividus-plugin-rest-api/src/main/java/org/vividus/http/transformer/JsonRestApiTableTransformer.java @@ -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. @@ -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; @@ -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; @@ -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> 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 columnsPerJsonPaths = Splitter.on(';').withKeyValueSeparator(Splitter.on('=').limit(2)) .split(columns); @@ -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(); @@ -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); + } } diff --git a/vividus-plugin-rest-api/src/test/java/org/vividus/http/transformer/JsonRestApiTableTransformerTests.java b/vividus-plugin-rest-api/src/test/java/org/vividus/http/transformer/JsonRestApiTableTransformerTests.java index e3e5f3092d..a30d830f91 100644 --- a/vividus-plugin-rest-api/src/test/java/org/vividus/http/transformer/JsonRestApiTableTransformerTests.java +++ b/vividus-plugin-rest-api/src/test/java/org/vividus/http/transformer/JsonRestApiTableTransformerTests.java @@ -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. @@ -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; @@ -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; @@ -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; @@ -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 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 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) @@ -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 @@ -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()); @@ -141,4 +171,11 @@ private TableProperties createProperties(String propertiesAsString) { return new TableProperties(propertiesAsString, keywords, parameterConverters); } + + private static List 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)); + } }