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 RuntimeException(String.format("Value expected 
to be null, but found '%s'", value));
    +            } else {
    +                String msg;
    +                if (isUseRegex()) {
    +                    msg="Value expected to match regexp '%s', but it did 
not match: '%s'";
    +                } else {
    +                    msg="Value expected to be '%s', but found '%s'";
    +                }
    +                throw new RuntimeException(String.format(msg, 
getExpectedValue(), objectToString(value)));
    +            }
    +        }
    +    }
    +
    +    private boolean arrayMatched(JSONArray value) {
    +        if (value.isEmpty() && getExpectedValue().equals("[]")) {
    --- End diff --
    
    Would this work with both regex on and off? I'm probably wrong but if you 
were looking for `[]` with a regex it would be `\[\]`?


---

Reply via email to