Skip to content
Permalink
Browse files
Fail JSONPath Assertion on indefinite paths and no assertion value
The docs for JSONPath Assertion state, that it will fail the assertion,
when no element is found with the given JSON path. This was currently
not followed, when an indefinite path was used. In such a case, the
JSONPath library would return an empty list, which the assertion logic
would log as OK.

With this change we let the assertion fail, when
 * an indefinite path was given
 * and an empty list is extracted
 * and no assertion value is given

Bugzilla Id: 65794
  • Loading branch information
FSchumacher committed Jan 19, 2022
1 parent 6a5cac0 commit 278255de85829c850d36ad1655d1541e66d5368f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
@@ -30,12 +30,12 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jayway.jsonpath.JsonPath;

import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import net.minidev.json.JSONValue;

import com.jayway.jsonpath.JsonPath;

/**
* This is main class for JSONPath Assertion which verifies assertion on
* previous sample result using JSON path expression
@@ -112,6 +112,12 @@ private void doAssert(String jsonString) {
Object value = JsonPath.read(jsonString, getJsonPath());

if (!isJsonValidationBool()) {
if (value instanceof JSONArray) {
JSONArray arrayValue = (JSONArray) value;
if (arrayValue.isEmpty() && !JsonPath.isPathDefinite(getJsonPath())) {
throw new IllegalStateException("JSONPath is indefinite and the extracted Value is an empty Array. Please use an assertion value, to be sure to get a correct result. " + getExpectedValue());
}
}
return;
}

@@ -22,6 +22,7 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.nio.charset.Charset;
import java.util.Locale;

import org.apache.jmeter.samplers.SampleResult;
@@ -270,6 +271,9 @@ void testGetResult_list_negative() {

@Test
void testGetResult_list_empty_novalidate() {
// With bug 65794 the outcome of this test has changed
// we now consider an indefinite path with no assertion value
// an error and set the AssertionResult to failure
SampleResult samplerResult = new SampleResult();
samplerResult.setResponseData("{\"myval\": []}".getBytes());

@@ -279,7 +283,7 @@ void testGetResult_list_empty_novalidate() {
AssertionResult expResult = new AssertionResult("");
AssertionResult result = instance.getResult(samplerResult);
assertEquals(expResult.getName(), result.getName());
assertFalse(result.isFailure());
assertTrue(result.isFailure());
}

@Test
@@ -237,6 +237,7 @@ however, the profile can't be updated while the test is running.
<li><bug>65681</bug>Use default values for null values when extracting with JSONPostProcessor</li>
<li>Allow setters in ConstantThroughputTimer to updating the values during the run time</li>
<li><bug>65782</bug>Use correct message format for MessageFormat in HTMLAssertion</li>
<li><bug>65794</bug>JSON Assertion always successful with indefinite paths</li>
</ul>

<h3>Functions</h3>
@@ -253,7 +254,6 @@ however, the profile can't be updated while the test is running.

<h3>Documentation</h3>
<ul>
<li><bug>65794</bug>JSON Assertion always successful with indefinite paths</li>
</ul>

<h3>General</h3>
@@ -5037,7 +5037,9 @@ please ensure that you select "<code>Store the message using MIME (raw)</code>"
Note that if the path will return array object, it will be iterated and if expected value is found, the assertion will succeed. To validate empty array use <code>[]</code> string. Also, if patch will return dictionary object, it will be converted to string before comparison.
<note>When using <a href="https://github.com/json-path/JsonPath#what-is-returned-when">indefinite JSON Paths</a>
you must assert the value due to the existing JSON library implementation, otherwise the assertion could always
return successful</note>
return successful.<br>
Since JMeter version 5.5 the assertion will fail, if an indefinite path is given, an empty list is extracted and
no assertion value is set.</note>
</p>
</description>
<properties>

0 comments on commit 278255d

Please sign in to comment.