You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by bt...@apache.org on 2021/04/16 01:57:22 UTC
[james-project] 01/02: JAMES-3529 Add StateMismatch error type
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 1c945082dc8bc792e23654b74f6096e1f74ec885
Author: quanth <hq...@linagora.com>
AuthorDate: Thu Apr 8 15:46:26 2021 +0700
JAMES-3529 Add StateMismatch error type
---
.../jmap/api/exception/StateMismatchException.java | 32 ++++++++++++++++++++++
.../api/filtering/impl/FilteringAggregate.java | 3 +-
.../api/filtering/FilteringManagementContract.java | 7 +++--
.../org/apache/james/jmap/core/SetError.scala | 4 +++
4 files changed, 42 insertions(+), 4 deletions(-)
diff --git a/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/exception/StateMismatchException.java b/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/exception/StateMismatchException.java
new file mode 100644
index 0000000..b848398
--- /dev/null
+++ b/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/exception/StateMismatchException.java
@@ -0,0 +1,32 @@
+/****************************************************************
+ * 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.api.exception;
+
+public class StateMismatchException extends IllegalStateException {
+ public static void checkState(boolean expression, String msg) {
+ if (!expression) {
+ throw new StateMismatchException(msg);
+ }
+ }
+
+ public StateMismatchException(String msg) {
+ super(msg);
+ }
+}
diff --git a/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/filtering/impl/FilteringAggregate.java b/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/filtering/impl/FilteringAggregate.java
index 31e1b14..6e3baf1 100644
--- a/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/filtering/impl/FilteringAggregate.java
+++ b/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/filtering/impl/FilteringAggregate.java
@@ -25,6 +25,7 @@ import java.util.Optional;
import org.apache.james.eventsourcing.Event;
import org.apache.james.eventsourcing.EventId;
import org.apache.james.eventsourcing.eventstore.History;
+import org.apache.james.jmap.api.exception.StateMismatchException;
import org.apache.james.jmap.api.filtering.Rule;
import org.apache.james.jmap.api.filtering.Rules;
import org.apache.james.jmap.api.filtering.Version;
@@ -68,7 +69,7 @@ public class FilteringAggregate {
public List<? extends Event> defineRules(DefineRulesCommand storeCommand) {
Preconditions.checkArgument(shouldNotContainDuplicates(storeCommand.getRules()));
- Preconditions.checkArgument(expectedState(storeCommand.getIfInState()), "Provided state must be as same as the current state");
+ StateMismatchException.checkState(expectedState(storeCommand.getIfInState()), "Provided state must be as same as the current state");
ImmutableList<RuleSetDefined> events = ImmutableList.of(
new RuleSetDefined(aggregateId, history.getNextEventId(), ImmutableList.copyOf(storeCommand.getRules())));
events.forEach(this::apply);
diff --git a/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/filtering/FilteringManagementContract.java b/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/filtering/FilteringManagementContract.java
index 9acb601..a23f440 100644
--- a/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/filtering/FilteringManagementContract.java
+++ b/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/filtering/FilteringManagementContract.java
@@ -33,6 +33,7 @@ import java.util.Optional;
import org.apache.james.core.Username;
import org.apache.james.eventsourcing.eventstore.EventStore;
+import org.apache.james.jmap.api.exception.StateMismatchException;
import org.apache.james.jmap.api.filtering.impl.EventSourcingFilteringManagement;
import org.junit.jupiter.api.Test;
@@ -154,7 +155,7 @@ public interface FilteringManagementContract {
Mono.from(testee.defineRulesForUser(USERNAME, Optional.empty(), RULE_3, RULE_2, RULE_1)).block();
assertThatThrownBy(() -> Mono.from(testee.defineRulesForUser(USERNAME, Optional.of(new Version(1)), RULE_2, RULE_1)).block())
- .isInstanceOf(IllegalArgumentException.class);
+ .isInstanceOf(StateMismatchException.class);
}
@Test
@@ -217,7 +218,7 @@ public interface FilteringManagementContract {
.isEqualTo(new Rules(ImmutableList.of(RULE_3, RULE_2, RULE_1), new Version(0)));
assertThatThrownBy(() -> Mono.from(testee.defineRulesForUser(USERNAME, Optional.of(Version.INITIAL), RULE_2, RULE_1)).block())
- .isInstanceOf(IllegalArgumentException.class);
+ .isInstanceOf(StateMismatchException.class);
}
@Test
@@ -225,7 +226,7 @@ public interface FilteringManagementContract {
FilteringManagement testee = instantiateFilteringManagement(eventStore);
assertThatThrownBy(() -> Mono.from(testee.defineRulesForUser(USERNAME, Optional.of(new Version(1)), RULE_2, RULE_1)).block())
- .isInstanceOf(IllegalArgumentException.class);
+ .isInstanceOf(StateMismatchException.class);
}
}
\ No newline at end of file
diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/core/SetError.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/core/SetError.scala
index 5e13ca5..685578d 100644
--- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/core/SetError.scala
+++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/core/SetError.scala
@@ -33,6 +33,7 @@ object SetError {
val invalidPatchValue: SetErrorType = "invalidPatch"
val notFoundValue: SetErrorType = "notFound"
val forbiddenValue: SetErrorType = "forbidden"
+ val stateMismatchValue: SetErrorType = "stateMismatch"
def invalidArguments(description: SetErrorDescription, properties: Option[Properties] = None): SetError =
SetError(invalidArgumentValue, description, properties)
@@ -48,6 +49,9 @@ object SetError {
def forbidden(description: SetErrorDescription, properties: Properties): SetError =
SetError(forbiddenValue, description, Some(properties))
+
+ def stateMismatch(description: SetErrorDescription, properties: Properties): SetError =
+ SetError(stateMismatchValue, description, Some(properties))
}
case class SetError(`type`: SetErrorType, description: SetErrorDescription, properties: Option[Properties])
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org