[GitHub] jmeter pull request #344: Migrate JSON Path Assertion into JMeter core
Github user asfgit closed the pull request at: https://github.com/apache/jmeter/pull/344 ---
[GitHub] jmeter pull request #344: Migrate JSON Path Assertion into JMeter core
Github user artem-fedorov commented on a diff in the pull request: https://github.com/apache/jmeter/pull/344#discussion_r154098390 --- Diff: src/components/org/apache/jmeter/extractor/json/jsonpath/JSONPathAssertion.java --- @@ -0,0 +1,218 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.apache.jmeter.extractor.json.jsonpath; + +import com.jayway.jsonpath.JsonPath; +import net.minidev.json.JSONArray; +import net.minidev.json.JSONObject; +import org.apache.jmeter.assertions.Assertion; +import org.apache.jmeter.assertions.AssertionResult; +import org.apache.jmeter.samplers.SampleResult; +import org.apache.jmeter.testelement.AbstractTestElement; +import org.apache.jmeter.util.JMeterUtils; +import org.apache.oro.text.regex.Pattern; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.Serializable; +import java.text.DecimalFormat; +import java.util.Map; + +/** + * This is main class for JSONPath Assertion which verifies assertion on + * previous sample result using JSON path expression + */ +public class JSONPathAssertion extends AbstractTestElement implements Serializable, Assertion { +private static final Logger log = LoggerFactory.getLogger(JSONPostProcessor.class); +private static final long serialVersionUID = 1L; +public static final String JSONPATH = "JSON_PATH"; +public static final String EXPECTEDVALUE = "EXPECTED_VALUE"; +public static final String JSONVALIDATION = "JSONVALIDATION"; +public static final String EXPECT_NULL = "EXPECT_NULL"; +public static final String INVERT = "INVERT"; +public static final String ISREGEX = "ISREGEX"; + +public static final DecimalFormat decimalFormatter = new DecimalFormat("#.#"); + +static { +decimalFormatter.setMaximumFractionDigits(340); // java.text.DecimalFormat.DOUBLE_FRACTION_DIGITS == 340 +decimalFormatter.setMinimumFractionDigits(1); +} + +public String getJsonPath() { +return getPropertyAsString(JSONPATH); +} + +public void setJsonPath(String jsonPath) { +setProperty(JSONPATH, jsonPath); +} + +public String getExpectedValue() { +return getPropertyAsString(EXPECTEDVALUE); +} + +public void setExpectedValue(String expectedValue) { +setProperty(EXPECTEDVALUE, expectedValue); +} + +public void setJsonValidationBool(boolean jsonValidation) { +setProperty(JSONVALIDATION, jsonValidation); +} + +public void setExpectNull(boolean val) { +setProperty(EXPECT_NULL, val); +} + +public boolean isExpectNull() { +return getPropertyAsBoolean(EXPECT_NULL); +} + +public boolean isJsonValidationBool() { +return getPropertyAsBoolean(JSONVALIDATION); +} + +public void setInvert(boolean invert) { +setProperty(INVERT, invert); +} + +public boolean isInvert() { +return getPropertyAsBoolean(INVERT); +} + +public void setIsRegex(boolean flag) { +setProperty(ISREGEX, flag); +} + +public boolean isUseRegex() { +return getPropertyAsBoolean(ISREGEX, true); +} + +private void doAssert(String jsonString) { +Object value = JsonPath.read(jsonString, getJsonPath()); + +if (isJsonValidationBool()) { +if (value instanceof JSONArray) { +if (arrayMatched((JSONArray) value)) { +return; +} +} else { +if (isExpectNull() && value == null) { +return; +} else if (isEquals(value)) { +return; +} +} + +if (isExpectNull()) { +throw new
[GitHub] jmeter pull request #344: Migrate JSON Path Assertion into JMeter core
Github user ham1 commented on a diff in the pull request: https://github.com/apache/jmeter/pull/344#discussion_r153870336 --- Diff: src/components/org/apache/jmeter/extractor/json/jsonpath/JSONPathAssertion.java --- @@ -0,0 +1,218 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.apache.jmeter.extractor.json.jsonpath; + +import com.jayway.jsonpath.JsonPath; +import net.minidev.json.JSONArray; +import net.minidev.json.JSONObject; +import org.apache.jmeter.assertions.Assertion; +import org.apache.jmeter.assertions.AssertionResult; +import org.apache.jmeter.samplers.SampleResult; +import org.apache.jmeter.testelement.AbstractTestElement; +import org.apache.jmeter.util.JMeterUtils; +import org.apache.oro.text.regex.Pattern; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.Serializable; +import java.text.DecimalFormat; +import java.util.Map; + +/** + * This is main class for JSONPath Assertion which verifies assertion on + * previous sample result using JSON path expression + */ +public class JSONPathAssertion extends AbstractTestElement implements Serializable, Assertion { +private static final Logger log = LoggerFactory.getLogger(JSONPostProcessor.class); +private static final long serialVersionUID = 1L; +public static final String JSONPATH = "JSON_PATH"; +public static final String EXPECTEDVALUE = "EXPECTED_VALUE"; +public static final String JSONVALIDATION = "JSONVALIDATION"; +public static final String EXPECT_NULL = "EXPECT_NULL"; +public static final String INVERT = "INVERT"; +public static final String ISREGEX = "ISREGEX"; + +public static final DecimalFormat decimalFormatter = new DecimalFormat("#.#"); + +static { +decimalFormatter.setMaximumFractionDigits(340); // java.text.DecimalFormat.DOUBLE_FRACTION_DIGITS == 340 +decimalFormatter.setMinimumFractionDigits(1); +} + +public String getJsonPath() { +return getPropertyAsString(JSONPATH); +} + +public void setJsonPath(String jsonPath) { +setProperty(JSONPATH, jsonPath); +} + +public String getExpectedValue() { +return getPropertyAsString(EXPECTEDVALUE); +} + +public void setExpectedValue(String expectedValue) { +setProperty(EXPECTEDVALUE, expectedValue); +} + +public void setJsonValidationBool(boolean jsonValidation) { +setProperty(JSONVALIDATION, jsonValidation); +} + +public void setExpectNull(boolean val) { +setProperty(EXPECT_NULL, val); +} + +public boolean isExpectNull() { +return getPropertyAsBoolean(EXPECT_NULL); +} + +public boolean isJsonValidationBool() { +return getPropertyAsBoolean(JSONVALIDATION); +} + +public void setInvert(boolean invert) { +setProperty(INVERT, invert); +} + +public boolean isInvert() { +return getPropertyAsBoolean(INVERT); +} + +public void setIsRegex(boolean flag) { +setProperty(ISREGEX, flag); +} + +public boolean isUseRegex() { +return getPropertyAsBoolean(ISREGEX, true); +} + +private void doAssert(String jsonString) { +Object value = JsonPath.read(jsonString, getJsonPath()); + +if (isJsonValidationBool()) { +if (value instanceof JSONArray) { +if (arrayMatched((JSONArray) value)) { +return; +} +} else { +if (isExpectNull() && value == null) { +return; +} else if (isEquals(value)) { +return; +} +} + +if (isExpectNull()) { +throw new
[GitHub] jmeter pull request #344: Migrate JSON Path Assertion into JMeter core
Github user ham1 commented on a diff in the pull request: https://github.com/apache/jmeter/pull/344#discussion_r154070701 --- Diff: test/src/org/apache/jmeter/assertions/TestJSONPathAssertion.java --- @@ -0,0 +1,383 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.apache.jmeter.assertions; + +import org.apache.jmeter.samplers.SampleResult; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class TestJSONPathAssertion { + +@Test +public void testGetJsonPath() { +JSONPathAssertion instance = new JSONPathAssertion(); +String expResult = ""; +String result = instance.getJsonPath(); +assertEquals(expResult, result); +} + +@Test +public void testSetJsonPath() { +String jsonPath = ""; +JSONPathAssertion instance = new JSONPathAssertion(); +instance.setJsonPath(jsonPath); +} + +@Test +public void testGetExpectedValue() { +JSONPathAssertion instance = new JSONPathAssertion(); +String expResult = ""; +String result = instance.getExpectedValue(); +assertEquals(expResult, result); +} + +@Test +public void testSetExpectedValue() { +String expectedValue = ""; +JSONPathAssertion instance = new JSONPathAssertion(); +instance.setExpectedValue(expectedValue); +} + +@Test +public void testSetJsonValidationBool() { +JSONPathAssertion instance = new JSONPathAssertion(); +instance.setJsonValidationBool(false); +} + +@Test +public void testIsJsonValidationBool() { +JSONPathAssertion instance = new JSONPathAssertion(); +boolean result = instance.isJsonValidationBool(); +assertEquals(false, result); +} + +@Test +public void testGetResult_positive() { --- End diff -- These tests would work really well in Spock e.g.: ```groovy def samplerResult = new SampleResult() def sut = new JSONPathAssertion() @Unroll def "get #jsonPath expect #expectedValue to fail=#failure"() { given: samplerResult.setResponseData("{\"myval\": 123}".getBytes()) sut.setJsonPath("$.myval") sut.setJsonValidationBool(true) sut.setExpectedValue("123") when: def result = sut.getResult(samplerResult) then: result.getName() == new AssertionResult("").getName() result.isFailure() == failure where: jsonPath || expectedValue | failure "$.myval" || "123" | false "$.myval" || "(123|456)" | false "$.myval" || "1234"| true } ``` ---
[GitHub] jmeter pull request #344: Migrate JSON Path Assertion into JMeter core
GitHub user artem-fedorov opened a pull request: https://github.com/apache/jmeter/pull/344 Migrate JSON Path Assertion into JMeter core You can merge this pull request into a Git repository by running: $ git pull https://github.com/artem-fedorov/jmeter donate-json-assertion Alternatively you can review and apply these changes as the patch at: https://github.com/apache/jmeter/pull/344.patch To close this pull request, make a commit to your master/trunk branch with (at least) the following in the commit message: This closes #344 commit bb4a2f46b64073726d3224fc8d6f791604f8109f Author: Artem FedorovDate: 2017-11-29T14:03:35Z add JSONPAthAssertion class commit e058138654fc9832aa899ca9033dc998a7264d5e Author: Artem Fedorov Date: 2017-11-29T14:03:50Z add JSONPathAssertionGui class commit 28903c3ddd6aaa515e283c109fb12b07ce0aa7e9 Author: Artem Fedorov Date: 2017-11-29T14:04:00Z add tests classes commit 0e327c996270e9ac0318326523b45a015916381a Author: Artem Fedorov Date: 2017-11-29T14:04:14Z add message properties commit 1a0588c767d5aaa9a6a9c6fc1b243249e6800d4a Author: Artem Fedorov Date: 2017-11-29T14:04:31Z add saveService properties commit 5d4582090984d20a0e40f6bea611814bb26bf500 Author: Artem Fedorov Date: 2017-11-29T14:05:12Z add html docs commit 9c3a6283db1773bf7694954676ac8d44e6de2cc4 Author: Artem Fedorov Date: 2017-11-29T14:05:28Z add xdocs commit cb6411c9fb39a211521df02066be1170b730a92a Author: Artem Fedorov Date: 2017-11-29T14:17:45Z add changelog ---