chibenwa commented on a change in pull request #529:
URL: https://github.com/apache/james-project/pull/529#discussion_r663984411



##########
File path: 
mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/NaiveThreadIdGuessingAlgorithm.java
##########
@@ -32,7 +32,12 @@
 
 public class NaiveThreadIdGuessingAlgorithm implements 
ThreadIdGuessingAlgorithm {
     @Override
-    public ThreadId guessThreadId(Username username, MessageId messageId, 
Optional<MimeMessageId> thisMimeMessageId, Optional<MimeMessageId> inReplyTo, 
Optional<List<MimeMessageId>> references, Optional<Subject> subject) {
+    public ThreadId guessThreadId(MessageId messageId, Optional<MimeMessageId> 
thisMimeMessageId, Optional<MimeMessageId> inReplyTo, 
Optional<List<MimeMessageId>> references, Optional<Subject> subject, 
MailboxSession session) {
         return ThreadId.fromBaseMessageId(messageId);
     }
+
+    @Override
+    public Mono<ThreadId> guessThreadIdReactive(MessageId messageId, 
Optional<MimeMessageId> thisMimeMessageId, Optional<MimeMessageId> inReplyTo, 
Optional<List<MimeMessageId>> references, Optional<Subject> subject, 
MailboxSession session) {

Review comment:
       This is a new interface, let's just have the reactive version!

##########
File path: 
mailbox/scanning-search/src/test/java/org/apache/james/mailbox/store/search/SearchThreadIdGuessingAlgorithmTest.java
##########
@@ -0,0 +1,153 @@
+/******************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one     *
+ * or more contributor license agreements.  See the NOTICE file   *
+ * distributed with this work for additional information          *
+ * regarding copyright ownership.  The ASF licenses this file     *
+ * to you under the Apache License, Version 2.0 (the              *
+ * "License"); you may not use this file except in compliance     *
+ * with the License.  You may obtain a copy of the License at     *
+ *                                                                *
+ * http://www.apache.org/licenses/LICENSE-2.0                     *
+ *                                                                *
+ * Unless required by applicable law or agreed to in writing,     *
+ * software distributed under the License is distributed on an    *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY         *
+ * KIND, either express or implied.  See the License for the      *
+ * specific language governing permissions and limitations        *
+ * under the License.                                             *
+ ******************************************************************/
+
+package org.apache.james.mailbox.store.search;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.Optional;
+
+import org.apache.james.core.Username;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MessageIdManager;
+import org.apache.james.mailbox.MessageManager;
+import org.apache.james.mailbox.inmemory.InMemoryMailboxManager;
+import org.apache.james.mailbox.inmemory.InMemoryMessageId;
+import org.apache.james.mailbox.inmemory.manager.InMemoryIntegrationResources;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.ThreadId;
+import org.apache.james.mailbox.store.mail.SearchThreadIdGuessingAlgorithm;
+import org.apache.james.mailbox.store.mail.model.MimeMessageId;
+import org.apache.james.mailbox.store.mail.model.Subject;
+import org.apache.james.mime4j.dom.Message;
+import org.apache.james.mime4j.stream.RawField;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+public class SearchThreadIdGuessingAlgorithmTest {

Review comment:
       Can we extract a junit 5 contract off this?
   
   Locate the junit 5 contract in mailbox/store.

##########
File path: 
mailbox/scanning-search/src/test/java/org/apache/james/mailbox/store/search/SearchThreadIdGuessingAlgorithmTest.java
##########
@@ -0,0 +1,153 @@
+/******************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one     *
+ * or more contributor license agreements.  See the NOTICE file   *
+ * distributed with this work for additional information          *
+ * regarding copyright ownership.  The ASF licenses this file     *
+ * to you under the Apache License, Version 2.0 (the              *
+ * "License"); you may not use this file except in compliance     *
+ * with the License.  You may obtain a copy of the License at     *
+ *                                                                *
+ * http://www.apache.org/licenses/LICENSE-2.0                     *
+ *                                                                *
+ * Unless required by applicable law or agreed to in writing,     *
+ * software distributed under the License is distributed on an    *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY         *
+ * KIND, either express or implied.  See the License for the      *
+ * specific language governing permissions and limitations        *
+ * under the License.                                             *
+ ******************************************************************/
+
+package org.apache.james.mailbox.store.search;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.Optional;
+
+import org.apache.james.core.Username;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MessageIdManager;
+import org.apache.james.mailbox.MessageManager;
+import org.apache.james.mailbox.inmemory.InMemoryMailboxManager;
+import org.apache.james.mailbox.inmemory.InMemoryMessageId;
+import org.apache.james.mailbox.inmemory.manager.InMemoryIntegrationResources;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.ThreadId;
+import org.apache.james.mailbox.store.mail.SearchThreadIdGuessingAlgorithm;
+import org.apache.james.mailbox.store.mail.model.MimeMessageId;
+import org.apache.james.mailbox.store.mail.model.Subject;
+import org.apache.james.mime4j.dom.Message;
+import org.apache.james.mime4j.stream.RawField;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+public class SearchThreadIdGuessingAlgorithmTest {

Review comment:
       No... 
   
   I mean have this test suite as an interface, and default methods (we call 
that a contract). The test then implements the contract and tells which 
implementation to use. The contract is in mailbox/store but the test can be in 
mailbox/scanning-search without issues.

##########
File path: 
mailbox/scanning-search/src/test/java/org/apache/james/mailbox/store/search/SearchThreadIdGuessingAlgorithmTest.java
##########
@@ -0,0 +1,51 @@
+/******************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one     *
+ * or more contributor license agreements.  See the NOTICE file   *
+ * distributed with this work for additional information          *
+ * regarding copyright ownership.  The ASF licenses this file     *
+ * to you under the Apache License, Version 2.0 (the              *
+ * "License"); you may not use this file except in compliance     *
+ * with the License.  You may obtain a copy of the License at     *
+ *                                                                *
+ * http://www.apache.org/licenses/LICENSE-2.0                     *
+ *                                                                *
+ * Unless required by applicable law or agreed to in writing,     *
+ * software distributed under the License is distributed on an    *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY         *
+ * KIND, either express or implied.  See the License for the      *
+ * specific language governing permissions and limitations        *
+ * under the License.                                             *
+ ******************************************************************/
+
+package org.apache.james.mailbox.store.search;
+
+import org.apache.james.mailbox.inmemory.InMemoryCombinationManagerTestSystem;
+import org.apache.james.mailbox.inmemory.InMemoryMessageId;
+import org.apache.james.mailbox.inmemory.manager.InMemoryIntegrationResources;
+import org.apache.james.mailbox.model.MessageId;
+import org.apache.james.mailbox.store.AbstractThreadIdGuessingAlgorithmTest;
+import org.apache.james.mailbox.store.CombinationManagerTestSystem;
+import org.apache.james.mailbox.store.mail.SearchThreadIdGuessingAlgorithm;
+import org.apache.james.mailbox.store.mail.ThreadIdGuessingAlgorithm;
+
+public class SearchThreadIdGuessingAlgorithmTest extends 
AbstractThreadIdGuessingAlgorithmTest {

Review comment:
       `AbstractThreadIdGuessingAlgorithmTest` => 
`ThreadIdGuessingAlgorithmContract`

##########
File path: 
mailbox/store/src/test/java/org/apache/james/mailbox/store/AbstractThreadIdGuessingAlgorithmTest.java
##########
@@ -0,0 +1,186 @@
+/******************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one     *
+ * or more contributor license agreements.  See the NOTICE file   *
+ * distributed with this work for additional information          *
+ * regarding copyright ownership.  The ASF licenses this file     *
+ * to you under the Apache License, Version 2.0 (the              *
+ * "License"); you may not use this file except in compliance     *
+ * with the License.  You may obtain a copy of the License at     *
+ *                                                                *
+ * http://www.apache.org/licenses/LICENSE-2.0                     *
+ *                                                                *
+ * Unless required by applicable law or agreed to in writing,     *
+ * software distributed under the License is distributed on an    *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY         *
+ * KIND, either express or implied.  See the License for the      *
+ * specific language governing permissions and limitations        *
+ * under the License.                                             *
+ ******************************************************************/
+
+package org.apache.james.mailbox.store;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.Optional;
+
+import org.apache.james.core.Username;
+import org.apache.james.mailbox.MailboxManager;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MessageManager;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.MessageId;
+import org.apache.james.mailbox.model.ThreadId;
+import org.apache.james.mailbox.store.mail.ThreadIdGuessingAlgorithm;
+import org.apache.james.mailbox.store.mail.model.MimeMessageId;
+import org.apache.james.mailbox.store.mail.model.Subject;
+import org.apache.james.mime4j.dom.Message;
+import org.apache.james.mime4j.stream.RawField;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+public abstract class AbstractThreadIdGuessingAlgorithmTest {
+    public static final Username USER = Username.of("quan");
+    private MailboxManager mailboxManager;
+    private MessageManager inbox;
+    private ThreadIdGuessingAlgorithm testee;
+    private MailboxSession mailboxSession;
+    private CombinationManagerTestSystem testingData;
+    private MessageId newBasedMessageId;
+
+
+    protected abstract CombinationManagerTestSystem createTestingData();
+    
+    protected abstract ThreadIdGuessingAlgorithm 
initThreadIdGuessingAlgorithm(CombinationManagerTestSystem testingData);
+
+    protected abstract MessageId initNewBasedMessageId();
+
+    @BeforeEach
+    void setUp() throws Exception {
+        testingData = createTestingData();
+        testee = initThreadIdGuessingAlgorithm(testingData);
+        newBasedMessageId = initNewBasedMessageId();
+
+        mailboxManager = testingData.getMailboxManager();
+        mailboxSession = mailboxManager.createSystemSession(USER);
+        mailboxManager.createMailbox(MailboxPath.inbox(USER), mailboxSession);
+        inbox = mailboxManager.getMailbox(MailboxPath.inbox(USER), 
mailboxSession);
+    }
+
+    @Test
+    void 
givenNonMailWhenAddAMailThenGuessingThreadIdShouldBasedOnGeneratedMessageId() 
throws Exception {
+        ThreadId threadId = testee.guessThreadIdReactive(newBasedMessageId, 
Optional.of(new MimeMessageId("abc")), Optional.empty(), Optional.empty(), 
Optional.of(new Subject("test")), mailboxSession).block();
+
+        assertThat(threadId.getBaseMessageId()).isEqualTo(newBasedMessageId);
+    }
+
+    @Test
+    void 
givenOldMailWhenAddNewRelatedMailsThenGuessingThreadIdShouldReturnSameThreadIdWithOldMail()
 throws Exception {
+        // given old mail
+        MessageManager.AppendResult message = 
inbox.appendMessage(MessageManager.AppendCommand.from(Message.Builder.of()
+            .setSubject("Test")
+            .setMessageId("Message-ID")
+            .setField(new RawField("In-Reply-To", "someInReplyTo"))
+            .addField(new RawField("References", "references1"))
+            .addField(new RawField("References", "references2"))
+            .setBody("testmail", StandardCharsets.UTF_8)), mailboxSession);
+
+        // add mails related to old message by subject and Message-ID (but 
this should not happen in real world cause every mail should have an unique 
MimeMessageId)
+        ThreadId threadId1 = testee.guessThreadIdReactive(newBasedMessageId, 
Optional.of(new MimeMessageId("Message-ID")), Optional.empty(), 
Optional.empty(), Optional.of(new Subject("Re: Test")), mailboxSession).block();
+        ThreadId threadId2 = testee.guessThreadIdReactive(newBasedMessageId, 
Optional.of(new MimeMessageId("someInReplyTo")), Optional.empty(), 
Optional.empty(), Optional.of(new Subject("Re: Test")), mailboxSession).block();
+        ThreadId threadId3 = testee.guessThreadIdReactive(newBasedMessageId, 
Optional.of(new MimeMessageId("references1")), Optional.empty(), 
Optional.empty(), Optional.of(new Subject("Re: Test")), mailboxSession).block();
+
+        // add mails related to old message by subject and In-Reply-To
+        ThreadId threadId4 = testee.guessThreadIdReactive(newBasedMessageId, 
Optional.empty(), Optional.of(new MimeMessageId("Message-ID")), 
Optional.empty(), Optional.of(new Subject("Re: Test")), mailboxSession).block();
+        ThreadId threadId5 = testee.guessThreadIdReactive(newBasedMessageId, 
Optional.empty(), Optional.of(new MimeMessageId("someInReplyTo")), 
Optional.empty(), Optional.of(new Subject("Re: Test")), mailboxSession).block();
+        ThreadId threadId6 = testee.guessThreadIdReactive(newBasedMessageId, 
Optional.empty(), Optional.of(new MimeMessageId("references2")), 
Optional.empty(), Optional.of(new Subject("Fwd: Re: Test")), 
mailboxSession).block();
+
+        // add mails related to old message by subject and References
+        ThreadId threadId7 = testee.guessThreadIdReactive(newBasedMessageId, 
Optional.empty(), Optional.empty(), Optional.of(List.of(new 
MimeMessageId("Message-ID"))), Optional.of(new Subject("Fwd: Re: Test")), 
mailboxSession).block();
+        ThreadId threadId8 = testee.guessThreadIdReactive(newBasedMessageId, 
Optional.empty(), Optional.empty(), Optional.of(List.of(new 
MimeMessageId("someInReplyTo"))), Optional.of(new Subject("Fwd: Re: Test")), 
mailboxSession).block();
+        ThreadId threadId9 = testee.guessThreadIdReactive(newBasedMessageId, 
Optional.empty(), Optional.empty(), Optional.of(List.of(new 
MimeMessageId("references1"), new MimeMessageId("NonRelated-references2"))), 
Optional.of(new Subject("Fwd: Re: Test")), mailboxSession).block();
+        ThreadId threadId10 = testee.guessThreadIdReactive(newBasedMessageId, 
Optional.empty(), Optional.empty(), Optional.of(List.of(new 
MimeMessageId("NonRelated-references1"), new MimeMessageId("references2"))), 
Optional.of(new Subject("Fwd: Re: Test")), mailboxSession).block();
+        ThreadId threadId11 = testee.guessThreadIdReactive(newBasedMessageId, 
Optional.empty(), Optional.empty(), Optional.of(List.of(new 
MimeMessageId("references1"), new MimeMessageId("references2"))), 
Optional.of(new Subject("Fwd: Re: Test")), mailboxSession).block();
+
+        // guess ThreadId should return the same ThreadId with old message
+        assertThat(threadId1).isEqualTo(message.getThreadId());
+        assertThat(threadId2).isEqualTo(message.getThreadId());
+        assertThat(threadId3).isEqualTo(message.getThreadId());
+        assertThat(threadId4).isEqualTo(message.getThreadId());
+        assertThat(threadId5).isEqualTo(message.getThreadId());
+        assertThat(threadId6).isEqualTo(message.getThreadId());
+        assertThat(threadId7).isEqualTo(message.getThreadId());
+        assertThat(threadId8).isEqualTo(message.getThreadId());
+        assertThat(threadId9).isEqualTo(message.getThreadId());
+        assertThat(threadId10).isEqualTo(message.getThreadId());
+        assertThat(threadId11).isEqualTo(message.getThreadId());

Review comment:
       Maybe you could have a look to parametrized tests to be making this more 
readable?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to