### Question:
FlowChainLogService { public List<FlowChainLog> collectRelatedFlowChains(FlowChainLog flowChain) { LOGGER.info("Finding out master flow chain based on chain id {}", flowChain.getFlowChainId()); FlowChainLog rootFlowChain = collectRootFlowChain(flowChain); LOGGER.info("Collecting child flow chains based on master chain id {}", rootFlowChain.getFlowChainId()); Map<String, FlowChainLog> flowChains = new HashMap<>(); flowChains.put(rootFlowChain.getFlowChainId(), rootFlowChain); collectChildFlowChains(flowChains, rootFlowChain); LOGGER.info("Collected flow chain ids for checking: {}", Joiner.on(",") .join(flowChains.keySet())); return flowChains.values() .stream() .sorted(comparing(FlowChainLog::getCreated)) .collect(toList()); }  Optional<FlowChainLog> findFirstByFlowChainIdOrderByCreatedDesc(String flowChainId); List<FlowChainLog> findByFlowChainIdOrderByCreatedDesc(String flowChainId); List<FlowChainLog> collectRelatedFlowChains(FlowChainLog flowChain); boolean hasEventInFlowChainQueue(List<FlowChainLog> flowChains); int purgeOrphanFLowChainLogs(); FlowChainLog save(FlowChainLog chainLog);  }### Answer:
@Test public void testFindAllFlowChainsInTree() { FlowChainLog flowChainLogA = flowChainLog("A", NO_PARENT, 1); FlowChainLog flowChainLogB = flowChainLog("B", flowChainLogA.getFlowChainId(), 2); FlowChainLog flowChainLogC = flowChainLog("C", flowChainLogA.getFlowChainId(), 3); FlowChainLog flowChainLogD = flowChainLog("D", flowChainLogC.getFlowChainId(), 4); FlowChainLog flowChainLogE = flowChainLog("E", flowChainLogC.getFlowChainId(), 5); setUpParent(flowChainLogB, flowChainLogA); setUpChildren(flowChainLogA, List.of(flowChainLogC, flowChainLogB)); setUpChildren(flowChainLogC, List.of(flowChainLogE, flowChainLogD)); List<FlowChainLog> result = underTest.collectRelatedFlowChains(flowChainLogB); assertEquals(List.of(flowChainLogA, flowChainLogB, flowChainLogC, flowChainLogD, flowChainLogE), result); }
@Test public void testFindAllLatestFlowChainsInTree() { FlowChainLog flowChainLogA = flowChainLog("A", NO_PARENT, 1); FlowChainLog flowChainLogB = flowChainLog("B", flowChainLogA.getFlowChainId(), 2); FlowChainLog flowChainLogC = flowChainLog("C", flowChainLogA.getFlowChainId(), 3); FlowChainLog flowChainLogD = flowChainLog("D", flowChainLogC.getFlowChainId(), 4); FlowChainLog flowChainLogES1 = flowChainLog("E", flowChainLogC.getFlowChainId(), 5); FlowChainLog flowChainLogES2 = flowChainLog("E", flowChainLogC.getFlowChainId(), 6); setUpParent(flowChainLogB, flowChainLogA); setUpChildren(flowChainLogA, List.of(flowChainLogC, flowChainLogB)); setUpChildren(flowChainLogC, List.of(flowChainLogES2, flowChainLogES1, flowChainLogD)); List<FlowChainLog> result = underTest.collectRelatedFlowChains(flowChainLogB); assertEquals(List.of(flowChainLogA, flowChainLogB, flowChainLogC, flowChainLogD, flowChainLogES2), result); }