0

In my script I am trying to make a get call and asserting on the response. This is working fine with valid response but for invalid response I am trying to fetch the UUID of the request URL but not working.

Request URL:

http://localhost:8080/log/log-events/6cef99be-591e-42e4-b07c-34b45f9540df

Response incase of failure:

{"trace-id":"8000043e-0001-eb00-b63f-84710c7967bb","invalid-param":[],"type":"https://tools.ietf.org/html/rfc7231#section-6.5.4","title":"The specified resource was not found.","status":404,"detail":"Resource not found","instance":"/log-events/6cef99be-591e-42e4-b07c-34b45f9540df"}

Assertion Script:

import groovy.json.JsonSlurper;
import java.util.regex.Pattern;

def failureMessage = "";
def jsonResponse = null;

JsonSlurper JSON = new JsonSlurper();

try {
  jsonResponse = JSON.parseText(prev.getResponseDataAsString());
  customerID = jsonResponse.customerId
} catch (Exception e) {
  def requestData = sampler.getUrl().toString();
  def pattern = Pattern.compile('^http?://localhost:8080/log/log-events/([^/]+)/.*$')
  def (_, refDATA) = (requestData =~ pattern)[0]
  failureMessage += refDATA
}
if (customerID.contains("Test") ||customerID.contains('KENDLE') || customerID.contains('CLINIRX') || customerID.contains('GSK')) {
  AssertionResult.setFailure(false);
} else {
        AssertionResult.setFailure(true);
        AssertionResult.setFailureMessage(failureMessage);

}

Error message details:

2021-08-13 10:40:21,803 ERROR o.a.j.a.JSR223Assertion: Problem in JSR223 script: ClientID_Validator
javax.script.ScriptException: java.lang.NullPointerException: Cannot invoke method contains() on null object
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:320) ~[groovy-jsr223-3.0.3.jar:3.0.3]
    at org.codehaus.groovy.jsr223.GroovyCompiledScript.eval(GroovyCompiledScript.java:71) ~[groovy-jsr223-3.0.3.jar:3.0.3]
    at javax.script.CompiledScript.eval(Unknown Source) ~[?:1.8.0_251]
    at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:222) ~[ApacheJMeter_core.jar:5.3]
    at org.apache.jmeter.assertions.JSR223Assertion.getResult(JSR223Assertion.java:50) [ApacheJMeter_components.jar:5.3]
    at org.apache.jmeter.threads.JMeterThread.processAssertion(JMeterThread.java:916) [ApacheJMeter_core.jar:5.3]
    at org.apache.jmeter.threads.JMeterThread.checkAssertions(JMeterThread.java:885) [ApacheJMeter_core.jar:5.3]
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:573) [ApacheJMeter_core.jar:5.3]
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:489) [ApacheJMeter_core.jar:5.3]
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:256) [ApacheJMeter_core.jar:5.3]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_251]

Just wanted to print the UUID:6cef99be-591e-42e4-b07c-34b45f9540df incase of failure

What I missed here?

2 Answers2

0

I fail to see any customerId object in your JSON response hence your line customerID = jsonResponse.customerId returns null and when later on you're trying to call customerID.contains() function it fails because customerID is null.

Without seeing successful response and not knowing what you're trying to achieve in case of success it's hard to advice anything meaningful, as a workaround you can add an extra check for the customerID like:

import groovy.json.JsonSlurper

import java.util.regex.Pattern

def failureMessage = "";
def jsonResponse = null;

JsonSlurper JSON = new JsonSlurper();

try {
    jsonResponse = JSON.parseText(prev.getResponseDataAsString());
    customerID = jsonResponse.customerId
} catch (Exception e) {
    def requestData = sampler.getUrl().toString();
    def pattern = Pattern.compile('^http?://localhost:8080/log/log-events/([^/]+)/.*$')
    def (_, refDATA) = (requestData =~ pattern)[0]
    failureMessage += refDATA
}
if (customerID != null) {
    if (customerID.contains("Test") || customerID.contains('KENDLE') || customerID.contains('CLINIRX') || customerID.contains('GSK')) {
        AssertionResult.setFailure(false);
    } else {
        AssertionResult.setFailure(true);
        AssertionResult.setFailureMessage(failureMessage);

    }
}

More information on using JSR223 Assertions: Scripting JMeter Assertions in Groovy - A Tutorial

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
0

You can split the URL and get the last item in the list. Here is a sample code to extract the UUID from the URL

def requestData ="http://localhost:8080/log/log-events/6cef99be-591e-42e4-b07c-34b45f9540df"

def uuid = requestData.split("/").last()

println "uuid = $uuid"

Janesh Kodikara
  • 1,670
  • 1
  • 12
  • 23