Search in sources :

Example 1 with Convert

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);
    }
}
Also used : InvalidConfiguration(de.simpleworks.staf.commons.exceptions.InvalidConfiguration) SystemException(de.simpleworks.staf.commons.exceptions.SystemException) URL(java.net.URL) Inject(com.google.inject.Inject) Parameter(org.apache.maven.plugins.annotations.Parameter) UtilsIO(de.simpleworks.staf.commons.utils.UtilsIO) ArrayList(java.util.ArrayList) UtilsMsTeams(de.simpleworks.staf.plugin.maven.msteams.utils.UtilsMsTeams) RequestBody(okhttp3.RequestBody) LinkedHashMap(java.util.LinkedHashMap) Mojo(org.apache.maven.plugins.annotations.Mojo) TestcaseReport(de.simpleworks.staf.commons.report.TestcaseReport) Response(okhttp3.Response) LifecyclePhase(org.apache.maven.plugins.annotations.LifecyclePhase) MapperTestplan(de.simpleworks.staf.commons.mapper.elements.MapperTestplan) Call(okhttp3.Call) Section(de.simpleworks.staf.plugin.maven.msteams.elements.Section) Convert(de.simpleworks.staf.commons.utils.Convert) MediaType(okhttp3.MediaType) Request(okhttp3.Request) JiraProperties(de.simpleworks.staf.module.jira.util.JiraProperties) StepReport(de.simpleworks.staf.commons.report.StepReport) UtilsCollection(de.simpleworks.staf.commons.utils.UtilsCollection) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) JsonPath(com.jayway.jsonpath.JsonPath) MapperTestcaseReport(de.simpleworks.staf.commons.mapper.report.MapperTestcaseReport) Collectors(java.util.stream.Collectors) File(java.io.File) TestCase(de.simpleworks.staf.commons.elements.TestCase) MojoFailureException(org.apache.maven.plugin.MojoFailureException) MsTeamsConsts(de.simpleworks.staf.plugin.maven.msteams.consts.MsTeamsConsts) Objects(java.util.Objects) TestPlan(de.simpleworks.staf.commons.elements.TestPlan) Arrays(edu.emory.mathcs.backport.java.util.Arrays) List(java.util.List) Logger(org.apache.logging.log4j.Logger) OkHttpClient(okhttp3.OkHttpClient) JSONObject(net.minidev.json.JSONObject) Named(com.google.inject.name.Named) Assert(org.junit.Assert) LogManager(org.apache.logging.log4j.LogManager) TestPlan(de.simpleworks.staf.commons.elements.TestPlan) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) StepReport(de.simpleworks.staf.commons.report.StepReport) TestcaseReport(de.simpleworks.staf.commons.report.TestcaseReport) MapperTestcaseReport(de.simpleworks.staf.commons.mapper.report.MapperTestcaseReport) RequestBody(okhttp3.RequestBody) Call(okhttp3.Call) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Request(okhttp3.Request) InvalidConfiguration(de.simpleworks.staf.commons.exceptions.InvalidConfiguration) Section(de.simpleworks.staf.plugin.maven.msteams.elements.Section) SystemException(de.simpleworks.staf.commons.exceptions.SystemException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Response(okhttp3.Response) JSONObject(net.minidev.json.JSONObject) TestCase(de.simpleworks.staf.commons.elements.TestCase) Objects(java.util.Objects) File(java.io.File)

Aggregations

Inject (com.google.inject.Inject)1 Named (com.google.inject.name.Named)1 JsonPath (com.jayway.jsonpath.JsonPath)1 TestCase (de.simpleworks.staf.commons.elements.TestCase)1 TestPlan (de.simpleworks.staf.commons.elements.TestPlan)1 InvalidConfiguration (de.simpleworks.staf.commons.exceptions.InvalidConfiguration)1 SystemException (de.simpleworks.staf.commons.exceptions.SystemException)1 MapperTestplan (de.simpleworks.staf.commons.mapper.elements.MapperTestplan)1 MapperTestcaseReport (de.simpleworks.staf.commons.mapper.report.MapperTestcaseReport)1 StepReport (de.simpleworks.staf.commons.report.StepReport)1 TestcaseReport (de.simpleworks.staf.commons.report.TestcaseReport)1 Convert (de.simpleworks.staf.commons.utils.Convert)1 UtilsCollection (de.simpleworks.staf.commons.utils.UtilsCollection)1 UtilsIO (de.simpleworks.staf.commons.utils.UtilsIO)1 JiraProperties (de.simpleworks.staf.module.jira.util.JiraProperties)1 MsTeamsConsts (de.simpleworks.staf.plugin.maven.msteams.consts.MsTeamsConsts)1 Section (de.simpleworks.staf.plugin.maven.msteams.elements.Section)1 UtilsMsTeams (de.simpleworks.staf.plugin.maven.msteams.utils.UtilsMsTeams)1 Arrays (edu.emory.mathcs.backport.java.util.Arrays)1 File (java.io.File)1