You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2019/01/16 06:59:13 UTC
[05/17] james-project git commit: MAILBOX-371 Conversion between
RoutingKeys And RegistrationKeys
MAILBOX-371 Conversion between RoutingKeys And RegistrationKeys
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/74fa711f
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/74fa711f
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/74fa711f
Branch: refs/heads/master
Commit: 74fa711f8c633cf9978ab5c19eb1291d9f3decb2
Parents: c754f3a
Author: datph <dp...@linagora.com>
Authored: Fri Jan 11 10:34:08 2019 +0700
Committer: datph <dp...@linagora.com>
Committed: Wed Jan 16 05:38:45 2019 +0700
----------------------------------------------------------------------
.../mailbox/events/RoutingKeyConverter.java | 90 ++++++++++++
.../mailbox/events/RoutingKeyConverterTest.java | 143 +++++++++++++++++++
2 files changed, 233 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/james-project/blob/74fa711f/mailbox/event/event-rabbitmq/src/main/java/org/apache/james/mailbox/events/RoutingKeyConverter.java
----------------------------------------------------------------------
diff --git a/mailbox/event/event-rabbitmq/src/main/java/org/apache/james/mailbox/events/RoutingKeyConverter.java b/mailbox/event/event-rabbitmq/src/main/java/org/apache/james/mailbox/events/RoutingKeyConverter.java
new file mode 100644
index 0000000..c8a23d1
--- /dev/null
+++ b/mailbox/event/event-rabbitmq/src/main/java/org/apache/james/mailbox/events/RoutingKeyConverter.java
@@ -0,0 +1,90 @@
+/****************************************************************
+ * 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.events;
+
+import static org.apache.james.backend.rabbitmq.Constants.EMPTY_ROUTING_KEY;
+
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+
+import javax.inject.Inject;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Joiner;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Splitter;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Iterables;
+
+class RoutingKeyConverter {
+ private static final String SEPARATOR = ":";
+
+ static class RoutingKey {
+
+ static RoutingKey empty() {
+ return new RoutingKey(Optional.empty());
+ }
+
+ static RoutingKey of(RegistrationKey key) {
+ return new RoutingKey(Optional.of(key));
+ }
+
+ private final Optional<RegistrationKey> registrationKey;
+
+ private RoutingKey(Optional<RegistrationKey> registrationKey) {
+ this.registrationKey = registrationKey;
+ }
+
+ String asString() {
+ return registrationKey.map(key -> key.getClass().getName() + SEPARATOR + key.asString())
+ .orElse(EMPTY_ROUTING_KEY);
+ }
+ }
+
+ @VisibleForTesting
+ static RoutingKeyConverter forFactories(RegistrationKey.Factory... factories) {
+ return new RoutingKeyConverter(ImmutableSet.copyOf(factories));
+ }
+
+ private final Set<RegistrationKey.Factory> factories;
+
+ @Inject
+ RoutingKeyConverter(Set<RegistrationKey.Factory> factories) {
+ this.factories = factories;
+ }
+
+ RegistrationKey toRegistrationKey(String routingKey) {
+ return toRegistrationKey(Splitter.on(SEPARATOR).splitToList(routingKey));
+ }
+
+ private RegistrationKey toRegistrationKey(List<String> parts) {
+ Preconditions.checkArgument(parts.size() >= 2, "Routing key needs to match the 'classFQDN:value' pattern");
+
+ String registrationClass = parts.get(0);
+ String value = Joiner.on(SEPARATOR).join(Iterables.skip(parts, 1));
+
+ return factories.stream()
+ .filter(factory -> factory.forClass().getName().equals(registrationClass))
+ .findAny()
+ .orElseThrow(() -> new IllegalArgumentException("No factory for " + registrationClass))
+ .fromString(value);
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/james-project/blob/74fa711f/mailbox/event/event-rabbitmq/src/test/java/org/apache/james/mailbox/events/RoutingKeyConverterTest.java
----------------------------------------------------------------------
diff --git a/mailbox/event/event-rabbitmq/src/test/java/org/apache/james/mailbox/events/RoutingKeyConverterTest.java b/mailbox/event/event-rabbitmq/src/test/java/org/apache/james/mailbox/events/RoutingKeyConverterTest.java
new file mode 100644
index 0000000..4b158dc
--- /dev/null
+++ b/mailbox/event/event-rabbitmq/src/test/java/org/apache/james/mailbox/events/RoutingKeyConverterTest.java
@@ -0,0 +1,143 @@
+/****************************************************************
+ * 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.events;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.util.Objects;
+
+import org.apache.james.mailbox.model.TestId;
+import org.junit.jupiter.api.Test;
+
+class RoutingKeyConverterTest {
+ static class TestRegistrationKey implements RegistrationKey {
+ static class Factory implements RegistrationKey.Factory {
+ @Override
+ public Class<? extends RegistrationKey> forClass() {
+ return TestRegistrationKey.class;
+ }
+
+ @Override
+ public RegistrationKey fromString(String asString) {
+ return new TestRegistrationKey(asString);
+ }
+ }
+
+ private final String value;
+
+ TestRegistrationKey(String value) {
+ this.value = value;
+ }
+
+ @Override
+ public String asString() {
+ return value;
+ }
+
+ @Override
+ public final boolean equals(Object o) {
+ if (o instanceof TestRegistrationKey) {
+ TestRegistrationKey that = (TestRegistrationKey) o;
+
+ return Objects.equals(this.value, that.value);
+ }
+ return false;
+ }
+
+ @Override
+ public final int hashCode() {
+ return Objects.hash(value);
+ }
+ }
+
+ private static final RegistrationKey REGISTRATION_KEY_1 = new MailboxIdRegistrationKey(TestId.of(42));
+ private static final String ROUTING_KEY_1 = "org.apache.james.mailbox.events.MailboxIdRegistrationKey:42";
+
+ private RoutingKeyConverter testee = RoutingKeyConverter.forFactories(
+ new TestRegistrationKey.Factory(),
+ new MailboxIdRegistrationKey.Factory(new TestId.Factory()));
+
+ @Test
+ void toRoutingKeyShouldTransformAKeyIntoAString() {
+ assertThat(RoutingKeyConverter.RoutingKey.of(REGISTRATION_KEY_1).asString())
+ .isEqualTo(ROUTING_KEY_1);
+ }
+
+ @Test
+ void toRegistrationKeyShouldReturnCorrespondingRoutingKey() {
+ assertThat(testee.toRegistrationKey(ROUTING_KEY_1))
+ .isEqualTo(REGISTRATION_KEY_1);
+ }
+
+ @Test
+ void toRoutingKeyShouldAcceptSeparator() {
+ assertThat(RoutingKeyConverter.RoutingKey.of(new TestRegistrationKey("a:b")).asString())
+ .isEqualTo("org.apache.james.mailbox.events.RoutingKeyConverterTest$TestRegistrationKey:a:b");
+ }
+
+ @Test
+ void toRegistrationKeyShouldAcceptSeparator() {
+ assertThat(testee.toRegistrationKey("org.apache.james.mailbox.events.RoutingKeyConverterTest$TestRegistrationKey:a:b"))
+ .isEqualTo(new TestRegistrationKey("a:b"));
+ }
+
+ @Test
+ void toRoutingKeyShouldAcceptEmptyValue() {
+ assertThat(RoutingKeyConverter.RoutingKey.of(new TestRegistrationKey("")).asString())
+ .isEqualTo("org.apache.james.mailbox.events.RoutingKeyConverterTest$TestRegistrationKey:");
+ }
+
+ @Test
+ void toRegistrationKeyShouldAcceptEmptyValue() {
+ assertThat(testee.toRegistrationKey("org.apache.james.mailbox.events.RoutingKeyConverterTest$TestRegistrationKey:"))
+ .isEqualTo(new TestRegistrationKey(""));
+ }
+
+ @Test
+ void toRegistrationKeyShouldRejectNull() {
+ assertThatThrownBy(() -> testee.toRegistrationKey(null))
+ .isInstanceOf(NullPointerException.class);
+ }
+
+ @Test
+ void toRegistrationKeyShouldRejectEmptyString() {
+ assertThatThrownBy(() -> testee.toRegistrationKey(""))
+ .isInstanceOf(IllegalArgumentException.class);
+ }
+
+ @Test
+ void toRegistrationKeyShouldRejectNoSeparator() {
+ assertThatThrownBy(() -> testee.toRegistrationKey("noSeparator"))
+ .isInstanceOf(IllegalArgumentException.class);
+ }
+
+ @Test
+ void toRegistrationKeyShouldRejectUnknownRegistrationKeyClass() {
+ assertThatThrownBy(() -> testee.toRegistrationKey("unknown:"))
+ .isInstanceOf(IllegalArgumentException.class);
+ }
+
+ @Test
+ void toRegistrationKeyShouldRejectInvalidValue() {
+ assertThatThrownBy(() -> testee.toRegistrationKey("org.apache.james.mailbox.events.MailboxIdRegistrationKey:invalid"))
+ .isInstanceOf(IllegalArgumentException.class);
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org