You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by bt...@apache.org on 2022/11/22 05:49:10 UTC

[james-project] branch master updated: JAMES-3852 Test for legacy data cohabitation (#1321)

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


The following commit(s) were added to refs/heads/master by this push:
     new 8dfd27d234 JAMES-3852 Test for legacy data cohabitation (#1321)
8dfd27d234 is described below

commit 8dfd27d2341dcba3eaa0da141fad951a24fed2b6
Author: Benoit TELLIER <bt...@linagora.com>
AuthorDate: Tue Nov 22 12:49:04 2022 +0700

    JAMES-3852 Test for legacy data cohabitation (#1321)
    
    Prior storage as mailboxName needs to coexist with the mailboxPath format. Tests for this and fixes.
---
 .../CassandraSubscriptionManagerTest.java          | 117 ++++++++++++++++-----
 .../mailbox/store/StoreSubscriptionManager.java    |  14 ++-
 2 files changed, 102 insertions(+), 29 deletions(-)

diff --git a/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/CassandraSubscriptionManagerTest.java b/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/CassandraSubscriptionManagerTest.java
index 4d155e3a11..37d980a9e2 100644
--- a/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/CassandraSubscriptionManagerTest.java
+++ b/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/CassandraSubscriptionManagerTest.java
@@ -19,10 +19,13 @@
 
 package org.apache.james.mailbox.cassandra;
 
+import static org.assertj.core.api.Assertions.assertThat;
+
 import org.apache.james.backends.cassandra.CassandraClusterExtension;
 import org.apache.james.backends.cassandra.components.CassandraModule;
 import org.apache.james.backends.cassandra.init.configuration.CassandraConfiguration;
 import org.apache.james.blob.api.BlobStore;
+import org.apache.james.core.Username;
 import org.apache.james.mailbox.SubscriptionManager;
 import org.apache.james.mailbox.SubscriptionManagerContract;
 import org.apache.james.mailbox.cassandra.mail.CassandraACLMapper;
@@ -47,11 +50,17 @@ import org.apache.james.mailbox.cassandra.mail.CassandraUserMailboxRightsDAO;
 import org.apache.james.mailbox.cassandra.mail.task.RecomputeMailboxCountersService;
 import org.apache.james.mailbox.cassandra.modules.CassandraAnnotationModule;
 import org.apache.james.mailbox.cassandra.modules.CassandraSubscriptionModule;
+import org.apache.james.mailbox.model.MailboxPath;
 import org.apache.james.mailbox.store.BatchSizes;
 import org.apache.james.mailbox.store.StoreSubscriptionManager;
+import org.apache.james.mailbox.store.user.model.Subscription;
 import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.RegisterExtension;
 
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+
 /**
  * Test Cassandra subscription against some general purpose written code.
  */
@@ -63,6 +72,7 @@ class CassandraSubscriptionManagerTest implements SubscriptionManagerContract {
         CassandraAnnotationModule.MODULE));
 
     private SubscriptionManager subscriptionManager;
+    private CassandraMailboxSessionMapperFactory mailboxSessionMapperFactory;
 
     @Override
     public SubscriptionManager getSubscriptionManager() {
@@ -93,31 +103,86 @@ class CassandraSubscriptionManagerTest implements SubscriptionManagerContract {
         CassandraModSeqProvider modSeqProvider = null;
         RecomputeMailboxCountersService recomputeMailboxCountersService = null;
 
-        subscriptionManager = new StoreSubscriptionManager(
-            new CassandraMailboxSessionMapperFactory(
-                uidProvider,
-                modSeqProvider,
-                cassandraCluster.getCassandraCluster().getConf(),
-                threadDAO,
-                threadLookupDAO,
-                messageDAO,
-                messageDAOV3,
-                messageIdDAO,
-                imapUidDAO,
-                mailboxCounterDAO,
-                mailboxRecentsDAO,
-                mailboxDAO,
-                mailboxPathV3DAO,
-                firstUnseenDAO,
-                applicableFlagDAO,
-                attachmentDAOV2,
-                deletedMessageDAO,
-                blobStore,
-                attachmentMessageIdDAO,
-                aclMapper,
-                userMailboxRightsDAO,
-                recomputeMailboxCountersService,
-                CassandraConfiguration.DEFAULT_CONFIGURATION,
-                BatchSizes.defaultValues()));
+        mailboxSessionMapperFactory = new CassandraMailboxSessionMapperFactory(
+            uidProvider,
+            modSeqProvider,
+            cassandraCluster.getCassandraCluster().getConf(),
+            threadDAO,
+            threadLookupDAO,
+            messageDAO,
+            messageDAOV3,
+            messageIdDAO,
+            imapUidDAO,
+            mailboxCounterDAO,
+            mailboxRecentsDAO,
+            mailboxDAO,
+            mailboxPathV3DAO,
+            firstUnseenDAO,
+            applicableFlagDAO,
+            attachmentDAOV2,
+            deletedMessageDAO,
+            blobStore,
+            attachmentMessageIdDAO,
+            aclMapper,
+            userMailboxRightsDAO,
+            recomputeMailboxCountersService,
+            CassandraConfiguration.DEFAULT_CONFIGURATION,
+            BatchSizes.defaultValues());
+        subscriptionManager = new StoreSubscriptionManager(mailboxSessionMapperFactory);
+    }
+
+    @Test
+    void legacySubscriptionsCanBeListed() throws Exception {
+        mailboxSessionMapperFactory.createSubscriptionMapper(SESSION)
+            .save(new Subscription(SESSION.getUser(), "whatever"));
+
+        assertThat(Flux.from(subscriptionManager.subscriptionsReactive(SESSION)).collectList().block())
+            .containsOnly(MailboxPath.forUser(SESSION.getUser(), "whatever"));
+    }
+
+    @Test
+    void legacySubscriptionsCanBeRemovedReactive() throws Exception {
+        mailboxSessionMapperFactory.createSubscriptionMapper(SESSION)
+            .save(new Subscription(SESSION.getUser(), "whatever"));
+
+        Mono.from(subscriptionManager.unsubscribeReactive(MailboxPath.forUser(SESSION.getUser(), "whatever"), SESSION))
+            .block();
+
+        assertThat(Flux.from(subscriptionManager.subscriptionsReactive(SESSION)).collectList().block())
+            .isEmpty();
+    }
+
+    @Test
+    void removingADelegatedSubscriptionShouldNotUnsubscribeLegacySubscriptionReactive() throws Exception {
+        mailboxSessionMapperFactory.createSubscriptionMapper(SESSION)
+            .save(new Subscription(SESSION.getUser(), "whatever"));
+
+        Mono.from(subscriptionManager.unsubscribeReactive(MailboxPath.forUser(Username.of("alice"), "whatever"), SESSION))
+            .block();
+
+        assertThat(Flux.from(subscriptionManager.subscriptionsReactive(SESSION)).collectList().block())
+            .containsOnly(MailboxPath.forUser(SESSION.getUser(), "whatever"));
+    }
+
+    @Test
+    void legacySubscriptionsCanBeRemoved() throws Exception {
+        mailboxSessionMapperFactory.createSubscriptionMapper(SESSION)
+            .save(new Subscription(SESSION.getUser(), "whatever"));
+
+        subscriptionManager.unsubscribe(SESSION, MailboxPath.forUser(SESSION.getUser(), "whatever"));
+
+        assertThat(Flux.from(subscriptionManager.subscriptionsReactive(SESSION)).collectList().block())
+            .isEmpty();
+    }
+
+    @Test
+    void removingADelegatedSubscriptionShouldNotUnsubscribeLegacySubscription() throws Exception {
+        mailboxSessionMapperFactory.createSubscriptionMapper(SESSION)
+            .save(new Subscription(SESSION.getUser(), "whatever"));
+
+        subscriptionManager.unsubscribe(SESSION, MailboxPath.forUser(Username.of("alice"), "whatever"));
+
+        assertThat(Flux.from(subscriptionManager.subscriptionsReactive(SESSION)).collectList().block())
+            .containsOnly(MailboxPath.forUser(SESSION.getUser(), "whatever"));
     }
 }
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreSubscriptionManager.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreSubscriptionManager.java
index 3a92db7f4d..8e0f9f1df0 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreSubscriptionManager.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreSubscriptionManager.java
@@ -20,6 +20,7 @@ package org.apache.james.mailbox.store;
 
 import java.util.Collection;
 import java.util.HashSet;
+import java.util.Optional;
 import java.util.stream.Collectors;
 
 import javax.inject.Inject;
@@ -80,7 +81,12 @@ public class StoreSubscriptionManager implements SubscriptionManager {
         try {
             SubscriptionMapper mapper = mapperFactory.getSubscriptionMapper(session);
             Subscription oldSubscription = new Subscription(session.getUser(), mailbox.asEscapedString());
-            return mapper.executeReactive(mapper.deleteReactive(oldSubscription));
+            Optional<Subscription> legacyOldSubscription = Optional.of(new Subscription(session.getUser(), mailbox.getName()))
+                .filter(any -> mailbox.belongsTo(session));
+            return mapper.executeReactive(mapper.deleteReactive(oldSubscription))
+                .then(legacyOldSubscription
+                    .map(subscription -> mapper.executeReactive(mapper.deleteReactive(subscription)))
+                    .orElse(Mono.empty()));
         } catch (SubscriptionException e) {
             return Mono.error(e);
         }
@@ -109,8 +115,10 @@ public class StoreSubscriptionManager implements SubscriptionManager {
         SubscriptionMapper mapper = mapperFactory.getSubscriptionMapper(session);
         try {
             mapper.execute(Mapper.toTransaction(() -> mapper.delete(new Subscription(session.getUser(), mailbox.asEscapedString()))));
-            // Legacy purposes, remove subscriptions created prior to the MailboxPath migration. Noops for those created after.
-            mapper.execute(Mapper.toTransaction(() -> mapper.delete(new Subscription(session.getUser(), mailbox.getName()))));
+            if (mailbox.belongsTo(session)) {
+                // Legacy purposes, remove subscriptions created prior to the MailboxPath migration. Noops for those created after.
+                mapper.execute(Mapper.toTransaction(() -> mapper.delete(new Subscription(session.getUser(), mailbox.getName()))));
+            }
         } catch (MailboxException e) {
             throw new SubscriptionException(e);
         }


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org