Skip to content

Commit

Permalink
Merge pull request #127 from reportportal/develop
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
HardNorth authored Apr 4, 2024
2 parents f6a10f8 + 896537f commit d36dca1
Show file tree
Hide file tree
Showing 12 changed files with 314 additions and 16 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Changelog

## [Unreleased]
### Added
- `@Description` annotation support, by @oleksandr-fomenko
### Changed
- Client version updated on [5.2.13](https://github.com/reportportal/client-java/releases/tag/5.2.13), by @HardNorth
### Removed
- JSR 305 dependency, by @HardNorth

## [5.3.0]
### Added
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ If you prefer using **Logback** logging library, add following dependencies:
<dependency>
<groupId>com.epam.reportportal</groupId>
<artifactId>logger-java-logback</artifactId>
<version>5.1.1</version>
<version>5.2.2</version>
</dependency>
```
> Up to date version could be found [here](https://search.maven.org/search?q=g:%22com.epam.reportportal%22%20AND%20a:%22logger-java-logback%22)
Expand All @@ -168,7 +168,7 @@ If you prefer using **Log4j** logging library, add following dependencies:
<dependency>
<groupId>com.epam.reportportal</groupId>
<artifactId>logger-java-log4j</artifactId>
<version>5.1.4</version>
<version>5.2.2</version>
</dependency>
```
> Up to date version could be found [here](https://search.maven.org/search?q=g:%22com.epam.reportportal%22%20AND%20a:%22logger-java-log4j%22)
Expand Down Expand Up @@ -383,7 +383,7 @@ The `junit.jupiter.extensions.autodetection.enabled = true` configuration parame
<dependency>
<groupId>com.epam.reportportal</groupId>
<artifactId>logger-java-log4j</artifactId>
<version>5.1.4</version>
<version>5.2.2</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -488,7 +488,7 @@ repositories {
}
dependencies {
implementation 'com.epam.reportportal:logger-java-log4j:5.1.4'
implementation 'com.epam.reportportal:logger-java-log4j:5.2.2'
implementation 'org.apache.logging.log4j:log4j-api:2.17.1'
implementation 'org.apache.logging.log4j:log4j-core:2.17.1'
implementation 'com.epam.reportportal:agent-java-junit5:5.3.0'
Expand Down
8 changes: 4 additions & 4 deletions README_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ If you prefer using **Logback** logging library, add following dependencies:
<dependency>
<groupId>com.epam.reportportal</groupId>
<artifactId>logger-java-logback</artifactId>
<version>5.1.1</version>
<version>5.2.2</version>
</dependency>
```
> Up to date version could be found [here](https://search.maven.org/search?q=g:%22com.epam.reportportal%22%20AND%20a:%22logger-java-logback%22)
Expand All @@ -168,7 +168,7 @@ If you prefer using **Log4j** logging library, add following dependencies:
<dependency>
<groupId>com.epam.reportportal</groupId>
<artifactId>logger-java-log4j</artifactId>
<version>5.1.4</version>
<version>5.2.2</version>
</dependency>
```
> Up to date version could be found [here](https://search.maven.org/search?q=g:%22com.epam.reportportal%22%20AND%20a:%22logger-java-log4j%22)
Expand Down Expand Up @@ -383,7 +383,7 @@ The `junit.jupiter.extensions.autodetection.enabled = true` configuration parame
<dependency>
<groupId>com.epam.reportportal</groupId>
<artifactId>logger-java-log4j</artifactId>
<version>5.1.4</version>
<version>5.2.2</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -488,7 +488,7 @@ repositories {
}
dependencies {
implementation 'com.epam.reportportal:logger-java-log4j:5.1.4'
implementation 'com.epam.reportportal:logger-java-log4j:5.2.2'
implementation 'org.apache.logging.log4j:log4j-api:2.17.1'
implementation 'org.apache.logging.log4j:log4j-core:2.17.1'
implementation 'com.epam.reportportal:agent-java-junit5:$LATEST_VERSION'
Expand Down
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ repositories {
}

dependencies {
api 'com.epam.reportportal:client-java:5.2.5'
api 'com.epam.reportportal:client-java:5.2.13'

compileOnly "org.junit.jupiter:junit-jupiter-api:${junit_version}"
implementation 'com.google.code.findbugs:jsr305:3.0.2'
implementation 'org.slf4j:slf4j-api:2.0.7'

testImplementation 'com.epam.reportportal:agent-java-test-utils:0.0.3'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.epam.reportportal.junit5;

import com.epam.reportportal.annotations.Description;
import com.epam.reportportal.annotations.ParameterKey;
import com.epam.reportportal.annotations.TestCaseId;
import com.epam.reportportal.annotations.attribute.Attributes;
Expand Down Expand Up @@ -350,8 +351,9 @@ public void afterTestExecution(ExtensionContext context) {
@Override
public void testDisabled(ExtensionContext context, Optional<String> reason) {
if (Boolean.parseBoolean(System.getProperty("reportDisabledTests"))) {
String description = reason.orElse(createStepDescription(context));
startTestItem(context, Collections.emptyList(), STEP, description, Calendar.getInstance().getTime());
final ItemType itemType = STEP;
String description = reason.orElse(createStepDescription(context, itemType));
startTestItem(context, Collections.emptyList(), itemType, description, Calendar.getInstance().getTime());
finishTest(context, SKIPPED);
}
}
Expand Down Expand Up @@ -455,7 +457,7 @@ protected void startTestItem(ExtensionContext context, ItemType type) {
* @param type a type of the item
*/
protected void startTestItem(ExtensionContext context, List<Object> arguments, ItemType type) {
startTestItem(context, arguments, type, createStepDescription(context), Calendar.getInstance().getTime());
startTestItem(context, arguments, type, createStepDescription(context, type), Calendar.getInstance().getTime());
}

/**
Expand Down Expand Up @@ -822,8 +824,41 @@ protected String createConfigurationName(@Nonnull Class<?> testClass, @Nonnull M
* @return Test/Step Description being sent to ReportPortal
*/
@SuppressWarnings("unused")
protected String createStepDescription(ExtensionContext context) {
return "";
protected String createStepDescription(ExtensionContext context, final ItemType itemType) {
String defaultValue = "";
if (itemType != STEP) {
return defaultValue;
}
Optional<Method> optionalMethod = getOptionalTestMethod(context);
if (!optionalMethod.isPresent()){
return defaultValue;
}
Method method = optionalMethod.get();

Description descriptionFromMethod = method.getAnnotation(Description.class);
if(descriptionFromMethod != null){
return descriptionFromMethod.value();
}

Description descriptionFromClass = method.getDeclaringClass().getAnnotation(Description.class);
if(descriptionFromClass != null){
return descriptionFromClass.value();
}

return defaultValue;
}

private Optional<Method> getOptionalTestMethod(ExtensionContext context){
Optional<Method> optionalMethod = context.getTestMethod();
if(!optionalMethod.isPresent()){
//if not present means that we are in the dynamic test, in this case we need to move to the parent context
Optional<ExtensionContext> parentContext = context.getParent();
if(!parentContext.isPresent()){
return Optional.empty();
}
return parentContext.get().getTestMethod();
}
return optionalMethod;
}

/**
Expand Down Expand Up @@ -853,7 +888,8 @@ protected void reportSkippedStep(ReflectiveInvocationContext<Method> invocationC
// to fix item ordering when @AfterEach starts in the same millisecond as skipped test
skipStartTime = new Date(skipStartTime.getTime() - 1);
}
startTestItem(context, invocationContext.getArguments(), STEP, createStepDescription(context), skipStartTime);
final ItemType itemType = STEP;
startTestItem(context, invocationContext.getArguments(), itemType, createStepDescription(context, itemType), skipStartTime);
createSkippedSteps(context, throwable);
FinishTestItemRQ finishRq = buildFinishTestItemRq(context, SKIPPED);
finishRq.setIssue(Launch.NOT_ISSUE);
Expand Down
148 changes: 148 additions & 0 deletions src/test/java/com/epam/reportportal/junit5/DescriptionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package com.epam.reportportal.junit5;

import com.epam.reportportal.junit5.features.description.*;
import com.epam.reportportal.junit5.util.TestUtils;
import com.epam.reportportal.service.Launch;
import com.epam.reportportal.util.test.CommonUtils;
import com.epam.ta.reportportal.ws.model.StartTestItemRQ;
import io.reactivex.Maybe;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.mockito.ArgumentCaptor;
import org.mockito.stubbing.Answer;

import java.util.List;
import java.util.stream.Collectors;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

/**
* @author <a href="mailto:[email protected]">Oleksandr Fomenko</a>
*/
public class DescriptionTest {
public static class TestExtension extends ReportPortalExtension {
static Launch LAUNCH;

@Override
protected Launch getLaunch(ExtensionContext context) {
return LAUNCH;
}
}

@BeforeEach
public void setupMock() {
TestExtension.LAUNCH = mock(Launch.class);
when(TestExtension.LAUNCH.startTestItem(any())).thenAnswer((Answer<Maybe<String>>) invocation -> CommonUtils.createMaybeUuid());
when(TestExtension.LAUNCH.startTestItem(any(),
any()
)).thenAnswer((Answer<Maybe<String>>) invocation -> CommonUtils.createMaybeUuid());
}

@Test
void descriptionFromMethodAnnotationTest() {
TestUtils.runClasses(DescriptionAnnotatedMethodTest.class);

Launch launch = TestExtension.LAUNCH;

verify(launch, times(1)).startTestItem(any()); // Start parent Suite

ArgumentCaptor<StartTestItemRQ> captor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(launch, times(1)).startTestItem(notNull(), captor.capture()); // Start a test

StartTestItemRQ request = captor.getValue();
assertThat(request.getDescription(), equalTo(DescriptionAnnotatedMethodTest.TEST_DESCRIPTION_METHOD));
}

@Test
void descriptionFromClassAnnotationTest() {
TestUtils.runClasses(DescriptionAnnotatedClassTest.class);

Launch launch = TestExtension.LAUNCH;

verify(launch, times(1)).startTestItem(any()); // Start parent Suite

ArgumentCaptor<StartTestItemRQ> captor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(launch, times(1)).startTestItem(notNull(), captor.capture()); // Start a test

StartTestItemRQ request = captor.getValue();
assertThat(request.getDescription(), equalTo(DescriptionAnnotatedClassTest.TEST_DESCRIPTION_CLASS));
}

@Test
void descriptionFromBothMethodAndClassAnnotationTest() {
TestUtils.runClasses(DescriptionAnnotatedMethodAndClassTest.class);

Launch launch = TestExtension.LAUNCH;

verify(launch, times(1)).startTestItem(any()); // Start parent Suite

ArgumentCaptor<StartTestItemRQ> captor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(launch, times(1)).startTestItem(notNull(), captor.capture()); // Start a test

StartTestItemRQ request = captor.getValue();
//from both annotations the expected one should be taken from the method
assertThat(request.getDescription(), equalTo(DescriptionAnnotatedMethodTest.TEST_DESCRIPTION_METHOD));
}

@Test
void descriptionFromMethodAnnotationDynamicTest() {
TestUtils.runClasses(DescriptionAnnotatedMethodDynamicTest.class);

Launch launch = TestExtension.LAUNCH;
verify(launch, times(1)).startTestItem(any()); // Start parent Suite

ArgumentCaptor<StartTestItemRQ> captor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(launch, times(2)).startTestItem(notNull(), captor.capture()); // Start a test

List<String> testStepsDescription = captor.getAllValues()
.stream()
.filter(e -> e.getType().equals(ItemType.STEP.name()))
.map(StartTestItemRQ::getDescription)
.collect(Collectors.toList());

assertThat(testStepsDescription, hasItem(DescriptionAnnotatedMethodDynamicTest.TEST_DESCRIPTION_DYNAMIC_METHOD));
}

@Test
void descriptionFromClassAnnotationDynamicTest() {
TestUtils.runClasses(DescriptionAnnotatedClassDynamicTest.class);

Launch launch = TestExtension.LAUNCH;
verify(launch, times(1)).startTestItem(any()); // Start parent Suite

ArgumentCaptor<StartTestItemRQ> captor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(launch, times(2)).startTestItem(notNull(), captor.capture()); // Start a test

List<String> testStepsDescription = captor.getAllValues()
.stream()
.filter(e -> e.getType().equals(ItemType.STEP.name()))
.map(StartTestItemRQ::getDescription)
.collect(Collectors.toList());

assertThat(testStepsDescription, hasItem(DescriptionAnnotatedClassDynamicTest.TEST_DESCRIPTION_DYNAMIC_CLASS));
}

@Test
void descriptionFromBothMethodAndClassAnnotationDynamicTest() {
TestUtils.runClasses(DescriptionAnnotatedMethodAndClassDynamicTest.class);

Launch launch = TestExtension.LAUNCH;
verify(launch, times(1)).startTestItem(any()); // Start parent Suite

ArgumentCaptor<StartTestItemRQ> captor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(launch, times(2)).startTestItem(notNull(), captor.capture()); // Start a test

List<String> testStepsDescription = captor.getAllValues()
.stream()
.filter(e -> e.getType().equals(ItemType.STEP.name()))
.map(StartTestItemRQ::getDescription)
.collect(Collectors.toList());
//from both annotations the expected one should be taken from the method
assertThat(testStepsDescription, hasItem(DescriptionAnnotatedMethodDynamicTest.TEST_DESCRIPTION_DYNAMIC_METHOD));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.epam.reportportal.junit5.features.description;

import com.epam.reportportal.annotations.Description;
import com.epam.reportportal.junit5.DescriptionTest;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.extension.ExtendWith;

import java.util.stream.Stream;

import static org.junit.jupiter.api.DynamicTest.dynamicTest;

@ExtendWith(DescriptionTest.TestExtension.class)
@Description(DescriptionAnnotatedClassDynamicTest.TEST_DESCRIPTION_DYNAMIC_CLASS)
public class DescriptionAnnotatedClassDynamicTest {
public static final String TEST_DESCRIPTION_DYNAMIC_CLASS = "My test description on the dynamic class";
@TestFactory

Stream<DynamicTest> testForTestFactory() {
return Stream.of(dynamicTest("My dynamic test", () -> System.out.println("Inside dynamic test")));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.epam.reportportal.junit5.features.description;

import com.epam.reportportal.annotations.Description;
import com.epam.reportportal.junit5.DescriptionTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

@Description(DescriptionAnnotatedClassTest.TEST_DESCRIPTION_CLASS)
@ExtendWith(DescriptionTest.TestExtension.class)
public class DescriptionAnnotatedClassTest {
public static final String TEST_DESCRIPTION_CLASS = "My test description on the class";
@Test
public void testDescriptionTest() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.epam.reportportal.junit5.features.description;

import com.epam.reportportal.annotations.Description;
import com.epam.reportportal.junit5.DescriptionTest;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.extension.ExtendWith;

import java.util.stream.Stream;

import static org.junit.jupiter.api.DynamicTest.dynamicTest;

@ExtendWith(DescriptionTest.TestExtension.class)
@Description(DescriptionAnnotatedClassDynamicTest.TEST_DESCRIPTION_DYNAMIC_CLASS)
public class DescriptionAnnotatedMethodAndClassDynamicTest {
@TestFactory
@Description(DescriptionAnnotatedMethodDynamicTest.TEST_DESCRIPTION_DYNAMIC_METHOD)
Stream<DynamicTest> testForTestFactory() {
return Stream.of(dynamicTest("My dynamic test", () -> System.out.println("Inside dynamic test")));
}
}
Loading

0 comments on commit d36dca1

Please sign in to comment.