Skip to content

Commit

Permalink
Support MessageHandler that throws checked exceptions in saga tests.
Browse files Browse the repository at this point in the history
Commit 28ce88e caused saga test fixture to break due to unchecked calls to MessageHandler. This commit adds "throws Exception" clause where required and intruduces a custom Consumer with a throws clause.
  • Loading branch information
MrRothstein committed Feb 29, 2016
1 parent 86e6923 commit b887e91
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 21 deletions.
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2010-2014. Axon Framework
*
* Licensed 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.axonframework.test.eventscheduler;

/**
* A consumer that allows implementations to throw a exceptions.
*
* @author Steve Chernyak
* @since 3.0
*/
@FunctionalInterface
public interface EventConsumer<T> {

void accept(T var) throws Exception;
}
Expand Up @@ -138,7 +138,7 @@ public EventMessage advanceToNextTrigger() {
* @param newDateTime The time to advance the "current time" of the scheduler to
* @param eventConsumer The function to invoke for each event to trigger
*/
public void advanceTime(ZonedDateTime newDateTime, Consumer<EventMessage<?>> eventConsumer) {
public void advanceTime(ZonedDateTime newDateTime, EventConsumer<EventMessage<?>> eventConsumer) throws Exception {
while (!scheduledEvents.isEmpty() && !scheduledEvents.first().getScheduleTime().isAfter(newDateTime)) {
eventConsumer.accept(advanceToNextTrigger());
}
Expand All @@ -154,7 +154,7 @@ public void advanceTime(ZonedDateTime newDateTime, Consumer<EventMessage<?>> eve
* @param duration The amount of time to advance the "current time" of the scheduler with
* @param eventConsumer The function to invoke for each event to trigger
*/
public void advanceTime(Duration duration, Consumer<EventMessage<?>> eventConsumer) {
public void advanceTime(Duration duration, EventConsumer<EventMessage<?>> eventConsumer) throws Exception {
advanceTime(currentDateTime.plus(duration), eventConsumer);
}
}
Expand Up @@ -89,7 +89,7 @@ public AnnotatedSagaTestFixture(Class<? extends AbstractAnnotatedSaga> sagaType)
}

@Override
public FixtureExecutionResult whenTimeElapses(Duration elapsedTime) {
public FixtureExecutionResult whenTimeElapses(Duration elapsedTime) throws Exception {
try {
fixtureExecutionResult.startRecording();
eventScheduler.advanceTime(elapsedTime, sagaManager::handle);
Expand All @@ -100,7 +100,7 @@ public FixtureExecutionResult whenTimeElapses(Duration elapsedTime) {
}

@Override
public FixtureExecutionResult whenTimeAdvancesTo(ZonedDateTime newDateTime) {
public FixtureExecutionResult whenTimeAdvancesTo(ZonedDateTime newDateTime) throws Exception {
try {
fixtureExecutionResult.startRecording();
eventScheduler.advanceTime(newDateTime, sagaManager::handle);
Expand Down Expand Up @@ -128,7 +128,7 @@ public GivenAggregateEventPublisher givenAggregate(String aggregateIdentifier) {
}

@Override
public ContinuedGivenState givenAPublished(Object event) {
public ContinuedGivenState givenAPublished(Object event) throws Exception {
sagaManager.handle(GenericEventMessage.asEventMessage(event));
return this;
}
Expand All @@ -144,19 +144,19 @@ public GivenAggregateEventPublisher andThenAggregate(String aggregateIdentifier)
}

@Override
public ContinuedGivenState andThenTimeElapses(final Duration elapsedTime) {
public ContinuedGivenState andThenTimeElapses(final Duration elapsedTime) throws Exception {
eventScheduler.advanceTime(elapsedTime, sagaManager::handle);
return this;
}

@Override
public ContinuedGivenState andThenTimeAdvancesTo(final ZonedDateTime newDateTime) {
public ContinuedGivenState andThenTimeAdvancesTo(final ZonedDateTime newDateTime) throws Exception {
eventScheduler.advanceTime(newDateTime, sagaManager::handle);
return this;
}

@Override
public ContinuedGivenState andThenAPublished(Object event) {
public ContinuedGivenState andThenAPublished(Object event) throws Exception {
sagaManager.handle(GenericEventMessage.asEventMessage(event));
return this;
}
Expand All @@ -168,7 +168,7 @@ public WhenAggregateEventPublisher whenAggregate(String aggregateIdentifier) {
}

@Override
public FixtureExecutionResult whenPublishingA(Object event) {
public FixtureExecutionResult whenPublishingA(Object event) throws Exception {
try {
fixtureExecutionResult.startRecording();
sagaManager.handle(GenericEventMessage.asEventMessage(event));
Expand Down Expand Up @@ -287,13 +287,13 @@ public AggregateEventPublisherImpl(String aggregateIdentifier) {
}

@Override
public ContinuedGivenState published(Object... events) {
public ContinuedGivenState published(Object... events) throws Exception {
publish(events);
return AnnotatedSagaTestFixture.this;
}

@Override
public FixtureExecutionResult publishes(Object event) {
public FixtureExecutionResult publishes(Object event) throws Exception {
try {
publish(event);
} finally {
Expand All @@ -302,7 +302,7 @@ public FixtureExecutionResult publishes(Object event) {
return fixtureExecutionResult;
}

private void publish(Object... events) {
private void publish(Object... events) throws Exception {
GenericEventMessage.clock = Clock.fixed(currentTime().toInstant(),currentTime().getZone());

try {
Expand Down
Expand Up @@ -47,7 +47,7 @@ public interface ContinuedGivenState extends WhenState {
* @param elapsedTime The amount of time that will elapse
* @return an object that allows registration of the actual events to send
*/
ContinuedGivenState andThenTimeElapses(Duration elapsedTime);
ContinuedGivenState andThenTimeElapses(Duration elapsedTime) throws Exception;

/**
* Simulate time shifts in the current given state. This can be useful when the time between given events is of
Expand All @@ -56,7 +56,7 @@ public interface ContinuedGivenState extends WhenState {
* @param newDateTime The time to advance the clock to
* @return an object that allows registration of the actual events to send
*/
ContinuedGivenState andThenTimeAdvancesTo(ZonedDateTime newDateTime);
ContinuedGivenState andThenTimeAdvancesTo(ZonedDateTime newDateTime) throws Exception;

/**
* Indicates that the given <code>event</code> has been published in the past. This event is sent to the associated
Expand All @@ -65,5 +65,5 @@ public interface ContinuedGivenState extends WhenState {
* @param event The event to publish
* @return an object that allows chaining of more given state
*/
ContinuedGivenState andThenAPublished(Object event);
ContinuedGivenState andThenAPublished(Object event) throws Exception;
}
Expand Up @@ -130,7 +130,7 @@ public interface FixtureConfiguration {
* @param event The event to publish
* @return an object that allows chaining of more given state
*/
ContinuedGivenState givenAPublished(Object event);
ContinuedGivenState givenAPublished(Object event) throws Exception;

/**
* Indicates that no relevant activity has occurred in the past.
Expand Down
Expand Up @@ -34,5 +34,5 @@ public interface GivenAggregateEventPublisher {
* @param events The events published by the aggregate
* @return a reference to the fixture to support a fluent interface
*/
ContinuedGivenState published(Object... events);
ContinuedGivenState published(Object... events) throws Exception;
}
Expand Up @@ -33,5 +33,5 @@ public interface WhenAggregateEventPublisher {
* @param event The event published by the aggregate
* @return a reference to the test results for the validation phase
*/
FixtureExecutionResult publishes(Object event);
FixtureExecutionResult publishes(Object event) throws Exception;
}
6 changes: 3 additions & 3 deletions test/src/main/java/org/axonframework/test/saga/WhenState.java
Expand Up @@ -54,7 +54,7 @@ public interface WhenState {
* @param event the event to publish
* @return an object allowing you to verify the test results
*/
FixtureExecutionResult whenPublishingA(Object event);
FixtureExecutionResult whenPublishingA(Object event) throws Exception;

/**
* Mimic an elapsed time with no relevant activity for the Saga. If any Events are scheduled to be published within
Expand All @@ -67,7 +67,7 @@ public interface WhenState {
* @param elapsedTime The amount of time to elapse
* @return an object allowing you to verify the test results
*/
FixtureExecutionResult whenTimeElapses(Duration elapsedTime);
FixtureExecutionResult whenTimeElapses(Duration elapsedTime) throws Exception;

/**
* Mimic an elapsed time with no relevant activity for the Saga. If any Events are scheduled to be published within
Expand All @@ -80,5 +80,5 @@ public interface WhenState {
* @param newDateTime The time to advance the clock to
* @return an object allowing you to verify the test results
*/
FixtureExecutionResult whenTimeAdvancesTo(ZonedDateTime newDateTime);
FixtureExecutionResult whenTimeAdvancesTo(ZonedDateTime newDateTime) throws Exception;
}

0 comments on commit b887e91

Please sign in to comment.