Skip to content

Commit

Permalink
Expose TestObservationRegistry as an AssertJ AssertProvider (#5551)
Browse files Browse the repository at this point in the history
This allows the assertions to be used with the generic `Assertions.assertThat` rather than directly with the specific `TestObservationRegistryAssert`.
  • Loading branch information
filiphr authored Oct 7, 2024
1 parent 80dfc76 commit ceb213a
Show file tree
Hide file tree
Showing 15 changed files with 127 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
import io.micrometer.observation.aop.ObservedAspect;
import io.micrometer.observation.docs.ObservationDocumentation;
import io.micrometer.observation.tck.TestObservationRegistry;
import io.micrometer.observation.tck.TestObservationRegistryAssert;
import org.junit.jupiter.api.Test;
import org.springframework.aop.aspectj.annotation.AspectJProxyFactory;
import org.springframework.lang.Nullable;

import static io.micrometer.docs.observation.ObservationHandlerTests.TaxObservationDocumentation.TaxHighCardinalityKeyNames.USER_ID;
import static io.micrometer.docs.observation.ObservationHandlerTests.TaxObservationDocumentation.TaxLowCardinalityKeyNames.TAX_TYPE;
import static org.assertj.core.api.Assertions.assertThat;

/**
* Sources for observation-components.adoc
Expand Down Expand Up @@ -158,7 +158,7 @@ void annotatedCallShouldBeObserved() {
service.call();

// assert that observation has been properly created
TestObservationRegistryAssert.assertThat(registry)
assertThat(registry)
.hasSingleObservationThat()
.hasBeenStopped()
.hasNameEqualTo("test.call")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationRegistry;
import io.micrometer.observation.tck.TestObservationRegistry;
import io.micrometer.observation.tck.TestObservationRegistryAssert;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Sources for observation-testing.adoc
*/
Expand All @@ -37,7 +38,7 @@ void should_assert_your_observation() {
new Example(registry).run();

// check your observation
TestObservationRegistryAssert.assertThat(registry)
assertThat(registry)
.doesNotHaveAnyRemainingCurrentObservation()
.hasObservationWithNameEqualTo("foo")
.that()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import io.micrometer.observation.ObservationHandler;
import io.micrometer.observation.ObservationRegistry;
import io.micrometer.observation.tck.TestObservationRegistry;
import io.micrometer.observation.tck.TestObservationRegistryAssert;
import io.micrometer.observation.transport.ReceiverContext;
import io.micrometer.observation.transport.SenderContext;
import org.apache.kafka.clients.admin.AdminClient;
Expand Down Expand Up @@ -56,6 +55,8 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import static org.assertj.core.api.Assertions.assertThat;

@Testcontainers
@Tag("docker")
class ObservationMessagingIntegrationTest {
Expand Down Expand Up @@ -134,15 +135,13 @@ void shouldManageProducerAndConsumerMetrics() throws ExecutionException, Interru
// end::consumer_side[]

// tag::test_assertions[]
TestObservationRegistryAssert.assertThat(registry)
.hasObservationWithNameEqualTo("kafka.send")
assertThat(registry).hasObservationWithNameEqualTo("kafka.send")
.that()
.hasBeenStarted()
.hasBeenStopped()
.hasLowCardinalityKeyValue("sent", "true");

TestObservationRegistryAssert.assertThat(registry)
.hasObservationWithNameEqualTo("kafka.receive")
assertThat(registry).hasObservationWithNameEqualTo("kafka.receive")
.that()
.hasBeenStarted()
.hasBeenStopped()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
import io.micrometer.observation.ObservationRegistry;
import io.micrometer.observation.ObservationTextPublisher;
import io.micrometer.observation.tck.TestObservationRegistry;
import io.micrometer.observation.tck.TestObservationRegistryAssert;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
Expand Down Expand Up @@ -170,7 +169,7 @@ void unaryRpc() {
assertThat(clientHandler.getEvents()).containsExactly(GrpcClientEvents.MESSAGE_SENT,
GrpcClientEvents.MESSAGE_RECEIVED);
// tag::assertion[]
TestObservationRegistryAssert.assertThat(observationRegistry)
assertThat(observationRegistry)
.hasAnObservation(observationContextAssert -> observationContextAssert.hasNameEqualTo("grpc.client"))
.hasAnObservation(observationContextAssert -> observationContextAssert.hasNameEqualTo("grpc.server"));
// end::assertion[]
Expand Down Expand Up @@ -205,7 +204,7 @@ public void onFailure(Throwable t) {

await().until(() -> futures.stream().allMatch(Future::isDone));
assertThat(responses).hasSize(count).containsExactlyInAnyOrderElementsOf(messages);
TestObservationRegistryAssert.assertThat(observationRegistry)
assertThat(observationRegistry)
.hasAnObservation(observationContextAssert -> observationContextAssert.hasNameEqualTo("grpc.client"))
.hasAnObservation(observationContextAssert -> observationContextAssert.hasNameEqualTo("grpc.server"));
verifyHeaders();
Expand Down Expand Up @@ -248,7 +247,7 @@ void clientStreamingRpc() {
verifyServerContext("grpc.testing.SimpleService", "ClientStreamingRpc",
"grpc.testing.SimpleService/ClientStreamingRpc", MethodType.CLIENT_STREAMING);
assertThat(serverHandler.getContext().getStatusCode()).isEqualTo(Code.OK);
TestObservationRegistryAssert.assertThat(observationRegistry)
assertThat(observationRegistry)
.hasAnObservation(observationContextAssert -> observationContextAssert.hasNameEqualTo("grpc.client"))
.hasAnObservation(observationContextAssert -> observationContextAssert.hasNameEqualTo("grpc.server"));
verifyHeaders();
Expand Down Expand Up @@ -283,7 +282,7 @@ void serverStreamingRpc() {
assertThat(clientHandler.getContext().getStatusCode()).isEqualTo(Code.OK);
assertThat(clientHandler.getEvents()).containsExactly(GrpcClientEvents.MESSAGE_SENT,
GrpcClientEvents.MESSAGE_RECEIVED, GrpcClientEvents.MESSAGE_RECEIVED);
TestObservationRegistryAssert.assertThat(observationRegistry)
assertThat(observationRegistry)
.hasAnObservation(observationContextAssert -> observationContextAssert.hasNameEqualTo("grpc.client"))
.hasAnObservation(observationContextAssert -> observationContextAssert.hasNameEqualTo("grpc.server"));
verifyHeaders();
Expand Down Expand Up @@ -336,7 +335,7 @@ void bidiStreamingRpc() {

assertThat(serverHandler.getContext().getStatusCode()).isEqualTo(Code.OK);
assertThat(clientHandler.getContext().getStatusCode()).isEqualTo(Code.OK);
TestObservationRegistryAssert.assertThat(observationRegistry)
assertThat(observationRegistry)
.hasAnObservation(observationContextAssert -> observationContextAssert.hasNameEqualTo("grpc.client"))
.hasAnObservation(observationContextAssert -> observationContextAssert.hasNameEqualTo("grpc.server"));
verifyHeaders();
Expand Down Expand Up @@ -408,9 +407,8 @@ void unaryRpcFailure() {
assertThat(clientHandler.getContext().getStatusCode()).isEqualTo(Code.UNKNOWN);
assertThat(serverHandler.getEvents()).containsExactly(GrpcServerEvents.MESSAGE_RECEIVED);
assertThat(clientHandler.getEvents()).containsExactly(GrpcClientEvents.MESSAGE_SENT);
TestObservationRegistryAssert.assertThat(observationRegistry)
.hasAnObservation(
observationContextAssert -> observationContextAssert.hasNameEqualTo("grpc.server").hasError());
assertThat(observationRegistry).hasAnObservation(
observationContextAssert -> observationContextAssert.hasNameEqualTo("grpc.server").hasError());
}

@Test
Expand All @@ -432,9 +430,8 @@ void clientStreamingRpcFailure() {
assertThat(serverHandler.getContext().getStatusCode()).isNull();
assertThat(clientHandler.getEvents()).isEmpty();
assertThat(serverHandler.getEvents()).isEmpty();
TestObservationRegistryAssert.assertThat(observationRegistry)
.hasAnObservation(
observationContextAssert -> observationContextAssert.hasNameEqualTo("grpc.server").hasError());
assertThat(observationRegistry).hasAnObservation(
observationContextAssert -> observationContextAssert.hasNameEqualTo("grpc.server").hasError());
}

@Test
Expand All @@ -458,9 +455,8 @@ void serverStreamingRpcFailure() {
assertThat(serverHandler.getContext().getStatusCode()).isNull();
assertThat(clientHandler.getEvents()).containsExactly(GrpcClientEvents.MESSAGE_SENT);
assertThat(serverHandler.getEvents()).containsExactly(GrpcServerEvents.MESSAGE_RECEIVED);
TestObservationRegistryAssert.assertThat(observationRegistry)
.hasAnObservation(
observationContextAssert -> observationContextAssert.hasNameEqualTo("grpc.server").hasError());
assertThat(observationRegistry).hasAnObservation(
observationContextAssert -> observationContextAssert.hasNameEqualTo("grpc.server").hasError());
}

@Test
Expand All @@ -483,9 +479,8 @@ void bidiStreamingRpcFailure() {
assertThat(serverHandler.getContext().getStatusCode()).isNull();
assertThat(clientHandler.getEvents()).isEmpty();
assertThat(serverHandler.getEvents()).isEmpty();
TestObservationRegistryAssert.assertThat(observationRegistry)
.hasAnObservation(
observationContextAssert -> observationContextAssert.hasNameEqualTo("grpc.server").hasError());
assertThat(observationRegistry).hasAnObservation(
observationContextAssert -> observationContextAssert.hasNameEqualTo("grpc.server").hasError());
}

private StreamObserver<SimpleResponse> createResponseObserver(AtomicBoolean errored) {
Expand Down Expand Up @@ -572,7 +567,7 @@ void cancel() {
this.service.requestInterrupted.set(true);
await().until(future::isDone);
assertThat(future.isCancelled()).isTrue();
TestObservationRegistryAssert.assertThat(observationRegistry)
assertThat(observationRegistry)
.hasAnObservation(observationContextAssert -> observationContextAssert.hasNameEqualTo("grpc.client")
.hasLowCardinalityKeyValue("grpc.status_code", "CANCELLED"))
.hasAnObservation(observationContextAssert -> observationContextAssert.hasNameEqualTo("grpc.server")
Expand All @@ -593,7 +588,7 @@ void shutdown() {
this.service.requestInterrupted.set(true);
await().until(channel::isTerminated);
await().until(future::isDone);
TestObservationRegistryAssert.assertThat(observationRegistry)
assertThat(observationRegistry)
.hasAnObservation(observationContextAssert -> observationContextAssert.hasNameEqualTo("grpc.client")
.hasLowCardinalityKeyValue("grpc.status_code", "UNAVAILABLE"));
assertThat(serverHandler.getEvents()).contains(GrpcServerEvents.CANCELLED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import io.micrometer.observation.ObservationRegistry;
import io.micrometer.observation.GlobalObservationConvention;
import io.micrometer.observation.tck.TestObservationRegistry;
import io.micrometer.observation.tck.TestObservationRegistryAssert;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
Expand Down Expand Up @@ -305,8 +304,7 @@ void contextualNameContainsRequestMethod(String method, @WiremockResolver.Wiremo
EntityUtils.consume(client.execute(new HttpCustomMethod(method, server.baseUrl())).getEntity());
break;
}
TestObservationRegistryAssert.assertThat(observationRegistry)
.hasSingleObservationThat()
assertThat(observationRegistry).hasSingleObservationThat()
.hasContextualNameEqualToIgnoringCase("http " + method);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import io.micrometer.observation.GlobalObservationConvention;
import io.micrometer.observation.ObservationRegistry;
import io.micrometer.observation.tck.TestObservationRegistry;
import io.micrometer.observation.tck.TestObservationRegistryAssert;
import org.apache.hc.client5.http.ClientProtocolException;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
Expand Down Expand Up @@ -318,8 +317,7 @@ void contextualNameContainsRequestMethod(String method, @WiremockResolver.Wiremo
execute(client, new HttpUriRequestBase(method, URI.create(server.baseUrl())));
break;
}
TestObservationRegistryAssert.assertThat(observationRegistry)
.hasSingleObservationThat()
assertThat(observationRegistry).hasSingleObservationThat()
.hasContextualNameEqualToIgnoringCase("http " + method);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.stubbing.Scenario.STARTED;
import static io.micrometer.core.instrument.binder.httpcomponents.hc5.ApacheHttpClientObservationDocumentation.ApacheHttpClientKeyNames.*;
import static io.micrometer.observation.tck.TestObservationRegistryAssert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import java.util.concurrent.atomic.AtomicInteger;

import static io.micrometer.core.instrument.binder.httpcomponents.hc5.ApacheHttpClientObservationDocumentation.ApacheHttpClientKeyNames.*;
import static io.micrometer.observation.tck.TestObservationRegistryAssert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import io.micrometer.observation.tck.TestObservationRegistry;
import org.junit.jupiter.api.Test;

import static io.micrometer.observation.tck.TestObservationRegistryAssert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;

class ObservationOrTimerCompatibleInstrumentationTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

import org.assertj.core.api.AssertProvider;

/**
* Implementation of {@link ObservationRegistry} used for testing.
*
Expand All @@ -31,7 +33,8 @@
* @author Marcin Grzejszczak
* @since 1.10.0
*/
public final class TestObservationRegistry implements ObservationRegistry {
public final class TestObservationRegistry
implements ObservationRegistry, AssertProvider<TestObservationRegistryAssert> {

private final ObservationRegistry delegate = ObservationRegistry.create();

Expand Down Expand Up @@ -81,6 +84,18 @@ public void clear() {
getContexts().clear();
}

/**
* Return an assert for AspectJ.
* @return an AspectJ assert
* @deprecated to prevent accidental use. Prefer standard AssertJ
* {@code assertThat(observationRegistry)...} calls instead.
*/
@Deprecated
@Override
public TestObservationRegistryAssert assertThat() {
return TestObservationRegistryAssert.assertThat(this);
}

private static class StoringObservationHandler implements ObservationHandler<Observation.Context> {

final Queue<TestObservationContext> contexts = new ConcurrentLinkedQueue<>();
Expand Down
Loading

0 comments on commit ceb213a

Please sign in to comment.