Skip to content

Commit

Permalink
Merge pull request #43235 from izeye
Browse files Browse the repository at this point in the history
* pr/43235:
  Polish 'Replace TestObservationRegistryAssert.assertThat()''
  Replace TestObservationRegistryAssert.assertThat()

Closes gh-43235
  • Loading branch information
philwebb committed Nov 21, 2024
2 parents 59317d5 + c9a8454 commit 933b7d3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,7 +20,6 @@
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.observation.ObservationRegistry;
import io.micrometer.observation.tck.TestObservationRegistry;
import io.micrometer.observation.tck.TestObservationRegistryAssert;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

Expand Down Expand Up @@ -72,8 +71,7 @@ void restClientCreatedWithBuilderIsInstrumented() {
RestClient restClient = buildRestClient(context);
restClient.get().uri("/projects/{project}", "spring-boot").retrieve().toBodilessEntity();
TestObservationRegistry registry = context.getBean(TestObservationRegistry.class);
TestObservationRegistryAssert.assertThat(registry)
.hasObservationWithNameEqualToIgnoringCase("http.client.requests");
assertThat(registry).hasObservationWithNameEqualToIgnoringCase("http.client.requests");
});
}

Expand All @@ -85,8 +83,7 @@ void restClientCreatedWithBuilderUsesCustomConventionName() {
RestClient restClient = buildRestClient(context);
restClient.get().uri("/projects/{project}", "spring-boot").retrieve().toBodilessEntity();
TestObservationRegistry registry = context.getBean(TestObservationRegistry.class);
TestObservationRegistryAssert.assertThat(registry)
.hasObservationWithNameEqualToIgnoringCase(observationName);
assertThat(registry).hasObservationWithNameEqualToIgnoringCase(observationName);
});
}

Expand All @@ -96,8 +93,7 @@ void restClientCreatedWithBuilderUsesCustomConvention() {
RestClient restClient = buildRestClient(context);
restClient.get().uri("/projects/{project}", "spring-boot").retrieve().toBodilessEntity();
TestObservationRegistry registry = context.getBean(TestObservationRegistry.class);
TestObservationRegistryAssert.assertThat(registry)
.hasObservationWithNameEqualTo("http.client.requests")
assertThat(registry).hasObservationWithNameEqualTo("http.client.requests")
.that()
.hasLowCardinalityKeyValue("project", "spring-boot");
});
Expand All @@ -118,8 +114,7 @@ void afterMaxUrisReachedFurtherUrisAreDenied(CapturedOutput output) {
restClient.get().uri("/test/" + i, String.class).retrieve().toBodilessEntity();
}
TestObservationRegistry registry = context.getBean(TestObservationRegistry.class);
TestObservationRegistryAssert.assertThat(registry)
.hasNumberOfObservationsWithNameEqualTo("http.client.requests", 3);
assertThat(registry).hasNumberOfObservationsWithNameEqualTo("http.client.requests", 3);
MeterRegistry meterRegistry = context.getBean(MeterRegistry.class);
assertThat(meterRegistry.find("http.client.requests").timers()).hasSize(2);
assertThat(output).contains("Reached the maximum number of URI tags for 'http.client.requests'.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,7 +18,6 @@

import io.micrometer.observation.ObservationRegistry;
import io.micrometer.observation.tck.TestObservationRegistry;
import io.micrometer.observation.tck.TestObservationRegistryAssert;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

Expand All @@ -34,6 +33,7 @@
import org.springframework.web.client.RestClient;
import org.springframework.web.client.RestClient.Builder;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;

Expand All @@ -59,8 +59,7 @@ void restClientCreatedWithBuilderIsInstrumented() {
RestClient restClient = buildRestClient(context);
restClient.get().uri("/projects/{project}", "spring-boot").retrieve().toBodilessEntity();
TestObservationRegistry registry = context.getBean(TestObservationRegistry.class);
TestObservationRegistryAssert.assertThat(registry)
.hasObservationWithNameEqualToIgnoringCase("http.client.requests");
assertThat(registry).hasObservationWithNameEqualToIgnoringCase("http.client.requests");
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,7 +20,6 @@
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.observation.ObservationRegistry;
import io.micrometer.observation.tck.TestObservationRegistry;
import io.micrometer.observation.tck.TestObservationRegistryAssert;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

Expand Down Expand Up @@ -70,8 +69,7 @@ void restTemplateCreatedWithBuilderIsInstrumented() {
RestTemplate restTemplate = buildRestTemplate(context);
restTemplate.getForEntity("/projects/{project}", Void.class, "spring-boot");
TestObservationRegistry registry = context.getBean(TestObservationRegistry.class);
TestObservationRegistryAssert.assertThat(registry)
.hasObservationWithNameEqualToIgnoringCase("http.client.requests");
assertThat(registry).hasObservationWithNameEqualToIgnoringCase("http.client.requests");
});
}

Expand All @@ -83,8 +81,7 @@ void restTemplateCreatedWithBuilderUsesCustomConventionName() {
RestTemplate restTemplate = buildRestTemplate(context);
restTemplate.getForEntity("/projects/{project}", Void.class, "spring-boot");
TestObservationRegistry registry = context.getBean(TestObservationRegistry.class);
TestObservationRegistryAssert.assertThat(registry)
.hasObservationWithNameEqualToIgnoringCase(observationName);
assertThat(registry).hasObservationWithNameEqualToIgnoringCase(observationName);
});
}

Expand All @@ -94,8 +91,7 @@ void restTemplateCreatedWithBuilderUsesCustomConvention() {
RestTemplate restTemplate = buildRestTemplate(context);
restTemplate.getForEntity("/projects/{project}", Void.class, "spring-boot");
TestObservationRegistry registry = context.getBean(TestObservationRegistry.class);
TestObservationRegistryAssert.assertThat(registry)
.hasObservationWithNameEqualTo("http.client.requests")
assertThat(registry).hasObservationWithNameEqualTo("http.client.requests")
.that()
.hasLowCardinalityKeyValue("project", "spring-boot");
});
Expand All @@ -115,8 +111,7 @@ void afterMaxUrisReachedFurtherUrisAreDenied(CapturedOutput output) {
restTemplate.getForObject("/test/" + i, String.class);
}
TestObservationRegistry registry = context.getBean(TestObservationRegistry.class);
TestObservationRegistryAssert.assertThat(registry)
.hasNumberOfObservationsWithNameEqualTo("http.client.requests", 3);
assertThat(registry).hasNumberOfObservationsWithNameEqualTo("http.client.requests", 3);
MeterRegistry meterRegistry = context.getBean(MeterRegistry.class);
assertThat(meterRegistry.find("http.client.requests").timers()).hasSize(2);
assertThat(output).contains("Reached the maximum number of URI tags for 'http.client.requests'.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,7 +18,6 @@

import io.micrometer.observation.ObservationRegistry;
import io.micrometer.observation.tck.TestObservationRegistry;
import io.micrometer.observation.tck.TestObservationRegistryAssert;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

Expand All @@ -34,6 +33,7 @@
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.web.client.RestTemplate;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;

Expand All @@ -58,8 +58,7 @@ void restTemplateCreatedWithBuilderIsInstrumented() {
RestTemplate restTemplate = buildRestTemplate(context);
restTemplate.getForEntity("/projects/{project}", Void.class, "spring-boot");
TestObservationRegistry registry = context.getBean(TestObservationRegistry.class);
TestObservationRegistryAssert.assertThat(registry)
.hasObservationWithNameEqualToIgnoringCase("http.client.requests");
assertThat(registry).hasObservationWithNameEqualToIgnoringCase("http.client.requests");
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,7 +22,6 @@
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.observation.ObservationRegistry;
import io.micrometer.observation.tck.TestObservationRegistry;
import io.micrometer.observation.tck.TestObservationRegistryAssert;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -84,14 +83,13 @@ void shouldUseCustomConventionIfAvailable() {
TestObservationRegistry registry = context.getBean(TestObservationRegistry.class);
WebClient.Builder builder = context.getBean(WebClient.Builder.class);
WebClient webClient = mockWebClient(builder);
TestObservationRegistryAssert.assertThat(registry).doesNotHaveAnyObservation();
assertThat(registry).doesNotHaveAnyObservation();
webClient.get()
.uri("https://example.org/projects/{project}", "spring-boot")
.retrieve()
.toBodilessEntity()
.block(Duration.ofSeconds(30));
TestObservationRegistryAssert.assertThat(registry)
.hasObservationWithNameEqualTo("http.client.requests")
assertThat(registry).hasObservationWithNameEqualTo("http.client.requests")
.that()
.hasLowCardinalityKeyValue("project", "spring-boot");
});
Expand All @@ -101,8 +99,7 @@ void shouldUseCustomConventionIfAvailable() {
void afterMaxUrisReachedFurtherUrisAreDenied(CapturedOutput output) {
this.contextRunner.withPropertyValues("management.metrics.web.client.max-uri-tags=2").run((context) -> {
TestObservationRegistry registry = getInitializedRegistry(context);
TestObservationRegistryAssert.assertThat(registry)
.hasNumberOfObservationsWithNameEqualTo("http.client.requests", 3);
assertThat(registry).hasNumberOfObservationsWithNameEqualTo("http.client.requests", 3);
MeterRegistry meterRegistry = context.getBean(MeterRegistry.class);
assertThat(meterRegistry.find("http.client.requests").timers()).hasSize(1);
// MeterFilter.maximumAllowableTags() works with prefix matching.
Expand All @@ -116,8 +113,7 @@ void afterMaxUrisReachedFurtherUrisAreDenied(CapturedOutput output) {
void shouldNotDenyNorLogIfMaxUrisIsNotReached(CapturedOutput output) {
this.contextRunner.withPropertyValues("management.metrics.web.client.max-uri-tags=5").run((context) -> {
TestObservationRegistry registry = getInitializedRegistry(context);
TestObservationRegistryAssert.assertThat(registry)
.hasNumberOfObservationsWithNameEqualTo("http.client.requests", 3);
assertThat(registry).hasNumberOfObservationsWithNameEqualTo("http.client.requests", 3);
MeterRegistry meterRegistry = context.getBean(MeterRegistry.class);
assertThat(meterRegistry.find("http.client.requests").timers()).hasSize(3);
assertThat(output).doesNotContain("Reached the maximum number of URI tags for 'http.client.requests'.")
Expand All @@ -140,14 +136,13 @@ private TestObservationRegistry getInitializedRegistry(AssertableApplicationCont

private void validateWebClient(WebClient.Builder builder, TestObservationRegistry registry) {
WebClient webClient = mockWebClient(builder);
TestObservationRegistryAssert.assertThat(registry).doesNotHaveAnyObservation();
assertThat(registry).doesNotHaveAnyObservation();
webClient.get()
.uri("https://example.org/projects/{project}", "spring-boot")
.retrieve()
.toBodilessEntity()
.block(Duration.ofSeconds(30));
TestObservationRegistryAssert.assertThat(registry)
.hasObservationWithNameEqualTo("http.client.requests")
assertThat(registry).hasObservationWithNameEqualTo("http.client.requests")
.that()
.hasLowCardinalityKeyValue("uri", "/projects/{project}");
}
Expand Down

0 comments on commit 933b7d3

Please # to comment.