@Test void runShouldReturnPartialWhenAtLeastOneAttachmentMigrationFails() { CassandraAttachmentDAO attachmentDAO = mock(CassandraAttachmentDAO.class); CassandraAttachmentDAOV2 attachmentDAOV2 = mock(CassandraAttachmentDAOV2.class); CassandraBlobsDAO blobsDAO = mock(CassandraBlobsDAO.class); migration = new AttachmentV2Migration(attachmentDAO, attachmentDAOV2, blobsDAO); when(attachmentDAO.retrieveAll()).thenReturn(Stream.of( attachment1, attachment2)); when(blobsDAO.save(attachment1.getBytes())) .thenReturn(CompletableFuture.completedFuture(BLOB_ID_FACTORY.forPayload(attachment1.getBytes()))); when(blobsDAO.save(attachment2.getBytes())) .thenThrow(new RuntimeException()); when(attachmentDAOV2.storeAttachment(any())).thenReturn(CompletableFuture.completedFuture(null)); when(attachmentDAO.deleteAttachment(any())).thenReturn(CompletableFuture.completedFuture(null)); assertThat(migration.run()).isEqualTo(Migration.Result.PARTIAL); }
@Test void runShouldReturnPartialWhenDeleteV1AttachmentFail() { CassandraAttachmentDAO attachmentDAO = mock(CassandraAttachmentDAO.class); CassandraAttachmentDAOV2 attachmentDAOV2 = mock(CassandraAttachmentDAOV2.class); CassandraBlobsDAO blobsDAO = mock(CassandraBlobsDAO.class); migration = new AttachmentV2Migration(attachmentDAO, attachmentDAOV2, blobsDAO); when(attachmentDAO.retrieveAll()).thenReturn(Stream.of( attachment1, attachment2)); when(blobsDAO.save(attachment1.getBytes())) .thenReturn(CompletableFuture.completedFuture(BLOB_ID_FACTORY.forPayload(attachment1.getBytes()))); when(blobsDAO.save(attachment2.getBytes())) .thenReturn(CompletableFuture.completedFuture(BLOB_ID_FACTORY.forPayload(attachment2.getBytes()))); when(attachmentDAOV2.storeAttachment(any())).thenReturn(CompletableFuture.completedFuture(null)); when(attachmentDAO.deleteAttachment(any())).thenThrow(new RuntimeException()); assertThat(migration.run()).isEqualTo(Migration.Result.PARTIAL); }
@Test void runShouldReturnPartialWhenSavingAttachmentV2Fail() { CassandraAttachmentDAO attachmentDAO = mock(CassandraAttachmentDAO.class); CassandraAttachmentDAOV2 attachmentDAOV2 = mock(CassandraAttachmentDAOV2.class); CassandraBlobsDAO blobsDAO = mock(CassandraBlobsDAO.class); migration = new AttachmentV2Migration(attachmentDAO, attachmentDAOV2, blobsDAO); when(attachmentDAO.retrieveAll()).thenReturn(Stream.of( attachment1, attachment2)); when(blobsDAO.save(attachment1.getBytes())) .thenReturn(CompletableFuture.completedFuture(BLOB_ID_FACTORY.forPayload(attachment1.getBytes()))); when(blobsDAO.save(attachment2.getBytes())) .thenReturn(CompletableFuture.completedFuture(BLOB_ID_FACTORY.forPayload(attachment2.getBytes()))); when(attachmentDAOV2.storeAttachment(any())).thenThrow(new RuntimeException()); assertThat(migration.run()).isEqualTo(Migration.Result.PARTIAL); }
@Test void migrationShouldMoveAttachmentsToV2() throws Exception { attachmentDAO.storeAttachment(attachment1).join(); attachmentDAO.storeAttachment(attachment2).join(); migration.run(); assertThat(attachmentDAOV2.getAttachment(ATTACHMENT_ID).join()) .contains(CassandraAttachmentDAOV2.from(attachment1, BLOB_ID_FACTORY.forPayload(attachment1.getBytes()))); assertThat(attachmentDAOV2.getAttachment(ATTACHMENT_ID_2).join()) .contains(CassandraAttachmentDAOV2.from(attachment2, BLOB_ID_FACTORY.forPayload(attachment2.getBytes()))); assertThat(blobsDAO.readBytes(BLOB_ID_FACTORY.forPayload(attachment1.getBytes())).join()) .isEqualTo(attachment1.getBytes()); assertThat(blobsDAO.readBytes(BLOB_ID_FACTORY.forPayload(attachment2.getBytes())).join()) .isEqualTo(attachment2.getBytes()); }
@Test public void forPayloadShouldCalculateDifferentHashesWhenCraftedSha1Collision() throws Exception { byte[] payload1 = ClassLoaderUtils.getSystemResourceAsByteArray("shattered-1.pdf"); byte[] payload2 = ClassLoaderUtils.getSystemResourceAsByteArray("shattered-2.pdf"); BlobId blobId1 = BLOB_ID_FACTORY.forPayload(payload1); BlobId blobId2 = BLOB_ID_FACTORY.forPayload(payload2); assertThat(blobId1).isNotEqualTo(blobId2); } }
@Test public void forPayloadShouldHashArray() { BlobId blobId = BLOB_ID_FACTORY.forPayload("content".getBytes(StandardCharsets.UTF_8)); assertThat(blobId.asString()).isEqualTo("ed7002b439e9ac845f22357d822bac1444730fbdb6016d3ec9432297b9ec9f73"); }
@Test public void forPayloadShouldThrowOnNull() { assertThatThrownBy(() -> BLOB_ID_FACTORY.forPayload(null)) .isInstanceOf(IllegalArgumentException.class); }
private Mono<BlobId> saveAsMono(byte[] data) { BlobId blobId = blobIdFactory.forPayload(data); return saveBlobParts(data, blobId) .flatMap(numberOfChunk -> saveBlobPartsReferences(blobId, numberOfChunk)); }
@Test public void forPayloadShouldHashEmptyArray() { BlobId blobId = BLOB_ID_FACTORY.forPayload(new byte[0]); assertThat(blobId.asString()).isEqualTo("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); }