LinshareBlobExportMechanism implements BlobExportMechanism { @Override public ShareeStage blobId(BlobId blobId) { return mailAddress -> explanation -> fileCustomPrefix -> fileExtension -> () -> { try { exportBlob(blobId, mailAddress, fileCustomPrefix, fileExtension); } catch (Exception e) { throw new BlobExportException(String.format("Error while exporting blob %s to %s", blobId.asString(), mailAddress.asString()), e); } }; } @Inject  LinshareBlobExportMechanism(LinshareAPI linshareAPI, BlobStore blobStore); @Override ShareeStage blobId(BlobId blobId);  }
@Test void exportShouldUploadTheDocumentToTargetUserViaLinshare() throws Exception { BlobId blobId = Mono.from(blobStore.save(blobStore.getDefaultBucketName(), FILE_CONTENT, LOW_COST)).block(); String filePrefix = "deleted-message-of-bob@james.org-"; testee.blobId(blobId) .with(new MailAddress(USER_2.getUsername())) .explanation(EXPLANATION) .filePrefix(filePrefix) .fileExtension(FILE_TEXT_EXTENSION) .export(); assertThat(user2API.listAllDocuments()) .hasSize(1) .allSatisfy(receivedShare -> assertThat(receivedShare.getName()).endsWith(".txt")) .allSatisfy(receivedShare -> assertThat(receivedShare.getName()).startsWith(filePrefix)); }
@Test void exportShouldUploadTheDocumentAndAllowDownloadViaLinshare(LinshareAPIForTechnicalAccountTesting delegationAPIForTesting) throws Exception { BlobId blobId = Mono.from(blobStore.save(blobStore.getDefaultBucketName(), FILE_CONTENT, LOW_COST)).block(); testee.blobId(blobId) .with(new MailAddress(USER_2.getUsername())) .explanation(EXPLANATION) .noFileCustomPrefix() .fileExtension(FILE_TEXT_EXTENSION) .export(); Document sharedDoc = user2API.listAllDocuments().get(0); byte[] sharedFile = delegationAPIForTesting.downloadFileFrom(USER_2, sharedDoc.getId()); assertThat(sharedFile).isEqualTo(FILE_CONTENT.getBytes(StandardCharsets.UTF_8)); }
@Test void exportShouldFailWhenBlobDoesNotExist() { BlobId blobId = blobIdFactory.randomId(); assertThatThrownBy( () -> testee.blobId(blobId) .with(new MailAddress(USER_2.getUsername())) .explanation(EXPLANATION) .noFileCustomPrefix() .fileExtension(FILE_TEXT_EXTENSION) .export()) .isInstanceOf(BlobExportMechanism.BlobExportException.class); }
@Test void exportWithFilePrefixShouldCreateFileWithCustomPrefix() throws Exception { BlobId blobId = Mono.from(blobStore.save(blobStore.getDefaultBucketName(), FILE_CONTENT, LOW_COST)).block(); String filePrefix = "deleted-message-of-bob@james.org"; testee.blobId(blobId) .with(new MailAddress(USER_2.getUsername())) .explanation(EXPLANATION) .filePrefix(filePrefix) .fileExtension(FILE_TEXT_EXTENSION) .export(); Document sharedDoc = user2API.listAllDocuments().get(0); assertThat(sharedDoc.getName()) .startsWith(filePrefix); }