Skip to content

Commit

Permalink
Excavator: Prefer AssertJ (#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
svc-excavator-bot authored and bulldozer-bot[bot] committed Oct 10, 2019
1 parent 32e088e commit 0dc3a39
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

package com.palantir.tracing.jersey;

import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
Expand Down Expand Up @@ -113,11 +110,11 @@ public void testTraceState_withHeaderUsesTraceId() {
.header(TraceHttpHeaders.PARENT_SPAN_ID, "parentSpanId")
.header(TraceHttpHeaders.SPAN_ID, "spanId")
.get();
assertThat(response.getHeaderString(TraceHttpHeaders.TRACE_ID), is("traceId"));
assertThat(response.getHeaderString(TraceHttpHeaders.PARENT_SPAN_ID), is(nullValue()));
assertThat(response.getHeaderString(TraceHttpHeaders.SPAN_ID), is(nullValue()));
assertThat(response.getHeaderString(TraceHttpHeaders.TRACE_ID)).isEqualTo("traceId");
assertThat(response.getHeaderString(TraceHttpHeaders.PARENT_SPAN_ID)).isNull();
assertThat(response.getHeaderString(TraceHttpHeaders.SPAN_ID)).isNull();
verify(observer).consume(spanCaptor.capture());
assertThat(spanCaptor.getValue().getOperation(), is("Jersey: GET /trace"));
assertThat(spanCaptor.getValue().getOperation()).isEqualTo("Jersey: GET /trace");
}

@Test
Expand All @@ -127,11 +124,11 @@ public void testTraceState_respectsMethod() {
.header(TraceHttpHeaders.PARENT_SPAN_ID, "parentSpanId")
.header(TraceHttpHeaders.SPAN_ID, "spanId")
.post(Entity.json(""));
assertThat(response.getHeaderString(TraceHttpHeaders.TRACE_ID), is("traceId"));
assertThat(response.getHeaderString(TraceHttpHeaders.PARENT_SPAN_ID), is(nullValue()));
assertThat(response.getHeaderString(TraceHttpHeaders.SPAN_ID), is(nullValue()));
assertThat(response.getHeaderString(TraceHttpHeaders.TRACE_ID)).isEqualTo("traceId");
assertThat(response.getHeaderString(TraceHttpHeaders.PARENT_SPAN_ID)).isNull();
assertThat(response.getHeaderString(TraceHttpHeaders.SPAN_ID)).isNull();
verify(observer).consume(spanCaptor.capture());
assertThat(spanCaptor.getValue().getOperation(), is("Jersey: POST /trace"));
assertThat(spanCaptor.getValue().getOperation()).isEqualTo("Jersey: POST /trace");
}

@Test
Expand All @@ -141,51 +138,51 @@ public void testTraceState_doesNotIncludePathParams() {
.header(TraceHttpHeaders.PARENT_SPAN_ID, "parentSpanId")
.header(TraceHttpHeaders.SPAN_ID, "spanId")
.get();
assertThat(response.getHeaderString(TraceHttpHeaders.TRACE_ID), is("traceId"));
assertThat(response.getHeaderString(TraceHttpHeaders.PARENT_SPAN_ID), is(nullValue()));
assertThat(response.getHeaderString(TraceHttpHeaders.SPAN_ID), is(nullValue()));
assertThat(response.getHeaderString(TraceHttpHeaders.TRACE_ID)).isEqualTo("traceId");
assertThat(response.getHeaderString(TraceHttpHeaders.PARENT_SPAN_ID)).isNull();
assertThat(response.getHeaderString(TraceHttpHeaders.SPAN_ID)).isNull();
verify(observer).consume(spanCaptor.capture());
assertThat(spanCaptor.getValue().getOperation(), is("Jersey: GET /trace/{param}"));
assertThat(spanCaptor.getValue().getOperation()).isEqualTo("Jersey: GET /trace/{param}");
}

@Test
public void testTraceState_withoutRequestHeadersGeneratesValidTraceResponseHeaders() {
Response response = target.path("/trace").request().get();
assertThat(response.getHeaderString(TraceHttpHeaders.TRACE_ID), not(nullValue()));
assertThat(response.getHeaderString(TraceHttpHeaders.PARENT_SPAN_ID), is(nullValue()));
assertThat(response.getHeaderString(TraceHttpHeaders.SPAN_ID), is(nullValue()));
assertThat(response.getHeaderString(TraceHttpHeaders.TRACE_ID)).isNotNull();
assertThat(response.getHeaderString(TraceHttpHeaders.PARENT_SPAN_ID)).isNull();
assertThat(response.getHeaderString(TraceHttpHeaders.SPAN_ID)).isNull();
verify(observer).consume(spanCaptor.capture());
assertThat(spanCaptor.getValue().getOperation(), is("Jersey: GET /trace"));
assertThat(spanCaptor.getValue().getOperation()).isEqualTo("Jersey: GET /trace");
}

@Test
public void testTraceState_withoutRequestHeadersGeneratesValidTraceResponseHeadersWhenFailing() {
Response response = target.path("/failing-trace").request().get();
assertThat(response.getHeaderString(TraceHttpHeaders.TRACE_ID), not(nullValue()));
assertThat(response.getHeaderString(TraceHttpHeaders.PARENT_SPAN_ID), is(nullValue()));
assertThat(response.getHeaderString(TraceHttpHeaders.SPAN_ID), is(nullValue()));
assertThat(response.getHeaderString(TraceHttpHeaders.TRACE_ID)).isNotNull();
assertThat(response.getHeaderString(TraceHttpHeaders.PARENT_SPAN_ID)).isNull();
assertThat(response.getHeaderString(TraceHttpHeaders.SPAN_ID)).isNull();
verify(observer).consume(spanCaptor.capture());
assertThat(spanCaptor.getValue().getOperation(), is("Jersey: GET /failing-trace"));
assertThat(spanCaptor.getValue().getOperation()).isEqualTo("Jersey: GET /failing-trace");
}

@Test
public void testTraceState_withoutRequestHeadersGeneratesValidTraceResponseHeadersWhenStreaming() {
Response response = target.path("/streaming-trace").request().get();
assertThat(response.getHeaderString(TraceHttpHeaders.TRACE_ID), not(nullValue()));
assertThat(response.getHeaderString(TraceHttpHeaders.PARENT_SPAN_ID), is(nullValue()));
assertThat(response.getHeaderString(TraceHttpHeaders.SPAN_ID), is(nullValue()));
assertThat(response.getHeaderString(TraceHttpHeaders.TRACE_ID)).isNotNull();
assertThat(response.getHeaderString(TraceHttpHeaders.PARENT_SPAN_ID)).isNull();
assertThat(response.getHeaderString(TraceHttpHeaders.SPAN_ID)).isNull();
verify(observer).consume(spanCaptor.capture());
assertThat(spanCaptor.getValue().getOperation(), is("Jersey: GET /streaming-trace"));
assertThat(spanCaptor.getValue().getOperation()).isEqualTo("Jersey: GET /streaming-trace");
}

@Test
public void testTraceState_withoutRequestHeadersGeneratesValidTraceResponseHeadersWhenFailingToStream() {
Response response = target.path("/failing-streaming-trace").request().get();
assertThat(response.getHeaderString(TraceHttpHeaders.TRACE_ID), not(nullValue()));
assertThat(response.getHeaderString(TraceHttpHeaders.PARENT_SPAN_ID), is(nullValue()));
assertThat(response.getHeaderString(TraceHttpHeaders.SPAN_ID), is(nullValue()));
assertThat(response.getHeaderString(TraceHttpHeaders.TRACE_ID)).isNotNull();
assertThat(response.getHeaderString(TraceHttpHeaders.PARENT_SPAN_ID)).isNull();
assertThat(response.getHeaderString(TraceHttpHeaders.SPAN_ID)).isNull();
verify(observer).consume(spanCaptor.capture());
assertThat(spanCaptor.getValue().getOperation(), is("Jersey: GET /failing-streaming-trace"));
assertThat(spanCaptor.getValue().getOperation()).isEqualTo("Jersey: GET /failing-streaming-trace");
}

@Test
Expand All @@ -208,18 +205,18 @@ public void testTraceState_withSamplingHeaderWithoutTraceIdDoesNotUseTraceSample
@Test
public void testTraceState_withEmptyTraceIdGeneratesValidTraceResponseHeaders() {
Response response = target.path("/trace").request().header(TraceHttpHeaders.TRACE_ID, "").get();
assertThat(response.getHeaderString(TraceHttpHeaders.TRACE_ID), not(nullValue()));
assertThat(response.getHeaderString(TraceHttpHeaders.PARENT_SPAN_ID), is(nullValue()));
assertThat(response.getHeaderString(TraceHttpHeaders.SPAN_ID), is(nullValue()));
assertThat(response.getHeaderString(TraceHttpHeaders.TRACE_ID)).isNotNull();
assertThat(response.getHeaderString(TraceHttpHeaders.PARENT_SPAN_ID)).isNull();
assertThat(response.getHeaderString(TraceHttpHeaders.SPAN_ID)).isNull();
verify(observer).consume(spanCaptor.capture());
assertThat(spanCaptor.getValue().getOperation(), is("Jersey: GET /trace"));
assertThat(spanCaptor.getValue().getOperation()).isEqualTo("Jersey: GET /trace");
}

@Test
public void testFilter_setsMdcIfTraceIdHeaderIsPresent() throws Exception {
when(request.getHeaderString(TraceHttpHeaders.TRACE_ID)).thenReturn("traceId");
TraceEnrichingFilter.INSTANCE.filter(request);
assertThat(MDC.get(Tracers.TRACE_ID_KEY), is("traceId"));
assertThat(MDC.get(Tracers.TRACE_ID_KEY)).isEqualTo("traceId");
verify(request).setProperty(TraceEnrichingFilter.TRACE_ID_PROPERTY_NAME, "traceId");
// Note: this will be set to a random value; we want to check whether the value is being set
verify(request).setProperty(eq(TraceEnrichingFilter.SAMPLED_PROPERTY_NAME), anyBoolean());
Expand All @@ -230,13 +227,13 @@ public void testFilter_createsReceiveAndSendEvents() throws Exception {
target.path("/trace").request().header(TraceHttpHeaders.TRACE_ID, "").get();
verify(observer).consume(spanCaptor.capture());
Span span = spanCaptor.getValue();
assertThat(span.type(), is(SpanType.SERVER_INCOMING));
assertThat(span.type()).isEqualTo(SpanType.SERVER_INCOMING);
}

@Test
public void testFilter_setsMdcIfTraceIdHeaderIsNotePresent() throws Exception {
TraceEnrichingFilter.INSTANCE.filter(request);
assertThat(MDC.get(Tracers.TRACE_ID_KEY).length(), is(16));
assertThat(MDC.get(Tracers.TRACE_ID_KEY).length()).isEqualTo(16);
verify(request).setProperty(eq(TraceEnrichingFilter.TRACE_ID_PROPERTY_NAME), anyString());
}

Expand Down
5 changes: 2 additions & 3 deletions tracing/src/test/java/com/palantir/tracing/TracerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import static com.palantir.logsafe.testing.Assertions.assertThatLoggableExceptionThrownBy;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -251,13 +250,13 @@ public void testCompleteSpanWithMetadataIncludesMetadata() {
"key2", "value2");
Tracer.fastStartSpan("operation");
Optional<Span> maybeSpan = Tracer.completeSpan(metadata);
assertTrue(maybeSpan.isPresent());
assertThat(maybeSpan.isPresent()).isTrue();
assertThat(maybeSpan.get().getMetadata()).isEqualTo(metadata);
}

@Test
public void testCompleteSpanWithoutMetadataHasNoMetadata() {
assertTrue(startAndCompleteSpan().getMetadata().isEmpty());
assertThat(startAndCompleteSpan().getMetadata().isEmpty()).isTrue();
}

@Test
Expand Down

0 comments on commit 0dc3a39

Please sign in to comment.