diff --git a/allure-jira-commons/src/main/java/io/qameta/allure/jira/JiraExportResult.java b/allure-jira-commons/src/main/java/io/qameta/allure/jira/JiraExportResult.java new file mode 100644 index 000000000..49cf2794d --- /dev/null +++ b/allure-jira-commons/src/main/java/io/qameta/allure/jira/JiraExportResult.java @@ -0,0 +1,35 @@ +/* + * Copyright 2019 Qameta Software OÜ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.qameta.allure.jira; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.Data; +import lombok.experimental.Accessors; + +/** + * Jira launch result export data. + */ +@Data +@Accessors(chain = true) +@JsonIgnoreProperties(ignoreUnknown = true) +public class JiraExportResult { + private String issueKey; + private String externalId; + private String status; + @JsonInclude(JsonInclude.Include.NON_NULL) + private String message; +} diff --git a/allure-jira-commons/src/main/java/io/qameta/allure/jira/JiraLaunch.java b/allure-jira-commons/src/main/java/io/qameta/allure/jira/JiraLaunch.java index 1542296d2..d48ec1533 100644 --- a/allure-jira-commons/src/main/java/io/qameta/allure/jira/JiraLaunch.java +++ b/allure-jira-commons/src/main/java/io/qameta/allure/jira/JiraLaunch.java @@ -28,19 +28,12 @@ @Accessors(chain = true) @JsonIgnoreProperties(ignoreUnknown = true) public class JiraLaunch { - private String externalId; - - private List issueKeys; - + private List statistic; private String name; private String url; private Long date; - private Long failed; - private Long broken; - private Long passed; - private Long skipped; - private Long unknown; + } diff --git a/allure-jira-commons/src/main/java/io/qameta/allure/jira/JiraService.java b/allure-jira-commons/src/main/java/io/qameta/allure/jira/JiraService.java index 778878419..115dcc69b 100644 --- a/allure-jira-commons/src/main/java/io/qameta/allure/jira/JiraService.java +++ b/allure-jira-commons/src/main/java/io/qameta/allure/jira/JiraService.java @@ -35,13 +35,13 @@ public interface JiraService { Response createIssueComment(@Path("issueKey") String issueKey, @Body JiraIssueComment comment); @POST("allure/1.0/launch") - JiraLaunch createJiraLaunch(@Body JiraLaunch launch); + List createJiraLaunch(@Body JiraLaunch launch, @Query("issueKey") List issueKey); @GET("allure/1.0/launch") List getJiraLaunches(@Query("issueKey") String issueKey); @POST("allure/1.0/testresult") - JiraTestResult createTestResult(@Body JiraTestResult launch); + List createTestResult(@Body JiraTestResult launch, @Query("issueKey") List issueKey); @GET("allure/1.0/testresult") List getTestResults(@Query("issueKey") String issueKey); @@ -49,7 +49,7 @@ public interface JiraService { @GET("raven/1.0/api/testexec/{issueKey}/test") List getTestRunsForTestExecution(@Path("issueKey") String issueKey); - @PUT("raven/1.0/api/testrun/{id}/status") - Response updateTestRunStatus(@Path("id") Integer id, @Query("status") String status); + @PUT("raven/1.0/api/testrun/{externalId}/status") + Response updateTestRunStatus(@Path("externalId") Integer externalId, @Query("status") String status); } diff --git a/allure-jira-commons/src/main/java/io/qameta/allure/jira/JiraTestResult.java b/allure-jira-commons/src/main/java/io/qameta/allure/jira/JiraTestResult.java index aed01eac6..5f689762d 100644 --- a/allure-jira-commons/src/main/java/io/qameta/allure/jira/JiraTestResult.java +++ b/allure-jira-commons/src/main/java/io/qameta/allure/jira/JiraTestResult.java @@ -19,8 +19,6 @@ import lombok.Data; import lombok.experimental.Accessors; -import java.util.List; - /** * Jira test result export data. */ @@ -28,16 +26,15 @@ @Accessors(chain = true) @JsonIgnoreProperties(ignoreUnknown = true) public class JiraTestResult { - - private int id; - private String externalId; - private List issueKeys; - + private String testCaseId; + private String historyKey; private String name; private String url; - private Long date; - private String status; - + private String color; + private Long date; + private String launchExternalId; + private String launchName; + private String launchUrl; } diff --git a/allure-jira-commons/src/main/java/io/qameta/allure/jira/LaunchStatisticExport.java b/allure-jira-commons/src/main/java/io/qameta/allure/jira/LaunchStatisticExport.java new file mode 100644 index 000000000..7cfd7c328 --- /dev/null +++ b/allure-jira-commons/src/main/java/io/qameta/allure/jira/LaunchStatisticExport.java @@ -0,0 +1,35 @@ +/* + * Copyright 2019 Qameta Software OÜ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.qameta.allure.jira; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import lombok.Data; +import lombok.experimental.Accessors; + +/** + * Statistics for Jira test result export data. + */ + +@Data +@Accessors(chain = true) +@JsonIgnoreProperties(ignoreUnknown = true) +public class LaunchStatisticExport { + private final String status; + private final String color; + private final long count; + +} diff --git a/allure-jira-commons/src/main/java/io/qameta/allure/jira/ResultStatus.java b/allure-jira-commons/src/main/java/io/qameta/allure/jira/ResultStatus.java new file mode 100644 index 000000000..db36785bb --- /dev/null +++ b/allure-jira-commons/src/main/java/io/qameta/allure/jira/ResultStatus.java @@ -0,0 +1,27 @@ +package io.qameta.allure.jira; + +import io.qameta.allure.entity.Status; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.experimental.Accessors; + +/** + * Mutable Test Results Enum for Jira Integration. + */ + +@AllArgsConstructor +@Accessors(fluent = true) +@Getter +public enum ResultStatus { + FAILED(Status.FAILED, "f90602"), + BROKEN(Status.BROKEN, "febe0d"), + PASSED(Status.PASSED, "78b63c"), + SKIPPED(Status.SKIPPED, "888888"), + UNKNOWN(Status.UNKNOWN, "bf34a6"); + + + private final Status statusName; + private final String color; + + +} diff --git a/plugins/jira-plugin/src/main/java/io/qameta/allure/jira/JiraExportPlugin.java b/plugins/jira-plugin/src/main/java/io/qameta/allure/jira/JiraExportPlugin.java index 0a1986813..2f8103d18 100644 --- a/plugins/jira-plugin/src/main/java/io/qameta/allure/jira/JiraExportPlugin.java +++ b/plugins/jira-plugin/src/main/java/io/qameta/allure/jira/JiraExportPlugin.java @@ -21,14 +21,14 @@ import io.qameta.allure.entity.ExecutorInfo; import io.qameta.allure.entity.Link; import io.qameta.allure.entity.Statistic; +import io.qameta.allure.entity.Status; import io.qameta.allure.entity.TestResult; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.nio.file.Path; -import java.util.Arrays; -import java.util.Collection; import java.util.List; +import java.util.Objects; import java.util.Optional; import java.util.function.Supplier; import java.util.stream.Collectors; @@ -38,12 +38,12 @@ /** * @author eroshenkoam (Artem Eroshenko). */ + + public class JiraExportPlugin implements Aggregator { private static final Logger LOGGER = LoggerFactory.getLogger(JiraExportPlugin.class); - private static final String EXECUTORS_BLOCK_NAME = "executor"; - private static final String ALLURE_JIRA_ENABLED = "ALLURE_JIRA_ENABLED"; private static final String ALLURE_JIRA_LAUNCH_ISSUES = "ALLURE_JIRA_LAUNCH_ISSUES"; @@ -74,124 +74,89 @@ public void aggregate(final Configuration configuration, if (enabled) { final JiraService jiraService = jiraServiceSupplier.get(); - final List issues = splitByComma(this.issues); - final ExecutorInfo executor = getExecutor(launchesResults); - final Statistic statistic = getStatistic(launchesResults); - - final JiraLaunch launch = getJiraLaunch(issues, executor, statistic); - final JiraLaunch created = exportLaunchToJira(jiraService, launch); + final List issues = JiraExportUtils.splitByComma(this.issues); + final ExecutorInfo executor = JiraExportUtils.getExecutor(launchesResults); + final Statistic statisticToConvert = JiraExportUtils.getStatistic(launchesResults); + final List statistic = JiraExportUtils.convertStatistics(statisticToConvert); + final JiraLaunch launch = JiraExportUtils.getJiraLaunch(executor, statistic); + exportLaunchToJira(jiraService, launch, issues); - getTestResults(launchesResults).stream() - .map(testResult -> getJiraTestResult(created, executor, testResult)) + JiraExportUtils.getTestResults(launchesResults).stream() + .map(testResult -> JiraExportUtils.getJiraTestResult(executor, testResult)) .filter(Optional::isPresent) .map(Optional::get) - .forEach(testResult -> exportTestResultToJira(jiraService, testResult)); + .forEach(jiraTestResult -> { + JiraExportUtils.getTestResults(launchesResults) + .stream() + .forEach(testResult -> + exportTestResultToJira(jiraService, jiraTestResult, testResult)); + }); } } - private JiraLaunch getJiraLaunch(final List issueKeys, - final ExecutorInfo executor, - final Statistic statistic) { - return new JiraLaunch() - .setIssueKeys(issueKeys) - .setName(executor.getBuildName()) - .setUrl(executor.getReportUrl()) - .setPassed(statistic.getPassed()) - .setFailed(statistic.getFailed()) - .setBroken(statistic.getBroken()) - .setSkipped(statistic.getSkipped()) - .setUnknown(statistic.getUnknown()) - .setDate(System.currentTimeMillis()); - } - - private Optional getJiraTestResult(final JiraLaunch launch, - final ExecutorInfo executor, - final TestResult testResult) { - final List issues = testResult.getLinks().stream() - .filter(this::isIssueLink) - .map(Link::getName) - .collect(Collectors.toList()); - if (issues.isEmpty()) { - return Optional.empty(); - } else { - final JiraTestResult jiraTestResult = new JiraTestResult() - .setIssueKeys(issues) - .setName(testResult.getName()) - .setUrl(getJiraTestResultUrl(executor.getReportUrl(), testResult.getUid())) - .setStatus(testResult.getStatus().toString()) - .setDate(testResult.getTime().getStop()) - .setExternalId(launch.getExternalId()); - return Optional.of(jiraTestResult); - } - } - - - private List getTestResults(final List launchesResults) { - return launchesResults.stream() - .map(LaunchResults::getAllResults) - .flatMap(Collection::stream) - .collect(Collectors.toList()); - } + private List exportLaunchToJira(final JiraService jiraService, + final JiraLaunch launch, + final List issues) { + try { + final List created = jiraService.createJiraLaunch(launch, issues); - private ExecutorInfo getExecutor(final List launchesResults) { - return launchesResults.stream() - .map(launchResults -> launchResults.getExtra(EXECUTORS_BLOCK_NAME)) - .filter(Optional::isPresent) - .map(Optional::get) - .filter(ExecutorInfo.class::isInstance) - .map(ExecutorInfo.class::cast) - .findFirst() - .orElse(new ExecutorInfo()); - } + final List failedExports = findFailuresInExportResult(created); - private Statistic getStatistic(final List launchesResults) { - final Statistic statistic = new Statistic(); - launchesResults.stream() - .map(LaunchResults::getAllResults) - .flatMap(Collection::stream) - .forEach(statistic::update); - return statistic; - } + if (!failedExports.isEmpty()) { + logErrorResults(failedExports); + } else { + LOGGER.info(String.format("Allure launch '%s' synced with issues successfully%n", + issues)); + LOGGER.info(String.format("Results of launch export %n %s", created)); + } - private JiraLaunch exportLaunchToJira(final JiraService jiraService, final JiraLaunch launch) { - try { - final JiraLaunch created = jiraService.createJiraLaunch(launch); - LOGGER.info(String.format("Allure launch '%s' synced with issues '%s' successfully", - created.getExternalId(), created.getIssueKeys())); return created; } catch (Throwable e) { - LOGGER.error(String.format("Allure launch sync with issue '%s' error", launch.getIssueKeys()), e); + LOGGER.error(String.format("Allure launch sync with issue '%s' error", issues), e); throw e; } } - private void exportTestResultToJira(final JiraService jiraService, final JiraTestResult testResult) { + private void exportTestResultToJira(final JiraService jiraService, + final JiraTestResult jiraTestResult, + final TestResult testResult) { + + if (!Objects.equals(jiraTestResult.getTestCaseId(), testResult.getUid())) { + return; + } + try { - final JiraTestResult created = jiraService.createTestResult(testResult); - LOGGER.info(String.format("Allure test result '%s' synced with issue '%s' successfully", - created.getId(), - created.getIssueKeys())); + final List issues = testResult.getLinks().stream() + .filter(JiraExportUtils::isIssueLink) + .map(Link::getName) + .collect(Collectors.toList()); + + final List created = jiraService.createTestResult(jiraTestResult, issues); + final List failedExports = findFailuresInExportResult(created); + + if (!failedExports.isEmpty()) { + logErrorResults(failedExports); + } else { + LOGGER.info("All Test Results have been successfully exported to Jira"); + } + } catch (Throwable e) { - LOGGER.error(String.format("Allure test result sync with issue '%s' failed", testResult.getIssueKeys()), e); + LOGGER.error(String.format("Allure test result sync with issue '%s' failed", + jiraTestResult.getExternalId()), e); throw e; } } - - private String getJiraTestResultUrl(final String reportUrl, final String uuid) { - return Optional.ofNullable(reportUrl) - .map(url -> url.endsWith("index.html") ? "%s#testresult/%s" : "%s/#testresult/%s") - .map(pattern -> String.format(pattern, reportUrl, uuid)) - .orElse(null); + private void logErrorResults(final List failedExportResults) { + LOGGER.error(String.format("There was an failure in response%n %s", failedExportResults)); } - private boolean isIssueLink(final Link link) { - return "issue".equals(link.getType()); - } - private static List splitByComma(final String value) { - return Arrays.asList(value.split(",")); + private List findFailuresInExportResult(final List exportResults) { + return exportResults.stream() + .filter(exportResult -> exportResult.getStatus().equals(Status.FAILED.value())) + .collect(Collectors.toList()); } } diff --git a/plugins/jira-plugin/src/main/java/io/qameta/allure/jira/JiraExportUtils.java b/plugins/jira-plugin/src/main/java/io/qameta/allure/jira/JiraExportUtils.java new file mode 100644 index 000000000..31053a4f0 --- /dev/null +++ b/plugins/jira-plugin/src/main/java/io/qameta/allure/jira/JiraExportUtils.java @@ -0,0 +1,142 @@ +/* + * Copyright 2019 Qameta Software OÜ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.qameta.allure.jira; + +import io.qameta.allure.core.LaunchResults; +import io.qameta.allure.entity.ExecutorInfo; +import io.qameta.allure.entity.Link; +import io.qameta.allure.entity.Statistic; +import io.qameta.allure.entity.TestResult; + +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** + * @author SeleniumTestAB. + *

+ * Utility Class for Jira Export Plugin + */ + +public final class JiraExportUtils { + + private static final String EXECUTORS_BLOCK_NAME = "executor"; + + private JiraExportUtils() { + } + + + public static JiraLaunch getJiraLaunch(final ExecutorInfo executor, + final List statistic) { + return new JiraLaunch() + .setExternalId(executor.getBuildName()) + .setStatistic(statistic) + .setName(executor.getBuildName()) + .setUrl(executor.getReportUrl()) + .setDate(System.currentTimeMillis()); + } + + public static Optional getJiraTestResult(final ExecutorInfo executor, + final TestResult testResult) { + final List issues = testResult.getLinks().stream() + .filter(JiraExportUtils::isIssueLink) + .map(Link::getName) + .collect(Collectors.toList()); + if (issues.isEmpty()) { + return Optional.empty(); + } else { + final JiraTestResult jiraTestResult = new JiraTestResult() + .setExternalId(testResult.getUid()) + .setTestCaseId(testResult.getUid()) + .setHistoryKey(testResult.getHistoryId()) + .setName(testResult.getName()) + .setUrl(getJiraTestResultUrl(executor.getReportUrl(), testResult.getUid())) + .setStatus(testResult.getStatus().toString()) + .setColor(findTestResultsStatusColor(testResult)) + .setDate(testResult.getTime().getStop()) + .setLaunchUrl(executor.getReportUrl()) + .setLaunchName(executor.getBuildName()) + .setLaunchExternalId(executor.getBuildName()); + return Optional.of(jiraTestResult); + } + } + + + public static List getTestResults(final List launchesResults) { + return launchesResults.stream() + .map(LaunchResults::getAllResults) + .flatMap(Collection::stream) + .collect(Collectors.toList()); + } + + public static ExecutorInfo getExecutor(final List launchesResults) { + return launchesResults.stream() + .map(launchResults -> launchResults.getExtra(EXECUTORS_BLOCK_NAME)) + .filter(Optional::isPresent) + .map(Optional::get) + .filter(ExecutorInfo.class::isInstance) + .map(ExecutorInfo.class::cast) + .findFirst() + .orElse(new ExecutorInfo()); + } + + public static Statistic getStatistic(final List launchesResults) { + final Statistic statistic = new Statistic(); + launchesResults.stream() + .map(LaunchResults::getAllResults) + .flatMap(Collection::stream) + .forEach(statistic::update); + return statistic; + } + + public static List convertStatistics(final Statistic statistic) { + return Stream.of(ResultStatus.values()).filter(resultStatus -> statistic.get(resultStatus.statusName()) != 0) + .map(resultStatus -> + new LaunchStatisticExport(resultStatus.statusName().value(), + resultStatus.color(), statistic.get(resultStatus.statusName()))) + .collect(Collectors.toList()); + + } + + + public static String getJiraTestResultUrl(final String reportUrl, final String uuid) { + return Optional.ofNullable(reportUrl) + .map(url -> url.endsWith("index.html") ? "%s#testresult/%s" : "%s/#testresult/%s") + .map(pattern -> String.format(pattern, reportUrl, uuid)) + .orElse(null); + } + + public static boolean isIssueLink(final Link link) { + return "issue".equals(link.getType()); + } + + public static List splitByComma(final String value) { + return Arrays.asList(value.split(",")); + } + + private static String findTestResultsStatusColor(final TestResult testResult) { + return Stream.of(ResultStatus.values()) + .filter(resultStatus -> testResult.getStatus() == resultStatus.statusName()) + .findFirst() + .orElseThrow(() -> + new IllegalArgumentException("There is no such status as " + testResult.getStatus() + .value())) + .color(); + } +} diff --git a/plugins/jira-plugin/src/test/java/io/qameta/allure/jira/JiraExportUtilitiesTest.java b/plugins/jira-plugin/src/test/java/io/qameta/allure/jira/JiraExportUtilitiesTest.java new file mode 100644 index 000000000..ab62887f3 --- /dev/null +++ b/plugins/jira-plugin/src/test/java/io/qameta/allure/jira/JiraExportUtilitiesTest.java @@ -0,0 +1,88 @@ +package io.qameta.allure.jira; + +import io.qameta.allure.core.LaunchResults; +import io.qameta.allure.entity.Statistic; +import io.qameta.allure.entity.Status; +import io.qameta.allure.entity.TestResult; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import static io.qameta.allure.jira.TestData.createTestResult; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class JiraExportUtilitiesTest { + + + @Test + public void testResultsFromLaunchResultsShouldConvertToLaunchStatisticExport() { + final long resultCount = 1; + final LaunchResults launchResults = mock(LaunchResults.class); + final TestResult passed = createTestResult(Status.PASSED); + final TestResult failed = createTestResult(Status.FAILED); + final TestResult broken = createTestResult(Status.BROKEN); + final TestResult skipped = createTestResult(Status.SKIPPED); + final TestResult unknown = createTestResult(Status.UNKNOWN); + + final Set results = new HashSet<>(Arrays.asList(passed, failed, broken, skipped, unknown)); + when(launchResults.getAllResults()).thenReturn(results); + + final Statistic statistic = JiraExportUtils.getStatistic(Arrays.asList(launchResults)); + List launchStatisticExports = JiraExportUtils.convertStatistics(statistic); + + assertThat(launchStatisticExports).isNotEmpty().hasSize(5); + + assertThat(launchStatisticExports).extracting(LaunchStatisticExport::getStatus) + .contains(Status.PASSED.value(), + Status.FAILED.value(), + Status.SKIPPED.value(), + Status.BROKEN.value(), + Status.UNKNOWN.value()); + + assertThat(launchStatisticExports).extracting(LaunchStatisticExport::getColor) + .contains(ResultStatus.FAILED.color(), + ResultStatus.PASSED.color(), + ResultStatus.SKIPPED.color(), + ResultStatus.BROKEN.color(), + ResultStatus.UNKNOWN.color()); + launchStatisticExports.forEach(launchStatisticExport -> assertThat(launchStatisticExport.getCount()).isEqualTo(resultCount)); + + } + + @Test + public void emptyTestResultsShouldBeIgnoredWhenConvertingToLaunchStatisticExport() { + final long resultCount = 1; + final LaunchResults launchResults = mock(LaunchResults.class); + final TestResult passed = createTestResult(Status.PASSED); + final TestResult failed = createTestResult(Status.FAILED); + final TestResult unknown = createTestResult(Status.UNKNOWN); + + final Set results = new HashSet<>(Arrays.asList(passed, failed, unknown)); + when(launchResults.getAllResults()).thenReturn(results); + + final Statistic statistic = JiraExportUtils.getStatistic(Arrays.asList(launchResults)); + final List launchStatisticExports = JiraExportUtils.convertStatistics(statistic); + + assertThat(launchStatisticExports).isNotEmpty().hasSize(3); + assertThat(launchStatisticExports).extracting(LaunchStatisticExport::getStatus) + .contains(Status.PASSED.value(), + Status.FAILED.value(), + Status.UNKNOWN.value()) + .doesNotContain(Status.SKIPPED.value(), Status.BROKEN.value()); + + assertThat(launchStatisticExports).extracting(LaunchStatisticExport::getColor) + .contains(ResultStatus.FAILED.color(), + ResultStatus.PASSED.color(), + ResultStatus.UNKNOWN.color()) + .doesNotContain(ResultStatus.SKIPPED.color(), ResultStatus.BROKEN.color()); + + launchStatisticExports.forEach(launchStatisticExport -> assertThat(launchStatisticExport.getCount()).isEqualTo(resultCount)); + } + + +} diff --git a/plugins/jira-plugin/src/test/java/io/qameta/allure/jira/JiraLaunchExportPluginTest.java b/plugins/jira-plugin/src/test/java/io/qameta/allure/jira/JiraLaunchExportPluginTest.java index 3c18bc1ed..fe8dd6f72 100644 --- a/plugins/jira-plugin/src/test/java/io/qameta/allure/jira/JiraLaunchExportPluginTest.java +++ b/plugins/jira-plugin/src/test/java/io/qameta/allure/jira/JiraLaunchExportPluginTest.java @@ -18,6 +18,7 @@ import io.qameta.allure.core.Configuration; import io.qameta.allure.core.LaunchResults; import io.qameta.allure.entity.ExecutorInfo; +import io.qameta.allure.entity.Statistic; import io.qameta.allure.entity.Status; import io.qameta.allure.entity.TestResult; import org.apache.commons.lang3.RandomStringUtils; @@ -31,18 +32,19 @@ import java.util.Optional; import java.util.Set; + import static io.qameta.allure.jira.TestData.createTestResult; -import static io.qameta.allure.jira.TestData.mockJiraService; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyList; import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; + class JiraLaunchExportPluginTest { - private static final List ISSUES = Arrays.asList("ALLURE-1", "ALLURE-2"); @Test void shouldExportLaunchToJira() { @@ -55,13 +57,14 @@ void shouldExportLaunchToJira() { final Set results = new HashSet<>(Arrays.asList(passed, failed, broken, skipped, unknown)); when(launchResults.getAllResults()).thenReturn(results); + final Statistic statistic = JiraExportUtils.getStatistic(Arrays.asList(launchResults)); + final List launchStatisticExports = JiraExportUtils.convertStatistics(statistic); final ExecutorInfo executorInfo = new ExecutorInfo() .setBuildName(RandomStringUtils.random(10)) .setReportUrl(RandomStringUtils.random(10)); when(launchResults.getExtra("executor")).thenReturn(Optional.of(executorInfo)); - - final JiraService service = mockJiraService(); + final JiraService service = TestData.mockJiraService(); final JiraExportPlugin jiraLaunchExportPlugin = new JiraExportPlugin( true, "ALLURE-1,ALLURE-2", @@ -74,18 +77,14 @@ void shouldExportLaunchToJira() { Paths.get("/") ); - verify(service, times(1)).createJiraLaunch(any(JiraLaunch.class)); - verify(service).createJiraLaunch(argThat(launch -> launch.getIssueKeys().size() == 2)); - verify(service).createJiraLaunch(argThat(launch -> ISSUES.equals(launch.getIssueKeys()))); + verify(service, times(1)).createJiraLaunch(any(JiraLaunch.class), anyList()); - verify(service).createJiraLaunch(argThat(launch -> executorInfo.getBuildName().equals(launch.getName()))); - verify(service).createJiraLaunch(argThat(launch -> executorInfo.getReportUrl().equals(launch.getUrl()))); + verify(service).createJiraLaunch(argThat(launch -> executorInfo.getBuildName().equals(launch.getExternalId())), anyList()); + verify(service).createJiraLaunch(argThat(launch -> executorInfo.getReportUrl().equals(launch.getUrl())), anyList()); + verify(service).createJiraLaunch(argThat(launch -> launchStatisticExports.equals(launch.getStatistic())), anyList()); + verify(service).createJiraLaunch(argThat(launch -> executorInfo.getBuildName().equals(launch.getName())), anyList()); + verify(service).createJiraLaunch(any(JiraLaunch.class), argThat(issues -> !issues.isEmpty())); - verify(service).createJiraLaunch(argThat(launch -> launch.getPassed() == 1)); - verify(service).createJiraLaunch(argThat(launch -> launch.getFailed() == 1)); - verify(service).createJiraLaunch(argThat(launch -> launch.getBroken() == 1)); - verify(service).createJiraLaunch(argThat(launch -> launch.getSkipped() == 1)); - verify(service).createJiraLaunch(argThat(launch -> launch.getUnknown() == 1)); } diff --git a/plugins/jira-plugin/src/test/java/io/qameta/allure/jira/JiraTestResultExportPluginTest.java b/plugins/jira-plugin/src/test/java/io/qameta/allure/jira/JiraTestResultExportPluginTest.java index d07bc2e7c..28ab2f6d9 100644 --- a/plugins/jira-plugin/src/test/java/io/qameta/allure/jira/JiraTestResultExportPluginTest.java +++ b/plugins/jira-plugin/src/test/java/io/qameta/allure/jira/JiraTestResultExportPluginTest.java @@ -17,35 +17,38 @@ import io.qameta.allure.core.Configuration; import io.qameta.allure.core.LaunchResults; + import io.qameta.allure.entity.ExecutorInfo; import io.qameta.allure.entity.Link; import io.qameta.allure.entity.Status; import io.qameta.allure.entity.TestResult; +import io.qameta.allure.entity.Time; import org.apache.commons.lang3.RandomStringUtils; import org.junit.jupiter.api.Test; import java.nio.file.Paths; -import java.util.Arrays; + import java.util.Collections; import java.util.HashSet; import java.util.List; -import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; +import static io.qameta.allure.jira.TestData.ISSUES; import static io.qameta.allure.jira.TestData.createTestResult; import static io.qameta.allure.jira.TestData.mockJiraService; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.argThat; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; + class JiraTestResultExportPluginTest { - private static final List ISSUES = Arrays.asList("ALLURE-1", "ALLURE_2"); @Test void shouldExportTestResultToJira() { @@ -55,7 +58,8 @@ void shouldExportTestResultToJira() { .collect(Collectors.toList()); final TestResult testResult = createTestResult(Status.PASSED) - .setLinks(links); + .setLinks(links) + .setTime(new Time().setStop(90L)); final Set results = new HashSet<>(Collections.singletonList(testResult)); when(launchResults.getAllResults()).thenReturn(results); @@ -77,16 +81,20 @@ void shouldExportTestResultToJira() { Collections.singletonList(launchResults), Paths.get("/") ); - verify(service, times(1)).createJiraLaunch(any(JiraLaunch.class)); - verify(service).createJiraLaunch(argThat(launch -> executorInfo.getBuildName().equals(launch.getName()))); - verify(service).createJiraLaunch(argThat(launch -> executorInfo.getReportUrl().equals(launch.getUrl()))); - - verify(service, times(1)).createTestResult(any(JiraTestResult.class)); - verify(service).createTestResult(argThat(result -> result.getIssueKeys().size() == 2)); - verify(service).createTestResult(argThat(result -> testResult.getName().equals(result.getName()))); - verify(service).createTestResult(argThat(result -> testResult.getStatus().toString().equals(result.getStatus()))); - verify(service).createTestResult(argThat(result -> result.getUrl().contains(testResult.getUid()))); - verify(service).createTestResult(argThat(result -> Objects.nonNull(result.getExternalId()))); + + + verify(service, times(1)).createTestResult(any(JiraTestResult.class), eq(ISSUES)); + verify(service).createTestResult(argThat(result -> result.getExternalId().equals(testResult.getUid())), eq(ISSUES)); + verify(service).createTestResult(argThat(result -> result.getTestCaseId().equals(testResult.getUid())), eq(ISSUES)); + verify(service).createTestResult(argThat(result -> result.getHistoryKey().equals(testResult.getHistoryId())), eq(ISSUES)); + verify(service).createTestResult(argThat(result -> result.getUrl().contains(testResult.getUid())), eq(ISSUES)); + verify(service).createTestResult(argThat(result -> result.getName().equals(testResult.getName())), eq(ISSUES)); + verify(service).createTestResult(argThat(result -> result.getStatus().equals(testResult.getStatus().toString())), eq(ISSUES)); + verify(service).createTestResult(argThat(result -> result.getColor().equals(ResultStatus.PASSED.color())), eq(ISSUES)); + verify(service).createTestResult(argThat(result -> result.getDate() == testResult.getTime().getStop().longValue()), eq(ISSUES)); + verify(service).createTestResult(argThat(result -> result.getLaunchUrl().equals(executorInfo.getReportUrl())), eq(ISSUES)); + verify(service).createTestResult(argThat(result -> result.getLaunchName().equals(executorInfo.getBuildName())), eq(ISSUES)); + verify(service).createTestResult(argThat(result -> result.getLaunchExternalId().equals(executorInfo.getBuildName())), eq(ISSUES)); } diff --git a/plugins/jira-plugin/src/test/java/io/qameta/allure/jira/TestData.java b/plugins/jira-plugin/src/test/java/io/qameta/allure/jira/TestData.java index 45ed51850..0737fee6b 100644 --- a/plugins/jira-plugin/src/test/java/io/qameta/allure/jira/TestData.java +++ b/plugins/jira-plugin/src/test/java/io/qameta/allure/jira/TestData.java @@ -18,37 +18,55 @@ import io.qameta.allure.entity.Status; import io.qameta.allure.entity.TestResult; import org.apache.commons.lang3.RandomStringUtils; -import org.apache.commons.lang3.RandomUtils; + +import java.util.Arrays; +import java.util.List; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyList; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; + public final class TestData { + public static final List ISSUES = Arrays.asList("ALLURE-1", "ALLURE_2"); - private TestData(){ + private TestData() { } public static JiraService mockJiraService() { final JiraService service = mock(JiraService.class); - when(service.createJiraLaunch(any(JiraLaunch.class))).thenAnswer(i -> { - final JiraLaunch launch = i.getArgument(0); - launch.setExternalId(String.valueOf(RandomUtils.nextInt())); - return launch; - }); - when(service.createTestResult(any(JiraTestResult.class))).thenAnswer(i -> { - final JiraTestResult testResult = i.getArgument(0); - testResult.setId(RandomUtils.nextInt()); - return testResult; - }); + when(service.createJiraLaunch(any(JiraLaunch.class), anyList())).thenAnswer(invocation -> + Arrays.asList( + new JiraExportResult().setExternalId("ALLURE-1") + .setIssueKey("ALLURE-1") + .setStatus("ok"), + new JiraExportResult().setExternalId("ALLURE-2") + .setIssueKey("ALLURE-2") + .setStatus("ok") + ) + ); + when(service.createTestResult(any(JiraTestResult.class), anyList())).thenAnswer(i -> + Arrays.asList( + new JiraExportResult().setExternalId("ALLURE-1") + .setIssueKey("ALLURE-1") + .setStatus("ok"), + new JiraExportResult().setExternalId("ALLURE-2") + .setIssueKey("ALLURE-2") + .setStatus("ok") + )); return service; } + + public static TestResult createTestResult(final Status status) { return new TestResult() .setUid(RandomStringUtils.random(10)) .setName(RandomStringUtils.random(10)) + .setHistoryId(RandomStringUtils.random(9)) .setStatus(status); } + }