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 575cac3b4a2d1e5814fcf6f46512c92bc9ed5928 Author: Benoit Tellier <[email protected]> AuthorDate: Wed Nov 27 10:08:53 2019 +0700 JAMES-2988 Full unit test coverage for FetchGroup generation in IMAP --- protocols/imap/pom.xml | 5 ++ .../apache/james/imap/api/message/FetchData.java | 33 ++++++--- .../imap/processor/fetch/FetchDataConverter.java | 83 ++++++++++++++++++++++ .../james/imap/processor/fetch/FetchProcessor.java | 57 +-------------- .../processor/fetch/FetchDataConverterTest.java | 79 ++++++++++++++++++++ 5 files changed, 190 insertions(+), 67 deletions(-) diff --git a/protocols/imap/pom.xml b/protocols/imap/pom.xml index 000e30b..bf957e0 100644 --- a/protocols/imap/pom.xml +++ b/protocols/imap/pom.xml @@ -90,6 +90,11 @@ <artifactId>commons-lang3</artifactId> </dependency> <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-params</artifactId> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <scope>test</scope> diff --git a/protocols/imap/src/main/java/org/apache/james/imap/api/message/FetchData.java b/protocols/imap/src/main/java/org/apache/james/imap/api/message/FetchData.java index 7e836b7..bc5095d 100644 --- a/protocols/imap/src/main/java/org/apache/james/imap/api/message/FetchData.java +++ b/protocols/imap/src/main/java/org/apache/james/imap/api/message/FetchData.java @@ -58,56 +58,63 @@ public class FetchData { return body; } - public void setBody(boolean body) { + public FetchData setBody(boolean body) { this.body = body; + return this; } public boolean isBodyStructure() { return bodyStructure; } - public void setBodyStructure(boolean bodyStructure) { + public FetchData setBodyStructure(boolean bodyStructure) { this.bodyStructure = bodyStructure; + return this; } public boolean isEnvelope() { return envelope; } - public void setEnvelope(boolean envelope) { + public FetchData setEnvelope(boolean envelope) { this.envelope = envelope; + return this; } public boolean isFlags() { return flags; } - public void setFlags(boolean flags) { + public FetchData setFlags(boolean flags) { this.flags = flags; + return this; } public boolean isInternalDate() { return internalDate; } - public void setInternalDate(boolean internalDate) { + public FetchData setInternalDate(boolean internalDate) { this.internalDate = internalDate; + return this; } public boolean isSize() { return size; } - public void setSize(boolean size) { + public FetchData setSize(boolean size) { this.size = size; + return this; } public boolean isUid() { return uid; } - public void setUid(boolean uid) { + public FetchData setUid(boolean uid) { this.uid = uid; + return this; } public boolean isSetSeen() { @@ -119,13 +126,15 @@ public class FetchData { return modSeq; } - public void setModSeq(boolean modSeq) { + public FetchData setModSeq(boolean modSeq) { this.modSeq = modSeq; + return this; } - public void setChangedSince(long changedSince) { + public FetchData setChangedSince(long changedSince) { this.changedSince = changedSince; this.modSeq = true; + return this; } public long getChangedSince() { @@ -137,8 +146,9 @@ public class FetchData { * * @param vanished */ - public void setVanished(boolean vanished) { + public FetchData setVanished(boolean vanished) { this.vanished = vanished; + return this; } /** @@ -150,11 +160,12 @@ public class FetchData { return vanished; } - public void add(BodyFetchElement element, boolean peek) { + public FetchData add(BodyFetchElement element, boolean peek) { if (!peek) { setSeen = true; } bodyElements.add(element); + return this; } public int hashCode() { diff --git a/protocols/imap/src/main/java/org/apache/james/imap/processor/fetch/FetchDataConverter.java b/protocols/imap/src/main/java/org/apache/james/imap/processor/fetch/FetchDataConverter.java new file mode 100644 index 0000000..54d4b5e --- /dev/null +++ b/protocols/imap/src/main/java/org/apache/james/imap/processor/fetch/FetchDataConverter.java @@ -0,0 +1,83 @@ +/**************************************************************** + * 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.imap.processor.fetch; + +import java.util.Collection; + +import org.apache.james.imap.api.message.BodyFetchElement; +import org.apache.james.imap.api.message.FetchData; +import org.apache.james.mailbox.model.FetchGroup; +import org.apache.james.mailbox.model.MimePath; + +class FetchDataConverter { + + static FetchGroup getFetchGroup(FetchData fetch) { + FetchGroup result = FetchGroup.MINIMAL; + + if (fetch.isEnvelope()) { + result = result.or(FetchGroup.HEADERS_MASK); + } + if (fetch.isBody() || fetch.isBodyStructure()) { + result = result.or(FetchGroup.MIME_DESCRIPTOR_MASK); + } + + Collection<BodyFetchElement> bodyElements = fetch.getBodyElements(); + if (bodyElements != null) { + for (BodyFetchElement element : bodyElements) { + final int sectionType = element.getSectionType(); + final int[] path = element.getPath(); + final boolean isBase = (path == null || path.length == 0); + switch (sectionType) { + case BodyFetchElement.CONTENT: + if (isBase) { + result = addContent(result, path, isBase, FetchGroup.FULL_CONTENT_MASK); + } else { + result = addContent(result, path, isBase, FetchGroup.MIME_CONTENT_MASK); + } + break; + case BodyFetchElement.HEADER: + case BodyFetchElement.HEADER_NOT_FIELDS: + case BodyFetchElement.HEADER_FIELDS: + result = addContent(result, path, isBase, FetchGroup.HEADERS_MASK); + break; + case BodyFetchElement.MIME: + result = addContent(result, path, isBase, FetchGroup.MIME_HEADERS_MASK); + break; + case BodyFetchElement.TEXT: + result = addContent(result, path, isBase, FetchGroup.BODY_CONTENT_MASK); + break; + default: + break; + } + + } + } + return result; + } + + private static FetchGroup addContent(FetchGroup result, int[] path, boolean isBase, int content) { + if (isBase) { + return result.or(content); + } else { + MimePath mimePath = new MimePath(path); + return result.addPartContent(mimePath, content); + } + } +} diff --git a/protocols/imap/src/main/java/org/apache/james/imap/processor/fetch/FetchProcessor.java b/protocols/imap/src/main/java/org/apache/james/imap/processor/fetch/FetchProcessor.java index ab86624..1ee1a24 100644 --- a/protocols/imap/src/main/java/org/apache/james/imap/processor/fetch/FetchProcessor.java +++ b/protocols/imap/src/main/java/org/apache/james/imap/processor/fetch/FetchProcessor.java @@ -21,13 +21,11 @@ package org.apache.james.imap.processor.fetch; import java.io.Closeable; import java.util.ArrayList; -import java.util.Collection; import java.util.List; import org.apache.james.imap.api.ImapConstants; import org.apache.james.imap.api.ImapSessionUtils; import org.apache.james.imap.api.display.HumanReadableText; -import org.apache.james.imap.api.message.BodyFetchElement; import org.apache.james.imap.api.message.FetchData; import org.apache.james.imap.api.message.IdRange; import org.apache.james.imap.api.message.response.StatusResponseFactory; @@ -47,7 +45,6 @@ import org.apache.james.mailbox.model.FetchGroup; import org.apache.james.mailbox.model.MessageRange; import org.apache.james.mailbox.model.MessageResult; import org.apache.james.mailbox.model.MessageResultIterator; -import org.apache.james.mailbox.model.MimePath; import org.apache.james.metrics.api.MetricFactory; import org.apache.james.util.MDCBuilder; import org.slf4j.Logger; @@ -148,7 +145,7 @@ public class FetchProcessor extends AbstractMailboxProcessor<FetchRequest> { */ protected void processMessageRanges(ImapSession session, MessageManager mailbox, List<MessageRange> ranges, FetchData fetch, boolean useUids, MailboxSession mailboxSession, Responder responder) throws MailboxException { final FetchResponseBuilder builder = new FetchResponseBuilder(new EnvelopeBuilder()); - FetchGroup resultToFetch = getFetchGroup(fetch); + FetchGroup resultToFetch = FetchDataConverter.getFetchGroup(fetch); for (MessageRange range : ranges) { MessageResultIterator messages = mailbox.getMessages(range, resultToFetch, mailboxSession); @@ -184,58 +181,6 @@ public class FetchProcessor extends AbstractMailboxProcessor<FetchRequest> { } - protected FetchGroup getFetchGroup(FetchData fetch) { - FetchGroup result = FetchGroup.MINIMAL; - - if (fetch.isEnvelope()) { - result = result.or(FetchGroup.HEADERS_MASK); - } - if (fetch.isBody() || fetch.isBodyStructure()) { - result = result.or(FetchGroup.MIME_DESCRIPTOR_MASK); - } - - Collection<BodyFetchElement> bodyElements = fetch.getBodyElements(); - if (bodyElements != null) { - for (BodyFetchElement element : bodyElements) { - final int sectionType = element.getSectionType(); - final int[] path = element.getPath(); - final boolean isBase = (path == null || path.length == 0); - switch (sectionType) { - case BodyFetchElement.CONTENT: - if (isBase) { - result = addContent(result, path, isBase, FetchGroup.FULL_CONTENT_MASK); - } else { - result = addContent(result, path, isBase, FetchGroup.MIME_CONTENT_MASK); - } - break; - case BodyFetchElement.HEADER: - case BodyFetchElement.HEADER_NOT_FIELDS: - case BodyFetchElement.HEADER_FIELDS: - result = addContent(result, path, isBase, FetchGroup.HEADERS_MASK); - break; - case BodyFetchElement.MIME: - result = addContent(result, path, isBase, FetchGroup.MIME_HEADERS_MASK); - break; - case BodyFetchElement.TEXT: - result = addContent(result, path, isBase, FetchGroup.BODY_CONTENT_MASK); - break; - default: - break; - } - - } - } - return result; - } - - private FetchGroup addContent(FetchGroup result, int[] path, boolean isBase, int content) { - if (isBase) { - return result.or(content); - } else { - MimePath mimePath = new MimePath(path); - return result.addPartContent(mimePath, content); - } - } @Override protected Closeable addContextToMDC(FetchRequest request) { diff --git a/protocols/imap/src/test/java/org/apache/james/imap/processor/fetch/FetchDataConverterTest.java b/protocols/imap/src/test/java/org/apache/james/imap/processor/fetch/FetchDataConverterTest.java new file mode 100644 index 0000000..2c304ec --- /dev/null +++ b/protocols/imap/src/test/java/org/apache/james/imap/processor/fetch/FetchDataConverterTest.java @@ -0,0 +1,79 @@ +/**************************************************************** + * 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.imap.processor.fetch; + +import static org.apache.james.imap.api.message.BodyFetchElement.CONTENT; +import static org.apache.james.imap.api.message.BodyFetchElement.HEADER; +import static org.apache.james.imap.api.message.BodyFetchElement.MIME; +import static org.apache.james.imap.api.message.BodyFetchElement.TEXT; +import static org.apache.james.mailbox.model.FetchGroup.MIME_DESCRIPTOR_MASK; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.stream.Stream; + +import org.apache.james.imap.api.ImapConstants; +import org.apache.james.imap.api.message.BodyFetchElement; +import org.apache.james.imap.api.message.FetchData; +import org.apache.james.mailbox.model.FetchGroup; +import org.apache.james.mailbox.model.MimePath; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +class FetchDataConverterTest { + private static final boolean PEEK = true; + private static final int[] PATH = new int[]{0, 1, 2}; + + static Stream<Arguments> getFetchGroupShouldReturnCorrectValue() { + return Stream.of( + Arguments.arguments(new FetchData(), FetchGroup.MINIMAL), + Arguments.arguments(new FetchData().setBody(true), FetchGroup.MINIMAL.or(MIME_DESCRIPTOR_MASK)), + Arguments.arguments(new FetchData().setBodyStructure(true), FetchGroup.MINIMAL.or(MIME_DESCRIPTOR_MASK)), + Arguments.arguments(new FetchData().setChangedSince(0L), FetchGroup.MINIMAL), + Arguments.arguments(new FetchData().setEnvelope(true), FetchGroup.HEADERS), + Arguments.arguments(new FetchData().setFlags(true), FetchGroup.MINIMAL), + Arguments.arguments(new FetchData().setInternalDate(true), FetchGroup.MINIMAL), + Arguments.arguments(new FetchData().setModSeq(true), FetchGroup.MINIMAL), + Arguments.arguments(new FetchData().setUid(true), FetchGroup.MINIMAL), + Arguments.arguments(new FetchData().setVanished(true), FetchGroup.MINIMAL), + Arguments.arguments(new FetchData().add(BodyFetchElement.createRFC822(), PEEK), FetchGroup.FULL_CONTENT), + Arguments.arguments(new FetchData().add(BodyFetchElement.createRFC822Header(), PEEK), FetchGroup.HEADERS), + Arguments.arguments(new FetchData().add(BodyFetchElement.createRFC822Text(), PEEK), FetchGroup.BODY_CONTENT), + Arguments.arguments(new FetchData().add(new BodyFetchElement(ImapConstants.FETCH_RFC822_HEADER, HEADER, PATH, null, null, null), PEEK), + FetchGroup.MINIMAL.addPartContent(new MimePath(PATH), FetchGroup.HEADERS_MASK)), + Arguments.arguments(new FetchData().add(new BodyFetchElement(ImapConstants.FETCH_RFC822_TEXT, HEADER, PATH, null, null, null), PEEK), + FetchGroup.MINIMAL.addPartContent(new MimePath(PATH), FetchGroup.BODY_CONTENT_MASK)), + Arguments.arguments(new FetchData().add(new BodyFetchElement(ImapConstants.FETCH_RFC822_TEXT, CONTENT, PATH, null, null, null), PEEK), + FetchGroup.MINIMAL.addPartContent(new MimePath(PATH), FetchGroup.BODY_CONTENT_MASK)), + Arguments.arguments(new FetchData().add(new BodyFetchElement(ImapConstants.FETCH_RFC822_TEXT, CONTENT, PATH, null, null, null), PEEK), + FetchGroup.MINIMAL.addPartContent(new MimePath(PATH), FetchGroup.MIME_CONTENT_MASK)), + Arguments.arguments(new FetchData().add(new BodyFetchElement(ImapConstants.FETCH_RFC822_TEXT, MIME, PATH, null, null, null), PEEK), + FetchGroup.MINIMAL.addPartContent(new MimePath(PATH), FetchGroup.MIME_HEADERS_MASK)), + Arguments.arguments(new FetchData().add(new BodyFetchElement(ImapConstants.FETCH_RFC822_TEXT, TEXT, PATH, null, null, null), PEEK), + FetchGroup.MINIMAL.addPartContent(new MimePath(PATH), FetchGroup.BODY_CONTENT_MASK))); + } + + @ParameterizedTest + @MethodSource + void getFetchGroupShouldReturnCorrectValue(FetchData initial, FetchGroup expected) { + assertThat(FetchDataConverter.getFetchGroup(initial)) + .isEqualTo(expected); + } +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
