@Override public Attachment visitAttachmentFile(final Path attachmentFile) { final RandomUidContext context = configuration.requireContext(RandomUidContext.class); return attachments.computeIfAbsent(attachmentFile, file -> { final String uid = context.getValue().get(); final String realType = probeContentType(file); final String extension = Optional.of(getExtension(file.toString())) .filter(s -> !s.isEmpty()) .map(s -> "." + s) .orElseGet(() -> getExtensionByMimeType(realType)); final String source = uid + (extension.isEmpty() ? "" : extension); final Long size = getFileSizeSafe(file); return new Attachment() .setUid(uid) .setName(file.getFileName().toString()) .setSource(source) .setType(realType) .setSize(size); }); }
@Override public void aggregate(final Configuration configuration, final List<LaunchResults> launchesResults, final Path outputDirectory) throws IOException { final Path attachmentsFolder = Files.createDirectories( outputDirectory.resolve(Constants.DATA_DIR).resolve("attachments") ); for (LaunchResults launch : launchesResults) { for (Map.Entry<Path, Attachment> entry : launch.getAttachments().entrySet()) { final Path file = attachmentsFolder.resolve(entry.getValue().getSource()); Files.copy(entry.getKey(), file, StandardCopyOption.REPLACE_EXISTING); } } } }
private Attachment convert(final Path source, final ResultsVisitor visitor, final ru.yandex.qatools.allure.model.Attachment attachment) { final Path attachmentFile = source.resolve(attachment.getSource()); if (Files.isRegularFile(attachmentFile)) { final Attachment found = visitor.visitAttachmentFile(attachmentFile); if (Objects.nonNull(attachment.getType())) { found.setType(attachment.getType()); } if (Objects.nonNull(attachment.getTitle())) { found.setName(attachment.getTitle()); } return found; } else { visitor.error("Could not find attachment " + attachment.getSource() + " in directory " + source); return new Attachment() .setType(attachment.getType()) .setName(attachment.getTitle()) .setSize(0L); } }
@Test public void shouldAddLogAsAttachment() throws Exception { final Attachment hey = new Attachment().setUid("some-uid"); when(visitor.visitAttachmentFile(any())).thenReturn(hey); process( "junitdata/TEST-test.SampleTest.xml", "TEST-test.SampleTest.xml", "junitdata/test.SampleTest.txt", "test.SampleTest.txt" ); final ArgumentCaptor<Path> attachmentCaptor = ArgumentCaptor.forClass(Path.class); verify(visitor, times(1)).visitAttachmentFile(attachmentCaptor.capture()); assertThat(attachmentCaptor.getValue()) .isRegularFile() .hasContent("some-test-log"); final ArgumentCaptor<TestResult> captor = ArgumentCaptor.forClass(TestResult.class); verify(visitor, times(1)).visitTestResult(captor.capture()); final StageResult testStage = captor.getValue().getTestStage(); assertThat(testStage) .describedAs("Should create a test stage") .isNotNull(); assertThat(testStage.getAttachments()) .describedAs("Should add an attachment") .hasSize(1) .describedAs("Attachment should has right uid and name") .extracting(Attachment::getName, Attachment::getUid) .containsExactly(Tuple.tuple("System out", "some-uid")); }
@Test public void shouldCountAttachments() throws Exception { final Step step = new Step().setSteps(asList( new Step().setAttachments(asList(new Attachment(), new Attachment())), new Step().setAttachments(singletonList(new Attachment())).setSteps(singletonList(new Step())) )).setAttachments(singletonList(new Attachment())); assertThat(step.getAttachmentsCount()) .isEqualTo(4L); }
private Attachment convert(final Path source, final ResultsVisitor visitor, final io.qameta.allure.model.Attachment attachment) { final Path attachmentFile = source.resolve(attachment.getSource()); if (Files.isRegularFile(attachmentFile)) { final Attachment found = visitor.visitAttachmentFile(attachmentFile); if (nonNull(attachment.getType())) { found.setType(attachment.getType()); } if (nonNull(attachment.getName())) { found.setName(attachment.getName()); } return found; } else { visitor.error("Could not find attachment " + attachment.getSource() + " in directory " + source); return new Attachment() .setType(attachment.getType()) .setName(attachment.getName()) .setSize(0L); } }
@Test public void shouldCountAttachmentsForHasContent() throws Exception { final Step step = new Step().setAttachments(singletonList(new Attachment())); assertThat(step.hasContent()) .isTrue(); }
@Override public Attachment visitAttachmentFile(final Path attachmentFile) { final RandomUidContext context = configuration.requireContext(RandomUidContext.class); return attachments.computeIfAbsent(attachmentFile, file -> { final String uid = context.getValue().get(); final String realType = probeContentType(file); final String extension = Optional.of(getExtension(file.toString())) .filter(s -> !s.isEmpty()) .map(s -> "." + s) .orElseGet(() -> getExtensionByMimeType(realType)); final String source = uid + (extension.isEmpty() ? "" : extension); final Long size = getFileSizeSafe(file); return new Attachment() .setUid(uid) .setName(file.getFileName().toString()) .setSource(source) .setType(realType) .setSize(size); }); }
private Attachment convert(final Path source, final ResultsVisitor visitor, final io.qameta.allure.model.Attachment attachment) { final Path attachmentFile = source.resolve(attachment.getSource()); if (Files.isRegularFile(attachmentFile)) { final Attachment found = visitor.visitAttachmentFile(attachmentFile); if (nonNull(attachment.getType())) { found.setType(attachment.getType()); } if (nonNull(attachment.getName())) { found.setName(attachment.getName()); } return found; } else { visitor.error("Could not find attachment " + attachment.getSource() + " in directory " + source); return new Attachment() .setType(attachment.getType()) .setName(attachment.getName()) .setSize(0L); } }
@Test public void shouldReadAttachments() throws Exception { final LaunchResults launchResults = process( "allure1/suite-with-attachments.xml", generateTestSuiteXmlName(), "allure1/sample-attachment.txt", "sample-attachment.txt" ); final Map<Path, Attachment> attachmentMap = launchResults.getAttachments(); final Set<TestResult> results = launchResults.getResults(); assertThat(attachmentMap) .hasSize(1); final Attachment storedAttachment = attachmentMap.values().iterator().next(); List<Attachment> attachments = results.stream() .flatMap(this::extractAttachments) .collect(Collectors.toList()); assertThat(attachments) .hasSize(1) .extracting(Attachment::getSource) .containsExactly(storedAttachment.getSource()); }
private Attachment convert(final Path source, final ResultsVisitor visitor, final ru.yandex.qatools.allure.model.Attachment attachment) { final Path attachmentFile = source.resolve(attachment.getSource()); if (Files.isRegularFile(attachmentFile)) { final Attachment found = visitor.visitAttachmentFile(attachmentFile); if (Objects.nonNull(attachment.getType())) { found.setType(attachment.getType()); } if (Objects.nonNull(attachment.getTitle())) { found.setName(attachment.getTitle()); } return found; } else { visitor.error("Could not find attachment " + attachment.getSource() + " in directory " + source); return new Attachment() .setType(attachment.getType()) .setName(attachment.getTitle()) .setSize(0L); } }
@Override public void aggregate(final Configuration configuration, final List<LaunchResults> launchesResults, final Path outputDirectory) throws IOException { final Path attachmentsFolder = Files.createDirectories( outputDirectory.resolve(Constants.DATA_DIR).resolve("attachments") ); for (LaunchResults launch : launchesResults) { for (Map.Entry<Path, Attachment> entry : launch.getAttachments().entrySet()) { final Path file = attachmentsFolder.resolve(entry.getValue().getSource()); Files.copy(entry.getKey(), file, StandardCopyOption.REPLACE_EXISTING); } } } }