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-json] Add patch json file step #1499

Merged
merged 1 commit into from
Mar 23, 2021
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
33 changes: 33 additions & 0 deletions docs/modules/plugins/pages/plugin-json.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
= JSON Plugin

The plugin provides different actions to interact with JSON.

== Installation

.build.gradle
[source,gradle,subs="attributes+"]
----
implementation(group: 'org.vividus', name: 'vividus-plugin-json', version: '{current-version}')
----

== Steps

=== Patch JSON

[source,gherkin]
----
When I patch JSON `$sourceJson` using `$jsonPatch` and save result to $scopes variable `$variableName`
----

* `$sourceJson` - JSON data to be patched.
* `$jsonPatch` - JSON data with required patch actions according to https://tools.ietf.org/html/rfc6902#page-6[RFC-6902]
* `$scopes` - xref:parameters:variable-scope.adoc[The comma-separated set of the variables scopes]
* `$variableName` - The variable name

=== Examples

.Patch JSON data
[source,gherkin]
----
When I patch JSON `{"a":"c"}` using `[{ "op": "replace", "path": "/a", "value": "c" }]` and save result to SCENARIO variable `patchedJSON`
----
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ include 'vividus-plugin-db'
include 'vividus-plugin-excel'
include 'vividus-plugin-electron'
include 'vividus-plugin-html'
include 'vividus-plugin-json'
EDbarvinsky marked this conversation as resolved.
Show resolved Hide resolved
include 'vividus-plugin-lambdatest'
include 'vividus-plugin-email'
include 'vividus-plugin-kafka'
Expand Down
1 change: 1 addition & 0 deletions vividus-docker-bundler/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies {
implementation project(':vividus-plugin-email')
implementation project(':vividus-plugin-excel')
implementation project(':vividus-plugin-html')
implementation project(':vividus-plugin-json')
implementation project(':vividus-plugin-kafka')
implementation project(':vividus-plugin-lambdatest')
implementation project(':vividus-plugin-mobile-app')
Expand Down
13 changes: 13 additions & 0 deletions vividus-plugin-json/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
project.description = 'Vividus plugin for JSON testing'

dependencies {
implementation project(':vividus-bdd-engine')
implementation (group: 'com.flipkart.zjsonpatch', name: 'zjsonpatch', version: '0.4.11')

testImplementation platform(group: 'org.junit', name: 'junit-bom', version: versions.junit)
testImplementation(group: 'org.junit.jupiter', name: 'junit-jupiter')
testImplementation(group: 'org.mockito', name: 'mockito-junit-jupiter', version: versions.mockito)
}
EDbarvinsky marked this conversation as resolved.
Show resolved Hide resolved

tasks.artifactoryPublish.enabled = false
tasks.publish.enabled = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2019-2021 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.vividus.bdd.steps.json;

import java.util.Set;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.flipkart.zjsonpatch.JsonPatch;

import org.jbehave.core.annotations.When;
import org.vividus.bdd.context.IBddVariableContext;
import org.vividus.bdd.variable.VariableScope;

public class JsonPatchSteps
{
private final IBddVariableContext bddVariableContext;
private final JsonMapper jsonMapper = new JsonMapper();

public JsonPatchSteps(IBddVariableContext bddVariableContext)
{
this.bddVariableContext = bddVariableContext;
}

/**
* Performs patching of a json and save it to the variable
* @param sourceJson json data to be patched
* @param jsonPatch json data with patch actions
* @param scopes The set of variable scopes (comma separated list of scopes e.g.: SCENARIO, STORY, NEXT_BATCHES)
* @param variableName Name of variable
*/
@When("I patch JSON `$sourceJson` using `$jsonPatch` and save result to $scopes variable `$variableName`")
EDbarvinsky marked this conversation as resolved.
Show resolved Hide resolved
public void patchJsonFile(String sourceJson, String jsonPatch, Set<VariableScope> scopes,
String variableName) throws JsonProcessingException
{
JsonNode jsonSource = jsonMapper.readTree(sourceJson);
JsonNode patchJson = jsonMapper.readTree(jsonPatch);
JsonNode patchedJson = JsonPatch.apply(patchJson, jsonSource);
bddVariableContext.putVariable(scopes, variableName, patchedJson.toString());
}
}
15 changes: 15 additions & 0 deletions vividus-plugin-json/src/main/resources/spring.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd"
default-lazy-init="true">

<bean id="jsonPatchSteps" class="org.vividus.bdd.steps.json.JsonPatchSteps" />

<util:list id="stepBeanNames-Json">
<idref bean="jsonPatchSteps" />
</util:list>
</beans>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2019-2021 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.vividus.bdd.steps.json;

import static org.mockito.Mockito.verify;

import java.util.Set;

import com.fasterxml.jackson.core.JsonProcessingException;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.vividus.bdd.context.IBddVariableContext;
import org.vividus.bdd.variable.VariableScope;

@ExtendWith(MockitoExtension.class)
class JsonPatchStepsTests
{
private static final String VARIABLE_NAME = "json";
private static final String SOURCE_JSON = "{\"a\":\"b\"}";
private static final String PATCH_JSON = "[{ \"op\": \"replace\", \"path\": \"/a\", \"value\": \"c\" }]";
private static final String EXPECTED_RESULT = "{\"a\":\"c\"}";

@Mock
private IBddVariableContext bddVariableContext;

@Test
void patchJsonFile() throws JsonProcessingException
{
Set<VariableScope> variableScope = Set.of(VariableScope.SCENARIO);
JsonPatchSteps jsonPatchSteps = new JsonPatchSteps(bddVariableContext);
jsonPatchSteps.patchJsonFile(SOURCE_JSON, PATCH_JSON, variableScope, VARIABLE_NAME);
verify(bddVariableContext).putVariable(variableScope, VARIABLE_NAME, EXPECTED_RESULT);
}
}
1 change: 1 addition & 0 deletions vividus-tests/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dependencies {
implementation project(':vividus-plugin-datetime')
implementation project(':vividus-plugin-kafka')
implementation project(':vividus-plugin-html')
implementation project(':vividus-plugin-json')
implementation project(':vividus-plugin-lambdatest')
implementation project(':vividus-plugin-rest-api')
implementation project(':vividus-plugin-saucelabs')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Description: Integration tests for JsonPatchSteps class.

Meta:
@epic vividus-plugin-json

Scenario: Step verification 'When I patch JSON `$sourceJson` using `$jsonPatch` and save result to $scopes variable `$variableName`'
When I patch JSON `{"a":"b"}` using `[{ "op": "replace", "path": "/a", "value": "c" }]` and save result to SCENARIO variable `patchedJson`
Then `{"a":"c"}` is equal to `${patchedJson}`