Skip to content

Commit

Permalink
Merge pull request #2141 from AxonFramework/feature/1125
Browse files Browse the repository at this point in the history
[#1125] Introduce `SagaLifecycle.associationValues()`
  • Loading branch information
CodeDrivenMitch committed Mar 22, 2022
2 parents 0c6ef17 + 59eb385 commit 8ea8532
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright (c) 2010-2018. Axon Framework
* Copyright (c) 2010-2022. 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
* 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,
Expand All @@ -19,6 +19,8 @@
import org.axonframework.messaging.Scope;
import org.axonframework.messaging.ScopeDescriptor;

import java.util.Set;

/**
* Abstract base class of a component that models a saga's life cycle.
*/
Expand Down Expand Up @@ -87,6 +89,17 @@ public static void end() {
getInstance().doEnd();
}

/**
* Retrieves the {@link AssociationValue AssociationValues} that have been associated with the current Saga so far.
* This includes the uncommitted ones, so adding or removing a {@link @link AssociationValue} through
* {@link SagaLifecycle#associateWith(AssociationValue)} or any other method will have an immediate effect.
*
* @return The {@link AssociationValue AssociationValues} that have been associated with the Saga so far
*/
public static Set<AssociationValue> associationValues() {
return getInstance().getAssociationValues().asSet();
}

/**
* {@link SagaLifecycle} instance method to mark the current saga as ended.
*/
Expand All @@ -108,6 +121,13 @@ public static void end() {
*/
protected abstract void doAssociateWith(AssociationValue associationValue);

/**
* {@link SagaLifecycle} instance method to retrieve the {@link AssociationValues} of the currently active Saga.
*
* @return The {@link AssociationValues} of the current saga.
*/
protected abstract AssociationValues getAssociationValues();

/**
* Get the current {@link SagaLifecycle} instance for the current thread. If none exists an {@link
* IllegalStateException} is thrown.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright (c) 2010-2020. Axon Framework
* Copyright (c) 2010-2022. 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
* 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,
Expand All @@ -28,6 +28,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import static org.axonframework.modelling.saga.SagaLifecycle.removeAssociationWith;
import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -143,6 +144,24 @@ void testPrepareResetWithResetContextThrowsResetNotSupportedException() {
assertThrows(ResetNotSupportedException.class, () -> testSubject.prepareReset("some-reset-context"));
}

@Test
void testLifecycle_associationValues() {
testSubject.doAssociateWith(new AssociationValue("propertyName", "id"));
testSubject.execute(() -> {
Set<AssociationValue> associationValues = SagaLifecycle.associationValues();
assertEquals(1, associationValues.size());
assertTrue(associationValues.contains(new AssociationValue("propertyName", "id")));
});

testSubject.doAssociateWith(new AssociationValue("someOtherProperty", "3"));
testSubject.execute(() -> {
Set<AssociationValue> associationValues = SagaLifecycle.associationValues();
assertEquals(2, associationValues.size());
assertTrue(associationValues.contains(new AssociationValue("propertyName", "id")));
assertTrue(associationValues.contains(new AssociationValue("someOtherProperty", "3")));
});
}

@SuppressWarnings("unused")
private static class StubAnnotatedSaga {

Expand Down

0 comments on commit 8ea8532

Please sign in to comment.