You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2020/04/29 09:02:38 UTC
[james-project] 02/05: JAMES-2810 Use Cassandra TestingSession
instrumentation instead of mocks
This is an automated email from the ASF dual-hosted git repository.
btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 61f667d634253de0e89dba11577a7aea3775a36a
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Sat Apr 25 12:48:04 2020 +0700
JAMES-2810 Use Cassandra TestingSession instrumentation instead of mocks
---
.../CassandraDeletedMessageMetadataVaultTest.java | 135 ++++++++++-----------
1 file changed, 66 insertions(+), 69 deletions(-)
diff --git a/mailbox/plugin/deleted-messages-vault-cassandra/src/test/java/org/apache/james/vault/metadata/CassandraDeletedMessageMetadataVaultTest.java b/mailbox/plugin/deleted-messages-vault-cassandra/src/test/java/org/apache/james/vault/metadata/CassandraDeletedMessageMetadataVaultTest.java
index 172385c..2ab2832 100644
--- a/mailbox/plugin/deleted-messages-vault-cassandra/src/test/java/org/apache/james/vault/metadata/CassandraDeletedMessageMetadataVaultTest.java
+++ b/mailbox/plugin/deleted-messages-vault-cassandra/src/test/java/org/apache/james/vault/metadata/CassandraDeletedMessageMetadataVaultTest.java
@@ -19,17 +19,13 @@
package org.apache.james.vault.metadata;
-import static org.apache.james.vault.DeletedMessageFixture.MESSAGE_ID;
+import static org.apache.james.backends.cassandra.Scenario.Builder.fail;
import static org.apache.james.vault.DeletedMessageFixture.USERNAME;
import static org.apache.james.vault.metadata.DeletedMessageMetadataModule.MODULE;
import static org.apache.james.vault.metadata.DeletedMessageVaultMetadataFixture.BUCKET_NAME;
import static org.apache.james.vault.metadata.DeletedMessageVaultMetadataFixture.DELETED_MESSAGE;
import static org.apache.james.vault.metadata.DeletedMessageVaultMetadataFixture.DELETED_MESSAGE_2;
-import static org.apache.james.vault.metadata.DeletedMessageVaultMetadataFixture.STORAGE_INFORMATION;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.reset;
-import static org.mockito.Mockito.spy;
-import static org.mockito.Mockito.when;
import java.util.Optional;
import java.util.stream.Stream;
@@ -64,9 +60,9 @@ public class CassandraDeletedMessageMetadataVaultTest implements DeletedMessageM
InMemoryMessageId.Factory messageIdFactory = new InMemoryMessageId.Factory();
DeletedMessageWithStorageInformationConverter dtoConverter = new DeletedMessageWithStorageInformationConverter(blobIdFactory, messageIdFactory, new InMemoryId.Factory());
- metadataDAO = spy(new MetadataDAO(cassandra.getConf(), messageIdFactory, new MetadataSerializer(dtoConverter)));
- storageInformationDAO = spy(new StorageInformationDAO(cassandra.getConf(), blobIdFactory));
- userPerBucketDAO = spy(new UserPerBucketDAO(cassandra.getConf()));
+ metadataDAO = new MetadataDAO(cassandra.getConf(), messageIdFactory, new MetadataSerializer(dtoConverter));
+ storageInformationDAO = new StorageInformationDAO(cassandra.getConf(), blobIdFactory);
+ userPerBucketDAO = new UserPerBucketDAO(cassandra.getConf());
testee = new CassandraDeletedMessageMetadataVault(metadataDAO, storageInformationDAO, userPerBucketDAO);
}
@@ -79,9 +75,11 @@ public class CassandraDeletedMessageMetadataVaultTest implements DeletedMessageM
@Nested
class ConsistencyTest {
@Test
- void listShouldNotReturnMessagesWhenStorageDAOFailed() {
- when(storageInformationDAO.referenceStorageInformation(USERNAME, MESSAGE_ID, STORAGE_INFORMATION))
- .thenReturn(Mono.error(new RuntimeException()));
+ void listShouldNotReturnMessagesWhenStorageDAOFailed(CassandraCluster cassandra) {
+ cassandra.getConf()
+ .registerScenario(fail()
+ .times(1)
+ .whenQueryStartsWith("INSERT INTO storageInformation (owner,messageId,bucketName,blobId) VALUES (:owner,:messageId,:bucketName,:blobId);"));
try {
Mono.from(testee.store(DELETED_MESSAGE)).block();
@@ -94,9 +92,11 @@ public class CassandraDeletedMessageMetadataVaultTest implements DeletedMessageM
}
@Test
- void listShouldNotReturnMessagesWhenMetadataDAOFailed() {
- when(metadataDAO.store(DELETED_MESSAGE))
- .thenReturn(Mono.error(new RuntimeException()));
+ void listShouldNotReturnMessagesWhenMetadataDAOFailed(CassandraCluster cassandra) {
+ cassandra.getConf()
+ .registerScenario(fail()
+ .times(1)
+ .whenQueryStartsWith("INSERT INTO deletedMessageMetadata (bucketName,owner,messageId,payload) VALUES (:bucketName,:owner,:messageId,:payload);"));
try {
Mono.from(testee.store(DELETED_MESSAGE)).block();
@@ -109,9 +109,11 @@ public class CassandraDeletedMessageMetadataVaultTest implements DeletedMessageM
}
@Test
- void listShouldReturnMessagesWhenUserPerBucketDAOFailed() {
- when(userPerBucketDAO.addUser(BUCKET_NAME, USERNAME))
- .thenReturn(Mono.error(new RuntimeException()));
+ void listShouldReturnMessagesWhenUserPerBucketDAOFailed(CassandraCluster cassandra) {
+ cassandra.getConf()
+ .registerScenario(fail()
+ .times(1)
+ .whenQueryStartsWith("INSERT INTO userPerBucket (bucketName,user) VALUES (:bucketName,:user);"));
try {
Mono.from(testee.store(DELETED_MESSAGE)).block();
@@ -124,9 +126,11 @@ public class CassandraDeletedMessageMetadataVaultTest implements DeletedMessageM
}
@Test
- void retrieveStorageInformationShouldReturnMetadataWhenUserPerBucketDAOStoreFailed() {
- when(userPerBucketDAO.addUser(BUCKET_NAME, USERNAME))
- .thenReturn(Mono.error(new RuntimeException()));
+ void retrieveStorageInformationShouldReturnMetadataWhenUserPerBucketDAOStoreFailed(CassandraCluster cassandra) {
+ cassandra.getConf()
+ .registerScenario(fail()
+ .times(1)
+ .whenQueryStartsWith("INSERT INTO userPerBucket (bucketName,user) VALUES (:bucketName,:user);"));
try {
Mono.from(testee.store(DELETED_MESSAGE)).block();
@@ -143,9 +147,11 @@ public class CassandraDeletedMessageMetadataVaultTest implements DeletedMessageM
@Disabled("The bucket being not referenced, the entry will not be dropped. Note that this get corrected by next " +
"metadata referenced for this user")
@Test
- void removingBucketShouldCleanUpInvalidStateForList() {
- when(userPerBucketDAO.addUser(BUCKET_NAME, USERNAME))
- .thenReturn(Mono.error(new RuntimeException()));
+ void removingBucketShouldCleanUpInvalidStateForList(CassandraCluster cassandra) {
+ cassandra.getConf()
+ .registerScenario(fail()
+ .times(1)
+ .whenQueryStartsWith("INSERT INTO userPerBucket (bucketName,user) VALUES (:bucketName,:user);"));
try {
Mono.from(testee.store(DELETED_MESSAGE)).block();
@@ -162,9 +168,11 @@ public class CassandraDeletedMessageMetadataVaultTest implements DeletedMessageM
@Disabled("The bucket being not referenced, the entry will not be dropped. Note that this get corrected by next " +
"metadata referenced for this user")
@Test
- void removingBucketShouldCleanUpInvalidStateForRetrievingMetadata() {
- when(userPerBucketDAO.addUser(BUCKET_NAME, USERNAME))
- .thenReturn(Mono.error(new RuntimeException()));
+ void removingBucketShouldCleanUpInvalidStateForRetrievingMetadata(CassandraCluster cassandra) {
+ cassandra.getConf()
+ .registerScenario(fail()
+ .times(1)
+ .whenQueryStartsWith("INSERT INTO userPerBucket (bucketName,user) VALUES (:bucketName,:user);"));
try {
Mono.from(testee.store(DELETED_MESSAGE)).block();
@@ -181,35 +189,18 @@ public class CassandraDeletedMessageMetadataVaultTest implements DeletedMessageM
}
@Test
- void removingBucketShouldBeEventuallyConsistentForList() {
- when(userPerBucketDAO.addUser(BUCKET_NAME, USERNAME))
- .thenReturn(Mono.error(new RuntimeException()));
+ void removingBucketShouldBeEventuallyConsistentForList(CassandraCluster cassandra) {
+ cassandra.getConf()
+ .registerScenario(fail()
+ .times(1)
+ .whenQueryStartsWith("INSERT INTO userPerBucket (bucketName,user) VALUES (:bucketName,:user);"));
try {
Mono.from(testee.store(DELETED_MESSAGE)).block();
} catch (Exception e) {
// ignored
}
- reset(userPerBucketDAO);
- Mono.from(testee.store(DELETED_MESSAGE_2)).block();
-
- Mono.from(testee.removeMetadataRelatedToBucket(BUCKET_NAME)).block();
-
- Stream<DeletedMessageWithStorageInformation> messages = Flux.from(metadataVault().listMessages(BUCKET_NAME, USERNAME)).toStream();
- assertThat(messages).isEmpty();
- }
- @Test
- void removingBucketShouldBeEventuallyConsistentForMetadata() {
- when(userPerBucketDAO.addUser(BUCKET_NAME, USERNAME))
- .thenReturn(Mono.error(new RuntimeException()));
-
- try {
- Mono.from(testee.store(DELETED_MESSAGE)).block();
- } catch (Exception e) {
- // ignored
- }
- reset(userPerBucketDAO);
Mono.from(testee.store(DELETED_MESSAGE_2)).block();
Mono.from(testee.removeMetadataRelatedToBucket(BUCKET_NAME)).block();
@@ -221,9 +212,11 @@ public class CassandraDeletedMessageMetadataVaultTest implements DeletedMessageM
}
@Test
- void directDeletionShouldCleanUpInvalidStateForList() {
- when(userPerBucketDAO.addUser(BUCKET_NAME, USERNAME))
- .thenReturn(Mono.error(new RuntimeException()));
+ void directDeletionShouldCleanUpInvalidStateForList(CassandraCluster cassandra) {
+ cassandra.getConf()
+ .registerScenario(fail()
+ .times(1)
+ .whenQueryStartsWith("INSERT INTO userPerBucket (bucketName,user) VALUES (:bucketName,:user);"));
try {
Mono.from(testee.store(DELETED_MESSAGE)).block();
@@ -241,9 +234,11 @@ public class CassandraDeletedMessageMetadataVaultTest implements DeletedMessageM
}
@Test
- void directDeletionShouldCleanUpInvalidStateForRetrievingMetadata() {
- when(userPerBucketDAO.addUser(BUCKET_NAME, USERNAME))
- .thenReturn(Mono.error(new RuntimeException()));
+ void directDeletionShouldCleanUpInvalidStateForRetrievingMetadata(CassandraCluster cassandra) {
+ cassandra.getConf()
+ .registerScenario(fail()
+ .times(1)
+ .whenQueryStartsWith("INSERT INTO userPerBucket (bucketName,user) VALUES (:bucketName,:user);"));
try {
Mono.from(testee.store(DELETED_MESSAGE)).block();
@@ -263,19 +258,19 @@ public class CassandraDeletedMessageMetadataVaultTest implements DeletedMessageM
}
@Test
- void retentionShouldBeRetriableWhenUserPerBucketDAOFails() {
+ void retentionShouldBeRetriableWhenUserPerBucketDAOFails(CassandraCluster cassandra) {
+ cassandra.getConf()
+ .registerScenario(fail()
+ .times(1)
+ .whenQueryStartsWith("DELETE FROM userPerBucket WHERE bucketName=:bucketName;"));
Mono.from(testee.store(DELETED_MESSAGE)).block();
- when(userPerBucketDAO.deleteBucket(BUCKET_NAME))
- .thenReturn(Mono.error(new RuntimeException()));
-
try {
Mono.from(testee.removeMetadataRelatedToBucket(BUCKET_NAME)).block();
} catch (Exception e) {
// ignored
}
- reset(userPerBucketDAO);
Mono.from(testee.removeMetadataRelatedToBucket(BUCKET_NAME)).block();
Stream<DeletedMessageWithStorageInformation> messages = Flux.from(metadataVault().listMessages(BUCKET_NAME, USERNAME)).toStream();
@@ -283,11 +278,13 @@ public class CassandraDeletedMessageMetadataVaultTest implements DeletedMessageM
}
@Test
- void retentionShouldBeRetriableWhenMetadataDAOFails() {
- Mono.from(testee.store(DELETED_MESSAGE)).block();
+ void retentionShouldBeRetriableWhenMetadataDAOFails(CassandraCluster cassandra) {
+ cassandra.getConf()
+ .registerScenario(fail()
+ .times(1)
+ .whenQueryStartsWith("DELETE FROM deletedMessageMetadata WHERE owner=:owner AND bucketName=:bucketName;"));
- when(metadataDAO.deleteInBucket(BUCKET_NAME, USERNAME))
- .thenReturn(Mono.error(new RuntimeException()));
+ Mono.from(testee.store(DELETED_MESSAGE)).block();
try {
Mono.from(testee.removeMetadataRelatedToBucket(BUCKET_NAME)).block();
@@ -295,7 +292,6 @@ public class CassandraDeletedMessageMetadataVaultTest implements DeletedMessageM
// ignored
}
- reset(metadataDAO);
Mono.from(testee.removeMetadataRelatedToBucket(BUCKET_NAME)).block();
Stream<DeletedMessageWithStorageInformation> messages = Flux.from(metadataVault().listMessages(BUCKET_NAME, USERNAME)).toStream();
@@ -303,11 +299,13 @@ public class CassandraDeletedMessageMetadataVaultTest implements DeletedMessageM
}
@Test
- void retentionShouldBeRetriableWhenStorageInformationDAOFails() {
- Mono.from(testee.store(DELETED_MESSAGE)).block();
+ void retentionShouldBeRetriableWhenStorageInformationDAOFails(CassandraCluster cassandra) {
+ cassandra.getConf()
+ .registerScenario(fail()
+ .times(1)
+ .whenQueryStartsWith("DELETE FROM storageInformation WHERE owner=:owner AND messageId=:messageId;"));
- when(storageInformationDAO.deleteStorageInformation(USERNAME, MESSAGE_ID))
- .thenReturn(Mono.error(new RuntimeException()));
+ Mono.from(testee.store(DELETED_MESSAGE)).block();
try {
Mono.from(testee.removeMetadataRelatedToBucket(BUCKET_NAME)).block();
@@ -315,7 +313,6 @@ public class CassandraDeletedMessageMetadataVaultTest implements DeletedMessageM
// ignored
}
- reset(storageInformationDAO);
Mono.from(testee.removeMetadataRelatedToBucket(BUCKET_NAME)).block();
Stream<DeletedMessageWithStorageInformation> messages = Flux.from(metadataVault().listMessages(BUCKET_NAME, USERNAME)).toStream();
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org