MAILBOX-339 Extract and test `too long names` error management
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/04adc91c Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/04adc91c Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/04adc91c Branch: refs/heads/master Commit: 04adc91cada4b3b9a79214220867056c928f2a6b Parents: e9014c2 Author: benwa <btell...@linagora.com> Authored: Wed May 16 09:40:12 2018 +0700 Committer: Matthieu Baechler <matth...@apache.org> Committed: Mon May 28 17:38:46 2018 +0200 ---------------------------------------------------------------------- .../cassandra/mail/CassandraMailboxMapper.java | 40 +-------- .../mail/utils/DriverExceptionHelper.java | 54 +++++++++++++ .../mail/utils/DriverExceptionHelperTest.java | 85 ++++++++++++++++++++ 3 files changed, 143 insertions(+), 36 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/04adc91c/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraMailboxMapper.java ---------------------------------------------------------------------- diff --git a/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraMailboxMapper.java b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraMailboxMapper.java index af580da..a36406c 100644 --- a/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraMailboxMapper.java +++ b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraMailboxMapper.java @@ -33,14 +33,13 @@ import java.util.stream.Stream; import javax.inject.Inject; -import org.apache.commons.lang3.StringUtils; import org.apache.james.backends.cassandra.init.CassandraConfiguration; import org.apache.james.mailbox.acl.ACLDiff; import org.apache.james.mailbox.cassandra.ids.CassandraId; +import org.apache.james.mailbox.cassandra.mail.utils.DriverExceptionHelper; import org.apache.james.mailbox.exception.MailboxException; import org.apache.james.mailbox.exception.MailboxExistsException; import org.apache.james.mailbox.exception.MailboxNotFoundException; -import org.apache.james.mailbox.exception.TooLongMailboxNameException; import org.apache.james.mailbox.model.MailboxACL; import org.apache.james.mailbox.model.MailboxACL.Right; import org.apache.james.mailbox.model.MailboxId; @@ -54,7 +53,6 @@ import org.apache.james.util.OptionalUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.datastax.driver.core.exceptions.InvalidQueryException; import com.github.fge.lambdas.Throwing; import com.github.steveash.guavate.Guavate; import com.google.common.base.Preconditions; @@ -63,8 +61,6 @@ import com.google.common.collect.ImmutableList; public class CassandraMailboxMapper implements MailboxMapper { public static final String WILDCARD = "%"; - public static final String VALUES_MAY_NOT_BE_LARGER_THAN_64_K = "Index expression values may not be larger than 64K"; - public static final String CLUSTERING_COLUMNS_IS_TOO_LONG = "The sum of all clustering columns is too long"; public static final Logger LOGGER = LoggerFactory.getLogger(CassandraMailboxMapper.class); private final CassandraMailboxDAO mailboxDAO; @@ -102,15 +98,7 @@ public class CassandraMailboxMapper implements MailboxMapper { .join() .orElseThrow(() -> new MailboxNotFoundException(path)); } catch (CompletionException e) { - if (e.getCause() instanceof InvalidQueryException) { - String errorMessage = e.getCause().getMessage(); - if (StringUtils.containsIgnoreCase(errorMessage, VALUES_MAY_NOT_BE_LARGER_THAN_64_K) - || StringUtils.containsIgnoreCase(errorMessage, CLUSTERING_COLUMNS_IS_TOO_LONG)) { - throw new TooLongMailboxNameException("too long mailbox name"); - } - throw new MailboxException("It has error with cassandra storage", e.getCause()); - } - throw e; + throw DriverExceptionHelper.handleStorageException(e); } } @@ -123,15 +111,7 @@ public class CassandraMailboxMapper implements MailboxMapper { .map(this::retrieveMailbox) .orElse(CompletableFuture.completedFuture(Optional.empty()))); } catch (CompletionException e) { - if (e.getCause() instanceof InvalidQueryException) { - String errorMessage = e.getCause().getMessage(); - if (StringUtils.containsIgnoreCase(errorMessage, VALUES_MAY_NOT_BE_LARGER_THAN_64_K) - || StringUtils.containsIgnoreCase(errorMessage, CLUSTERING_COLUMNS_IS_TOO_LONG)) { - throw new TooLongMailboxNameException("too long mailbox name"); - } - throw new MailboxException("It has error with cassandra storage", e.getCause()); - } - throw e; + throw DriverExceptionHelper.handleStorageException(e); } } @@ -197,7 +177,7 @@ public class CassandraMailboxMapper implements MailboxMapper { throw new MailboxExistsException(mailbox.generateAssociatedPath().asString()); } } catch (CompletionException e) { - manageException(e); + throw DriverExceptionHelper.handleStorageException(e); } return cassandraId; } @@ -213,18 +193,6 @@ public class CassandraMailboxMapper implements MailboxMapper { mailboxDAO.save(cassandraMailbox))))); } - private void manageException(CompletionException e) throws MailboxException { - if (e.getCause() instanceof InvalidQueryException) { - String errorMessage = e.getCause().getMessage(); - if (StringUtils.containsIgnoreCase(errorMessage, VALUES_MAY_NOT_BE_LARGER_THAN_64_K) || - StringUtils.containsIgnoreCase(errorMessage, CLUSTERING_COLUMNS_IS_TOO_LONG)) { - throw new TooLongMailboxNameException("too long mailbox name"); - } - throw new MailboxException("It has error with cassandra storage", e.getCause()); - } - throw e; - } - private CassandraId retrieveId(SimpleMailbox cassandraMailbox) { if (cassandraMailbox.getMailboxId() == null) { return CassandraId.timeBased(); http://git-wip-us.apache.org/repos/asf/james-project/blob/04adc91c/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/utils/DriverExceptionHelper.java ---------------------------------------------------------------------- diff --git a/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/utils/DriverExceptionHelper.java b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/utils/DriverExceptionHelper.java new file mode 100644 index 0000000..f5d669e --- /dev/null +++ b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/utils/DriverExceptionHelper.java @@ -0,0 +1,54 @@ +/**************************************************************** + * 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.cassandra.mail.utils; + +import java.util.concurrent.CompletionException; + +import org.apache.commons.lang3.StringUtils; +import org.apache.james.mailbox.exception.MailboxException; +import org.apache.james.mailbox.exception.TooLongMailboxNameException; + +import com.datastax.driver.core.exceptions.InvalidQueryException; + +public class DriverExceptionHelper { + public static final String VALUES_MAY_NOT_BE_LARGER_THAN_64_K = "Index expression values may not be larger than 64K"; + public static final String CLUSTERING_COLUMNS_IS_TOO_LONG = "The sum of all clustering columns is too long"; + + public static MailboxException handleStorageException(CompletionException e) throws MailboxException { + if (e.getCause() instanceof InvalidQueryException) { + return handleInvalidQuery((InvalidQueryException) e.getCause()); + } + throw e; + } + + public static MailboxException handleInvalidQuery(InvalidQueryException cause) throws MailboxException { + if (isTooLong(cause)) { + throw new TooLongMailboxNameException("too long mailbox name"); + } + throw new MailboxException("Error while interacting with cassandra storage", cause); + } + + public static boolean isTooLong(InvalidQueryException e) { + String errorMessage = e.getMessage(); + return StringUtils.containsIgnoreCase(errorMessage, VALUES_MAY_NOT_BE_LARGER_THAN_64_K) + || StringUtils.containsIgnoreCase(errorMessage, CLUSTERING_COLUMNS_IS_TOO_LONG); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/04adc91c/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/utils/DriverExceptionHelperTest.java ---------------------------------------------------------------------- diff --git a/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/utils/DriverExceptionHelperTest.java b/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/utils/DriverExceptionHelperTest.java new file mode 100644 index 0000000..c8ae149 --- /dev/null +++ b/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/utils/DriverExceptionHelperTest.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.mailbox.cassandra.mail.utils; + +import static org.apache.james.mailbox.cassandra.mail.utils.DriverExceptionHelper.CLUSTERING_COLUMNS_IS_TOO_LONG; +import static org.apache.james.mailbox.cassandra.mail.utils.DriverExceptionHelper.VALUES_MAY_NOT_BE_LARGER_THAN_64_K; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.util.concurrent.CompletionException; + +import org.apache.james.mailbox.exception.MailboxException; +import org.apache.james.mailbox.exception.TooLongMailboxNameException; +import org.junit.Test; + +import com.datastax.driver.core.exceptions.InvalidQueryException; + +public class DriverExceptionHelperTest { + + @Test + public void handleStorageExceptionShouldPropagateWhenNoCause() { + CompletionException completionException = new CompletionException() {}; + + assertThatThrownBy(() -> + DriverExceptionHelper.handleStorageException(completionException)) + .isEqualTo(completionException); + } + + @Test + public void handleStorageExceptionShouldPropagateWhenCauseIsNotInvalidQuery() { + CompletionException exception = new CompletionException("message", new RuntimeException()); + + assertThatThrownBy(() -> + DriverExceptionHelper.handleStorageException(exception)) + .isEqualTo(exception); + } + + @Test + public void handleStorageExceptionShouldUnwrapWhenCauseIsInvalidQuery() { + InvalidQueryException invalidQueryException = new InvalidQueryException("message"); + CompletionException exception = new CompletionException("message", invalidQueryException); + + assertThatThrownBy(() -> + DriverExceptionHelper.handleStorageException(exception)) + .isInstanceOf(MailboxException.class) + .hasCause(invalidQueryException); + } + + @Test + public void handleStorageExceptionShouldThrowTooLongWhenClusteringColumnsTooLong() { + InvalidQueryException invalidQueryException = new InvalidQueryException(CLUSTERING_COLUMNS_IS_TOO_LONG); + CompletionException exception = new CompletionException("message", invalidQueryException); + + assertThatThrownBy(() -> + DriverExceptionHelper.handleStorageException(exception)) + .isInstanceOf(TooLongMailboxNameException.class); + } + + @Test + public void handleStorageExceptionShouldThrowTooLongWhenValueMoreThan64K() { + InvalidQueryException invalidQueryException = new InvalidQueryException(VALUES_MAY_NOT_BE_LARGER_THAN_64_K); + CompletionException exception = new CompletionException("message", invalidQueryException); + + assertThatThrownBy(() -> + DriverExceptionHelper.handleStorageException(exception)) + .isInstanceOf(TooLongMailboxNameException.class); + } + +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org