### Question:
RequestActorBuilder { public RequestActorContext build(SagaRequest[] requests, Map<String, SagaTask> tasks, ActorRef sagaActor) { RequestActorContext context = new RequestActorContext(childrenExtractor); ActorRef rootActor = rootActor(context, tasks); ActorRef leafActor = leafActor(context, tasks); createRequestActors(requests, tasks, context); linkActorsById(rootActor, requests, context); addLeafToChildless(leafActor, requests, context); context.addParent(NoOpSagaRequest.SAGA_START_REQUEST.id(), sagaActor); context.addChild(NoOpSagaRequest.SAGA_END_REQUEST.id(), sagaActor); return context; } RequestActorBuilder(
      ActorSystem actorSystem,
      FromJsonFormat<Set<String>> childrenExtractor); RequestActorContext build(SagaRequest[] requests, Map<String, SagaTask> tasks, ActorRef sagaActor);  }### Answer:
@Test public void createOneActorPerRequest() throws Exception { new TestKit(actorSystem) {{ ArgumentCaptor<SagaResponse> argumentCaptor = ArgumentCaptor.forClass(SagaResponse.class); when(task.commit(eq(NoOpSagaRequest.SAGA_END_REQUEST), argumentCaptor.capture())).thenReturn( SagaResponse.EMPTY_RESPONSE); ActorRef root = actorBuilder.build(requests, tasks, getRef()).actorOf(NoOpSagaRequest.SAGA_START_REQUEST.id()); root.tell(new TransactMessage(NoOpSagaRequest.SAGA_START_REQUEST, SagaResponse.EMPTY_RESPONSE), getRef()); List<SagaResponse> responses = receiveN(1, Duration.of(2, SECONDS)).stream() .map(o -> ((TransactMessage) o).response()) .collect(Collectors.toList()); assertThat(responses, Matchers.containsInAnyOrder(SagaResponse.EMPTY_RESPONSE)); verify(task).commit(NoOpSagaRequest.SAGA_START_REQUEST, SagaResponse.EMPTY_RESPONSE); verify(task).commit(request1, SagaResponse.EMPTY_RESPONSE); verify(task).commit(request2, response1); verify(task).commit(request3, response1); verify(task).commit(eq(NoOpSagaRequest.SAGA_END_REQUEST), any(SagaResponse.class)); SagaResponse response = argumentCaptor.getValue(); assertThat(response, instanceOf(CompositeSagaResponse.class)); assertThat(((CompositeSagaResponse) response).responses(), containsInAnyOrder(response2, response3)); }}; }
@Test public void compensateAllCompletedTransactions() throws Exception { new TestKit(actorSystem) {{ ArgumentCaptor<SagaResponse> argumentCaptor = ArgumentCaptor.forClass(SagaResponse.class); when(task.commit(eq(NoOpSagaRequest.SAGA_END_REQUEST), argumentCaptor.capture())).thenReturn( SagaResponse.EMPTY_RESPONSE); ActorRef root = actorBuilder.build(requests, tasks, getRef()).actorOf(NoOpSagaRequest.SAGA_START_REQUEST.id()); root.tell(new TransactMessage(NoOpSagaRequest.SAGA_START_REQUEST, SagaResponse.EMPTY_RESPONSE), getRef()); List<SagaResponse> responses = receiveN(1, Duration.of(2, SECONDS)).stream() .map(o -> ((TransactMessage) o).response()) .collect(Collectors.toList()); assertThat(responses, Matchers.containsInAnyOrder(SagaResponse.EMPTY_RESPONSE)); CompensateMessage message = new CompensateMessage(response1); getLastSender().tell(message, getRef()); expectMsg(message); verify(task).compensate(NoOpSagaRequest.SAGA_START_REQUEST); verify(task).compensate(request1); verify(task).compensate(request2); verify(task).compensate(request3); verify(task).compensate(NoOpSagaRequest.SAGA_END_REQUEST); }}; }