Repository: james-project Updated Branches: refs/heads/master 00678d881 -> 3b09ad753
JAMES-1692 add unit tests for SetMessagesMethod's delegates ... that were recently added or extracted Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/9388764f Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/9388764f Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/9388764f Branch: refs/heads/master Commit: 9388764f9196879ef8befc047b28e4f17acde9a4 Parents: b21eae3 Author: Fabien Vignon <[email protected]> Authored: Wed Feb 17 19:36:15 2016 +0100 Committer: Raphael Ouazana <[email protected]> Committed: Tue Mar 1 15:42:53 2016 +0100 ---------------------------------------------------------------------- .../jmap/methods/MIMEMessageConverterTest.java | 109 ++++++++++++++++++ .../SetMessagesCreationProcessorTest.java | 113 +++++++++++++------ .../methods/SetMessagesUpdateProcessorTest.java | 85 ++++++++++++++ .../UpdateMessagePatchConverterTest.java | 73 ++++++++++++ 4 files changed, 346 insertions(+), 34 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/9388764f/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/MIMEMessageConverterTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/MIMEMessageConverterTest.java b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/MIMEMessageConverterTest.java new file mode 100644 index 0000000..af7d17a --- /dev/null +++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/MIMEMessageConverterTest.java @@ -0,0 +1,109 @@ +/**************************************************************** + * 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.jmap.methods; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.sql.Date; +import java.time.Instant; +import java.time.ZoneId; +import java.time.ZonedDateTime; + +import org.apache.james.jmap.model.CreationMessage; +import org.apache.james.jmap.model.Emailer; +import org.apache.james.mime4j.dom.Message; +import org.apache.james.mime4j.dom.address.Mailbox; +import org.apache.james.mime4j.stream.Field; +import org.junit.Test; + +import com.google.common.collect.ImmutableList; + +public class MIMEMessageConverterTest { + @Test + public void convertToMimeShouldAddInReplyToHeaderWhenProvided() { + // Given + MIMEMessageConverter sut = new MIMEMessageConverter(); + + String matchingMessageId = "unique-message-id"; + CreationMessage messageHavingInReplyTo = CreationMessage.builder() + .inReplyToMessageId(matchingMessageId) + .mailboxIds(ImmutableList.of("Hey!")) + .subject("subject") + .build(); + + // When + Message result = sut.convertToMime(new MessageWithId.CreationMessageEntry( + "user|mailbox|1", messageHavingInReplyTo)); + + // Then + assertThat(result.getHeader().getFields("In-Reply-To")).extracting(Field::getBody) + .containsOnly(matchingMessageId); + } + + @Test(expected = IllegalArgumentException.class) + public void convertToMimeShouldThrowWhenMessageIsNull() { + MIMEMessageConverter sut = new MIMEMessageConverter(); + + sut.convertToMime(new MessageWithId.CreationMessageEntry("any", null)); + } + + @Test + public void convertToMimeShouldSetBothFromAndSenderHeaders() { + // Given + MIMEMessageConverter sut = new MIMEMessageConverter(); + + String joesEmail = "[email protected]"; + CreationMessage testMessage = CreationMessage.builder() + .mailboxIds(ImmutableList.of("deadbeef-dead-beef-dead-beef")) + .subject("subject") + .from(Emailer.builder().email(joesEmail).name("joe").build()) + .build(); + + // When + Message result = sut.convertToMime(new MessageWithId.CreationMessageEntry( + "user|mailbox|1", testMessage)); + + // Then + assertThat(result.getFrom()).extracting(Mailbox::getAddress).allMatch(f -> f.toString().equals(joesEmail)); + assertThat(result.getSender().getAddress()).isEqualTo(joesEmail); + } + + @Test + public void convertToMimeShouldSetCorrectLocalDate() { + // Given + MIMEMessageConverter sut = new MIMEMessageConverter(); + + Instant now = Instant.now(); + ZonedDateTime messageDate = ZonedDateTime.ofInstant(now, ZoneId.systemDefault()); + + CreationMessage testMessage = CreationMessage.builder() + .mailboxIds(ImmutableList.of("dead-bada55")) + .subject("subject") + .date(messageDate) + .build(); + + // When + Message result = sut.convertToMime(new MessageWithId.CreationMessageEntry( + "user|mailbox|1", testMessage)); + + // Then + assertThat(result.getDate()).isEqualToIgnoringMillis(Date.from(now)); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/james-project/blob/9388764f/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetMessagesCreationProcessorTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetMessagesCreationProcessorTest.java b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetMessagesCreationProcessorTest.java index 0b73ee2..06dc7c4 100644 --- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetMessagesCreationProcessorTest.java +++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetMessagesCreationProcessorTest.java @@ -21,13 +21,16 @@ package org.apache.james.jmap.methods; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.time.ZonedDateTime; import java.util.Optional; import java.util.function.Function; +import org.apache.james.jmap.exceptions.MailboxRoleNotFoundException; import org.apache.james.jmap.model.CreationMessage; import org.apache.james.jmap.model.Emailer; import org.apache.james.jmap.model.Message; @@ -37,9 +40,10 @@ import org.apache.james.jmap.model.SetMessagesResponse; import org.apache.james.mailbox.MailboxSession; import org.apache.james.mailbox.exception.MailboxException; import org.apache.james.mailbox.store.MailboxSessionMapperFactory; +import org.apache.james.mailbox.store.TestId; import org.apache.james.mailbox.store.mail.MessageMapper; import org.apache.james.mailbox.store.mail.model.Mailbox; -import org.apache.james.mailbox.store.mail.model.MailboxId; +import org.apache.james.mailbox.store.mail.model.MailboxMessage; import org.junit.Test; import com.google.common.collect.ImmutableList; @@ -47,13 +51,25 @@ import com.google.common.collect.ImmutableMap; public class SetMessagesCreationProcessorTest { + private static final Message FAKE_MESSAGE = Message.builder() + .id(MessageId.of("user|outbox|1")) + .blobId("anything") + .threadId("anything") + .mailboxIds(ImmutableList.of("mailboxId")) + .headers(ImmutableMap.of()) + .subject("anything") + .size(0) + .date(ZonedDateTime.now()) + .preview("anything") + .build(); + @Test - @SuppressWarnings("unchecked") public void processShouldReturnEmptyCreatedWhenRequestHasEmptyCreate() { - SetMessagesCreationProcessor<MailboxId> sut = new SetMessagesCreationProcessor<MailboxId>(null, null, null, null) { + SetMessagesCreationProcessor<TestId> sut = new SetMessagesCreationProcessor<TestId>(null, null, null, null) { @Override - protected Optional<Mailbox<MailboxId>> getOutbox(MailboxSession session) throws MailboxException { - Mailbox<MailboxId> fakeOutbox = mock(Mailbox.class); + protected Optional<Mailbox<TestId>> getOutbox(MailboxSession session) throws MailboxException { + @SuppressWarnings("unchecked") + Mailbox<TestId> fakeOutbox = (Mailbox<TestId>) mock(Mailbox.class); when(fakeOutbox.getName()).thenReturn("outbox"); return Optional.of(fakeOutbox); } @@ -79,26 +95,75 @@ public class SetMessagesCreationProcessorTest { @Test @SuppressWarnings("unchecked") public void processShouldReturnNonEmptyCreatedWhenRequestHasNonEmptyCreate() throws MailboxException { - - MessageMapper<MailboxId> stubMapper = mock(MessageMapper.class); - MailboxSessionMapperFactory<MailboxId> mockSessionMapperFactory = mock(MailboxSessionMapperFactory.class); + // Given + MessageMapper<TestId> stubMapper = mock(MessageMapper.class); + MailboxSessionMapperFactory<TestId> mockSessionMapperFactory = mock(MailboxSessionMapperFactory.class); when(mockSessionMapperFactory.createMessageMapper(any(MailboxSession.class))) .thenReturn(stubMapper); - SetMessagesCreationProcessor<MailboxId> sut = new SetMessagesCreationProcessor<MailboxId>(null, null, mockSessionMapperFactory, null) { + SetMessagesCreationProcessor<TestId> sut = new SetMessagesCreationProcessor<TestId>(null, null, mockSessionMapperFactory, null) { + @Override + protected MessageWithId<Message> createMessageInOutbox(MessageWithId.CreationMessageEntry createdEntry, MailboxSession session, Mailbox<TestId> outbox, Function<Long, MessageId> buildMessageIdFromUid) { + return new MessageWithId<>(createdEntry.getCreationId(), FAKE_MESSAGE); + } + @Override + protected Optional<Mailbox<TestId>> getOutbox(MailboxSession session) throws MailboxException { + Mailbox<TestId> fakeOutbox = mock(Mailbox.class); + when(fakeOutbox.getName()).thenReturn("outbox"); + return Optional.of(fakeOutbox); + } + }; + // When + SetMessagesResponse result = sut.process(buildFakeCreationRequest(), buildStubbedSession()); + + // Then + assertThat(result.getCreated()).isNotEmpty(); + assertThat(result.getNotCreated()).isEmpty(); + } + + @Test(expected = MailboxRoleNotFoundException.class) + public void processShouldThrowWhenOutboxNotFound() { + // Given + SetMessagesCreationProcessor<TestId> sut = new SetMessagesCreationProcessor<TestId>(null, null, null, null) { @Override - protected MessageWithId<Message> createMessageInOutbox(MessageWithId.CreationMessageEntry createdEntry, MailboxSession session, Mailbox<MailboxId> outbox, Function<Long, MessageId> buildMessageIdFromUid) { - return new MessageWithId<>(createdEntry.creationId, getFakeMessage()); + protected Optional<Mailbox<TestId>> getOutbox(MailboxSession session) throws MailboxException { + return Optional.empty(); } + }; + // When + sut.process(buildFakeCreationRequest(), null); + } + + @Test + @SuppressWarnings("unchecked") + public void processShouldCallMessageMapperWhenRequestHasNonEmptyCreate() throws MailboxException { + // Given + Mailbox<TestId> fakeOutbox = mock(Mailbox.class); + MessageMapper<TestId> mockMapper = mock(MessageMapper.class); + MailboxSessionMapperFactory<TestId> stubSessionMapperFactory = mock(MailboxSessionMapperFactory.class); + when(stubSessionMapperFactory.createMessageMapper(any(MailboxSession.class))) + .thenReturn(mockMapper); + + SetMessagesCreationProcessor<TestId> sut = new SetMessagesCreationProcessor<TestId>(null, null, + stubSessionMapperFactory, new MIMEMessageConverter()) { @Override - protected Optional<Mailbox<MailboxId>> getOutbox(MailboxSession session) throws MailboxException { - Mailbox<MailboxId> fakeOutbox = mock(Mailbox.class); + protected Optional<Mailbox<TestId>> getOutbox(MailboxSession session) throws MailboxException { + TestId stubMailboxId = mock(TestId.class); + when(stubMailboxId.serialize()).thenReturn("user|outbox|12345"); + when(fakeOutbox.getMailboxId()).thenReturn(stubMailboxId); when(fakeOutbox.getName()).thenReturn("outbox"); return Optional.of(fakeOutbox); } }; + // When + sut.process(buildFakeCreationRequest(), buildStubbedSession()); + + // Then + verify(mockMapper).add(eq(fakeOutbox), any(MailboxMessage.class)); + } - SetMessagesRequest creationRequest = SetMessagesRequest.builder() + private SetMessagesRequest buildFakeCreationRequest() { + return SetMessagesRequest.builder() .create(ImmutableMap.of("anything-really", CreationMessage.builder() .from(Emailer.builder().name("alice").email("[email protected]").build()) .to(ImmutableList.of(Emailer.builder().name("bob").email("[email protected]").build())) @@ -106,26 +171,6 @@ public class SetMessagesCreationProcessorTest { .mailboxIds(ImmutableList.of("mailboxId")) .build() )) - .build() - ; - - SetMessagesResponse result = sut.process(creationRequest, buildStubbedSession()); - - assertThat(result.getCreated()).isNotEmpty(); - assertThat(result.getNotCreated()).isEmpty(); - } - - private Message getFakeMessage() { - return Message.builder() - .id(org.apache.james.jmap.model.MessageId.of("user|outbox|1")) - .blobId("anything") - .threadId("anything") - .mailboxIds(ImmutableList.of("mailboxId")) - .headers(ImmutableMap.of()) - .subject("anything") - .size(0) - .date(ZonedDateTime.now()) - .preview("anything") .build(); } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/james-project/blob/9388764f/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetMessagesUpdateProcessorTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetMessagesUpdateProcessorTest.java b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetMessagesUpdateProcessorTest.java new file mode 100644 index 0000000..9a625c3 --- /dev/null +++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetMessagesUpdateProcessorTest.java @@ -0,0 +1,85 @@ +/**************************************************************** + * 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.jmap.methods; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import org.apache.james.jmap.model.MessageId; +import org.apache.james.jmap.model.MessageProperties; +import org.apache.james.jmap.model.SetMessagesRequest; +import org.apache.james.jmap.model.SetMessagesResponse; +import org.apache.james.jmap.model.UpdateMessagePatch; +import org.apache.james.mailbox.store.TestId; +import org.junit.Test; + +import com.fasterxml.jackson.databind.node.JsonNodeFactory; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; + +public class SetMessagesUpdateProcessorTest { + + @Test + public void processShouldReturnEmptyUpdatedWhenRequestHasEmptyUpdate() { + SetMessagesUpdateProcessor<TestId> sut = new SetMessagesUpdateProcessor<>(null, null, null); + SetMessagesRequest requestWithEmptyUpdate = SetMessagesRequest.builder().build(); + + SetMessagesResponse result = sut.process(requestWithEmptyUpdate, null); + + assertThat(result.getUpdated()).isEmpty(); + assertThat(result.getNotUpdated()).isEmpty(); + } + + @Test + public void processShouldReturnNonEmptyNotUpdatedWhenRequestHasInvalidUpdate() { + // Given + UpdateMessagePatchConverter mockConverter = mock(UpdateMessagePatchConverter.class); + UpdateMessagePatch mockInvalidPatch = mock(UpdateMessagePatch.class); + when(mockInvalidPatch.isValid()).thenReturn(false); + + MessageProperties.MessageProperty invalidProperty = MessageProperties.MessageProperty.from; + ImmutableList<ValidationResult> nonEmptyValidationResult = ImmutableList.of(ValidationResult.builder() + .property(invalidProperty.toString()).build()); + when(mockInvalidPatch.getValidationErrors()) + .thenReturn(nonEmptyValidationResult); + when(mockConverter.fromJsonNode(any(ObjectNode.class))) + .thenReturn(mockInvalidPatch); + + SetMessagesUpdateProcessor<TestId> sut = new SetMessagesUpdateProcessor<>(mockConverter, null, null); + MessageId requestMessageId = MessageId.of("user|inbox|1"); + SetMessagesRequest requestWithInvalidUpdate = SetMessagesRequest.builder() + .update(ImmutableMap.of(requestMessageId, JsonNodeFactory.instance.objectNode())) + .build(); + + // When + SetMessagesResponse result = sut.process(requestWithInvalidUpdate, null); + + // Then + assertThat(result.getNotUpdated()).isNotEmpty().describedAs("NotUpdated should not be empty"); + assertThat(result.getNotUpdated()).containsKey(requestMessageId); + assertThat(result.getNotUpdated().get(requestMessageId).getProperties()).isPresent(); + assertThat(result.getNotUpdated().get(requestMessageId).getProperties().get()).contains(invalidProperty); + assertThat(result.getUpdated()).isEmpty(); + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/james-project/blob/9388764f/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/UpdateMessagePatchConverterTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/UpdateMessagePatchConverterTest.java b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/UpdateMessagePatchConverterTest.java new file mode 100644 index 0000000..f67c92a --- /dev/null +++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/UpdateMessagePatchConverterTest.java @@ -0,0 +1,73 @@ +/**************************************************************** + * 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.jmap.methods; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.verify; + +import org.apache.james.jmap.model.UpdateMessagePatch; +import org.junit.Test; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.JsonNodeFactory; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.google.common.collect.ImmutableSet; + +public class UpdateMessagePatchConverterTest { + + @Test + public void fromJsonNodeShouldSetValidationResultWhenPatchIsInvalid() { + + UpdateMessagePatchValidator stubValidator = mock(UpdateMessagePatchValidator.class); + when(stubValidator.isValid(any(ObjectNode.class))).thenReturn(false); + ImmutableSet<ValidationResult> nonEmptyValidationResult = ImmutableSet.of(ValidationResult.builder().build()); + when(stubValidator.validate(any(ObjectNode.class))).thenReturn(nonEmptyValidationResult); + + UpdateMessagePatchConverter sut = new UpdateMessagePatchConverter(null, stubValidator); + + ObjectNode dummynode = JsonNodeFactory.instance.objectNode(); + UpdateMessagePatch result = sut.fromJsonNode(dummynode); + + assertThat(result).extracting(UpdateMessagePatch::getValidationErrors) + .isNotEmpty(); + } + + @Test + public void fromJsonNodeShouldReturnNoErrorWhenPatchIsValid() { + + UpdateMessagePatchValidator mockValidator = mock(UpdateMessagePatchValidator.class); + when(mockValidator.isValid(any(ObjectNode.class))).thenReturn(true); + when(mockValidator.validate(any(ObjectNode.class))).thenReturn(ImmutableSet.of()); + verify(mockValidator, never()).validate(any(ObjectNode.class)); + ObjectMapper jsonParser = new ObjectMapper(); + + UpdateMessagePatchConverter sut = new UpdateMessagePatchConverter(jsonParser, mockValidator); + + ObjectNode dummynode = JsonNodeFactory.instance.objectNode(); + UpdateMessagePatch result = sut.fromJsonNode(dummynode); + + assertThat(result.getValidationErrors().isEmpty()).isTrue(); + } + +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
