Skip to content

Commit 9991f0a

Browse files
committed
use attachment meta steps in default attachment processor
1 parent ed837de commit 9991f0a

File tree

2 files changed

+90
-8
lines changed

2 files changed

+90
-8
lines changed

allure-attachments/src/main/java/io/qameta/allure/attachment/DefaultAttachmentProcessor.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,14 @@
1717

1818
import io.qameta.allure.Allure;
1919
import io.qameta.allure.AllureLifecycle;
20+
import io.qameta.allure.model.Status;
21+
import io.qameta.allure.model.StepResult;
2022

2123
import java.nio.charset.StandardCharsets;
24+
import java.util.UUID;
25+
26+
import static io.qameta.allure.util.ResultsUtils.getStatus;
27+
import static io.qameta.allure.util.ResultsUtils.getStatusDetails;
2228

2329
/**
2430
* @author charlie (Dmitry Baev).
@@ -39,11 +45,25 @@ public DefaultAttachmentProcessor(final AllureLifecycle lifecycle) {
3945
public void addAttachment(final AttachmentData attachmentData,
4046
final AttachmentRenderer<AttachmentData> renderer) {
4147
final AttachmentContent content = renderer.render(attachmentData);
42-
lifecycle.addAttachment(
43-
attachmentData.getName(),
44-
content.getContentType(),
45-
content.getFileExtension(),
46-
content.getContent().getBytes(StandardCharsets.UTF_8)
47-
);
48+
final String uuid = UUID.randomUUID().toString();
49+
lifecycle.startStep(uuid, new StepResult().setName(attachmentData.getName()));
50+
try {
51+
lifecycle.addAttachment(
52+
attachmentData.getName(),
53+
content.getContentType(),
54+
content.getFileExtension(),
55+
content.getContent().getBytes(StandardCharsets.UTF_8)
56+
);
57+
lifecycle.updateStep(uuid, step -> step
58+
.setStatus(Status.PASSED)
59+
);
60+
} catch (Exception e) {
61+
lifecycle.updateStep(uuid, step -> step
62+
.setStatus(getStatus(e).orElse(Status.BROKEN))
63+
.setStatusDetails(getStatusDetails(e).orElse(null))
64+
);
65+
} finally {
66+
lifecycle.stopStep(uuid);
67+
}
4868
}
4969
}

allure-attachments/src/test/java/io/qameta/allure/attachment/DefaultAttachmentProcessorTest.java

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@
1717

1818
import io.qameta.allure.AllureLifecycle;
1919
import io.qameta.allure.attachment.http.HttpRequestAttachment;
20+
import io.qameta.allure.model.Status;
21+
import io.qameta.allure.model.StepResult;
2022
import io.qameta.allure.test.AllureFeatures;
2123
import org.junit.jupiter.api.Test;
24+
import org.mockito.ArgumentCaptor;
2225

2326
import java.nio.charset.StandardCharsets;
27+
import java.util.function.Consumer;
2428

2529
import static io.qameta.allure.attachment.testdata.TestData.randomAttachmentContent;
2630
import static io.qameta.allure.attachment.testdata.TestData.randomHttpRequestAttachment;
31+
import static org.mockito.ArgumentMatchers.any;
2732
import static org.mockito.ArgumentMatchers.eq;
2833
import static org.mockito.Mockito.doReturn;
2934
import static org.mockito.Mockito.mock;
@@ -35,13 +40,12 @@
3540
*/
3641
class DefaultAttachmentProcessorTest {
3742

38-
@SuppressWarnings("unchecked")
3943
@AllureFeatures.Attachments
4044
@Test
4145
void shouldProcessAttachments() {
4246
final HttpRequestAttachment attachment = randomHttpRequestAttachment();
4347
final AllureLifecycle lifecycle = mock(AllureLifecycle.class);
44-
final AttachmentRenderer<AttachmentData> renderer = mock(AttachmentRenderer.class);
48+
final AttachmentRenderer<AttachmentData> renderer = mock();
4549
final AttachmentContent content = randomAttachmentContent();
4650
doReturn(content)
4751
.when(renderer)
@@ -59,4 +63,62 @@ void shouldProcessAttachments() {
5963
eq(content.getContent().getBytes(StandardCharsets.UTF_8))
6064
);
6165
}
66+
67+
@AllureFeatures.Attachments
68+
@Test
69+
void shouldProcessWrapAttachmentsWithMetaSteps() {
70+
final HttpRequestAttachment attachment = randomHttpRequestAttachment();
71+
final AllureLifecycle lifecycle = mock(AllureLifecycle.class);
72+
final AttachmentRenderer<AttachmentData> renderer = mock();
73+
final AttachmentContent content = randomAttachmentContent();
74+
doReturn(content)
75+
.when(renderer)
76+
.render(attachment);
77+
78+
new DefaultAttachmentProcessor(lifecycle)
79+
.addAttachment(attachment, renderer);
80+
81+
verify(renderer, times(1)).render(attachment);
82+
83+
final ArgumentCaptor<String> stepUuidCaptor = ArgumentCaptor.captor();
84+
85+
verify(lifecycle, times(1))
86+
.startStep(
87+
stepUuidCaptor.capture(),
88+
eq(new StepResult().setName(attachment.getName()))
89+
);
90+
91+
verify(lifecycle, times(1))
92+
.addAttachment(
93+
eq(attachment.getName()),
94+
eq(content.getContentType()),
95+
eq(content.getFileExtension()),
96+
eq(content.getContent().getBytes(StandardCharsets.UTF_8))
97+
);
98+
99+
final ArgumentCaptor<Consumer<StepResult>> consumerArgumentCaptor = ArgumentCaptor.captor();
100+
101+
verify(lifecycle, times(1))
102+
.updateStep(
103+
eq(stepUuidCaptor.getValue()),
104+
consumerArgumentCaptor.capture()
105+
);
106+
107+
final StepResult stepResultCheck = mock();
108+
109+
doReturn(stepResultCheck)
110+
.when(stepResultCheck)
111+
.setStatus(any());
112+
113+
consumerArgumentCaptor.getValue().accept(stepResultCheck);
114+
115+
verify(stepResultCheck, times(1))
116+
.setStatus(Status.PASSED);
117+
118+
verify(lifecycle, times(1))
119+
.stopStep(
120+
eq(stepUuidCaptor.getValue())
121+
);
122+
}
123+
62124
}

0 commit comments

Comments
 (0)