From 4c84d1755a56d728109de3b35ae7bb769338120a Mon Sep 17 00:00:00 2001 From: Johnny Lim Date: Sat, 8 Oct 2022 20:24:34 +0900 Subject: [PATCH] Polish TestObservationRegistryAssert changes See gh-3432 --- .../tck/TestObservationRegistryAssert.java | 61 ++++++++----------- .../TestObservationRegistryAssertTests.java | 10 +-- 2 files changed, 31 insertions(+), 40 deletions(-) diff --git a/micrometer-observation-test/src/main/java/io/micrometer/observation/tck/TestObservationRegistryAssert.java b/micrometer-observation-test/src/main/java/io/micrometer/observation/tck/TestObservationRegistryAssert.java index 014e043dd9..4c7d0cce17 100644 --- a/micrometer-observation-test/src/main/java/io/micrometer/observation/tck/TestObservationRegistryAssert.java +++ b/micrometer-observation-test/src/main/java/io/micrometer/observation/tck/TestObservationRegistryAssert.java @@ -172,7 +172,6 @@ public TestObservationRegistryAssert hasHandledContextsThatSatisfy( * @throws AssertionError if there is no Observation with the given name * @throws AssertionError if there is an Observation with the given name but the * additional assertion is not successful - * @since 1.0.0 */ @SuppressWarnings("rawtypes") public TestObservationRegistryAssert forAllObservationsWithNameEqualTo(String name, @@ -198,9 +197,8 @@ public TestObservationRegistryAssert forAllObservationsWithNameEqualTo(String na * @throws AssertionError if the actual value is {@code null}. * @throws AssertionError if there is no Observation with the given name (ignoring * case) - * @throws AssertionError if there is a Observation with the given name (ignoring + * @throws AssertionError if there is an Observation with the given name (ignoring * case) but the additional assertion is not successful - * @since 1.0.0 */ @SuppressWarnings("rawtypes") public TestObservationRegistryAssert forAllObservationsWithNameEqualToIgnoreCase(String name, @@ -225,12 +223,11 @@ public TestObservationRegistryAssert forAllObservationsWithNameEqualToIgnoreCase * @throws AssertionError if the actual value is {@code null}. * @throws AssertionError if the number of Observations is different from the desired * one - * @since 1.0.0 */ public TestObservationRegistryAssert hasNumberOfObservationsEqualTo(int expectedNumberOfObservations) { isNotNull(); if (this.actual.getContexts().size() != expectedNumberOfObservations) { - failWithMessage("There should be <%s> Observations but there were <%s>. Found following Observations \n%s", + failWithMessage("There should be <%s> Observations but there were <%s>. Found following Observations:\n%s", expectedNumberOfObservations, this.actual.getContexts().size(), observationNames(this.actual.getContexts())); } @@ -252,7 +249,6 @@ public TestObservationRegistryAssert hasNumberOfObservationsEqualTo(int expected * @throws AssertionError if the actual value is {@code null}. * @throws AssertionError if the number of properly named Observations is different * from the desired one - * @since 1.0.0 */ public TestObservationRegistryAssert hasNumberOfObservationsWithNameEqualTo(String observationName, int expectedNumberOfObservations) { @@ -261,7 +257,7 @@ public TestObservationRegistryAssert hasNumberOfObservationsWithNameEqualTo(Stri .filter(f -> observationName.equals(f.getContext().getName())).count(); if (observationsWithNameSize != expectedNumberOfObservations) { failWithMessage( - "There should be <%s> Observations with name <%s> but there were <%s>. Found following Observations \n%s", + "There should be <%s> Observations with name <%s> but there were <%s>. Found following Observations:\n%s", expectedNumberOfObservations, observationName, observationsWithNameSize, observationNames(this.actual.getContexts())); } @@ -273,10 +269,10 @@ public TestObservationRegistryAssert hasNumberOfObservationsWithNameEqualTo(Stri * (ignoring case). *

* Examples:

 // assertions succeed
-     * assertThat(testObservationRegistry).hasNumberOfObservationsWithNameEqualToIgnoreCase(1);
+     * assertThat(testObservationRegistry).hasNumberOfObservationsWithNameEqualToIgnoreCase("foo", 1);
      *
      * // assertions fail - assuming that there's only 1 such observation
-     * assertThat(testObservationRegistry).hasNumberOfObservationsWithNameEqualToIgnoreCase(2);
+ * assertThat(testObservationRegistry).hasNumberOfObservationsWithNameEqualToIgnoreCase("foo", 2); * @param observationName Observation name * @param expectedNumberOfObservations expected number of Observations with the given * name (ignoring case) @@ -284,7 +280,6 @@ public TestObservationRegistryAssert hasNumberOfObservationsWithNameEqualTo(Stri * @throws AssertionError if the actual value is {@code null}. * @throws AssertionError if the number of properly named Observations is different * from the desired one - * @since 1.0.0 */ public TestObservationRegistryAssert hasNumberOfObservationsWithNameEqualToIgnoreCase(String observationName, int expectedNumberOfObservations) { @@ -293,7 +288,7 @@ public TestObservationRegistryAssert hasNumberOfObservationsWithNameEqualToIgnor .filter(f -> observationName.equalsIgnoreCase(f.getContext().getName())).count(); if (observationsWithNameSize != expectedNumberOfObservations) { failWithMessage( - "There should be <%s> Observations with name (ignoring case) <%s> but there were <%s>. Found following Observations \n%s", + "There should be <%s> Observations with name (ignoring case) <%s> but there were <%s>. Found following Observations:\n%s", expectedNumberOfObservations, observationName, observationsWithNameSize, observationNames(this.actual.getContexts())); } @@ -301,19 +296,18 @@ public TestObservationRegistryAssert hasNumberOfObservationsWithNameEqualToIgnor } /** - * Verifies that there is a Observation with a key value. + * Verifies that there is an Observation with a key value. *

* Examples:

 // assertions succeed
      * assertThat(testObservationRegistry).hasAnObservationWithAKeyValue("foo", "bar");
      *
-     * // assertions fail - assuming that there is no such tags in any observation
+     * // assertions fail - assuming that there is no such a key value in any observation
      * assertThat(testObservationRegistry).hasAnObservationWithAKeyValue("foo", "bar");
- * @param key expected tag key - * @param value expected tag value + * @param key expected key name + * @param value expected key value * @return {@code this} assertion object. * @throws AssertionError if the actual value is {@code null}. - * @throws AssertionError if there is no Observation with given tag key and value - * @since 1.0.0 + * @throws AssertionError if there is no Observation with given key name and value */ public TestObservationRegistryAssert hasAnObservationWithAKeyValue(String key, String value) { isNotNull(); @@ -321,7 +315,7 @@ public TestObservationRegistryAssert hasAnObservationWithAKeyValue(String key, S .filter(keyValue -> keyValue.getKey().equals(key) && keyValue.getValue().equals(value)).findFirst() .orElseThrow(() -> { failWithMessage( - "There should be at least one Observation with tag key <%s> and value <%s> but found none. Found following Observations \n%s", + "There should be at least one Observation with key name <%s> and value <%s> but found none. Found following Observations:\n%s", key, value, observations()); return new AssertionError(); }); @@ -329,25 +323,24 @@ public TestObservationRegistryAssert hasAnObservationWithAKeyValue(String key, S } /** - * Verifies that there is a Observation with a key value key. + * Verifies that there is an Observation with a key name. *

* Examples:

 // assertions succeed
      * assertThat(testObservationRegistry).hasAnObservationWithAKeyName("foo");
      *
-     * // assertions fail - assuming that the observation doesn't have a key value with such a key
+     * // assertions fail - assuming that there are no observations with such a key name
      * assertThat(testObservationRegistry).hasAnObservationWithAKeyName("foo");
- * @param key expected tag key + * @param key expected key name * @return {@code this} assertion object. * @throws AssertionError if the actual value is {@code null}. - * @throws AssertionError if there is no Observation with given tag key - * @since 1.0.0 + * @throws AssertionError if there is no Observation with given key name */ public TestObservationRegistryAssert hasAnObservationWithAKeyName(String key) { isNotNull(); this.actual.getContexts().stream().flatMap(f -> f.getContext().getAllKeyValues().stream()) .filter(keyValue -> keyValue.getKey().equals(key)).findFirst().orElseThrow(() -> { failWithMessage( - "There should be at least one Observation with tag key <%s> but found none. Found following Observations \n%s", + "There should be at least one Observation with key name <%s> but found none. Found following Observations:\n%s", key, observations()); return new AssertionError(); }); @@ -355,37 +348,35 @@ public TestObservationRegistryAssert hasAnObservationWithAKeyName(String key) { } /** - * Verifies that there is a Observation with a tag. + * Verifies that there is an Observation with a key value. *

* Examples:

 // assertions succeed
      * assertThat(testObservationRegistry).hasAnObservationWithAKeyValue(SomeKeyName.FOO, "bar");
      *
-     * // assertions fail - assuming that the observation doesn't have such a key value
+     * // assertions fail - assuming that there are no observations with such a key value
      * assertThat(testObservationRegistry).hasAnObservationWithAKeyValue(SomeKeyName.FOO, "baz");
- * @param key expected tag key - * @param value expected tag value + * @param key expected key name + * @param value expected key value * @return {@code this} assertion object. * @throws AssertionError if the actual value is {@code null}. - * @throws AssertionError if there is no Observation with given tag key - * @since 1.0.0 + * @throws AssertionError if there is no Observation with given key name and value */ public TestObservationRegistryAssert hasAnObservationWithAKeyValue(KeyName key, String value) { return hasAnObservationWithAKeyValue(key.asString(), value); } /** - * Verifies that there is a Observation with a tag key. + * Verifies that there is an Observation with a key name. *

* Examples:

 // assertions succeed
      * assertThat(testObservationRegistry).hasAnObservationWithAKeyName(SomeKeyName.FOO);
      *
-     * // assertions fail - assuming that the observation doesn't have such a key name
+     * // assertions fail - assuming that there are no observation with such a key name
      * assertThat(testObservationRegistry).hasAnObservationWithAKeyName(SomeKeyName.FOO);
- * @param key expected tag key + * @param key expected key name * @return {@code this} assertion object. * @throws AssertionError if the actual value is {@code null}. - * @throws AssertionError if there is no Observation with given tag key - * @since 1.0.0 + * @throws AssertionError if there is no Observation with given key name */ public TestObservationRegistryAssert hasAnObservationWithAKeyName(KeyName key) { return hasAnObservationWithAKeyName(key.asString()); diff --git a/micrometer-observation-test/src/test/java/io/micrometer/observation/tck/TestObservationRegistryAssertTests.java b/micrometer-observation-test/src/test/java/io/micrometer/observation/tck/TestObservationRegistryAssertTests.java index d2d92b9950..b4164c5f33 100644 --- a/micrometer-observation-test/src/test/java/io/micrometer/observation/tck/TestObservationRegistryAssertTests.java +++ b/micrometer-observation-test/src/test/java/io/micrometer/observation/tck/TestObservationRegistryAssertTests.java @@ -153,7 +153,7 @@ void should_fail_when_there_is_no_observation_with_name() { } @Test - void should_fail_when_all_observations_match_the_assertion() { + void should_fail_when_all_observations_do_not_match_the_assertion() { Observation.createNotStarted("foo", registry).start().stop(); thenThrownBy(() -> TestObservationRegistryAssert.assertThat(registry).forAllObservationsWithNameEqualTo("foo", @@ -211,7 +211,7 @@ void should_not_fail_when_number_of_observations_matches() { } @Test - void should_fail_when_number_is_correct_but_names_dont_match() { + void should_fail_when_names_match_but_number_is_incorrect() { Observation.createNotStarted("foo", registry).start().stop(); thenThrownBy(() -> TestObservationRegistryAssert.assertThat(registry) @@ -219,7 +219,7 @@ void should_fail_when_number_is_correct_but_names_dont_match() { } @Test - void should_fail_when_number_is_incorrect_but_names_match() { + void should_fail_when_number_is_correct_but_names_do_not_match() { Observation.createNotStarted("foo", registry).start().stop(); thenThrownBy(() -> TestObservationRegistryAssert.assertThat(registry) @@ -235,7 +235,7 @@ void should_not_fail_when_number_and_names_match() { } @Test - void should_fail_when_number_is_correct_but_names_dont_match_ignore_case() { + void should_fail_when_names_match_but_number_is_incorrect_ignore_case() { Observation.createNotStarted("FOO", registry).start().stop(); thenThrownBy(() -> TestObservationRegistryAssert.assertThat(registry) @@ -243,7 +243,7 @@ void should_fail_when_number_is_correct_but_names_dont_match_ignore_case() { } @Test - void should_fail_when_number_is_incorrect_but_names_match_ignore_case() { + void should_fail_when_number_is_correct_but_names_do_not_match_ignore_case() { Observation.createNotStarted("FOO", registry).start().stop(); thenThrownBy(() -> TestObservationRegistryAssert.assertThat(registry)