Skip to content

Commit

Permalink
Merge branch 'master' into ISSUE-193-OAuth2-Support
Browse files Browse the repository at this point in the history
  • Loading branch information
prayascoriolis authored Oct 29, 2024
2 parents 14d65c9 + 739dd2b commit 626f82e
Show file tree
Hide file tree
Showing 16 changed files with 240 additions and 10 deletions.
6 changes: 6 additions & 0 deletions core/src/main/java/org/jsmart/zerocode/core/AddService.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ public Integer squareMyNumber(MyNumber myNumber) {
return myNumber.getNumber() * myNumber.getNumber();
}

public Double squareRoot(Double number) {
if (number < 0.0)
throw new RuntimeException("Can not square root a negative number");
return Math.sqrt(number);
}

public Integer anInteger() {
logger.debug("Returning a number ");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class ScenarioSpec {
Expand All @@ -15,19 +16,30 @@ public class ScenarioSpec {
private final String scenarioName;
private final List<Step> steps;
private final Parameterized parameterized;
private static Map<String, List<String>> meta;

// Add getter and setter for meta
public static Map<String, List<String>> getMeta() {
return meta;
}
public void setMeta(Map<String, List<String>> meta) {
this.meta = meta;
}

@JsonCreator
public ScenarioSpec(
@JsonProperty("stepLoop") Integer loop,
@JsonProperty("ignoreStepFailures") Boolean ignoreStepFailures,
@JsonProperty("scenarioName") String scenarioName,
@JsonProperty("steps") List<Step> steps,
@JsonProperty("parameterized") Parameterized parameterized) {
@JsonProperty("parameterized") Parameterized parameterized,
@JsonProperty("meta") Map<String, List<String>> meta) {
this.loop = loop;
this.ignoreStepFailures = ignoreStepFailures;
this.scenarioName = scenarioName;
this.steps = steps;
this.parameterized = parameterized;
this.meta=meta;
}

public Integer getLoop() {
Expand Down Expand Up @@ -58,6 +70,8 @@ public String toString() {
", scenarioName='" + scenarioName + '\'' +
", steps=" + steps +
", parameterized=" + parameterized +
", meta=" + meta +
'}';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public class ZeroCodeCsvReportBuilder {
String requestTimeStamp;
String responseTimeStamp;
private Double responseDelayMilliSec;
private String metaAuthors;
private String metaTickets;
private String metaCategories;
private String metaOthers;

public static ZeroCodeCsvReportBuilder newInstance() {
return new ZeroCodeCsvReportBuilder();
Expand All @@ -21,6 +25,10 @@ public static ZeroCodeCsvReportBuilder newInstance() {
public ZeroCodeCsvReport build() {
ZeroCodeCsvReport built = new ZeroCodeCsvReport(scenarioName,scenarioLoop,stepName, stepLoop,
correlationId, result, method, requestTimeStamp, responseTimeStamp, responseDelayMilliSec);
built.setMetaAuthors(metaAuthors);
built.setMetaTickets(metaTickets);
built.setMetaCategories(metaCategories);
built.setMetaOthers(metaOthers);
return built;
}

Expand Down Expand Up @@ -73,4 +81,24 @@ public ZeroCodeCsvReportBuilder responseDelayMilliSec(Double responseDelayMilliS
this.responseDelayMilliSec = responseDelayMilliSec;
return this;
}

public ZeroCodeCsvReportBuilder setMetaAuthors(String metaAuthors) {
this.metaAuthors = metaAuthors;
return this;
}

public ZeroCodeCsvReportBuilder setMetaTickets(String metaTickets) {
this.metaTickets = metaTickets;
return this;
}

public ZeroCodeCsvReportBuilder setMetaCategories(String metaCategories) {
this.metaCategories = metaCategories;
return this;
}

public ZeroCodeCsvReportBuilder setMetaOthers(String metaOthers) {
this.metaOthers = metaOthers;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@

import org.jsmart.zerocode.core.domain.reports.ZeroCodeReportStep;
import org.jsmart.zerocode.core.domain.reports.ZeroCodeExecResult;
import org.jsmart.zerocode.core.domain.ScenarioSpec;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class ZeroCodeExecReportBuilder {
private String scenarioName;
private Integer loop;
private List<ZeroCodeReportStep> steps = Collections.synchronizedList(new ArrayList());
private Map<String, List<String>> meta = ScenarioSpec.getMeta();

public static ZeroCodeExecReportBuilder newInstance() {
return new ZeroCodeExecReportBuilder();
}

public ZeroCodeExecResult build() {
ZeroCodeExecResult built = new ZeroCodeExecResult(scenarioName, loop, steps);
ZeroCodeExecResult built = new ZeroCodeExecResult(scenarioName, loop, steps, meta);
return built;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.concurrent.Executors;

import static org.jsmart.zerocode.core.constants.ZeroCodeReportConstants.TARGET_REPORT_DIR;
import static org.jsmart.zerocode.core.utils.SmartUtils.sanitizeReportFileName;
import static org.slf4j.LoggerFactory.getLogger;

public class ZeroCodeIoWriteBuilder {
Expand Down Expand Up @@ -60,6 +61,7 @@ public synchronized void printToFile(String fileName) {

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

fileName = sanitizeReportFileName(fileName);
File file = new File(TARGET_REPORT_DIR + fileName);
file.getParentFile().mkdirs();
mapper.writeValue(file, this.built);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,25 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class ZeroCodeExecResult {
private String scenarioName;
private Integer loop;
private List<ZeroCodeReportStep> steps = new ArrayList<>();

private Map<String, List<String>> meta;

@JsonCreator
public ZeroCodeExecResult(
@JsonProperty("scenarioName")String scenarioName,
@JsonProperty("stepLoop")Integer loop,
@JsonProperty("steps")List<ZeroCodeReportStep> steps) {
@JsonProperty("steps")List<ZeroCodeReportStep> steps,
@JsonProperty("meta") Map<String, List<String>> meta) {
this.scenarioName = scenarioName;
this.loop = loop;
this.steps = steps;
this.meta = meta;
}

public String getScenarioName() {
Expand All @@ -47,4 +51,9 @@ public String toString() {
", steps=" + steps +
'}';
}

public Map<String, List<String>> getMeta() {
return meta;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public class ZeroCodeCsvReport {
String requestTimeStamp;
String responseTimeStamp;
private Double responseDelayMilliSec;
// defining meta data fields
private String metaAuthors;
private String metaTickets;
private String metaCategories;
private String metaOthers;

public ZeroCodeCsvReport(String scenarioName, Integer scenarioLoop, String stepName, Integer stepLoop,
String correlationId, String result, String method, String requestTimeStamp,
Expand Down Expand Up @@ -67,6 +72,40 @@ public String getResponseTimeStamp() {
return responseTimeStamp;
}

// defining meta data field setters and getters

public String getMetaAuthors() {
return metaAuthors;
}

public void setMetaAuthors(String metaAuthors) {
this.metaAuthors = metaAuthors;
}

public String getMetaTickets() {
return metaTickets;
}

public void setMetaTickets(String metaTickets) {
this.metaTickets = metaTickets;
}

public String getMetaCategories() {
return metaCategories;
}

public void setMetaCategories(String metaCategories) {
this.metaCategories = metaCategories;
}

public String getMetaOthers() {
return metaOthers;
}

public void setMetaOthers(String metaOthers) {
this.metaOthers = metaOthers;
}

@Override
public String toString() {
return "ZeroCodeCsvReport{" +
Expand All @@ -75,11 +114,15 @@ public String toString() {
", stepName='" + stepName + '\'' +
", stepLoop=" + stepLoop +
", correlationId='" + correlationId + '\'' +
", requestTimeStamp='" + requestTimeStamp + '\'' +
", responseDelayMilliSec=" + responseDelayMilliSec +
", responseTimeStamp='" + responseTimeStamp + '\'' +
", result='" + result + '\'' +
", method='" + method + '\'' +
", requestTimeStamp=" + requestTimeStamp +
", responseTimeStamp=" + responseTimeStamp +
", responseDelayMilliSec=" + responseDelayMilliSec +
", metaAuthors='" + metaAuthors + '\'' +
", metaTickets='" + metaTickets + '\'' +
", metaCategories='" + metaCategories + '\'' +
", metaOthers='" + metaOthers + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Object executeWithParams(String qualifiedClassName, String methodName, Object...
} catch (Exception e) {
String errMsg = format("Java exec(): Invocation failed for method %s in class %s", methodName, qualifiedClassName);
LOGGER.error(errMsg + ". Exception - " + e);
throw new RuntimeException(errMsg);
throw new RuntimeException(errMsg, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.markuputils.CodeLanguage;
import com.aventstack.extentreports.markuputils.ExtentColor;
import com.aventstack.extentreports.markuputils.MarkupHelper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
Expand Down Expand Up @@ -98,6 +99,17 @@ public void generateExtentReport() {
thisReport.getResults().forEach(thisScenario -> {
ExtentTest test = extentReports.createTest(thisScenario.getScenarioName());

/**This code checks if the scenario has meta data.
If it does, it iterates through each meta data entry and adds it to
the Extent report as an info label.**/
if (thisScenario.getMeta() != null) {
for (Map.Entry<String, List<String>> entry : thisScenario.getMeta().entrySet()) {
String key = entry.getKey();
List<String> values = entry.getValue();
test.info(MarkupHelper.createLabel(key + ": " + String.join(", ", values), ExtentColor.BLUE));
}
}

// Assign Category
test.assignCategory(DEFAULT_REGRESSION_CATEGORY); //Super set
String[] hashTagsArray = optionalCategories(thisScenario.getScenarioName()).toArray(new String[0]);
Expand Down Expand Up @@ -276,6 +288,11 @@ public void generateCsvReport(List<ZeroCodeCsvReport> zeroCodeCsvReportRows) {
.addColumn("responseTimeStamp")
.addColumn("result")
.addColumn("method")
// This adds new columns to the CSV schema for each type of meta data.
.addColumn("metaAuthors")
.addColumn("metaTickets")
.addColumn("metaCategories")
.addColumn("metaOthers")
.build();

CsvMapper csvMapper = new CsvMapper();
Expand Down Expand Up @@ -310,6 +327,18 @@ public List<ZeroCodeCsvReport> buildCsvRows() {
csvFileBuilder.scenarioLoop(thisResult.getLoop());
csvFileBuilder.scenarioName(thisResult.getScenarioName());

// Add meta information
Map<String, List<String>> meta = thisResult.getMeta();
if (meta != null) {
/**This code retrieves the meta data from the test result. If meta data exists,
* it joins the list of values for each meta data type into a comma-separated
* string and adds it to the CSV row.**/
csvFileBuilder.setMetaAuthors(String.join(", ", meta.getOrDefault("authors", Collections.emptyList())));
csvFileBuilder.setMetaTickets(String.join(", ", meta.getOrDefault("tickets", Collections.emptyList())));
csvFileBuilder.setMetaCategories(String.join(", ", meta.getOrDefault("categories", Collections.emptyList())));
csvFileBuilder.setMetaOthers(String.join(", ", meta.getOrDefault("others", Collections.emptyList())));
}

thisResult.getSteps().forEach(thisStep -> {
csvFileBuilder.stepLoop(thisStep.getLoop());
csvFileBuilder.stepName(thisStep.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ public Map<String, Object> readJsonStringAsMap(String json) throws IOException {
return map;
}

public static String sanitizeReportFileName(String fileName) {
return fileName.replaceAll("[^A-Za-z0-9 \\-_.]", "_");
}

public static List<String> getAllEndPointFiles(String packageName) {
if(isValidAbsolutePath(packageName)){
return retrieveScenariosByAbsPath(packageName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.inject.Guice;
import com.google.inject.Injector;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import org.jsmart.zerocode.core.di.main.ApplicationMainModule;
Expand All @@ -16,6 +17,9 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.StringContains.containsString;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThrows;

public class JavaMethodExecutorImplTest {

Expand Down Expand Up @@ -124,6 +128,29 @@ public void willExecuteJsonRequestFor_CustomObject_java_method() throws Exceptio
assertThat(result, is(900));
}

@Test
public void willPropagateExceptions() throws Exception {
String scenariosJsonAsString = SmartUtils.readJsonAsString("unit_test_files/java_apis/03_test_json_java_service_method_Exception.json");
final ScenarioSpec scenarioSpec = smartUtils.getMapper().readValue(scenariosJsonAsString, ScenarioSpec.class);

String serviceName = scenarioSpec.getSteps().get(0).getUrl();
String methodName = scenarioSpec.getSteps().get(0).getOperation();
String requestJson = scenarioSpec.getSteps().get(0).getRequest().toString();
List<Class<?>> argumentTypes = methodExecutor.getParameterTypes(serviceName, methodName);

Object request = mapper.readValue(requestJson, argumentTypes.get(0));

RuntimeException exception = assertThrows(RuntimeException.class, () -> {
methodExecutor.executeWithParams(serviceName, methodName, request);
});
exception.printStackTrace();
assertThat(exception.getMessage(), containsString("Invocation failed for method squareRoot"));
// The target exception is included in this exception (inside of the InvocationTargetException)
assertThat(exception.getCause(), is(notNullValue()));
assertThat(((InvocationTargetException)exception.getCause()).getTargetException().getMessage(),
is("Can not square root a negative number"));
}

@Test
public void willExecuteJsonWithParams_CustomObject_viaJson() throws Exception {
String requestJson = "{\n" +
Expand Down
Loading

0 comments on commit 626f82e

Please sign in to comment.