LocalFileBlobExportMechanism implements BlobExportMechanism { @Override public ShareeStage blobId(BlobId blobId) { return mailAddress -> explanation -> fileCustomPrefix -> fileExtension -> () -> { String fileUrl = copyBlobToFile(blobId, fileCustomPrefix, fileExtension); sendMail(mailAddress, fileUrl, explanation); }; } @VisibleForTesting @Inject  LocalFileBlobExportMechanism(MailetContext mailetContext, BlobStore blobStore, FileSystem fileSystem, DNSService dnsService, Configuration configuration); @Override ShareeStage blobId(BlobId blobId);  }
@Test void exportingBlobShouldSendAMail() { BlobId blobId = Mono.from(blobStore.save(blobStore.getDefaultBucketName(), BLOB_CONTENT, LOW_COST)).block(); String explanation = "The content of a deleted message vault had been shared with you."; testee.blobId(blobId) .with(MailAddressFixture.RECIPIENT1) .explanation(explanation) .noFileCustomPrefix() .noFileExtension() .export(); assertThat(mailetContext.getSentMails()).hasSize(1) .element(0) .satisfies(sentMail -> { try { assertThat(sentMail.getSender()).isEqualTo(MailAddressFixture.POSTMASTER_AT_JAMES); assertThat(sentMail.getRecipients()).containsExactly(MailAddressFixture.RECIPIENT1); assertThat(sentMail.getMsg().getFrom()).contains(new InternetAddress(MailAddressFixture.POSTMASTER_AT_JAMES.asString())); assertThat(sentMail.getMsg().getRecipients(Message.RecipientType.TO)).contains(new InternetAddress(MailAddressFixture.RECIPIENT1.asString())); assertThat(sentMail.getMsg().getSubject()).isEqualTo("Some content had had just been exported"); String mailContent = MimeMessageUtil.asString(sentMail.getMsg()); assertThat(mailContent).contains(explanation); assertThat(mailContent).contains("The content of this blob can be read directly on James host filesystem (james-host) in this file: "); } catch (Exception e) { throw new RuntimeException(e); } }); }
@Test void exportingBlobShouldCreateAFileWithTheCorrespondingContent(FileSystem fileSystem) { BlobId blobId = Mono.from(blobStore.save(blobStore.getDefaultBucketName(), BLOB_CONTENT, LOW_COST)).block(); testee.blobId(blobId) .with(MailAddressFixture.RECIPIENT1) .explanation("The content of a deleted message vault had been shared with you.") .noFileCustomPrefix() .noFileExtension() .export(); assertThat(mailetContext.getSentMails()) .element(0) .satisfies(sentMail -> { try { String absoluteUrl = sentMail.getMsg().getHeader(LocalFileBlobExportMechanism.CORRESPONDING_FILE_HEADER)[0]; assertThat(new FileInputStream(absoluteUrl)).hasSameContentAs(IOUtils.toInputStream(BLOB_CONTENT, StandardCharsets.UTF_8)); } catch (Exception e) { throw new RuntimeException(e); } }); }
@Test void shareShouldFailWhenBlobDoesNotExist() { BlobId blobId = new HashBlobId.Factory().forPayload("not existing".getBytes(StandardCharsets.UTF_8)); assertThatThrownBy(() -> testee.blobId(blobId) .with(MailAddressFixture.RECIPIENT1) .explanation("The content of a deleted message vault had been shared with you.") .noFileCustomPrefix() .noFileExtension() .export()) .isInstanceOf(ObjectStoreException.class); }
@Test void exportingBlobShouldCreateAFileWithoutExtensionWhenNotDeclaringExtension() { BlobId blobId = Mono.from(blobStore.save(blobStore.getDefaultBucketName(), BLOB_CONTENT, LOW_COST)).block(); testee.blobId(blobId) .with(MailAddressFixture.RECIPIENT1) .explanation("The content of a deleted message vault had been shared with you.") .noFileCustomPrefix() .noFileExtension() .export(); assertThat(mailetContext.getSentMails()) .element(0) .satisfies(sentMail -> { try { String fileUrl = sentMail.getMsg().getHeader(LocalFileBlobExportMechanism.CORRESPONDING_FILE_HEADER)[0]; assertThat(FilenameUtils.getExtension(fileUrl)) .isEmpty(); } catch (Exception e) { throw new RuntimeException(e); } }); }
@Test void exportingBlobShouldCreateAFileWithExtensionWhenDeclaringExtension() { BlobId blobId = Mono.from(blobStore.save(blobStore.getDefaultBucketName(), BLOB_CONTENT, LOW_COST)).block(); testee.blobId(blobId) .with(MailAddressFixture.RECIPIENT1) .explanation("The content of a deleted message vault had been shared with you.") .noFileCustomPrefix() .fileExtension(FileExtension.ZIP) .export(); assertThat(mailetContext.getSentMails()) .element(0) .satisfies(sentMail -> { try { String fileUrl = sentMail.getMsg().getHeader(LocalFileBlobExportMechanism.CORRESPONDING_FILE_HEADER)[0]; String fileExtensionInString = FilenameUtils.getExtension(fileUrl); assertThat(FileExtension.of(fileExtensionInString)) .isEqualTo(FileExtension.ZIP); } catch (Exception e) { throw new RuntimeException(e); } }); }
@Test void exportingBlobShouldCreateAFileWithPrefixWhenDeclaringPrefix() { BlobId blobId = Mono.from(blobStore.save(blobStore.getDefaultBucketName(), BLOB_CONTENT, LOW_COST)).block(); String filePrefix = "deleted-message-of-bob@james.org"; testee.blobId(blobId) .with(MailAddressFixture.RECIPIENT1) .explanation("The content of a deleted message vault had been shared with you.") .filePrefix(filePrefix) .fileExtension(FileExtension.ZIP) .export(); assertThat(mailetContext.getSentMails()) .element(0) .satisfies(sentMail -> { try { String fileUrl = sentMail.getMsg().getHeader(LocalFileBlobExportMechanism.CORRESPONDING_FILE_HEADER)[0]; assertThat(FilenameUtils.getName(fileUrl)) .startsWith(filePrefix); } catch (Exception e) { throw new RuntimeException(e); } }); }