From 1ade5c0cca87f5f8696ae8e61ad812f6dfe0a2c6 Mon Sep 17 00:00:00 2001 From: Vadzim Hushchanskou Date: Mon, 18 Nov 2024 10:54:52 +0300 Subject: [PATCH] Update examples --- .../attributes/AttributeReporter.java | 107 ++++++++++++++++++ .../attributes/CustomAttributeReporter.java | 48 -------- .../attributes/KeyValueAttributeReporter.java | 54 --------- .../example/cucumber6/BasicReRunTest.java | 2 +- .../example/cucumber6/BasicRunTest.java | 2 +- .../RunAttributesCustomReporterTest.java | 30 ----- 6 files changed, 109 insertions(+), 134 deletions(-) create mode 100644 example-cucumber6/src/main/java/com/epam/reportportal/example/cucumber6/attributes/AttributeReporter.java delete mode 100644 example-cucumber6/src/main/java/com/epam/reportportal/example/cucumber6/attributes/CustomAttributeReporter.java delete mode 100644 example-cucumber6/src/main/java/com/epam/reportportal/example/cucumber6/attributes/KeyValueAttributeReporter.java delete mode 100644 example-cucumber6/src/test/java/com/epam/reportportal/example/cucumber6/RunAttributesCustomReporterTest.java diff --git a/example-cucumber6/src/main/java/com/epam/reportportal/example/cucumber6/attributes/AttributeReporter.java b/example-cucumber6/src/main/java/com/epam/reportportal/example/cucumber6/attributes/AttributeReporter.java new file mode 100644 index 00000000..fa589791 --- /dev/null +++ b/example-cucumber6/src/main/java/com/epam/reportportal/example/cucumber6/attributes/AttributeReporter.java @@ -0,0 +1,107 @@ +/* + * Copyright 2022 EPAM Systems + * + * 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 com.epam.reportportal.example.cucumber6.attributes; + +import com.epam.reportportal.cucumber.ScenarioReporter; +import com.epam.reportportal.listeners.ListenerParameters; +import com.epam.ta.reportportal.ws.model.StartTestItemRQ; +import com.epam.ta.reportportal.ws.model.attribute.ItemAttributeResource; +import com.epam.ta.reportportal.ws.model.attribute.ItemAttributesRQ; +import com.epam.ta.reportportal.ws.model.launch.StartLaunchRQ; +import io.cucumber.plugin.event.TestCase; +import org.apache.commons.lang3.StringUtils; + +import javax.annotation.Nonnull; +import java.net.URI; +import java.util.Collections; +import java.util.Date; +import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; + +/** + * Attribute Reporter to demonstrate attribute customization for different cases. + *

+ * Case 1: Add custom attributes to the Launch on its start. + * Case 2: Process key-value attributes for scenarios. + */ +public class AttributeReporter extends ScenarioReporter { + public static final String ATTRIBUTE_VALUE_SEPARATOR = ":"; + public static final String TEST_TRACKING_TICKET_PREFIX = "JIRA"; + + /** + * Use this method if you need to add custom attributes on your launch before it start. + * + * @return a map of attributes + */ + private Map getCustomLaunchAttributes() { + return Collections.singletonMap("custom-launch-attribute-name", "custom-launch-attribute-value"); + } + + /** + * Override buildStartLaunchRq method to add custom attributes to the launch. + * + * @param startTime start time of the launch + * @param parameters listener parameters + * @return StartLaunchRQ object + */ + @Override + protected StartLaunchRQ buildStartLaunchRq(Date startTime, ListenerParameters parameters) { + StartLaunchRQ rq = super.buildStartLaunchRq(startTime, parameters); + getCustomLaunchAttributes().forEach((key, value) -> rq.getAttributes().add(new ItemAttributesRQ(key, value))); + return rq; + } + + /** + * Process all attributes for scenarios. + * + * @param rq StartTestItemRQ object + */ + private static void processAttributes(StartTestItemRQ rq) { + rq.setAttributes(rq.getAttributes().stream().map(ItemAttributeResource::getValue).filter(StringUtils::isNotBlank).map(attr -> { + if (attr.contains(ATTRIBUTE_VALUE_SEPARATOR)) { + // split attribute value by separator and create an attribute object with key-value + String[] parts = attr.split(ATTRIBUTE_VALUE_SEPARATOR, 2); + return new ItemAttributesRQ(parts[0].trim(), parts[1].trim()); + } else if (attr.startsWith(TEST_TRACKING_TICKET_PREFIX)) { + // set Test Case ID if attribute starts with a specific prefix + rq.setTestCaseId(attr); + return null; + } else { + // Leave attribute as Tag in all other cases + return new ItemAttributesRQ(null, attr); + } + }).filter(Objects::nonNull).collect(Collectors.toUnmodifiableSet())); + } + + /** + * Override buildStartScenarioRequest method to process Test Case ID and key-value attributes for scenarios. + * + * @param testCase test case + * @param name scenario name + * @param uri scenario uri + * @param line scenario line + * @return StartTestItemRQ object + */ + @Override + @Nonnull + protected StartTestItemRQ buildStartScenarioRequest(@Nonnull TestCase testCase, @Nonnull String name, @Nonnull URI uri, int line) { + StartTestItemRQ rq = super.buildStartScenarioRequest(testCase, name, uri, line); + processAttributes(rq); + return rq; + } +} diff --git a/example-cucumber6/src/main/java/com/epam/reportportal/example/cucumber6/attributes/CustomAttributeReporter.java b/example-cucumber6/src/main/java/com/epam/reportportal/example/cucumber6/attributes/CustomAttributeReporter.java deleted file mode 100644 index 7f398e17..00000000 --- a/example-cucumber6/src/main/java/com/epam/reportportal/example/cucumber6/attributes/CustomAttributeReporter.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2022 EPAM Systems - * - * 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 com.epam.reportportal.example.cucumber6.attributes; - -import com.epam.reportportal.cucumber.ScenarioReporter; -import com.epam.reportportal.listeners.ListenerParameters; -import com.epam.ta.reportportal.ws.model.attribute.ItemAttributesRQ; -import com.epam.ta.reportportal.ws.model.launch.StartLaunchRQ; - -import java.util.Collections; -import java.util.Date; -import java.util.Map; - -/** - * Attribute Reporter to demonstrate attribute customization for different cases - */ -public class CustomAttributeReporter extends ScenarioReporter { - - /** - * Use this method if you need to add custom attributes on your launch before it start. - * - * @return a map of attributes - */ - private Map getCustomLaunchAttributes() { - return Collections.singletonMap("custom-launch-attribute-name", "custom-launch-attribute-value"); - } - - @Override - protected StartLaunchRQ buildStartLaunchRq(Date startTime, ListenerParameters parameters) { - StartLaunchRQ rq = super.buildStartLaunchRq(startTime, parameters); - getCustomLaunchAttributes().forEach((key, value) -> rq.getAttributes().add(new ItemAttributesRQ(key, value))); - return rq; - } -} diff --git a/example-cucumber6/src/main/java/com/epam/reportportal/example/cucumber6/attributes/KeyValueAttributeReporter.java b/example-cucumber6/src/main/java/com/epam/reportportal/example/cucumber6/attributes/KeyValueAttributeReporter.java deleted file mode 100644 index 461a4a93..00000000 --- a/example-cucumber6/src/main/java/com/epam/reportportal/example/cucumber6/attributes/KeyValueAttributeReporter.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright 2022 EPAM Systems - * - * 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 com.epam.reportportal.example.cucumber6.attributes; - -import com.epam.reportportal.cucumber.ScenarioReporter; -import com.epam.ta.reportportal.ws.model.StartTestItemRQ; -import com.epam.ta.reportportal.ws.model.attribute.ItemAttributeResource; -import com.epam.ta.reportportal.ws.model.attribute.ItemAttributesRQ; -import io.cucumber.plugin.event.TestCase; - -import javax.annotation.Nonnull; -import java.net.URI; -import java.util.Objects; -import java.util.stream.Collectors; - -/** - * Attribute Reporter to demonstrate attribute customization for different cases. - */ -public class KeyValueAttributeReporter extends ScenarioReporter { - public static final String ATTRIBUTE_VALUE_SEPARATOR = ":"; - - private static void processKeyValueAttributes(StartTestItemRQ rq) { - rq.setAttributes(rq.getAttributes().stream().map(ItemAttributeResource::getValue).filter(Objects::nonNull).map(attr -> { - if (attr.contains(ATTRIBUTE_VALUE_SEPARATOR)) { - String[] parts = attr.split(ATTRIBUTE_VALUE_SEPARATOR, 2); - return new ItemAttributesRQ(parts[0].trim(), parts[1].trim()); - } else { - return new ItemAttributesRQ(null, attr); - } - }).collect(Collectors.toUnmodifiableSet())); - } - - @Override - @Nonnull - protected StartTestItemRQ buildStartScenarioRequest(@Nonnull TestCase testCase, @Nonnull String name, @Nonnull URI uri, int line) { - StartTestItemRQ rq = super.buildStartScenarioRequest(testCase, name, uri, line); - processKeyValueAttributes(rq); - return rq; - } -} diff --git a/example-cucumber6/src/test/java/com/epam/reportportal/example/cucumber6/BasicReRunTest.java b/example-cucumber6/src/test/java/com/epam/reportportal/example/cucumber6/BasicReRunTest.java index 1594a251..05eab18f 100644 --- a/example-cucumber6/src/test/java/com/epam/reportportal/example/cucumber6/BasicReRunTest.java +++ b/example-cucumber6/src/test/java/com/epam/reportportal/example/cucumber6/BasicReRunTest.java @@ -6,7 +6,7 @@ @RunWith(Cucumber.class) @CucumberOptions( - plugin = { "pretty", "html:report.html", "com.epam.reportportal.example.cucumber6.attributes.KeyValueAttributeReporter" }, + plugin = { "pretty", "html:report.html", "com.epam.reportportal.example.cucumber6.attributes.AttributeReporter" }, features = "@rerun.txt", glue = "com.epam.reportportal.example.cucumber6.glue") public class BasicReRunTest { diff --git a/example-cucumber6/src/test/java/com/epam/reportportal/example/cucumber6/BasicRunTest.java b/example-cucumber6/src/test/java/com/epam/reportportal/example/cucumber6/BasicRunTest.java index ede4b03f..9c8b8c79 100644 --- a/example-cucumber6/src/test/java/com/epam/reportportal/example/cucumber6/BasicRunTest.java +++ b/example-cucumber6/src/test/java/com/epam/reportportal/example/cucumber6/BasicRunTest.java @@ -6,7 +6,7 @@ @RunWith(Cucumber.class) @CucumberOptions( - plugin = { "pretty", "html:report.html", "com.epam.reportportal.example.cucumber6.attributes.KeyValueAttributeReporter", + plugin = { "pretty", "html:report.html", "com.epam.reportportal.example.cucumber6.attributes.AttributeReporter", "rerun:rerun.txt" }, features = "src/test/resources/features", tags = "not @ignore", diff --git a/example-cucumber6/src/test/java/com/epam/reportportal/example/cucumber6/RunAttributesCustomReporterTest.java b/example-cucumber6/src/test/java/com/epam/reportportal/example/cucumber6/RunAttributesCustomReporterTest.java deleted file mode 100644 index 5fdfc6d4..00000000 --- a/example-cucumber6/src/test/java/com/epam/reportportal/example/cucumber6/RunAttributesCustomReporterTest.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2021 EPAM Systems - * - * 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 com.epam.reportportal.example.cucumber6; - -import io.cucumber.junit.Cucumber; -import io.cucumber.junit.CucumberOptions; -import org.junit.runner.RunWith; - -@RunWith(Cucumber.class) -@CucumberOptions( - plugin = { "pretty", "html:report.html", "com.epam.reportportal.example.cucumber6.attributes.CustomAttributeReporter" }, - features = "src/test/resources/features/attributes/belly.feature", - tags = "not @ignore", - glue = "com.epam.reportportal.example.cucumber6.attributes") -public class RunAttributesCustomReporterTest { -}