Skip to content

Commit ad4fba4

Browse files
authored
Merge branch 'master' into 680-db-sql-executor
2 parents dfb3098 + 739dd2b commit ad4fba4

File tree

7 files changed

+61
-1
lines changed

7 files changed

+61
-1
lines changed

core/src/main/java/org/jsmart/zerocode/core/AddService.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ public Integer squareMyNumber(MyNumber myNumber) {
2121
return myNumber.getNumber() * myNumber.getNumber();
2222
}
2323

24+
public Double squareRoot(Double number) {
25+
if (number < 0.0)
26+
throw new RuntimeException("Can not square root a negative number");
27+
return Math.sqrt(number);
28+
}
29+
2430
public Integer anInteger() {
2531
logger.debug("Returning a number ");
2632

core/src/main/java/org/jsmart/zerocode/core/domain/builders/ZeroCodeIoWriteBuilder.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.concurrent.Executors;
1717

1818
import static org.jsmart.zerocode.core.constants.ZeroCodeReportConstants.TARGET_REPORT_DIR;
19+
import static org.jsmart.zerocode.core.utils.SmartUtils.sanitizeReportFileName;
1920
import static org.slf4j.LoggerFactory.getLogger;
2021

2122
public class ZeroCodeIoWriteBuilder {
@@ -60,6 +61,7 @@ public synchronized void printToFile(String fileName) {
6061

6162
final ObjectMapper mapper = new ObjectMapperProvider().get();
6263

64+
fileName = sanitizeReportFileName(fileName);
6365
File file = new File(TARGET_REPORT_DIR + fileName);
6466
file.getParentFile().mkdirs();
6567
mapper.writeValue(file, this.built);

core/src/main/java/org/jsmart/zerocode/core/engine/executor/javaapi/JavaMethodExecutorImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ Object executeWithParams(String qualifiedClassName, String methodName, Object...
111111
} catch (Exception e) {
112112
String errMsg = format("Java exec(): Invocation failed for method %s in class %s", methodName, qualifiedClassName);
113113
LOGGER.error(errMsg + ". Exception - " + e);
114-
throw new RuntimeException(errMsg);
114+
throw new RuntimeException(errMsg, e);
115115
}
116116
}
117117

core/src/main/java/org/jsmart/zerocode/core/utils/SmartUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ public Map<String, Object> readJsonStringAsMap(String json) throws IOException {
8787
return map;
8888
}
8989

90+
public static String sanitizeReportFileName(String fileName) {
91+
return fileName.replaceAll("[^A-Za-z0-9 \\-_.]", "_");
92+
}
93+
9094
public static List<String> getAllEndPointFiles(String packageName) {
9195
if(isValidAbsolutePath(packageName)){
9296
return retrieveScenariosByAbsPath(packageName);

core/src/test/java/org/jsmart/zerocode/core/engine/executor/javaapi/JavaMethodExecutorImplTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import com.google.inject.Guice;
55
import com.google.inject.Injector;
6+
import java.lang.reflect.InvocationTargetException;
67
import java.lang.reflect.Method;
78
import java.util.List;
89
import org.jsmart.zerocode.core.di.main.ApplicationMainModule;
@@ -16,6 +17,9 @@
1617

1718
import static org.hamcrest.MatcherAssert.assertThat;
1819
import static org.hamcrest.core.Is.is;
20+
import static org.hamcrest.core.StringContains.containsString;
21+
import static org.hamcrest.CoreMatchers.notNullValue;
22+
import static org.junit.Assert.assertThrows;
1923

2024
public class JavaMethodExecutorImplTest {
2125

@@ -124,6 +128,29 @@ public void willExecuteJsonRequestFor_CustomObject_java_method() throws Exceptio
124128
assertThat(result, is(900));
125129
}
126130

131+
@Test
132+
public void willPropagateExceptions() throws Exception {
133+
String scenariosJsonAsString = SmartUtils.readJsonAsString("unit_test_files/java_apis/03_test_json_java_service_method_Exception.json");
134+
final ScenarioSpec scenarioSpec = smartUtils.getMapper().readValue(scenariosJsonAsString, ScenarioSpec.class);
135+
136+
String serviceName = scenarioSpec.getSteps().get(0).getUrl();
137+
String methodName = scenarioSpec.getSteps().get(0).getOperation();
138+
String requestJson = scenarioSpec.getSteps().get(0).getRequest().toString();
139+
List<Class<?>> argumentTypes = methodExecutor.getParameterTypes(serviceName, methodName);
140+
141+
Object request = mapper.readValue(requestJson, argumentTypes.get(0));
142+
143+
RuntimeException exception = assertThrows(RuntimeException.class, () -> {
144+
methodExecutor.executeWithParams(serviceName, methodName, request);
145+
});
146+
exception.printStackTrace();
147+
assertThat(exception.getMessage(), containsString("Invocation failed for method squareRoot"));
148+
// The target exception is included in this exception (inside of the InvocationTargetException)
149+
assertThat(exception.getCause(), is(notNullValue()));
150+
assertThat(((InvocationTargetException)exception.getCause()).getTargetException().getMessage(),
151+
is("Can not square root a negative number"));
152+
}
153+
127154
@Test
128155
public void willExecuteJsonWithParams_CustomObject_viaJson() throws Exception {
129156
String requestJson = "{\n" +

core/src/test/java/org/jsmart/zerocode/core/utils/SmartUtilsTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.Map;
2424

2525
import static org.hamcrest.CoreMatchers.containsString;
26+
import static org.hamcrest.CoreMatchers.equalTo;
2627
import static org.hamcrest.CoreMatchers.is;
2728
import static org.hamcrest.CoreMatchers.notNullValue;
2829
import static org.hamcrest.MatcherAssert.assertThat;
@@ -236,6 +237,13 @@ public void testSuiteFolder_symAbsolutePath() {
236237
assertThat(allScenarios.get(0), containsString("cherry_pick_tests/folder_b/test_case_2.json"));
237238
}
238239

240+
@Test
241+
public void testSanitizeReportFile() {
242+
String orig = "file !#$%&'()*+,-./09:;<=>?@AZ[]^_`az{|}~\"\\";
243+
String dest = "file ___________-._09_______AZ_____az______";
244+
assertThat(SmartUtils.sanitizeReportFileName(orig), equalTo(dest));
245+
}
246+
239247
// Move this to File Util class
240248
private static File createCascadeIfNotExisting(String fileName) {
241249
try {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"scenarioName": "Given_When_Then-Flow name For Java Service",
3+
"steps": [
4+
{
5+
"name": "javaMethodException",
6+
"url": "org.jsmart.zerocode.core.AddService",
7+
"operation": "squareRoot",
8+
"request": "-255.0", //<-- This negative number should throw an exception
9+
"assertions": {
10+
}
11+
}
12+
]
13+
}

0 commit comments

Comments
 (0)