### Question:
GraphBuilder { @Segment(name = "buildGraph", category = "application", library = "kamon") public SingleLeafDirectedAcyclicGraph<SagaRequest> build(SagaRequest[] sagaRequests) { Map<String, Node<SagaRequest>> requestNodes = requestsToNodes(sagaRequests); SingleLeafDirectedAcyclicGraph<SagaRequest> graph = linkNodesToGraph(sagaRequests, requestNodes); detectCycle(graph); return graph; } GraphBuilder(GraphCycleDetector<SagaRequest> detector); @Segment(name = "buildGraph", category = "application", library = "kamon") SingleLeafDirectedAcyclicGraph<SagaRequest> build(SagaRequest[] sagaRequests);  }### Answer:
@Test public void buildsGraphOfParallelRequests() { SingleLeafDirectedAcyclicGraph<SagaRequest> tasks = graphBuilder.build(requests); Traveller<SagaRequest> traveller = new ByLevelTraveller<>(tasks, new FromRootTraversalDirection<SagaRequest>()); Collection<Node<SagaRequest>> nodes = traveller.nodes(); traveller.next(); assertThat(requestsOf(nodes), IsIterableContainingInOrder.contains(NoOpSagaRequest.SAGA_START_REQUEST)); nodes.clear(); traveller.next(); assertThat(requestsOf(nodes), contains(request1, request2)); nodes.clear(); traveller.next(); assertThat(requestsOf(nodes), contains(request3)); nodes.clear(); traveller.next(); assertThat(requestsOf(nodes), IsIterableContainingInOrder.contains(NoOpSagaRequest.SAGA_END_REQUEST)); }
@Test public void blowsUpWhenJsonContainsDuplicateRequestId() { try { graphBuilder.build(duplicateRequests); fail(SagaException.class.getSimpleName() + " is expected, but none thrown"); } catch (SagaException e) { assertThat(e.getMessage(), is("Failed to interpret requests with duplicate request id: request-duplicate-id")); } }
@Test public void blowsUpWhenGraphContainsCycle() { reset(detector); when(detector.cycleJoints((SingleLeafDirectedAcyclicGraph<SagaRequest>) any())).thenReturn(singleton(new Node<SagaRequest>(0L, null))); try { graphBuilder.build(requests); expectFailing(SagaException.class); } catch (SagaException e) { assertThat(e.getMessage(), startsWith("Cycle detected in the request graph at nodes ")); } }