From 9991f0aa0ffdcbe29eefebd1b12ffbfe7989ab9d Mon Sep 17 00:00:00 2001 From: Dmitry Baev Date: Tue, 19 Mar 2024 12:42:23 +0000 Subject: [PATCH] use attachment meta steps in default attachment processor --- .../DefaultAttachmentProcessor.java | 32 +++++++-- .../DefaultAttachmentProcessorTest.java | 66 ++++++++++++++++++- 2 files changed, 90 insertions(+), 8 deletions(-) diff --git a/allure-attachments/src/main/java/io/qameta/allure/attachment/DefaultAttachmentProcessor.java b/allure-attachments/src/main/java/io/qameta/allure/attachment/DefaultAttachmentProcessor.java index 652c8ca5..470d05c4 100644 --- a/allure-attachments/src/main/java/io/qameta/allure/attachment/DefaultAttachmentProcessor.java +++ b/allure-attachments/src/main/java/io/qameta/allure/attachment/DefaultAttachmentProcessor.java @@ -17,8 +17,14 @@ import io.qameta.allure.Allure; import io.qameta.allure.AllureLifecycle; +import io.qameta.allure.model.Status; +import io.qameta.allure.model.StepResult; import java.nio.charset.StandardCharsets; +import java.util.UUID; + +import static io.qameta.allure.util.ResultsUtils.getStatus; +import static io.qameta.allure.util.ResultsUtils.getStatusDetails; /** * @author charlie (Dmitry Baev). @@ -39,11 +45,25 @@ public DefaultAttachmentProcessor(final AllureLifecycle lifecycle) { public void addAttachment(final AttachmentData attachmentData, final AttachmentRenderer renderer) { final AttachmentContent content = renderer.render(attachmentData); - lifecycle.addAttachment( - attachmentData.getName(), - content.getContentType(), - content.getFileExtension(), - content.getContent().getBytes(StandardCharsets.UTF_8) - ); + final String uuid = UUID.randomUUID().toString(); + lifecycle.startStep(uuid, new StepResult().setName(attachmentData.getName())); + try { + lifecycle.addAttachment( + attachmentData.getName(), + content.getContentType(), + content.getFileExtension(), + content.getContent().getBytes(StandardCharsets.UTF_8) + ); + lifecycle.updateStep(uuid, step -> step + .setStatus(Status.PASSED) + ); + } catch (Exception e) { + lifecycle.updateStep(uuid, step -> step + .setStatus(getStatus(e).orElse(Status.BROKEN)) + .setStatusDetails(getStatusDetails(e).orElse(null)) + ); + } finally { + lifecycle.stopStep(uuid); + } } } diff --git a/allure-attachments/src/test/java/io/qameta/allure/attachment/DefaultAttachmentProcessorTest.java b/allure-attachments/src/test/java/io/qameta/allure/attachment/DefaultAttachmentProcessorTest.java index a1d2def2..752e47af 100644 --- a/allure-attachments/src/test/java/io/qameta/allure/attachment/DefaultAttachmentProcessorTest.java +++ b/allure-attachments/src/test/java/io/qameta/allure/attachment/DefaultAttachmentProcessorTest.java @@ -17,13 +17,18 @@ import io.qameta.allure.AllureLifecycle; import io.qameta.allure.attachment.http.HttpRequestAttachment; +import io.qameta.allure.model.Status; +import io.qameta.allure.model.StepResult; import io.qameta.allure.test.AllureFeatures; import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; import java.nio.charset.StandardCharsets; +import java.util.function.Consumer; import static io.qameta.allure.attachment.testdata.TestData.randomAttachmentContent; import static io.qameta.allure.attachment.testdata.TestData.randomHttpRequestAttachment; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; @@ -35,13 +40,12 @@ */ class DefaultAttachmentProcessorTest { - @SuppressWarnings("unchecked") @AllureFeatures.Attachments @Test void shouldProcessAttachments() { final HttpRequestAttachment attachment = randomHttpRequestAttachment(); final AllureLifecycle lifecycle = mock(AllureLifecycle.class); - final AttachmentRenderer renderer = mock(AttachmentRenderer.class); + final AttachmentRenderer renderer = mock(); final AttachmentContent content = randomAttachmentContent(); doReturn(content) .when(renderer) @@ -59,4 +63,62 @@ void shouldProcessAttachments() { eq(content.getContent().getBytes(StandardCharsets.UTF_8)) ); } + + @AllureFeatures.Attachments + @Test + void shouldProcessWrapAttachmentsWithMetaSteps() { + final HttpRequestAttachment attachment = randomHttpRequestAttachment(); + final AllureLifecycle lifecycle = mock(AllureLifecycle.class); + final AttachmentRenderer renderer = mock(); + final AttachmentContent content = randomAttachmentContent(); + doReturn(content) + .when(renderer) + .render(attachment); + + new DefaultAttachmentProcessor(lifecycle) + .addAttachment(attachment, renderer); + + verify(renderer, times(1)).render(attachment); + + final ArgumentCaptor stepUuidCaptor = ArgumentCaptor.captor(); + + verify(lifecycle, times(1)) + .startStep( + stepUuidCaptor.capture(), + eq(new StepResult().setName(attachment.getName())) + ); + + verify(lifecycle, times(1)) + .addAttachment( + eq(attachment.getName()), + eq(content.getContentType()), + eq(content.getFileExtension()), + eq(content.getContent().getBytes(StandardCharsets.UTF_8)) + ); + + final ArgumentCaptor> consumerArgumentCaptor = ArgumentCaptor.captor(); + + verify(lifecycle, times(1)) + .updateStep( + eq(stepUuidCaptor.getValue()), + consumerArgumentCaptor.capture() + ); + + final StepResult stepResultCheck = mock(); + + doReturn(stepResultCheck) + .when(stepResultCheck) + .setStatus(any()); + + consumerArgumentCaptor.getValue().accept(stepResultCheck); + + verify(stepResultCheck, times(1)) + .setStatus(Status.PASSED); + + verify(lifecycle, times(1)) + .stopStep( + eq(stepUuidCaptor.getValue()) + ); + } + }