This is an automated email from the ASF dual-hosted git repository.

rcordier 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 3bb549a593 Provision Current Quota when MailboxAdded event
3bb549a593 is described below

commit 3bb549a593a1c197063a53c6d48e810ed195c298
Author: vttran <vtt...@linagora.com>
AuthorDate: Thu Mar 14 13:33:10 2024 +0700

    Provision Current Quota when MailboxAdded event
---
 .../store/quota/ListeningCurrentQuotaUpdater.java  | 22 ++++++++++++-
 .../quota/ListeningCurrentQuotaUpdaterTest.java    | 38 ++++++++++++++++++++++
 2 files changed, 59 insertions(+), 1 deletion(-)

diff --git 
a/mailbox/store/src/main/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdater.java
 
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdater.java
index e6b158e34c..99062971bc 100644
--- 
a/mailbox/store/src/main/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdater.java
+++ 
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdater.java
@@ -30,8 +30,10 @@ import org.apache.james.events.EventBus;
 import org.apache.james.events.EventListener;
 import org.apache.james.events.Group;
 import org.apache.james.events.RegistrationKey;
+import org.apache.james.mailbox.events.MailboxEvents;
 import org.apache.james.mailbox.events.MailboxEvents.Added;
 import org.apache.james.mailbox.events.MailboxEvents.Expunged;
+import org.apache.james.mailbox.events.MailboxEvents.MailboxAdded;
 import org.apache.james.mailbox.events.MailboxEvents.MailboxDeletion;
 import org.apache.james.mailbox.events.MailboxEvents.MetaDataHoldingEvent;
 import org.apache.james.mailbox.model.QuotaOperation;
@@ -74,7 +76,10 @@ public class ListeningCurrentQuotaUpdater implements 
EventListener.ReactiveGroup
 
     @Override
     public boolean isHandling(Event event) {
-        return event instanceof Added || event instanceof Expunged || event 
instanceof MailboxDeletion;
+        return event instanceof Added
+            || event instanceof Expunged
+            || event instanceof MailboxDeletion
+            || event instanceof MailboxAdded;
     }
 
     @Override
@@ -90,6 +95,9 @@ public class ListeningCurrentQuotaUpdater implements 
EventListener.ReactiveGroup
         } else if (event instanceof MailboxDeletion) {
             MailboxDeletion mailboxDeletionEvent = (MailboxDeletion) event;
             return handleMailboxDeletionEvent(mailboxDeletionEvent);
+        } else if (event instanceof MailboxAdded) {
+            MailboxEvents.MailboxAdded mailboxAdded = 
(MailboxEvents.MailboxAdded) event;
+            return handleMailboxAddedEvent(mailboxAdded);
         }
         return Mono.empty();
     }
@@ -149,4 +157,16 @@ public class ListeningCurrentQuotaUpdater implements 
EventListener.ReactiveGroup
         return Mono.empty();
     }
 
+    private Mono<Void> handleMailboxAddedEvent(MailboxAdded mailboxAdded) {
+        return provisionCurrentQuota(mailboxAdded);
+    }
+
+    private Mono<Void> provisionCurrentQuota(MailboxAdded mailboxAdded) {
+        return 
Mono.from(quotaRootResolver.getQuotaRootReactive(mailboxAdded.getMailboxPath()))
+            .flatMap(quotaRoot -> 
Mono.from(currentQuotaManager.getCurrentQuotas(quotaRoot))
+                .map(any -> quotaRoot)
+                .switchIfEmpty(Mono.defer(() -> 
Mono.from(currentQuotaManager.setCurrentQuotas(new QuotaOperation(quotaRoot, 
QuotaCountUsage.count(0), QuotaSizeUsage.ZERO)))
+                    .thenReturn(quotaRoot))))
+            .then();
+    }
 }
\ No newline at end of file
diff --git 
a/mailbox/store/src/test/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdaterTest.java
 
b/mailbox/store/src/test/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdaterTest.java
index bd0c937944..b01935faa1 100644
--- 
a/mailbox/store/src/test/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdaterTest.java
+++ 
b/mailbox/store/src/test/java/org/apache/james/mailbox/store/quota/ListeningCurrentQuotaUpdaterTest.java
@@ -42,9 +42,11 @@ import org.apache.james.events.EventBus;
 import org.apache.james.events.Group;
 import org.apache.james.mailbox.MessageUid;
 import org.apache.james.mailbox.ModSeq;
+import org.apache.james.mailbox.events.MailboxEvents;
 import org.apache.james.mailbox.events.MailboxEvents.Added;
 import org.apache.james.mailbox.events.MailboxEvents.Expunged;
 import org.apache.james.mailbox.events.MailboxEvents.MailboxDeletion;
+import org.apache.james.mailbox.model.CurrentQuotas;
 import org.apache.james.mailbox.model.MailboxId;
 import org.apache.james.mailbox.model.MailboxPath;
 import org.apache.james.mailbox.model.MessageMetaData;
@@ -194,4 +196,40 @@ class ListeningCurrentQuotaUpdaterTest {
 
         verifyNoMoreInteractions(mockedCurrentQuotaManager);
     }
+
+    @Test
+    void mailboxAddEventShouldProvisionCurrentQuota() throws Exception {
+        QuotaOperation operation = new QuotaOperation(QUOTA_ROOT, 
QuotaCountUsage.count(0), QuotaSizeUsage.size(0));
+
+        MailboxEvents.MailboxAdded added;
+        added = mock(MailboxEvents.MailboxAdded.class);
+
+        when(added.getMailboxId()).thenReturn(MAILBOX_ID);
+        when(added.getMailboxPath()).thenReturn(MAILBOX_PATH);
+        when(added.getUsername()).thenReturn(USERNAME_BENWA);
+        when(mockedQuotaRootResolver.getQuotaRootReactive(eq(MAILBOX_PATH)))
+            .thenReturn(Mono.just(QUOTA_ROOT));
+        
when(mockedCurrentQuotaManager.getCurrentQuotas(QUOTA_ROOT)).thenAnswer(any -> 
Mono.empty());
+        
when(mockedCurrentQuotaManager.setCurrentQuotas(operation)).thenAnswer(any -> 
Mono.empty());
+
+        testee.event(added);
+
+        verify(mockedCurrentQuotaManager).setCurrentQuotas(operation);
+    }
+
+    @Test
+    void mailboxAddEventShouldNotProvisionWhenAlreadyExist() throws Exception {
+        MailboxEvents.MailboxAdded added = 
mock(MailboxEvents.MailboxAdded.class);
+        when(added.getMailboxId()).thenReturn(MAILBOX_ID);
+        when(added.getMailboxPath()).thenReturn(MAILBOX_PATH);
+        when(added.getUsername()).thenReturn(USERNAME_BENWA);
+        when(mockedQuotaRootResolver.getQuotaRootReactive(eq(MAILBOX_PATH)))
+            .thenReturn(Mono.just(QUOTA_ROOT));
+        when(mockedCurrentQuotaManager.getCurrentQuotas(QUOTA_ROOT))
+            .thenAnswer(any -> Mono.just(CurrentQuotas.from(QUOTA)));
+
+        testee.event(added);
+
+        verify(mockedCurrentQuotaManager, never()).setCurrentQuotas(any());
+    }
 }


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

Reply via email to