BusinessGlobalTransactionServiceImpl implements BusinessGlobalTransactionService { String trace(RequestGlobalTransaction rgt) { String trace; if (rgt.getParent() != null) { trace = rgt.getTraceId(); } else { if(tracer.getCurrentSpan()==null){ trace = tracer.createSpan("inParentGlobalTransaction").traceIdString(); publisher.publishEvent(new GtxSpanStarted()); } else { trace = tracer.getCurrentSpan().traceIdString(); } } return trace; } BusinessGlobalTransactionServiceImpl(); @Autowired  BusinessGlobalTransactionServiceImpl(GlobalTransactionRepository globalTransactionRepository,
												Tracer tracer,
												ApplicationEventPublisher publisher); @Override Optional<GlobalTransaction> findGlobalTransaction(final String id); @Override GlobalTransaction participateToGlobalTransaction(Optional<RequestGlobalTransaction> orgt); @Override Optional<GlobalTransaction> retrieveGlobalTransaction(Optional<RequestGlobalTransaction> orgt);  }
@Test public void testtraceWhenParentNull(){ final RequestGlobalTransaction rgt = mock(RequestGlobalTransaction.class); final String traceId = "traceId"; when(rgt.getTraceId()).thenReturn(traceId); when(rgt.getParent()).thenReturn("parent"); String result = businessGlobalTransactionServiceImplUnderTest.trace(rgt); assertEquals(result, traceId); verify(mockTracer, never()).createSpan(anyString()); verify(mockPublisher, never()).publishEvent(any(GtxSpanStarted.class)); }
@Test public void testtraceWhenParentNotNullAndCurrentSpanNull(){ final RequestGlobalTransaction rgt = mock(RequestGlobalTransaction.class); final String traceId = "traceId"; final String spanName = "inParentGlobalTransaction"; final Span span = mock(Span.class); when(rgt.getParent()).thenReturn(null); when(mockTracer.getCurrentSpan()).thenReturn(null); when(mockTracer.createSpan(spanName)).thenReturn(span); when(span.traceIdString()).thenReturn(traceId); String result = businessGlobalTransactionServiceImplUnderTest.trace(rgt); assertEquals(result, traceId); verify(mockTracer, times(1)).createSpan(same(spanName)); verify(mockPublisher, times(1)).publishEvent(any(GtxSpanStarted.class)); }
@Test public void testtraceWhenParentNotNullAndCurrentSpanNotNull(){ final RequestGlobalTransaction rgt = mock(RequestGlobalTransaction.class); final String traceId = "traceId"; final Span span = mock(Span.class); when(rgt.getParent()).thenReturn(null); when(mockTracer.getCurrentSpan()).thenReturn(span); when(span.traceIdString()).thenReturn(traceId); String result = businessGlobalTransactionServiceImplUnderTest.trace(rgt); assertEquals(result, traceId); verify(mockTracer, never()).createSpan(anyString()); verify(mockPublisher, never()).publishEvent(any(GtxSpanStarted.class)); }