Skip to content

Error in running the zerocode tests in JDK23/21 #704 #713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
10 changes: 5 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Setting up JDK8
uses: actions/setup-java@v3
- name: Setting up JDK11
uses: actions/setup-java@v4
with:
java-version: '8'
distribution: 'adopt'
java-version: '11'
distribution: 'temurin'

- name: Install Docker Compose
run: |
Expand Down
10 changes: 5 additions & 5 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,6 @@
<groupId>com.google.classpath-explorer</groupId>
<artifactId>classpath-explorer</artifactId>
</dependency>
<dependency>
<groupId>org.jukito</groupId>
<artifactId>jukito</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
Expand Down Expand Up @@ -150,6 +145,11 @@
<artifactId>junit</artifactId>
<!--<scope>test</scope>--> <!-- added with compile scope. Do not change this -->
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.jsmart.zerocode.core.di.module;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.util.Optional;

public class OptionalTypeAdapter<T> extends TypeAdapter<Optional<T>> {
private final TypeAdapter<T> delegate;

public OptionalTypeAdapter(TypeAdapter<T> delegate) {
this.delegate = delegate;
}

@Override
public void write(JsonWriter out, Optional<T> value) throws IOException {
if (value == null || !value.isPresent()) {
out.nullValue();
} else {
delegate.write(out, value.get());
}
}

@Override
public Optional<T> read(JsonReader in) throws IOException {
if (in.peek() == com.google.gson.stream.JsonToken.NULL) {
in.nextNull();
return Optional.empty();
} else {
return Optional.ofNullable(delegate.read(in));
}
}

public static <T> TypeAdapter<Optional<T>> factory(TypeAdapter<T> delegate) {
return new OptionalTypeAdapter<>(delegate);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.jsmart.zerocode.core.di.module;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Optional;

public class OptionalTypeAdapterFactory implements TypeAdapterFactory {

@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
if (!Optional.class.isAssignableFrom(typeToken.getRawType())) {
return null;
}

Type type = typeToken.getType();
if (type instanceof ParameterizedType) {
Type elementType = ((ParameterizedType) type).getActualTypeArguments()[0];
TypeAdapter<?> elementAdapter = gson.getAdapter(TypeToken.get(elementType));
return (TypeAdapter<T>) OptionalTypeAdapter.factory(elementAdapter);
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@
import jakarta.inject.Provider;
import org.apache.kafka.common.header.Headers;
import org.apache.kafka.common.header.internals.RecordHeaders;
import org.jsmart.zerocode.core.di.module.OptionalTypeAdapterFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class GsonSerDeProvider implements Provider<Gson> {

@Override
public Gson get() {
return new GsonBuilder()
.registerTypeAdapterFactory(KafkaHeadersAdapter.FACTORY)
.registerTypeAdapterFactory(new OptionalTypeAdapterFactory())
.create();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.JSON_CONTENT;
import static org.jsmart.zerocode.core.utils.FieldTypeConversionUtils.deepTypeCast;
import static org.jsmart.zerocode.core.utils.FieldTypeConversionUtils.fieldTypes;
import static org.jsmart.zerocode.core.utils.HelperJsonUtils.readJsonPath;
import static org.jsmart.zerocode.core.utils.PropertiesProviderUtils.loadAbsoluteProperties;
import static org.jsmart.zerocode.core.utils.SmartUtils.checkDigNeeded;
import static org.jsmart.zerocode.core.utils.SmartUtils.getJsonFilePhToken;
Expand Down Expand Up @@ -138,7 +139,7 @@ public String resolveJsonPaths(String jsonString, String scenarioState) {
* Use escapeJava, do not use escapeJavaScript, as escapeJavaScript also escapes single quotes
* which in turn throws Jackson Exception
*/
String escapedString = escapeJava(JsonPath.read(scenarioState, thisPath));
String escapedString = escapeJava(readJsonPath(scenarioState, thisPath, String.class));
paramMap.put(thisPath, escapedString);

} else if (thisPath.matches(LEAF_VAL_REGEX) || thisPath.endsWith($VALUE)) {
Expand All @@ -154,7 +155,7 @@ public String resolveJsonPaths(String jsonString, String scenarioState) {

} else {

paramMap.put(thisPath, JsonPath.read(scenarioState, thisPath));
paramMap.put(thisPath, readJsonPath(scenarioState, thisPath, String.class));

}
}
Expand Down Expand Up @@ -448,7 +449,7 @@ void resolveLeafOnlyNodeValue(String scenarioState, Map<String, String> paramMap
String actualPath = thisPath.substring(0, thisPath.indexOf($VALUE));
int index = findArrayIndex(thisPath, actualPath);

List<String> leafValuesAsArray = JsonPath.read(scenarioState, actualPath);
List<String> leafValuesAsArray = readJsonPath(scenarioState, actualPath, List.class);
paramMap.put(thisPath, leafValuesAsArray.get(index));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import static org.jsmart.zerocode.core.kafka.KafkaConstants.PROTO;
import static org.jsmart.zerocode.core.kafka.KafkaConstants.RAW;
import static org.jsmart.zerocode.core.kafka.common.KafkaCommonUtils.resolveValuePlaceHolders;
import static org.jsmart.zerocode.core.utils.HelperJsonUtils.readJsonPath;
import static org.jsmart.zerocode.core.utils.SmartUtils.prettyPrintJson;

public class KafkaConsumerHelper {
Expand Down Expand Up @@ -387,7 +388,7 @@ public static String prepareResult(ConsumerLocalConfigs testConfigs,

// Optional filter applied. if not supplied, original result is returned as response
if (testConfigs != null && testConfigs.getFilterByJsonPath() != null) {
String filteredResult = JsonPath.read(result, testConfigs.getFilterByJsonPath()).toString();
String filteredResult = readJsonPath(result, testConfigs.getFilterByJsonPath(), String.class).toString();
List<ConsumerJsonRecord> filteredRecords = objectMapper.readValue(filteredResult, List.class);
result = prettyPrintJson(objectMapper.writeValueAsString(new ConsumerJsonRecords(filteredRecords)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.jsmart.zerocode.core.di.provider.ObjectMapperProvider;
import org.jsmart.zerocode.core.engine.assertion.FieldAssertionMatcher;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.jsmart.zerocode.core.engine.assertion.FieldAssertionMatcher.aMatchingMessage;
import static org.jsmart.zerocode.core.engine.assertion.FieldAssertionMatcher.aNotMatchingMessage;
Expand Down Expand Up @@ -161,4 +160,34 @@ public static Object readJsonPathOrElseNull(String requestJson, String jsonPath)
return null;
}
}

/**
* Reads a value from a JSON string using a JSON Path expression and converts it to the specified type.
* <p>
* The method extracts and converts a JSON value to a target type using JSON Path,
* designed for JDK 9+ to handle strict type inference with {@code Class<T>} for reliable type conversion. *
* </p>
* <p>
* If the JSON Path does not exist, or the value is null, the method logs a warning and returns {@code null}.
* Any other errors during JSON Path evaluation or type conversion are logged as errors, and {@code null} is returned.
* </p>
*/
public static <T> T readJsonPath(final String requestJson, final String jsonPath, final Class<T> clazz) {
try {
// Read the raw value from JSON Path
final Object result = JsonPath.read(requestJson, jsonPath);
if (result == null) {
LOGGER.warn("JSON Path {} returned null.", jsonPath);
return null;
}
// Convert the result to the target class
return mapper.convertValue(result, clazz);
} catch (final PathNotFoundException pEx) {
LOGGER.warn("No {} was present in the request. Returned null.", jsonPath);
return null;
} catch (Exception e) {
LOGGER.error("Error converting JSON Path {} to type {}: {}", jsonPath, clazz.getSimpleName(), e.getMessage());
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
package org.jsmart.zerocode.core.db;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertThrows;
import com.google.inject.Inject;
import com.univocity.parsers.csv.CsvParser;
import org.junit.Test;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.jukito.JukitoRunner;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.google.inject.Inject;
import com.univocity.parsers.csv.CsvParser;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertThrows;

@RunWith(JukitoRunner.class)
public class DbCsvLoaderTest extends DbTestBase{

private DbCsvLoader loader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.jukito.JukitoRunner;


import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(JukitoRunner.class)

public class DbSqlRunnerTest extends DbTestBase {

@Before
Expand Down
26 changes: 15 additions & 11 deletions core/src/test/java/org/jsmart/zerocode/core/db/DbTestBase.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
package org.jsmart.zerocode.core.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.google.inject.AbstractModule;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import org.apache.commons.dbutils.DbUtils;
import org.jsmart.zerocode.core.di.main.ApplicationMainModule;
import org.jukito.TestModule;
import org.jsmart.zerocode.core.guice.ZeroCodeGuiceTestRule;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;

import com.google.inject.Inject;
import com.google.inject.name.Named;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
* Base class for the unit DB test classes: manages connections,
* execution of queries and DBMS specific features
*/
public abstract class DbTestBase {
@Rule
public ZeroCodeGuiceTestRule guiceRule = new ZeroCodeGuiceTestRule(this, ZeroCodeTestModule.class);

// Subclasses must use JukitoRunner
public static class JukitoModule extends TestModule {
public static class ZeroCodeTestModule extends AbstractModule {
@Override
protected void configureTest() {
protected void configure() {
ApplicationMainModule applicationMainModule = new ApplicationMainModule("db_test.properties");
install(applicationMainModule);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package org.jsmart.zerocode.core.db;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertThrows;
import org.junit.Ignore;
import org.junit.Test;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
Expand All @@ -12,11 +11,10 @@
import java.util.Map;
import java.util.TimeZone;

import org.jukito.JukitoRunner;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertThrows;

@RunWith(JukitoRunner.class)
public class DbValueConverterTest extends DbTestBase {

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
package org.jsmart.zerocode.core.di;

import com.google.inject.AbstractModule;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import org.jsmart.zerocode.core.di.main.ApplicationMainModule;
import org.jsmart.zerocode.core.guice.ZeroCodeGuiceTestRule;
import org.jsmart.zerocode.core.utils.SmartUtils;
import org.jukito.JukitoRunner;
import org.jukito.TestModule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
//this config file is to test backword compatibility, remove this test only after release 1.2.9 or later releases
@RunWith(JukitoRunner.class)
public class ApplicationMainModuleBackwordCompatibilityTest {

public static class JukitoModule extends TestModule {
@Rule
public ZeroCodeGuiceTestRule guiceRule = new ZeroCodeGuiceTestRule(this, ZeroCodeTestModule.class);
public static class ZeroCodeTestModule extends AbstractModule {
@Override
protected void configureTest() {
protected void configure() {
ApplicationMainModule applicationMainModule = new ApplicationMainModule("config_hosts_test_backword_compatibility.properties");

/* Finally install the main module */
Expand Down
Loading