@VisibleForTesting public CassandraBlobsDAO(Session session) { this(session, CassandraConfiguration.DEFAULT_CONFIGURATION, new HashBlobId.Factory()); }
@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 public void fromShouldThrowOnNull() { assertThatThrownBy(() -> BLOB_ID_FACTORY.from(null)) .isInstanceOf(IllegalArgumentException.class); }
@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 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); }
@BeforeEach void setUp(CassandraCluster cassandra) { messageIdFactory = new CassandraMessageId.Factory(); messageId = messageIdFactory.generate(); CassandraBlobsDAO blobsDAO = new CassandraBlobsDAO(cassandra.getConf()); HashBlobId.Factory blobIdFactory = new HashBlobId.Factory(); testee = new CassandraMessageDAO(cassandra.getConf(), cassandra.getTypesProvider(), blobsDAO, blobIdFactory, CassandraUtils.WITH_DEFAULT_CONFIGURATION, new CassandraMessageId.Factory()); messageIds = ImmutableList.of(ComposedMessageIdWithMetaData.builder() .composedMessageId(new ComposedMessageId(MAILBOX_ID, messageId, messageUid)) .flags(new Flags()) .modSeq(1) .build()); }
@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 void getAttachmentShouldReturnAttachmentWhenStored() { Attachment attachment = Attachment.builder() .attachmentId(ATTACHMENT_ID) .type("application/json") .bytes("{\"property\":`\"value\"}".getBytes(StandardCharsets.UTF_8)) .build(); BlobId blobId = BLOB_ID_FACTORY.from("blobId"); DAOAttachment daoAttachment = CassandraAttachmentDAOV2.from(attachment, blobId); testee.storeAttachment(daoAttachment).join(); Optional<DAOAttachment> actual = testee.getAttachment(ATTACHMENT_ID).join(); assertThat(actual).contains(daoAttachment); } }
@Override public BlobId.Factory blobIdFactory() { return new HashBlobId.Factory(); }
@Override public BlobId.Factory blobIdFactory() { return new HashBlobId.Factory(); }
@Test public void builtBlobsDAOCanStoreAndRetrieve() throws Exception { ObjectStorageBlobsDAOBuilder builder = ObjectStorageBlobsDAO .builder(testConfig) .container(containerName) .blobIdFactory(new HashBlobId.Factory()); assertBlobsDAOCanStoreAndRetrieve(builder); } }
@ParameterizedTest @ValueSource(strings = {PROJECT_CONFIG_KEY, PROJECT_DOMAIN_ID_KEY, PROJECT_DOMAIN_NAME_KEY}) public void builtBlobsDAOCanStoreAndRetrieve(String key) throws Exception { SwiftKeystone3ObjectStorage.Configuration config = configBuilders.get(key).endpoint(dockerSwift.keystoneV3Endpoint()).build(); ObjectStorageBlobsDAOBuilder builder = ObjectStorageBlobsDAO .builder(config) .container(containerName) .blobIdFactory(new HashBlobId.Factory()); assertBlobsDAOCanStoreAndRetrieve(builder); } }
@Test void containerNameIsMandatoryToBuildBlobsDAO() throws Exception { ObjectStorageBlobsDAOBuilder builder = ObjectStorageBlobsDAO .builder(testConfig) .blobIdFactory(new HashBlobId.Factory()); assertThatThrownBy(builder::build).isInstanceOf(IllegalStateException.class); }
public static Injector testInjector(Session session, CassandraTypesProvider typesProvider, CassandraMessageId.Factory messageIdFactory, CassandraConfiguration configuration) { return Guice.createInjector( Modules.combine( binder -> binder.bind(MessageId.Factory.class).toInstance(messageIdFactory), binder -> binder.bind(BlobId.Factory.class).toInstance(new HashBlobId.Factory()), binder -> binder.bind(BlobStore.class).to(CassandraBlobsDAO.class), binder -> binder.bind(Session.class).toInstance(session), binder -> binder.bind(CassandraTypesProvider.class).toInstance(typesProvider), binder -> binder.bind(CassandraConfiguration.class).toInstance(configuration))); } }
@BeforeEach void setUp(CassandraCluster cassandra) { testee = new CassandraBlobsDAO(cassandra.getConf(), CassandraConfiguration.builder() .blobPartSize(CHUNK_SIZE) .build(), new HashBlobId.Factory()); }
@Test void containerNameIsMandatoryToBuildBlobsDAO() throws Exception { ObjectStorageBlobsDAOBuilder builder = ObjectStorageBlobsDAO .builder(testConfig) .blobIdFactory(new HashBlobId.Factory()); assertThatThrownBy(builder::build).isInstanceOf(IllegalStateException.class); }
@BeforeEach void setUp(CassandraCluster cassandra) { testee = new MetricableBlobStore( metricsTestExtension.getMetricFactory(), new CassandraBlobsDAO(cassandra.getConf(), CassandraConfiguration.builder() .blobPartSize(CHUNK_SIZE) .build(), new HashBlobId.Factory())); }
@Test public void forPayloadShouldThrowOnNull() { assertThatThrownBy(() -> BLOB_ID_FACTORY.forPayload(null)) .isInstanceOf(IllegalArgumentException.class); }
@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"); }