From 486dad9774df6d288d70cbcc18e23b40ebfd4b8c Mon Sep 17 00:00:00 2001 From: anjiboddupally Date: Wed, 3 Jan 2024 20:14:46 +0000 Subject: [PATCH] Adding Parametrized tests to zephyrscale_result.json --- pom.xml | 32 ++++-- .../zephyrscale/junit/ExecutionListener.java | 88 +++++++++-------- .../builder/CustomFormatContainerBuilder.java | 51 ---------- .../builder/CustomFormatExecutionBuilder.java | 24 ----- .../builder/CustomFormatTestCaseBuilder.java | 29 ------ .../customformat/CustomFormatContainer.java | 51 +++++----- .../customformat/CustomFormatExecution.java | 45 ++++----- .../customformat/CustomFormatTestCase.java | 39 ++++---- .../decorator/TestDescriptionDecorator.java | 64 +++++++----- .../junit/file/CustomFormatFile.java | 10 +- .../junit/ExecutionListenerTest.java | 98 ++++++++++++------- 11 files changed, 240 insertions(+), 291 deletions(-) delete mode 100644 src/main/java/com/smartbear/zephyrscale/junit/builder/CustomFormatContainerBuilder.java delete mode 100644 src/main/java/com/smartbear/zephyrscale/junit/builder/CustomFormatExecutionBuilder.java delete mode 100644 src/main/java/com/smartbear/zephyrscale/junit/builder/CustomFormatTestCaseBuilder.java diff --git a/pom.xml b/pom.xml index b257f3e..dc00fda 100644 --- a/pom.xml +++ b/pom.xml @@ -6,13 +6,22 @@ com.smartbear zephyrscale-junit-integration - 2.0.1 + 2.0.2-SNAPSHOT jar Zephyr Scale JUnit Integration JUnit utility to integrate JUnit tests with Zephyr Scale. https://bitbucket.org/smartbeartm4j/zephyrscale-junit-integration + + UTF-8 + 4.11 + 2.13.4.1 + 3.0 + 1.7.25 + 1.18.26 + + The Apache License, Version 2.0 @@ -38,26 +47,35 @@ junit junit - 4.11 + ${junit.version} com.fasterxml.jackson.core jackson-databind - 2.13.4.1 + ${jackson-databind.version} org.apache.commons commons-lang3 - 3.0 + ${commons-lang3.version} org.slf4j slf4j-simple - 1.7.25 + ${slf4j-simple.version} + + + org.projectlombok + lombok + ${lombok.version} + provided + + + @@ -143,8 +161,4 @@ https://oss.sonatype.org/service/local/staging/deploy/maven2/ - - - UTF-8 - \ No newline at end of file diff --git a/src/main/java/com/smartbear/zephyrscale/junit/ExecutionListener.java b/src/main/java/com/smartbear/zephyrscale/junit/ExecutionListener.java index 83c5663..8117503 100644 --- a/src/main/java/com/smartbear/zephyrscale/junit/ExecutionListener.java +++ b/src/main/java/com/smartbear/zephyrscale/junit/ExecutionListener.java @@ -1,74 +1,82 @@ package com.smartbear.zephyrscale.junit; -import com.smartbear.zephyrscale.junit.annotation.TestCase; -import com.smartbear.zephyrscale.junit.builder.CustomFormatContainerBuilder; -import com.smartbear.zephyrscale.junit.decorator.TestDescriptionDecorator; +import static com.smartbear.zephyrscale.junit.file.CustomFormatFile.generateCustomFormatFile; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + import org.junit.runner.Description; import org.junit.runner.Result; import org.junit.runner.notification.Failure; import org.junit.runner.notification.RunListener; -import org.slf4j.Logger; -import java.util.ArrayList; -import java.util.List; - -import static com.smartbear.zephyrscale.junit.file.CustomFormatFile.generateCustomFormatFile; -import static org.apache.commons.lang3.StringUtils.isEmpty; +import com.smartbear.zephyrscale.junit.annotation.TestCase; +import com.smartbear.zephyrscale.junit.customformat.CustomFormatContainer; +import com.smartbear.zephyrscale.junit.customformat.CustomFormatExecution; +import com.smartbear.zephyrscale.junit.customformat.CustomFormatExecution.CustomFormatExecutionBuilder; +import com.smartbear.zephyrscale.junit.customformat.CustomFormatTestCase; +import com.smartbear.zephyrscale.junit.customformat.CustomFormatTestCase.CustomFormatTestCaseBuilder; public class ExecutionListener extends RunListener { - private final Logger logger = org.slf4j.LoggerFactory.getLogger(ExecutionListener.class); - - private CustomFormatContainerBuilder customFormatContainerBuilder; - private List errorMessages = new ArrayList<>(); + List executionList = null; + + private boolean status = true; @Override public void testRunStarted(Description description) throws Exception { + super.testRunStarted(description); - customFormatContainerBuilder = new CustomFormatContainerBuilder(); + executionList = Collections.synchronizedList(new ArrayList()); } @Override public void testFailure(Failure failure) throws Exception { + super.testFailure(failure); - - customFormatContainerBuilder.registerFailure(new TestDescriptionDecorator(failure.getDescription())); + status = false; + String message = failure.getException() != null && failure.getMessage() != null ? "Failed: " + failure.getMessage() : "Failed"; + executionList.add(getExecution(failure.getDescription(), message)); + } + + + @Override + public void testIgnored(Description description) throws Exception { + super.testIgnored(description); + status = false; + executionList.add(getExecution(description, "Ignored")); } @Override public void testFinished(Description description) throws Exception { super.testFinished(description); - checkTestCaseAnnotation(description); - - customFormatContainerBuilder.registerFinished(new TestDescriptionDecorator(description)); + if(status) + executionList.add(getExecution(description, "Passed")); + status = true; } @Override public void testRunFinished(Result result) throws Exception { super.testRunFinished(result); - - if (!errorMessages.isEmpty()) { - printErrorMessages(); - } else { - generateCustomFormatFile(customFormatContainerBuilder.getCustomFormatContainer()); - } - } - - private void checkTestCaseAnnotation(Description description) { - TestCase testCaseAnnotation = description != null ? description.getAnnotation(TestCase.class) : null; - - if (testCaseAnnotation != null && allFieldsAreEmpty(testCaseAnnotation)) { - errorMessages.add("[ERROR - Zephyr Scale] You must inform at least one parameter to TestCase annotation in method " + description.getDisplayName()); - } - } - - private boolean allFieldsAreEmpty(TestCase testCaseAnnotation) { - return isEmpty(testCaseAnnotation.key()) && isEmpty(testCaseAnnotation.name()); + + generateCustomFormatFile(CustomFormatContainer.builder().executions(executionList).build()); } - private void printErrorMessages() { - logger.error("[ERROR - Zephyr Scale] Zephyr Scale Results JSON File was not generated due to the following errors"); - errorMessages.forEach(errorMessage -> logger.error(errorMessage)); + private CustomFormatExecution getExecution(Description description, String result) { + + CustomFormatExecutionBuilder custFormatExecutionBuilder = CustomFormatExecution.builder(); + custFormatExecutionBuilder.source(description.getTestClass().getName() + "." + description.getMethodName()); + custFormatExecutionBuilder.result(result); + + CustomFormatTestCaseBuilder customFormatTestCaseBuilder = CustomFormatTestCase.builder(); + + TestCase testCase = description.getAnnotation(TestCase.class); + if(testCase != null) { + customFormatTestCaseBuilder.key(testCase.key()).name(testCase.name()); + custFormatExecutionBuilder.testCase(customFormatTestCaseBuilder.build()); + } + return custFormatExecutionBuilder.build(); } } diff --git a/src/main/java/com/smartbear/zephyrscale/junit/builder/CustomFormatContainerBuilder.java b/src/main/java/com/smartbear/zephyrscale/junit/builder/CustomFormatContainerBuilder.java deleted file mode 100644 index c6bcb61..0000000 --- a/src/main/java/com/smartbear/zephyrscale/junit/builder/CustomFormatContainerBuilder.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.smartbear.zephyrscale.junit.builder; - -import com.smartbear.zephyrscale.junit.decorator.TestDescriptionDecorator; -import com.smartbear.zephyrscale.junit.customformat.CustomFormatExecution; -import com.smartbear.zephyrscale.junit.customformat.CustomFormatContainer; - -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; - -import static com.smartbear.zephyrscale.junit.customformat.CustomFormatConstants.FAILED; -import static com.smartbear.zephyrscale.junit.customformat.CustomFormatConstants.PASSED; - -public class CustomFormatContainerBuilder { - - private CustomFormatContainer customFormatContainer = new CustomFormatContainer(); - private List failedTests = new ArrayList<>(); - - public void registerFailure(TestDescriptionDecorator testDescriptionDecorator) { - failedTests.add(testDescriptionDecorator); - } - - public void registerFinished(TestDescriptionDecorator testDescriptionDecorator) { - Optional optionalCustomFormatExecution = customFormatContainer.getExecutionBySource(testDescriptionDecorator.getSource()); - - if (optionalCustomFormatExecution.isPresent()) { - CustomFormatExecution customFormatExecution = optionalCustomFormatExecution.get(); - - updateResult(customFormatExecution, testDescriptionDecorator); - } else { - createResult(testDescriptionDecorator); - } - } - - private void createResult(TestDescriptionDecorator testDescriptionDecorator) { - CustomFormatExecution customFormatExecution = new CustomFormatExecutionBuilder().build(testDescriptionDecorator).getCustomFormatExecution(); - - updateResult(customFormatExecution, testDescriptionDecorator); - - customFormatContainer.addResult(customFormatExecution); - } - - private void updateResult(CustomFormatExecution customFormatExecution, TestDescriptionDecorator testDescriptionDecorator) { - String result = failedTests.contains(testDescriptionDecorator) ? FAILED : PASSED; - customFormatExecution.setResult(result); - } - - public CustomFormatContainer getCustomFormatContainer() { - return customFormatContainer; - } -} diff --git a/src/main/java/com/smartbear/zephyrscale/junit/builder/CustomFormatExecutionBuilder.java b/src/main/java/com/smartbear/zephyrscale/junit/builder/CustomFormatExecutionBuilder.java deleted file mode 100644 index 608a81a..0000000 --- a/src/main/java/com/smartbear/zephyrscale/junit/builder/CustomFormatExecutionBuilder.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.smartbear.zephyrscale.junit.builder; - -import com.smartbear.zephyrscale.junit.customformat.CustomFormatExecution; -import com.smartbear.zephyrscale.junit.decorator.TestDescriptionDecorator; - -public class CustomFormatExecutionBuilder { - - private final CustomFormatExecution customFormatExecution; - - public CustomFormatExecutionBuilder() { - customFormatExecution = new CustomFormatExecution(); - } - - public CustomFormatExecutionBuilder build(TestDescriptionDecorator testDescriptionDecorator) { - customFormatExecution.setTestCase(testDescriptionDecorator.getTestCase()); - customFormatExecution.setSource(testDescriptionDecorator.getSource()); - - return this; - } - - public CustomFormatExecution getCustomFormatExecution() { - return customFormatExecution; - } -} diff --git a/src/main/java/com/smartbear/zephyrscale/junit/builder/CustomFormatTestCaseBuilder.java b/src/main/java/com/smartbear/zephyrscale/junit/builder/CustomFormatTestCaseBuilder.java deleted file mode 100644 index dabab73..0000000 --- a/src/main/java/com/smartbear/zephyrscale/junit/builder/CustomFormatTestCaseBuilder.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.smartbear.zephyrscale.junit.builder; - -import com.smartbear.zephyrscale.junit.annotation.TestCase; -import com.smartbear.zephyrscale.junit.customformat.CustomFormatTestCase; -import org.junit.runner.Description; - -import static org.apache.commons.lang3.StringUtils.isNotEmpty; - -public class CustomFormatTestCaseBuilder { - private CustomFormatTestCase testCase; - - public CustomFormatTestCaseBuilder build(Description description) { - TestCase testCaseAnnotation = description.getAnnotation(TestCase.class); - String testCaseKey = testCaseAnnotation != null ? testCaseAnnotation.key() : null; - String testCaseName = testCaseAnnotation != null ? testCaseAnnotation.name(): null; - - if(testCaseKey != null || testCaseName != null) { - testCase = new CustomFormatTestCase(); - testCase.setKey(isNotEmpty(testCaseKey) ? testCaseKey : null); - testCase.setName(isNotEmpty(testCaseName) ? testCaseName : null); - } - - return this; - } - - public CustomFormatTestCase getTestCase() { - return testCase; - } -} diff --git a/src/main/java/com/smartbear/zephyrscale/junit/customformat/CustomFormatContainer.java b/src/main/java/com/smartbear/zephyrscale/junit/customformat/CustomFormatContainer.java index 9a4283b..42bf128 100644 --- a/src/main/java/com/smartbear/zephyrscale/junit/customformat/CustomFormatContainer.java +++ b/src/main/java/com/smartbear/zephyrscale/junit/customformat/CustomFormatContainer.java @@ -1,39 +1,32 @@ package com.smartbear.zephyrscale.junit.customformat; -import java.util.ArrayList; import java.util.List; -import java.util.Optional; -public class CustomFormatContainer { - - private Integer version = 1; - - private List executions = new ArrayList<>(); +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; - public List getExecutions() { - return executions; - } +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Builder.Default; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; - public void setExecutions(List executions) { - this.executions = executions; - } - public void addResult(CustomFormatExecution customFormatExecution) { - executions.add(customFormatExecution); - } - - public Optional getExecutionBySource(String source) { - return executions - .stream() - .filter(r -> r.getSource().equals(source)) - .findFirst(); - } +@Builder +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +@JsonInclude(Include.NON_NULL) +public class CustomFormatContainer { - public Integer getVersion() { - return version; - } + @Default + private Integer version = 1; - public void setVersion(Integer version) { - this.version = version; - } + private List executions; + + // To mitigate javadoc compile time issue for lombok + // https://stackoverflow.com/questions/51947791/javadoc-cannot-find-symbol-error-when-using-lomboks-builder-annotation + public static class CustomFormatContainerBuilder {} } diff --git a/src/main/java/com/smartbear/zephyrscale/junit/customformat/CustomFormatExecution.java b/src/main/java/com/smartbear/zephyrscale/junit/customformat/CustomFormatExecution.java index a2029b6..ca5a0b6 100644 --- a/src/main/java/com/smartbear/zephyrscale/junit/customformat/CustomFormatExecution.java +++ b/src/main/java/com/smartbear/zephyrscale/junit/customformat/CustomFormatExecution.java @@ -1,37 +1,30 @@ package com.smartbear.zephyrscale.junit.customformat; import com.fasterxml.jackson.annotation.JsonInclude; - +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@JsonInclude(Include.NON_NULL) +@Builder +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor public class CustomFormatExecution { private String source; private String result; - - @JsonInclude(JsonInclude.Include.NON_NULL) + private CustomFormatTestCase testCase; + + // To mitigate javadoc compile time issue for lombok + // https://stackoverflow.com/questions/51947791/javadoc-cannot-find-symbol-error-when-using-lomboks-builder-annotation + public static class CustomFormatExecutionBuilder {} - public String getResult() { - return result; - } - - public void setResult(String result) { - this.result = result; - } - - public String getSource() { - return source; - } - - public void setSource(String source) { - this.source = source; - } - - public CustomFormatTestCase getTestCase() { - return testCase; - } - - public void setTestCase(CustomFormatTestCase testCase) { - this.testCase = testCase; - } } diff --git a/src/main/java/com/smartbear/zephyrscale/junit/customformat/CustomFormatTestCase.java b/src/main/java/com/smartbear/zephyrscale/junit/customformat/CustomFormatTestCase.java index 3a0e96f..26d2470 100644 --- a/src/main/java/com/smartbear/zephyrscale/junit/customformat/CustomFormatTestCase.java +++ b/src/main/java/com/smartbear/zephyrscale/junit/customformat/CustomFormatTestCase.java @@ -1,28 +1,27 @@ package com.smartbear.zephyrscale.junit.customformat; import com.fasterxml.jackson.annotation.JsonInclude; - +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@JsonInclude(Include.NON_NULL) +@Builder +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor public class CustomFormatTestCase { - - @JsonInclude(JsonInclude.Include.NON_NULL) + private String key; - @JsonInclude(JsonInclude.Include.NON_NULL) private String name; - - public String getKey() { - return key; - } - - public void setKey(String key) { - this.key = key; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } + + // To mitigate javadoc compile time issue for lombok + // https://stackoverflow.com/questions/51947791/javadoc-cannot-find-symbol-error-when-using-lomboks-builder-annotation + public static class CustomFormatTestCaseBuilder {} } diff --git a/src/main/java/com/smartbear/zephyrscale/junit/decorator/TestDescriptionDecorator.java b/src/main/java/com/smartbear/zephyrscale/junit/decorator/TestDescriptionDecorator.java index c9f161e..5689629 100644 --- a/src/main/java/com/smartbear/zephyrscale/junit/decorator/TestDescriptionDecorator.java +++ b/src/main/java/com/smartbear/zephyrscale/junit/decorator/TestDescriptionDecorator.java @@ -1,39 +1,51 @@ package com.smartbear.zephyrscale.junit.decorator; -import com.smartbear.zephyrscale.junit.builder.CustomFormatTestCaseBuilder; -import com.smartbear.zephyrscale.junit.customformat.CustomFormatTestCase; import org.junit.runner.Description; +import com.smartbear.zephyrscale.junit.annotation.TestCase; +import com.smartbear.zephyrscale.junit.customformat.CustomFormatTestCase; +import com.smartbear.zephyrscale.junit.customformat.CustomFormatTestCase.CustomFormatTestCaseBuilder; + public class TestDescriptionDecorator { - private static final String PARAMETERIZED_TEST_NAME_PATTERN = "\\[[0-9]+]$"; - private Description description; + private static final String PARAMETERIZED_TEST_NAME_PATTERN = "\\[[0-9]+]$"; + + private Description description; + + public TestDescriptionDecorator(Description description) { + this.description = description; + } + + public String getSource() { + String testClassName = description.getTestClass().getName(); + String methodName = getMethodName(); + return testClassName + "." + methodName; + } + + public CustomFormatTestCase getTestCase() { + + TestCase testCase = description.getAnnotation(TestCase.class); - public TestDescriptionDecorator(Description description) { - this.description = description; - } + CustomFormatTestCaseBuilder customFormatTestCaseBuilder = CustomFormatTestCase.builder(); - public String getSource() { - String testClassName = description.getTestClass().getName(); - String methodName = getMethodName(); - return testClassName + "." + methodName; - } + if (testCase != null) { + customFormatTestCaseBuilder.key(testCase.key()).name(testCase.name()); + } - public CustomFormatTestCase getTestCase() { - return new CustomFormatTestCaseBuilder().build(description).getTestCase(); - } + return customFormatTestCaseBuilder.build(); + } - private String getMethodName() { - return description.getMethodName().replaceFirst(PARAMETERIZED_TEST_NAME_PATTERN, ""); - } + private String getMethodName() { + return description.getMethodName().replaceFirst(PARAMETERIZED_TEST_NAME_PATTERN, ""); + } - @Override - public boolean equals(Object obj) { - if (obj instanceof TestDescriptionDecorator) { - TestDescriptionDecorator testDescriptionDecorator = (TestDescriptionDecorator) obj; - return this.getSource().equals(testDescriptionDecorator.getSource()); - } + @Override + public boolean equals(Object obj) { + if (obj instanceof TestDescriptionDecorator) { + TestDescriptionDecorator testDescriptionDecorator = (TestDescriptionDecorator) obj; + return this.getSource().equals(testDescriptionDecorator.getSource()); + } - return false; - } + return false; + } } diff --git a/src/main/java/com/smartbear/zephyrscale/junit/file/CustomFormatFile.java b/src/main/java/com/smartbear/zephyrscale/junit/file/CustomFormatFile.java index 9018d0c..f4818d4 100644 --- a/src/main/java/com/smartbear/zephyrscale/junit/file/CustomFormatFile.java +++ b/src/main/java/com/smartbear/zephyrscale/junit/file/CustomFormatFile.java @@ -1,11 +1,15 @@ package com.smartbear.zephyrscale.junit.file; -import com.smartbear.zephyrscale.junit.customformat.CustomFormatContainer; -import com.fasterxml.jackson.databind.ObjectMapper; - import java.io.File; import java.io.IOException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.smartbear.zephyrscale.junit.customformat.CustomFormatContainer; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +@NoArgsConstructor(access=AccessLevel.PRIVATE) public class CustomFormatFile { public static final String DEFAULT_ZEPHYRSCALE_RESULT_FILE_NAME = "zephyrscale_result.json"; diff --git a/src/test/java/com/smartbear/zephyrscale/junit/ExecutionListenerTest.java b/src/test/java/com/smartbear/zephyrscale/junit/ExecutionListenerTest.java index a44f967..c17825c 100644 --- a/src/test/java/com/smartbear/zephyrscale/junit/ExecutionListenerTest.java +++ b/src/test/java/com/smartbear/zephyrscale/junit/ExecutionListenerTest.java @@ -1,27 +1,31 @@ package com.smartbear.zephyrscale.junit; -import com.smartbear.zephyrscale.junit.annotation.TestCase; -import com.smartbear.zephyrscale.junit.builder.CustomFormatContainerBuilder; -import com.smartbear.zephyrscale.junit.decorator.TestDescriptionDecorator; -import com.smartbear.zephyrscale.junit.customformat.CustomFormatExecution; -import com.smartbear.zephyrscale.junit.customformat.CustomFormatContainer; -import com.smartbear.zephyrscale.junit.file.CustomFormatFile; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Test; -import org.junit.runner.Description; -import org.junit.runner.notification.Failure; - -import java.io.File; -import java.io.IOException; -import java.lang.reflect.Method; - import static com.smartbear.zephyrscale.junit.customformat.CustomFormatConstants.FAILED; import static com.smartbear.zephyrscale.junit.customformat.CustomFormatConstants.PASSED; import static com.smartbear.zephyrscale.junit.file.CustomFormatFile.generateCustomFormatFile; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; +import org.junit.runner.Description; +import org.junit.runner.notification.Failure; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.smartbear.zephyrscale.junit.annotation.TestCase; +import com.smartbear.zephyrscale.junit.customformat.CustomFormatContainer; +import com.smartbear.zephyrscale.junit.customformat.CustomFormatExecution; +import com.smartbear.zephyrscale.junit.customformat.CustomFormatExecution.CustomFormatExecutionBuilder; +import com.smartbear.zephyrscale.junit.customformat.CustomFormatTestCase; +import com.smartbear.zephyrscale.junit.customformat.CustomFormatTestCase.CustomFormatTestCaseBuilder; +import com.smartbear.zephyrscale.junit.decorator.TestDescriptionDecorator; +import com.smartbear.zephyrscale.junit.file.CustomFormatFile; + public class ExecutionListenerTest { class TestCaseAnnotationTest { @@ -73,7 +77,7 @@ public void shouldNotSetNameWhenItIsNotSetInTestCaseAnnotation() throws Exceptio assertEquals(1, customFormatContainer.getExecutions().size()); CustomFormatExecution jqat1Result = customFormatContainer.getExecutions().get(0); - assertNull(jqat1Result.getTestCase().getName()); + assertTrue(jqat1Result.getTestCase().getName().isEmpty()); } @Test @@ -92,7 +96,7 @@ public void shouldNotSetKeyWhenItIsNotSetInTestCaseAnnotation() throws Exception assertEquals(1, customFormatContainer.getExecutions().size()); CustomFormatExecution jqat1Result = customFormatContainer.getExecutions().get(0); - assertNull(jqat1Result.getTestCase().getKey()); + assertTrue(jqat1Result.getTestCase().getKey().isEmpty()); } @Test @@ -227,49 +231,58 @@ public void shouldHaveMultipleResultsForTheSameTestCaseKeyWhenTheyAreAnnotatedIn assertEquals("JQA-T1", jqat2Result.getTestCase().getKey()); assertEquals(FAILED, jqat2Result.getResult()); } - + + @Test public void shouldHaveOnlyOneResultForTestCaseKeyWhenItRunsAsParameterizedJUnitTest() throws IOException, NoSuchMethodException { - CustomFormatContainerBuilder customFormatContainerBuilder = new CustomFormatContainerBuilder(); - + + List executionList = new ArrayList<>(); + Method shouldHaveTestCaseWithKeyJQAT1 = TestCaseAnnotationTest.class.getDeclaredMethod("shouldHaveTestCaseWithKeyJQAT1"); Description descriptionParam1 = Description.createTestDescription(this.getClass(), shouldHaveTestCaseWithKeyJQAT1.getName() + "[0]", shouldHaveTestCaseWithKeyJQAT1.getAnnotations()); - customFormatContainerBuilder.registerFinished(new TestDescriptionDecorator(descriptionParam1)); + CustomFormatExecution execution1 = getExecution(descriptionParam1, PASSED); + executionList.add(execution1); + Description descriptionParam2 = Description.createTestDescription(this.getClass(), shouldHaveTestCaseWithKeyJQAT1.getName() + "[1]", shouldHaveTestCaseWithKeyJQAT1.getAnnotations()); - customFormatContainerBuilder.registerFinished(new TestDescriptionDecorator(descriptionParam2)); + CustomFormatExecution execution2 = getExecution(descriptionParam2, PASSED); + executionList.add(execution2); - generateCustomFormatFile(customFormatContainerBuilder.getCustomFormatContainer()); + generateCustomFormatFile(CustomFormatContainer.builder().executions(executionList).build()); CustomFormatContainer customFormatContainer = getZephyrScaleJUnitResults(); - assertEquals(1, customFormatContainer.getExecutions().size()); + assertEquals(2, customFormatContainer.getExecutions().size()); assertEquals(PASSED, customFormatContainer.getExecutions().get(0).getResult()); + assertEquals(PASSED, customFormatContainer.getExecutions().get(1).getResult()); } @Test public void shouldHaveFailedResultForTestCaseKeyWhenItRunsAsParameterizedJUnitTestAndOneOfThemHaveFailed() throws IOException, NoSuchMethodException { - CustomFormatContainerBuilder customFormatContainerBuilder = new CustomFormatContainerBuilder(); - + + List executionList = new ArrayList<>(); + Method shouldHaveTestCaseWithKeyJQAT1 = TestCaseAnnotationTest.class.getDeclaredMethod("shouldHaveTestCaseWithKeyJQAT1"); Description descriptionParam1 = Description.createTestDescription(this.getClass(), shouldHaveTestCaseWithKeyJQAT1.getName() + "[0]", shouldHaveTestCaseWithKeyJQAT1.getAnnotations()); - TestDescriptionDecorator failedTestMethodId = new TestDescriptionDecorator(descriptionParam1); - customFormatContainerBuilder.registerFailure(failedTestMethodId); - customFormatContainerBuilder.registerFinished(failedTestMethodId); - + CustomFormatExecution execution1 = getExecution(descriptionParam1, FAILED); + executionList.add(execution1); + Description descriptionParam2 = Description.createTestDescription(this.getClass(), shouldHaveTestCaseWithKeyJQAT1.getName() + "[1]", shouldHaveTestCaseWithKeyJQAT1.getAnnotations()); - customFormatContainerBuilder.registerFinished(new TestDescriptionDecorator(descriptionParam2)); + CustomFormatExecution execution2 = getExecution(descriptionParam2, PASSED); + executionList.add(execution2); - generateCustomFormatFile(customFormatContainerBuilder.getCustomFormatContainer()); + generateCustomFormatFile(CustomFormatContainer.builder().executions(executionList).build()); CustomFormatContainer customFormatContainer = getZephyrScaleJUnitResults(); - assertEquals(1, customFormatContainer.getExecutions().size()); + assertEquals(2, customFormatContainer.getExecutions().size()); assertEquals(FAILED, customFormatContainer.getExecutions().get(0).getResult()); + assertEquals(PASSED, customFormatContainer.getExecutions().get(1).getResult()); } + private CustomFormatExecution getResultByTestCaseKey(CustomFormatContainer customFormatContainer, String testCaseKey) { return customFormatContainer.getExecutions() .stream() @@ -298,4 +311,21 @@ private CustomFormatContainer getZephyrScaleJUnitResults() throws IOException { File generatedResultFile = new File(CustomFormatFile.DEFAULT_ZEPHYRSCALE_RESULT_FILE_NAME); return new ObjectMapper().readValue(generatedResultFile, CustomFormatContainer.class); } + + + private CustomFormatExecution getExecution(Description description, String result) { + + CustomFormatExecutionBuilder custFormatExecutionBuilder = CustomFormatExecution.builder(); + custFormatExecutionBuilder.source(description.getTestClass().getName() + "." + description.getMethodName()); + custFormatExecutionBuilder.result(result); + + CustomFormatTestCaseBuilder customFormatTestCaseBuilder = CustomFormatTestCase.builder(); + + TestCase testCase = description.getAnnotation(TestCase.class); + if(testCase != null) { + customFormatTestCaseBuilder.key(testCase.key()).name(testCase.name()); + custFormatExecutionBuilder.testCase(customFormatTestCaseBuilder.build()); + } + return custFormatExecutionBuilder.build(); + } }