Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.intuit.tank.http.json;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import tools.jackson.databind.json.JsonMapper;

/**
* Global shared, threads-safe JsonMapper instance.
*/
public class GenericJsonHandler {
private static final Logger logger = LogManager.getLogger(GenericJsonHandler.class);
private static final JsonMapper jsonMapper = JsonMapper.builder().build();

/**
* Converts a JSON string to a Java object of a specified class.
* @param json
* @param clazz
* @return
*/
protected static <T> T fromJson(String json, Class<T> clazz) {
try {
return jsonMapper.readValue(json, clazz);
} catch (Exception ex) {
logger.warn("Unable to parse the response string as a JSON object: {}", json, ex);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
* #L%
*/

import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
Expand All @@ -46,13 +45,13 @@ public JsonResponse(String resp) {

@Override
public void setResponseBody(String body) {
this.response = this.cleanString(body);
this.response = body.strip();
}

@Override
public void setResponseBody(byte[] byteArray) {
this.responseByteArray = byteArray;
this.response = this.cleanString(new String(byteArray));
this.response = new String(byteArray).strip();
}

@Override
Expand All @@ -79,24 +78,10 @@ public String getValue(String key) {
}
}

private String cleanString(String input) {
try {
return StringUtils.remove(input.trim(),"(\r\n)+");
} catch (Exception ex) {
return input;
}
}

private void initialize() {
try {
if (!StringUtils.isEmpty(this.response)) {
this.jsonMap = new ObjectMapper().readValue(this.response, HashMap.class);
} else {
this.jsonMap = new HashMap();
}
} catch (IOException ex) {
logger.warn("Unable to parse the response string as a JSON object: " + this.response, ex);
}
this.jsonMap = (StringUtils.isNotEmpty(this.response)) ?
GenericJsonHandler.fromJson(this.response, HashMap.class) :
Collections.emptyMap();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;

import org.apache.commons.lang3.tuple.ImmutablePair;
Expand Down Expand Up @@ -93,7 +92,7 @@ public void testGetValue_1()
public void testJsonResponseBody() throws Exception{

JsonResponse fixture = new JsonResponse();
fixture.setResponseBody(readFile("src/test/resources/json-response.json"));
fixture.setResponseBody(Files.readString(Paths.get("src/test/resources/json-response.json")));

Stream<Pair<String, String>> keys = Stream.of(
new ImmutablePair<>("/data/data/returns/IRS1040/Return/ReturnData/PPPerson/SpouseFilerInfoPP/FieldAttributes/UUID", "de7f702f-e40b-4f16-8a7d-e4263f11421d"),
Expand All @@ -114,18 +113,4 @@ public void testJsonResponseBody() throws Exception{

keys.forEach(pair -> assertEquals(pair.getValue(), fixture.getValue(pair.getKey())));
}

private String readFile( String file ) throws IOException {

try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line = null;
StringBuilder stringBuilder = new StringBuilder();
String ls = System.getProperty("line.separator");
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append(ls);
}
return stringBuilder.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.util.zip.ZipException;
import java.util.zip.ZipInputStream;

import com.fasterxml.jackson.core.JsonProcessingException;
import tools.jackson.core.JacksonException;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -111,7 +111,7 @@ public void startTest(final StandaloneAgentRequest request) {
currentAvailability.setAvailabilityStatus(AgentAvailabilityStatus.AVAILABLE);
try {
sendAvailability();
} catch (JsonProcessingException ex) {
} catch (JacksonException ex) {
throw new RuntimeException(ex);
}
}
Expand Down Expand Up @@ -152,7 +152,7 @@ private void startPinger() {
t.start();
}

private void sendAvailability() throws JsonProcessingException {
private void sendAvailability() throws JacksonException {
// create new availability as a copy of the original
AgentAvailability availability = new AgentAvailability(currentAvailability.getInstanceId(),
currentAvailability.getInstanceUrl(), currentAvailability.getCapacity(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
import java.net.http.HttpResponse;
import java.util.Date;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import tools.jackson.core.JacksonException;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.ObjectWriter;
import org.apache.http.HttpHeaders;
import org.apache.http.entity.ContentType;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -169,7 +169,7 @@ public synchronized static void setJobStatus(JobStatus jobStatus) {
}
}

protected static void setInstanceStatus(String instanceId, CloudVmStatus VmStatus) throws URISyntaxException, JsonProcessingException {
protected static void setInstanceStatus(String instanceId, CloudVmStatus VmStatus) throws URISyntaxException, JacksonException {
String json = objectWriter.writeValueAsString(VmStatus);
String token = APITestHarness.getInstance().getTankConfig().getAgentConfig().getAgentToken();
HttpRequest request = HttpRequest.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.concurrent.Semaphore;
import java.util.zip.GZIPInputStream;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.intuit.tank.http.TankHttpClient;
import com.intuit.tank.vm.api.enumerated.*;
import org.apache.commons.io.FileUtils;
Expand Down Expand Up @@ -66,6 +65,7 @@
import com.intuit.tank.vm.common.TankConstants;
import com.intuit.tank.vm.settings.TankConfig;
import software.amazon.awssdk.regions.internal.util.EC2MetadataUtils;
import tools.jackson.databind.json.JsonMapper;

public class APITestHarness {
private static final Logger LOG = LogManager.getLogger(APITestHarness.class);
Expand Down Expand Up @@ -299,8 +299,8 @@ private void startHttp(String baseUrl, String token) {
LOG.info(LogUtil.getLogMessage("Sending AgentData to controller: " + data.toString()));
while (count < FIBONACCI.length) {
try {
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writerFor(AgentData.class)
JsonMapper jsonMapper = new JsonMapper();
String json = jsonMapper.writerFor(AgentData.class)
.withDefaultPrettyPrinter().writeValueAsString(data);
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(baseUrl + "/v2/agent/ready"))
Expand All @@ -310,10 +310,10 @@ private void startHttp(String baseUrl, String token) {
.POST(BodyPublishers.ofString(json))
.build();
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
startData = objectMapper.readerFor(AgentTestStartData.class).readValue(response.body());
startData = jsonMapper.readerFor(AgentTestStartData.class).readValue(response.body());
break;
} catch (Exception e) {
LOG.error("Error sending ready: " + e, e);
LOG.error("Error sending ready: {}", e, e);
try {
Thread.sleep(FIBONACCI[count++] * 1000);
} catch ( InterruptedException ignored) {}
Expand Down Expand Up @@ -345,7 +345,7 @@ private void startHttp(String baseUrl, String token) {
thread.setDaemon(false);
thread.start();
} catch (Exception e) {
LOG.error("Error communicating with controller: " + e, e);
LOG.error("Error communicating with controller: {}", e, e);
System.exit(0);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.apache.commons.jexl3.JexlBuilder;
import org.apache.commons.jexl3.JexlContext;
Expand Down Expand Up @@ -60,16 +62,19 @@ public class Variables {

private static final Pattern p = Pattern.compile(TankConstants.EXPRESSION_REGEX);

// Cache for compiled JEXL expressions to avoid repeated parsing
private static final Map<String, JexlExpression> expressionCache = new ConcurrentHashMap<>();

private JexlContext context;

/**
* Get the single instance of the variables class
*
*
* @return The variable class instance
*
*
* static public Variables getInstance(){ if (Variables.instance == null) Variables.instance = new
* Variables(); return Variables.instance; }
*
*
* /** Constructor
*/
public Variables() {
Expand Down Expand Up @@ -98,33 +103,42 @@ public JexlContext getContext() {
}

public String evaluate(String s) {
if (StringUtils.isNotEmpty(s)) {
if (s.contains("#")) { // Performance Shortcut
Matcher m = p.matcher(s);
while (m.find()) { // find next match, very costly
String match = m.group();
String group = m.group(1);
JexlExpression expression = jexl.createExpression(group);
String result = (String) expression.evaluate(context);
if (result == null && (group.contains("getCSVData") || group.contains("getFile"))) {
APITestHarness.getInstance().addKill();
LOG.error(LogUtil.getLogMessage("CSV file (" + group + ") has no more data.",
LogEventType.Validation, LoggingProfile.USER_VARIABLE));
throw new KillScriptException("CSV file (" + group + ") has no more data.");
}
s = StringUtils.replace(s, match, result != null ? result : "");
}
}
}
return s;
if (StringUtils.isEmpty(s) || s.indexOf('#') == -1) {
return s; // Fast path: no expressions to evaluate
}

Matcher m = p.matcher(s);
if (!m.find()) {
return s; // No matches found
}

// Use StringBuilder with appendReplacement for efficient string building
StringBuilder result = new StringBuilder(s.length() + 64);
do {
String group = m.group(1);

// Get or create cached expression
JexlExpression expression = expressionCache.computeIfAbsent(group, jexl::createExpression);

String evalResult = (String) expression.evaluate(context);
if (evalResult == null && (group.contains("getCSVData") || group.contains("getFile"))) {
APITestHarness.getInstance().addKill();
LOG.error(LogUtil.getLogMessage("CSV file (" + group + ") has no more data.",
LogEventType.Validation, LoggingProfile.USER_VARIABLE));
throw new KillScriptException("CSV file (" + group + ") has no more data.");
}

// Use appendReplacement to build result efficiently
m.appendReplacement(result, Matcher.quoteReplacement(evalResult != null ? evalResult : ""));
} while (m.find());

m.appendTail(result);
return result.toString();
}

public Map<String, String> getVariableValues() {
Map<String, String> ret = new HashMap<String, String>();
for (Entry<String, VariableValue> entry : variables.entrySet()) {
ret.put(entry.getKey(), entry.getValue().getValue());
}
return ret;
return variables.entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, e -> e.getValue().getValue()));
}

/**
Expand Down Expand Up @@ -165,17 +179,13 @@ public void addVariable(String key, String value) {
* true to allow override of variable
*/
public void addVariable(String key, String value, boolean allowOverride) {
String result = null;
if (value.length() < 1) {
result = this.processString(key, value);
} else if (ValidationUtil.isFunction(value)) {
result = this.processFunction(key, value);
} else {
result = this.processString(key, value);
}
VariableValue variableValue = this.variables.get(key);
String result = ValidationUtil.isFunction(value)
? processFunction(key, value)
: processString(key, value);

VariableValue variableValue = variables.get(key);
if (variableValue == null || variableValue.allowOverride) {
this.variables.put(key, new VariableValue(result, allowOverride));
variables.put(key, new VariableValue(result, allowOverride));
context.set(key, result);
}
}
Expand Down Expand Up @@ -207,7 +217,7 @@ public Double getDoubleValue(String key) {
return Double.valueOf(variable);
}
} catch (NumberFormatException e) {
LOG.error(variable + " is not a Double.");
LOG.error("{} is not a Double.", variable);
}
return null;
}
Expand All @@ -225,7 +235,7 @@ public Integer getIntegerValue(String key) {
return Integer.valueOf(variable);
}
} catch (NumberFormatException e) {
LOG.error(variable + " is not a Double.");
LOG.error("{} is not a Integer.", variable);
}
return null;
}
Expand Down Expand Up @@ -256,7 +266,6 @@ private String processFunction(String key, String value) {
* The string value
*/
private String processString(String key, String value) {

value = evaluate(value);
logVariable(key, value);
return value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.intuit.tank.harness;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.ObjectWriter;
import com.intuit.tank.reporting.api.ResultsReporter;
import com.intuit.tank.reporting.api.TPSInfoContainer;
import com.intuit.tank.vm.api.enumerated.VMImageType;
Expand Down
2 changes: 1 addition & 1 deletion agent/http_client_3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<groupId>tools.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
Expand Down
Loading