Skip to content

Commit

Permalink
JAMES-3529 Add StateMismatch error type
Browse files Browse the repository at this point in the history
  • Loading branch information
quantranhong1999 committed Apr 8, 2021
1 parent 5ca0f79 commit faeafeb
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
@@ -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 RuntimeException {
public StateMismatchException(String msg) {
super(msg);
}

public static void checkState(boolean expression, String msg) {
if (!expression) {
throw new StateMismatchException(msg);
}
}
}
Expand Up @@ -25,6 +25,7 @@
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;
Expand Down Expand Up @@ -68,7 +69,7 @@ private FilteringAggregate(FilteringAggregateId aggregateId, History history) {

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);
Expand Down
Expand Up @@ -33,6 +33,7 @@

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;

Expand Down Expand Up @@ -154,7 +155,7 @@ default void modifyExistingRulesWithWrongCurrentVersionShouldFail(EventStore eve
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
Expand Down Expand Up @@ -217,15 +218,15 @@ default void setRulesWithIfInStateIsInitialWhenAStateIsDefinedShouldFail(EventSt
.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
default void setRulesWithIfInStateIsOneWhenNonStateIsDefinedShouldFail(EventStore eventStore) {
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);
}

}
Expand Up @@ -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)
Expand All @@ -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])

0 comments on commit faeafeb

Please sign in to comment.