GlobalTransactionMapper { Optional<RequestGlobalTransaction> mapTo(GlobalTransactionStepDefinition<BaseGlobalTransactionEvent> pipeline) { Optional<BubbleMessage> bubleMessage = txUtility.getBubleMessage(pipeline); RequestGlobalTransaction.RequestGlobalTransactionBuilder requestGlobalTransactionBuilder = new RequestGlobalTransaction.RequestGlobalTransactionBuilder(); requestGlobalTransactionBuilder.name(pipeline.getMeta().getTransactionName()); requestGlobalTransactionBuilder.eventRequestor(pipeline.getIn()); bubleMessage.ifPresent(bm -> { requestGlobalTransactionBuilder.gtx(bm.getGLTX()).parent(bm.getPGTX()).traceId(bm.getTraceId()); if (bm.getCommandId() != null) { InboundCommand inboundCommand = (InboundCommand) commandRepository.findOne(bm.getCommandId()); requestGlobalTransactionBuilder.commandRequestor(inboundCommand); } }); RequestGlobalTransaction rgt = requestGlobalTransactionBuilder.build(); return Optional.of(rgt); }    }
@Test public void testMapToWhenBubbleMessageNotExist() { final BaseGlobalTransactionEvent event = mock(BaseGlobalTransactionEvent.class); final WorkerMeta meta = mock(WorkerMeta.class); final String txName = "txname"; final GlobalTransactionStepDefinition<BaseGlobalTransactionEvent> pipeline = GlobalTransactionStepDefinition.builder() .in(event) .meta(meta) .build(); when(meta.getTransactionName()).thenReturn(txName); when(mockTransactionUtilities.getBubleMessage(pipeline)).thenReturn(Optional.empty()); final RequestGlobalTransaction result = globalTransactionMapperUnderTest.mapTo(pipeline).get(); assertEquals(result.getName(), txName); assertEquals(result.getEventRequestor(), event); assertEquals(result.getGTX(), null); assertEquals(result.getParent(), null); assertEquals(result.getTraceId(), null); assertEquals(result.getCommandRequestor(), null); }
@Test public void testMapToWhenBubbleMessageExistAndCommandIdNull() { final BaseGlobalTransactionEvent event = mock(BaseGlobalTransactionEvent.class); final WorkerMeta meta = mock(WorkerMeta.class); final BubbleMessage bubbleMessage = mock(BubbleMessage.class); final String txName = "txname"; final String gtx = "gtx"; final String pgtx = "pgtx"; final String traceId = "traceId"; final GlobalTransactionStepDefinition<BaseGlobalTransactionEvent> pipeline = GlobalTransactionStepDefinition.builder() .in(event) .meta(meta) .build(); when(mockTransactionUtilities.getBubleMessage(pipeline)).thenReturn(Optional.of(bubbleMessage)); when(bubbleMessage.getGLTX()).thenReturn(gtx); when(bubbleMessage.getPGTX()).thenReturn(pgtx); when(bubbleMessage.getTraceId()).thenReturn(traceId); when(bubbleMessage.getCommandId()).thenReturn(null); when(meta.getTransactionName()).thenReturn(txName); final RequestGlobalTransaction result = globalTransactionMapperUnderTest.mapTo(pipeline).get(); assertEquals(result.getName(), txName); assertEquals(result.getEventRequestor(), event); assertEquals(result.getGTX(), gtx); assertEquals(result.getParent(), pgtx); assertEquals(result.getTraceId(), traceId); assertEquals(result.getCommandRequestor(), null); }
@Test public void testMapToWhenBubbleMessageExistAndCommandIdNotNull() { final BaseGlobalTransactionEvent event = mock(BaseGlobalTransactionEvent.class); final WorkerMeta meta = mock(WorkerMeta.class); final BubbleMessage bubbleMessage = mock(BubbleMessage.class); final InboundCommand command = mock(InboundCommand.class); final String txName = "txname"; final String gtx = "gtx"; final String pgtx = "pgtx"; final String traceId = "traceId"; final String commandId = "commandId"; final GlobalTransactionStepDefinition<BaseGlobalTransactionEvent> pipeline = GlobalTransactionStepDefinition.builder() .in(event) .meta(meta) .build(); when(mockTransactionUtilities.getBubleMessage(pipeline)).thenReturn(Optional.of(bubbleMessage)); when(bubbleMessage.getGLTX()).thenReturn(gtx); when(bubbleMessage.getPGTX()).thenReturn(pgtx); when(bubbleMessage.getTraceId()).thenReturn(traceId); when(bubbleMessage.getCommandId()).thenReturn(commandId); when(meta.getTransactionName()).thenReturn(txName); when(mockCommandRepository.findOne(commandId)).thenReturn(command); final RequestGlobalTransaction result = globalTransactionMapperUnderTest.mapTo(pipeline).get(); assertEquals(result.getName(), txName); assertEquals(result.getEventRequestor(), event); assertEquals(result.getGTX(), gtx); assertEquals(result.getParent(), pgtx); assertEquals(result.getTraceId(), traceId); assertEquals(result.getCommandRequestor(), command); }