use of de.simpleworks.staf.commons.utils.Convert in project staf by simpleworks-gmbh.
the class NotifyTestResultMojo method execute.
@SuppressWarnings("unchecked")
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
try {
// init variables
init();
for (final TestPlan plan : testplans) {
for (final File file : resultFiles) {
final List<TestcaseReport> testresults = mapperTestcaseReport.readAll(file);
if (Convert.isEmpty(testresults)) {
throw new IllegalArgumentException("testresults can't be null or empty.");
}
final List<TestcaseReport> skippedTestcases = new ArrayList<>();
for (final TestCase testcase : plan.getTestCases()) {
if (!testresults.stream().filter(testresult -> testresult.getId().equals(testcase.getId())).findAny().isPresent()) {
if (!skippedTestcases.stream().filter(tc -> tc.getId().equals(testcase.getId())).findAny().isPresent()) {
final TestcaseReport skippedTestcaseReport = new TestcaseReport(testcase.getId());
skippedTestcases.add(skippedTestcaseReport);
skippedTestcases.get(0).addStep(new StepReport(String.format("Skipped Testcase %s", skippedTestcases.get(0).getId())));
}
}
}
if (!Convert.isEmpty(skippedTestcases)) {
if (!testresults.addAll(skippedTestcases)) {
throw new Exception("can't add skipped testcases.");
}
}
final List<Section> sections = testresults.stream().map(section -> {
try {
return UtilsMsTeams.convert(section);
} catch (Exception ex) {
return null;
}
}).filter(Objects::nonNull).collect(Collectors.toList()).stream().map(section -> {
try {
section.setActivityText(String.format("[Current Test-Execution](%s)", String.format("%s/plugins/servlet/ac/com.xpandit.plugins.xray/execution-page?ac.testExecIssueKey=%s&ac.testIssueKey=%s", properties.getUrl().toString(), testplans.get(0).getId(), section.getActivityTitle())));
} catch (final InvalidConfiguration ex) {
NotifyTestResultMojo.logger.error("can't setup link to current test execution.", ex);
}
return section;
}).collect(Collectors.toList());
final List<JSONObject> convertedSections = sections.stream().map(UtilsMsTeams::convert).collect(Collectors.toList());
final String jsonString = UtilsIO.getAllContentFromFile(templateFile);
@SuppressWarnings("rawtypes") final LinkedHashMap jsonarray = JsonPath.read(jsonString, "$");
final JSONObject jsonObject = new JSONObject();
final List<JSONObject> tmp = new ArrayList<>();
for (int itr = 0; itr < convertedSections.size(); itr += 1) {
JSONObject convertedSection = convertedSections.get(itr);
tmp.add(convertedSection);
jsonObject.put("sections", UtilsCollection.toArray(JSONObject.class, tmp));
jsonarray.put("sections", jsonObject.get("sections"));
final RequestBody body = RequestBody.create(MediaType.parse("application/json"), new JSONObject(jsonarray).toString());
/**
* We need to ensure that the payload is less than 28KB We will send 10KB, to be
* sure to handle further limitations.
*
* Furthermore we will ensure, that we send data, if we are at the "last index"
*/
if ((body.contentLength() / 1024) > 10 || (itr == convertedSections.size() - 1)) {
final Request request = new Request.Builder().url(webhook).post(body).build();
final Call call = client.newCall(request);
try (Response response = call.execute()) {
Assert.assertTrue(String.format("The response code %s, does not match the expected one %s.", Integer.toString(response.code()), "200"), response.code() == 200);
}
tmp.clear();
}
}
}
}
} catch (final Exception ex) {
final String msg = "can't upload testresults.";
NotifyTestResultMojo.logger.error(ex);
throw new MojoExecutionException(msg);
}
}
Aggregations