Skip to content

Commit

Permalink
fix: entityId should be propagated to command context in ES testkit
Browse files Browse the repository at this point in the history
  • Loading branch information
aludwiko committed Nov 21, 2024
1 parent 34cbd24 commit af17e7a
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ public class EventSourcedTestKit<S, E, ES extends EventSourcedEntity<S, E>>
extends EventSourcedEntityEffectsRunner<S, E> {

private final ES entity;
private final String entityId;

private final JsonMessageCodec messageCodec;

private EventSourcedTestKit(ES entity) {
private EventSourcedTestKit(ES entity, String entityId) {
super(entity);
this.entity = entity;
this.entityId = entityId;
this.messageCodec = new JsonMessageCodec();
}

Expand Down Expand Up @@ -72,7 +74,7 @@ public static <S, E, ES extends EventSourcedEntity<S, E>> EventSourcedTestKit<S,
public static <S, E, ES extends EventSourcedEntity<S, E>> EventSourcedTestKit<S, E, ES> of(
String entityId, Function<EventSourcedEntityContext, ES> entityFactory) {
EventSourcedEntityContext context = new TestKitEventSourcedEntityContext(entityId);
return new EventSourcedTestKit<>(entityFactory.apply(context));
return new EventSourcedTestKit<>(entityFactory.apply(context), entityId);
}

/**
Expand All @@ -99,7 +101,7 @@ public <R> EventSourcedResult<R> call(Function<ES, EventSourcedEntity.Effect<R>>
* @return a EventSourcedResult
*/
public <R> EventSourcedResult<R> call(Function<ES, EventSourcedEntity.Effect<R>> func, Metadata metadata) {
return interpretEffects(() -> func.apply(entity), metadata);
return interpretEffects(() -> func.apply(entity), entityId, metadata);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public List<E> getAllEvents() {
*/
@SuppressWarnings("unchecked") // event type in loop
protected <R> EventSourcedResult<R> interpretEffects(
Supplier<EventSourcedEntity.Effect<R>> effect, Metadata metadata) {
var commandContext = new TestKitEventSourcedEntityCommandContext(metadata);
Supplier<EventSourcedEntity.Effect<R>> effect, String entityId, Metadata metadata) {
var commandContext = new TestKitEventSourcedEntityCommandContext(entityId, metadata);
EventSourcedEntity.Effect<R> effectExecuted;
try {
entity._internalSetCommandContext(Optional.of(commandContext));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ final class TestKitEventSourcedEntityCommandContext(
extends CommandContext
with InternalContext {

def this(metadata: Metadata) = {
this(metadata = metadata, commandName = "stubCommandName")
def this(entityId: String, metadata: Metadata) = {
this(metadata = metadata, commandName = "stubCommandName", entityId = entityId)
}

override def tracing(): Tracing = TestKitTracing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ public class CounterEventSourcedEntity extends EventSourcedEntity<Integer, Incre

public Effect<String> increaseBy(Integer value) {
if (value <= 0) return effects().error("Can't increase with a negative value");
else return effects().persist(new Increased(value)).thenReply(__ -> "Ok");
else return effects().persist(new Increased(commandContext().entityId(), value)).thenReply(__ -> "Ok");
}

public Effect<String> increaseFromMeta() {
return effects().persist(new Increased(Integer.parseInt(commandContext().metadata().get("value").get()))).thenReply(__ -> "Ok");
return effects().persist(new Increased(commandContext().entityId(), Integer.parseInt(commandContext().metadata().get("value").get()))).thenReply(__ -> "Ok");
}

public Effect<String> doubleIncreaseBy(Integer value) {
if (value < 0) return effects().error("Can't increase with a negative value");
else {
Increased event = new Increased(value);
Increased event = new Increased(commandContext().entityId(), value);
return effects().persist(event, event).thenReply(__ -> "Ok");
}
}

@Override
public Integer applyEvent(Increased increased) {
if (currentState() == null) return increased.value;
else return currentState() + increased.value;
if (currentState() == null) return increased.value();
else return currentState() + increased.value();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import akka.javasdk.testkit.EventSourcedResult;
import akka.javasdk.testkit.EventSourcedTestKit;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand All @@ -19,20 +20,22 @@ public void testIncrease() {
EventSourcedTestKit.of(ctx -> new CounterEventSourcedEntity());
EventSourcedResult<String> result = testKit.call(entity -> entity.increaseBy(10));
assertTrue(result.isReply());
assertEquals(result.getReply(), "Ok");
assertEquals(testKit.getState(), 10);
assertEquals(testKit.getAllEvents().size(), 1);
assertEquals("Ok", result.getReply());
assertEquals(10, testKit.getState());
assertEquals(1, testKit.getAllEvents().size());
}

@Test
public void testIncreaseWithMetadata() {
String counterId = "123";
EventSourcedTestKit<Integer, Increased, CounterEventSourcedEntity> testKit =
EventSourcedTestKit.of(ctx -> new CounterEventSourcedEntity());
EventSourcedTestKit.of(counterId, ctx -> new CounterEventSourcedEntity());
EventSourcedResult<String> result = testKit.call(entity -> entity.increaseFromMeta(), Metadata.EMPTY.add("value", "10"));
assertTrue(result.isReply());
assertEquals(result.getReply(), "Ok");
assertEquals(testKit.getState(), 10);
assertEquals(testKit.getAllEvents().size(), 1);
assertEquals(new Increased(counterId, 10), result.getNextEventOfType(Increased.class));
assertEquals("Ok", result.getReply());
assertEquals(10, testKit.getState());
assertEquals(1, testKit.getAllEvents().size());
}

@Test
Expand All @@ -41,9 +44,9 @@ public void testDoubleIncrease() {
EventSourcedTestKit.of(ctx -> new CounterEventSourcedEntity());
EventSourcedResult<String> result = testKit.call(entity -> entity.doubleIncreaseBy(10));
assertTrue(result.isReply());
assertEquals(result.getReply(), "Ok");
assertEquals(testKit.getState(), 20);
assertEquals(testKit.getAllEvents().size(), 2);
assertEquals("Ok", result.getReply());
assertEquals(20, testKit.getState());
assertEquals(2, testKit.getAllEvents().size());
}

@Test
Expand All @@ -52,6 +55,6 @@ public void testIncreaseWithNegativeValue() {
EventSourcedTestKit.of(ctx -> new CounterEventSourcedEntity());
EventSourcedResult<String> result = testKit.call(entity -> entity.increaseBy(-10));
assertTrue(result.isError());
assertEquals(result.getError(), "Can't increase with a negative value");
assertEquals("Can't increase with a negative value", result.getError());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,5 @@

package akka.javasdk.testkit.eventsourced;

public class Increased {

public final Integer value;

public Increased(Integer value) {
this.value = value;
}
public record Increased(String counterId, Integer value) {
}

0 comments on commit af17e7a

Please sign in to comment.