JAMES-2145 String typing for Username
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/0e5872cd Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/0e5872cd Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/0e5872cd Branch: refs/heads/master Commit: 0e5872cdff268f7e0271930c2ba79910f4683cf6 Parents: 8000999 Author: benwa <[email protected]> Authored: Wed Sep 20 16:41:46 2017 +0700 Committer: Antoine Duprat <[email protected]> Committed: Wed Sep 20 16:25:36 2017 +0200 ---------------------------------------------------------------------- .../mail/CassandraAttachmentMapper.java | 5 +- .../mail/CassandraAttachmentOwnerDAO.java | 13 ++-- .../mail/CassandraAttachmentOwnerDAOTest.java | 5 +- .../inmemory/mail/InMemoryAttachmentMapper.java | 7 +- .../mailbox/store/StoreAttachmentManager.java | 7 +- .../mailbox/store/mail/AttachmentMapper.java | 5 +- .../mailbox/store/mail/model/Username.java | 74 ++++++++++++++++++++ .../store/mail/model/AttachmentMapperTest.java | 16 ++--- .../mailbox/store/mail/model/UsernameTest.java | 72 +++++++++++++++++++ 9 files changed, 179 insertions(+), 25 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/0e5872cd/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentMapper.java ---------------------------------------------------------------------- diff --git a/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentMapper.java b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentMapper.java index a0d2749..1eccb88 100644 --- a/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentMapper.java +++ b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentMapper.java @@ -35,6 +35,7 @@ import org.apache.james.mailbox.model.Attachment; import org.apache.james.mailbox.model.AttachmentId; import org.apache.james.mailbox.model.MessageId; import org.apache.james.mailbox.store.mail.AttachmentMapper; +import org.apache.james.mailbox.store.mail.model.Username; import org.apache.james.util.FluentFutureStream; import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; @@ -122,7 +123,7 @@ public class CassandraAttachmentMapper implements AttachmentMapper { } @Override - public void storeAttachmentForOwner(Attachment attachment, String owner) throws MailboxException { + public void storeAttachmentForOwner(Attachment attachment, Username owner) throws MailboxException { ownerDAO.addOwner(attachment.getAttachmentId(), owner) .thenCompose(any -> blobsDAO.save(attachment.getBytes())) .thenApply(blobId -> CassandraAttachmentDAOV2.from(attachment, blobId)) @@ -145,7 +146,7 @@ public class CassandraAttachmentMapper implements AttachmentMapper { } @Override - public Collection<String> getOwners(AttachmentId attachmentId) throws MailboxException { + public Collection<Username> getOwners(AttachmentId attachmentId) throws MailboxException { return ownerDAO.retrieveOwners(attachmentId).join(); } http://git-wip-us.apache.org/repos/asf/james-project/blob/0e5872cd/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentOwnerDAO.java ---------------------------------------------------------------------- diff --git a/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentOwnerDAO.java b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentOwnerDAO.java index fe2d361..1be5175 100644 --- a/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentOwnerDAO.java +++ b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentOwnerDAO.java @@ -37,6 +37,7 @@ import javax.inject.Inject; import org.apache.james.backends.cassandra.utils.CassandraAsyncExecutor; import org.apache.james.backends.cassandra.utils.CassandraUtils; import org.apache.james.mailbox.model.AttachmentId; +import org.apache.james.mailbox.store.mail.model.Username; import com.datastax.driver.core.PreparedStatement; import com.datastax.driver.core.Row; @@ -74,14 +75,14 @@ public class CassandraAttachmentOwnerDAO { .where(eq(ID, bindMarker(ID)))); } - public CompletableFuture<Void> addOwner(AttachmentId attachmentId, String owner) { + public CompletableFuture<Void> addOwner(AttachmentId attachmentId, Username owner) { return executor.executeVoid( addStatement.bind() .setUUID(ID, attachmentId.asUUID()) - .setString(OWNER, owner)); + .setString(OWNER, owner.getValue())); } - public CompletableFuture<Collection<String>> retrieveOwners(AttachmentId attachmentId) { + public CompletableFuture<Collection<Username>> retrieveOwners(AttachmentId attachmentId) { return executor.execute( selectStatement.bind() .setUUID(ID, attachmentId.asUUID())) @@ -89,7 +90,9 @@ public class CassandraAttachmentOwnerDAO { .thenApply(this::toOwners); } - private ImmutableList<String> toOwners(Stream<Row> stream) { - return stream.map(row -> row.getString(OWNER)).collect(Guavate.toImmutableList()); + private ImmutableList<Username> toOwners(Stream<Row> stream) { + return stream.map(row -> row.getString(OWNER)) + .map(Username::fromRawValue) + .collect(Guavate.toImmutableList()); } } http://git-wip-us.apache.org/repos/asf/james-project/blob/0e5872cd/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentOwnerDAOTest.java ---------------------------------------------------------------------- diff --git a/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentOwnerDAOTest.java b/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentOwnerDAOTest.java index 8082f5b..9f75612 100644 --- a/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentOwnerDAOTest.java +++ b/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentOwnerDAOTest.java @@ -26,14 +26,15 @@ import org.apache.james.backends.cassandra.DockerCassandraRule; import org.apache.james.backends.cassandra.utils.CassandraUtils; import org.apache.james.mailbox.cassandra.modules.CassandraAttachmentModule; import org.apache.james.mailbox.model.AttachmentId; +import org.apache.james.mailbox.store.mail.model.Username; import org.junit.Before; import org.junit.ClassRule; import org.junit.Test; public class CassandraAttachmentOwnerDAOTest { public static final AttachmentId ATTACHMENT_ID = AttachmentId.from("id1"); - public static final String OWNER_1 = "owner1"; - public static final String OWNER_2 = "owner2"; + public static final Username OWNER_1 = Username.fromRawValue("owner1"); + public static final Username OWNER_2 = Username.fromRawValue("owner2"); @ClassRule public static DockerCassandraRule cassandraServer = new DockerCassandraRule(); http://git-wip-us.apache.org/repos/asf/james-project/blob/0e5872cd/mailbox/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryAttachmentMapper.java ---------------------------------------------------------------------- diff --git a/mailbox/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryAttachmentMapper.java b/mailbox/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryAttachmentMapper.java index 4ba0691..e97e65f 100644 --- a/mailbox/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryAttachmentMapper.java +++ b/mailbox/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryAttachmentMapper.java @@ -29,6 +29,7 @@ import org.apache.james.mailbox.model.Attachment; import org.apache.james.mailbox.model.AttachmentId; import org.apache.james.mailbox.model.MessageId; import org.apache.james.mailbox.store.mail.AttachmentMapper; +import org.apache.james.mailbox.store.mail.model.Username; import com.google.common.base.Preconditions; import com.google.common.collect.HashMultimap; @@ -42,7 +43,7 @@ public class InMemoryAttachmentMapper implements AttachmentMapper { private static final int INITIAL_SIZE = 128; private final Map<AttachmentId, Attachment> attachmentsById; private final Multimap<AttachmentId, MessageId> messageIdsByAttachmentId; - private final Multimap<AttachmentId, String> ownersByAttachmentId; + private final Multimap<AttachmentId, Username> ownersByAttachmentId; public InMemoryAttachmentMapper() { attachmentsById = new ConcurrentHashMap<>(INITIAL_SIZE); @@ -72,7 +73,7 @@ public class InMemoryAttachmentMapper implements AttachmentMapper { } @Override - public void storeAttachmentForOwner(Attachment attachment, String owner) throws MailboxException { + public void storeAttachmentForOwner(Attachment attachment, Username owner) throws MailboxException { attachmentsById.put(attachment.getAttachmentId(), attachment); ownersByAttachmentId.put(attachment.getAttachmentId(), owner); } @@ -101,7 +102,7 @@ public class InMemoryAttachmentMapper implements AttachmentMapper { } @Override - public Collection<String> getOwners(final AttachmentId attachmentId) throws MailboxException { + public Collection<Username> getOwners(final AttachmentId attachmentId) throws MailboxException { return ownersByAttachmentId.get(attachmentId); } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/james-project/blob/0e5872cd/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreAttachmentManager.java ---------------------------------------------------------------------- diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreAttachmentManager.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreAttachmentManager.java index 7b6ce8a..6708d67 100644 --- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreAttachmentManager.java +++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreAttachmentManager.java @@ -33,6 +33,7 @@ import org.apache.james.mailbox.model.Attachment; import org.apache.james.mailbox.model.AttachmentId; import org.apache.james.mailbox.model.MessageId; import org.apache.james.mailbox.store.mail.AttachmentMapperFactory; +import org.apache.james.mailbox.store.mail.model.Username; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -71,7 +72,7 @@ public class StoreAttachmentManager implements AttachmentManager { @Override public void storeAttachment(Attachment attachment, MailboxSession mailboxSession) throws MailboxException { attachmentMapperFactory.getAttachmentMapper(mailboxSession) - .storeAttachmentForOwner(attachment, mailboxSession.getUser().getUserName()); + .storeAttachmentForOwner(attachment, Username.fromMailboxSession(mailboxSession)); } @Override @@ -97,10 +98,10 @@ public class StoreAttachmentManager implements AttachmentManager { } private boolean isExplicitlyAOwner(AttachmentId attachmentId, MailboxSession mailboxSession) throws MailboxException { - Collection<String> explicitOwners = attachmentMapperFactory.getAttachmentMapper(mailboxSession) + Collection<Username> explicitOwners = attachmentMapperFactory.getAttachmentMapper(mailboxSession) .getOwners(attachmentId); return explicitOwners.stream() - .anyMatch(username -> mailboxSession.getUser().isSameUser(username)); + .anyMatch(username -> mailboxSession.getUser().isSameUser(username.getValue())); } private Collection<MessageId> getRelatedMessageIds(AttachmentId attachmentId, MailboxSession mailboxSession) throws MailboxException { http://git-wip-us.apache.org/repos/asf/james-project/blob/0e5872cd/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/AttachmentMapper.java ---------------------------------------------------------------------- diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/AttachmentMapper.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/AttachmentMapper.java index 7973a20..09b444d 100644 --- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/AttachmentMapper.java +++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/AttachmentMapper.java @@ -26,6 +26,7 @@ import org.apache.james.mailbox.exception.MailboxException; import org.apache.james.mailbox.model.Attachment; import org.apache.james.mailbox.model.AttachmentId; import org.apache.james.mailbox.model.MessageId; +import org.apache.james.mailbox.store.mail.model.Username; import org.apache.james.mailbox.store.transaction.Mapper; public interface AttachmentMapper extends Mapper { @@ -34,11 +35,11 @@ public interface AttachmentMapper extends Mapper { List<Attachment> getAttachments(Collection<AttachmentId> attachmentIds); - void storeAttachmentForOwner(Attachment attachment, String owner) throws MailboxException; + void storeAttachmentForOwner(Attachment attachment, Username owner) throws MailboxException; void storeAttachmentsForMessage(Collection<Attachment> attachments, MessageId ownerMessageId) throws MailboxException; Collection<MessageId> getRelatedMessageIds(AttachmentId attachmentId) throws MailboxException; - Collection<String> getOwners(AttachmentId attachmentId) throws MailboxException; + Collection<Username> getOwners(AttachmentId attachmentId) throws MailboxException; } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/james-project/blob/0e5872cd/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/model/Username.java ---------------------------------------------------------------------- diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/model/Username.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/model/Username.java new file mode 100644 index 0000000..5dacc38 --- /dev/null +++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/model/Username.java @@ -0,0 +1,74 @@ +/**************************************************************** + * 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.mail.model; + +import java.util.Objects; + +import org.apache.james.mailbox.MailboxSession; + +import com.google.common.base.MoreObjects; +import com.google.common.base.Preconditions; + +public class Username { + public static Username fromMailboxSession(MailboxSession mailboxSession) { + Preconditions.checkNotNull(mailboxSession); + Preconditions.checkNotNull(mailboxSession.getUser()); + + return fromRawValue(mailboxSession.getUser().getUserName()); + } + + public static Username fromRawValue(String value) { + return new Username(value); + } + + private final String value; + + private Username(final String value) { + Preconditions.checkNotNull(value); + + this.value = value; + } + + public String getValue() { + return value; + } + + @Override + public final boolean equals(final Object o) { + if (o instanceof Username) { + Username username = (Username) o; + + return Objects.equals(this.value, username.value); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(value); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("value", value) + .toString(); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/0e5872cd/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/AttachmentMapperTest.java ---------------------------------------------------------------------- diff --git a/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/AttachmentMapperTest.java b/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/AttachmentMapperTest.java index bc411a9..01514bb 100644 --- a/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/AttachmentMapperTest.java +++ b/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/AttachmentMapperTest.java @@ -40,8 +40,8 @@ import com.google.common.collect.ImmutableList; public abstract class AttachmentMapperTest { private static final AttachmentId UNKNOWN_ATTACHMENT_ID = AttachmentId.forPayloadAndType("unknown".getBytes(Charsets.UTF_8), "type"); - public static final String OWNER = "owner"; - public static final String ADDITIONAL_OWNER = "additionalOwner"; + public static final Username OWNER = Username.fromRawValue("owner"); + public static final Username ADDITIONAL_OWNER = Username.fromRawValue("additionalOwner"); private AttachmentMapper attachmentMapper; @@ -278,8 +278,8 @@ public abstract class AttachmentMapperTest { attachmentMapper.storeAttachmentForOwner(attachment, OWNER); //When - Collection<String> expectedOwners = ImmutableList.of(OWNER); - Collection<String> actualOwners = attachmentMapper.getOwners(attachmentId); + Collection<Username> expectedOwners = ImmutableList.of(OWNER); + Collection<Username> actualOwners = attachmentMapper.getOwners(attachmentId); //Then assertThat(actualOwners).containsOnlyElementsOf(expectedOwners); } @@ -296,7 +296,7 @@ public abstract class AttachmentMapperTest { attachmentMapper.storeAttachmentsForMessage(ImmutableList.of(attachment), generateMessageId()); //When - Collection<String> actualOwners = attachmentMapper.getOwners(attachmentId); + Collection<Username> actualOwners = attachmentMapper.getOwners(attachmentId); //Then assertThat(actualOwners).isEmpty(); } @@ -314,15 +314,15 @@ public abstract class AttachmentMapperTest { attachmentMapper.storeAttachmentForOwner(attachment, ADDITIONAL_OWNER); //When - Collection<String> expectedOwners = ImmutableList.of(OWNER, ADDITIONAL_OWNER); - Collection<String> actualOwners = attachmentMapper.getOwners(attachmentId); + Collection<Username> expectedOwners = ImmutableList.of(OWNER, ADDITIONAL_OWNER); + Collection<Username> actualOwners = attachmentMapper.getOwners(attachmentId); //Then assertThat(actualOwners).containsOnlyElementsOf(expectedOwners); } @Test public void getOwnersShouldReturnEmptyWhenUnknownAttachmentId() throws Exception { - Collection<String> actualOwners = attachmentMapper.getOwners(AttachmentId.from("any")); + Collection<Username> actualOwners = attachmentMapper.getOwners(AttachmentId.from("any")); assertThat(actualOwners).isEmpty(); } http://git-wip-us.apache.org/repos/asf/james-project/blob/0e5872cd/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/UsernameTest.java ---------------------------------------------------------------------- diff --git a/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/UsernameTest.java b/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/UsernameTest.java new file mode 100644 index 0000000..225449b --- /dev/null +++ b/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/UsernameTest.java @@ -0,0 +1,72 @@ +/**************************************************************** + * 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.mail.model; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import org.apache.james.mailbox.MailboxSession; +import org.junit.Test; + +import nl.jqno.equalsverifier.EqualsVerifier; + +public class UsernameTest { + + @Test + public void shouldRespectBeanContract() { + EqualsVerifier.forClass(Username.class) + .allFieldsShouldBeUsed() + .verify(); + } + + @Test + public void fromRawValueShouldThrowOnNull() { + assertThatThrownBy(() -> Username.fromRawValue(null)) + .isInstanceOf(NullPointerException.class); + } + + @Test + public void fromMailboxSessionShouldThrowOnNull() { + assertThatThrownBy(() -> Username.fromMailboxSession(null)) + .isInstanceOf(NullPointerException.class); + } + + @Test + public void fromMailboxSessionShouldThrowOnNullUser() { + MailboxSession mailboxSession = mock(MailboxSession.class); + when(mailboxSession.getUser()).thenReturn(null); + + assertThatThrownBy(() -> Username.fromMailboxSession(mailboxSession)) + .isInstanceOf(NullPointerException.class); + } + + @Test + public void fromMailboxSessionShouldThrowOnNullUsername() { + MailboxSession mailboxSession = mock(MailboxSession.class); + MailboxSession.User user = mock(MailboxSession.User.class); + when(mailboxSession.getUser()).thenReturn(user); + when(user.getUserName()).thenReturn(null); + + assertThatThrownBy(() -> Username.fromMailboxSession(mailboxSession)) + .isInstanceOf(NullPointerException.class); + } + +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
