From f82464fc36eaa28b075f02bafe6c42b79e8d0328 Mon Sep 17 00:00:00 2001 From: Vadzim Hushchanskou Date: Wed, 30 Oct 2024 16:23:44 +0300 Subject: [PATCH] Add key-value attribute reporting example --- .../attributes/KeyValueAttributeReporter.java | 54 +++++++++++++++++++ .../RunAttributesCustomReporterTest.java | 7 ++- .../RunAttributesKeyValueReporterTest.java | 30 +++++++++++ .../features/attributes/key-value.feature | 7 +++ 4 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 example-cucumber6/src/main/java/com/epam/reportportal/example/cucumber6/attributes/KeyValueAttributeReporter.java create mode 100644 example-cucumber6/src/test/java/com/epam/reportportal/example/cucumber6/RunAttributesKeyValueReporterTest.java create mode 100644 example-cucumber6/src/test/resources/features/attributes/key-value.feature 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 new file mode 100644 index 00000000..461a4a93 --- /dev/null +++ b/example-cucumber6/src/main/java/com/epam/reportportal/example/cucumber6/attributes/KeyValueAttributeReporter.java @@ -0,0 +1,54 @@ +/* + * 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/RunAttributesCustomReporterTest.java b/example-cucumber6/src/test/java/com/epam/reportportal/example/cucumber6/RunAttributesCustomReporterTest.java index 78bbbfb1..5fdfc6d4 100644 --- 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 @@ -21,7 +21,10 @@ 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", tags = "not @ignore", glue = "com.epam.reportportal.example.cucumber6.attributes") +@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 { } diff --git a/example-cucumber6/src/test/java/com/epam/reportportal/example/cucumber6/RunAttributesKeyValueReporterTest.java b/example-cucumber6/src/test/java/com/epam/reportportal/example/cucumber6/RunAttributesKeyValueReporterTest.java new file mode 100644 index 00000000..24091fd2 --- /dev/null +++ b/example-cucumber6/src/test/java/com/epam/reportportal/example/cucumber6/RunAttributesKeyValueReporterTest.java @@ -0,0 +1,30 @@ +/* + * 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.KeyValueAttributeReporter" }, + features = "src/test/resources/features/attributes/key-value.feature", + tags = "not @ignore", + glue = "com.epam.reportportal.example.cucumber6.attributes") +public class RunAttributesKeyValueReporterTest { +} diff --git a/example-cucumber6/src/test/resources/features/attributes/key-value.feature b/example-cucumber6/src/test/resources/features/attributes/key-value.feature new file mode 100644 index 00000000..fb4e8c5d --- /dev/null +++ b/example-cucumber6/src/test/resources/features/attributes/key-value.feature @@ -0,0 +1,7 @@ +Feature: Belly + + @MyAttribute:MyValue + Scenario: a few cukes + Given I have 42 cukes in my belly + When I wait 1 hour + Then my belly should growl