Skip to content

Commit

Permalink
use attachment meta steps in default attachment processor
Browse files Browse the repository at this point in the history
  • Loading branch information
baev committed Mar 19, 2024
1 parent ed837de commit 9991f0a
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -39,11 +45,25 @@ public DefaultAttachmentProcessor(final AllureLifecycle lifecycle) {
public void addAttachment(final AttachmentData attachmentData,
final AttachmentRenderer<AttachmentData> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<AttachmentData> renderer = mock(AttachmentRenderer.class);
final AttachmentRenderer<AttachmentData> renderer = mock();
final AttachmentContent content = randomAttachmentContent();
doReturn(content)
.when(renderer)
Expand All @@ -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<AttachmentData> 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<String> 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<Consumer<StepResult>> 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())
);
}

}

0 comments on commit 9991f0a

Please sign in to comment.