Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Nov 18, 2024
1 parent a066f1d commit 1ade5c0
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 134 deletions.
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* 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<String, String> getCustomLaunchAttributes() {
return Collections.singletonMap("custom-launch-attribute-name", "custom-launch-attribute-value");
}

/**
* Override <code>buildStartLaunchRq</code> 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 <code>buildStartScenarioRequest</code> 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;
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

This file was deleted.

0 comments on commit 1ade5c0

Please sign in to comment.