@Test
  public void testExecuteHttpCodeNoSuccess() throws Exception {

    final SpanId mockSpanId = mock(SpanId.class);
    when(mockSpanId.getTraceId()).thenReturn(TRACE_ID);
    when(mockSpanId.getSpanId()).thenReturn(SPAN_ID);
    when(mockSpanId.getParentSpanId()).thenReturn(PARENT_SPAN_ID);

    when(mockClientTracer.startNewSpan(PATH)).thenReturn(mockSpanId);
    when(mockClientResponse.getStatus()).thenReturn(SERVER_ERROR_STATUS);

    assertSame(mockClientResponse, interceptor.execute(mockExecutionContext));

    final InOrder inOrder = inOrder(mockClientTracer, mockClientRequest, mockExecutionContext);

    inOrder.verify(mockClientTracer).startNewSpan(PATH);
    inOrder.verify(mockClientRequest).header(BraveHttpHeaders.Sampled.getName(), "true");
    inOrder.verify(mockClientRequest).header(BraveHttpHeaders.TraceId.getName(), TRACE_ID_HEX);
    inOrder.verify(mockClientRequest).header(BraveHttpHeaders.SpanId.getName(), SPAN_ID_HEX);
    inOrder
        .verify(mockClientRequest)
        .header(BraveHttpHeaders.ParentSpanId.getName(), PARENT_SPAN_ID_HEX);
    inOrder.verify(mockClientTracer).setCurrentClientServiceName(CONTEXT_PATH);
    inOrder
        .verify(mockClientTracer)
        .submitBinaryAnnotation(REQUEST_ANNOTATION, HTTP_METHOD + " " + URI);
    inOrder.verify(mockClientTracer).setClientSent();
    inOrder.verify(mockExecutionContext).proceed();
    inOrder
        .verify(mockClientTracer)
        .submitBinaryAnnotation("http.responsecode", SERVER_ERROR_STATUS);
    inOrder.verify(mockClientTracer).submitAnnotation("failure");
    inOrder.verify(mockClientTracer).setClientReceived();
    verifyNoMoreInteractions(mockClientTracer);
  }
  @Test
  public void testExecuteTracingNoParentSpan() throws Exception {

    final SpanId mockSpanId = mock(SpanId.class);
    when(mockSpanId.getTraceId()).thenReturn(TRACE_ID);
    when(mockSpanId.getSpanId()).thenReturn(SPAN_ID);
    when(mockSpanId.getParentSpanId()).thenReturn(null);

    when(mockClientTracer.startNewSpan(PATH)).thenReturn(mockSpanId);
    assertSame(mockClientResponse, interceptor.execute(mockExecutionContext));

    final InOrder inOrder = inOrder(mockClientTracer, mockClientRequest, mockExecutionContext);

    inOrder.verify(mockClientTracer).startNewSpan(PATH);
    inOrder.verify(mockClientRequest).header(BraveHttpHeaders.Sampled.getName(), "true");
    inOrder.verify(mockClientRequest).header(BraveHttpHeaders.TraceId.getName(), TRACE_ID_HEX);
    inOrder.verify(mockClientRequest).header(BraveHttpHeaders.SpanId.getName(), SPAN_ID_HEX);
    inOrder.verify(mockClientTracer).setCurrentClientServiceName(CONTEXT_PATH);
    inOrder
        .verify(mockClientTracer)
        .submitBinaryAnnotation(REQUEST_ANNOTATION, HTTP_METHOD + " " + URI);
    inOrder.verify(mockClientTracer).setClientSent();
    inOrder.verify(mockExecutionContext).proceed();
    inOrder
        .verify(mockClientTracer)
        .submitBinaryAnnotation(HTTP_RESPONSE_CODE_ANNOTATION, OK_STATUS);
    inOrder.verify(mockClientTracer).setClientReceived();
    verifyNoMoreInteractions(mockClientTracer);
  }
  @Test
  public void testExecuteNoTracingSpanNameNotSpecified() throws Exception {

    when(mockClientTracer.startNewSpan(CUSTOM_SPAN_NAME)).thenReturn(null);
    assertSame(mockClientResponse, interceptor.execute(mockExecutionContext));

    final InOrder inOrder = inOrder(mockClientTracer, mockClientRequest, mockExecutionContext);

    inOrder.verify(mockClientTracer).startNewSpan(PATH);
    inOrder.verify(mockClientRequest).header(BraveHttpHeaders.Sampled.getName(), "false");
    inOrder.verify(mockClientTracer).setCurrentClientServiceName(CONTEXT_PATH);
    inOrder
        .verify(mockClientTracer)
        .submitBinaryAnnotation(REQUEST_ANNOTATION, HTTP_METHOD + " " + URI);
    inOrder.verify(mockClientTracer).setClientSent();
    inOrder.verify(mockExecutionContext).proceed();
    inOrder
        .verify(mockClientTracer)
        .submitBinaryAnnotation(HTTP_RESPONSE_CODE_ANNOTATION, OK_STATUS);
    inOrder.verify(mockClientTracer).setClientReceived();
    verifyNoMoreInteractions(mockClientTracer);
  }
  @Test
  public void testExecuteProceedThrowsException() throws Exception {

    final SpanId mockSpanId = mock(SpanId.class);
    when(mockSpanId.getTraceId()).thenReturn(TRACE_ID);
    when(mockSpanId.getSpanId()).thenReturn(SPAN_ID);
    when(mockSpanId.getParentSpanId()).thenReturn(PARENT_SPAN_ID);

    when(mockClientTracer.startNewSpan(PATH)).thenReturn(mockSpanId);
    final IllegalStateException exception = new IllegalStateException("Test exception");
    when(mockExecutionContext.proceed()).thenThrow(exception);

    try {
      interceptor.execute(mockExecutionContext);
      fail("Expected exception.");
    } catch (final IllegalStateException e) {
      assertSame(exception, e);
    }

    final InOrder inOrder = inOrder(mockClientTracer, mockClientRequest, mockExecutionContext);

    inOrder.verify(mockClientTracer).startNewSpan(PATH);
    inOrder.verify(mockClientRequest).header(BraveHttpHeaders.Sampled.getName(), "true");
    inOrder.verify(mockClientRequest).header(BraveHttpHeaders.TraceId.getName(), TRACE_ID_HEX);
    inOrder.verify(mockClientRequest).header(BraveHttpHeaders.SpanId.getName(), SPAN_ID_HEX);
    inOrder
        .verify(mockClientRequest)
        .header(BraveHttpHeaders.ParentSpanId.getName(), Long.toHexString(PARENT_SPAN_ID));
    inOrder.verify(mockClientTracer).setCurrentClientServiceName(CONTEXT_PATH);
    inOrder
        .verify(mockClientTracer)
        .submitBinaryAnnotation(REQUEST_ANNOTATION, HTTP_METHOD + " " + URI);
    inOrder.verify(mockClientTracer).setClientSent();
    inOrder.verify(mockExecutionContext).proceed();
    inOrder.verify(mockClientTracer).submitBinaryAnnotation(HTTP_RESPONSE_CODE_ANNOTATION, 0);
    inOrder.verify(mockClientTracer).submitAnnotation("failure");
    inOrder.verify(mockClientTracer).setClientReceived();
    verifyNoMoreInteractions(mockClientTracer);
  }