-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
109 additions
and
134 deletions.
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
...6/src/main/java/com/epam/reportportal/example/cucumber6/attributes/AttributeReporter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
48 changes: 0 additions & 48 deletions
48
...main/java/com/epam/reportportal/example/cucumber6/attributes/CustomAttributeReporter.java
This file was deleted.
Oops, something went wrong.
54 changes: 0 additions & 54 deletions
54
...in/java/com/epam/reportportal/example/cucumber6/attributes/KeyValueAttributeReporter.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 0 additions & 30 deletions
30
...rc/test/java/com/epam/reportportal/example/cucumber6/RunAttributesCustomReporterTest.java
This file was deleted.
Oops, something went wrong.