Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polish TestObservationRegistryAssert changes #3480

Merged
merged 1 commit into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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()));
}
Expand All @@ -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) {
Expand All @@ -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()));
}
Expand All @@ -273,18 +269,17 @@ public TestObservationRegistryAssert hasNumberOfObservationsWithNameEqualTo(Stri
* (ignoring case).
* <p>
* Examples: <pre><code class='java'> // 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);</code></pre>
* assertThat(testObservationRegistry).hasNumberOfObservationsWithNameEqualToIgnoreCase("foo", 2);</code></pre>
* @param observationName Observation name
* @param expectedNumberOfObservations expected number of Observations with the given
* name (ignoring case)
* @return {@code this} assertion object.
* @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) {
Expand All @@ -293,99 +288,95 @@ 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()));
}
return this;
}

/**
* Verifies that there is a Observation with a key value.
* Verifies that there is an Observation with a key value.
* <p>
* Examples: <pre><code class='java'> // 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");</code></pre>
* @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();
this.actual.getContexts().stream().flatMap(f -> f.getContext().getAllKeyValues().stream())
.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();
});
return this;
}

/**
* Verifies that there is a Observation with a key value key.
* Verifies that there is an Observation with a key name.
* <p>
* Examples: <pre><code class='java'> // 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");</code></pre>
* @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();
});
return this;
}

/**
* Verifies that there is a Observation with a tag.
* Verifies that there is an Observation with a key value.
* <p>
* Examples: <pre><code class='java'> // 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");</code></pre>
* @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.
* <p>
* Examples: <pre><code class='java'> // 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);</code></pre>
* @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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -211,15 +211,15 @@ 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)
.hasNumberOfObservationsWithNameEqualTo("foo", 0)).isInstanceOf(AssertionError.class);
}

@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)
Expand All @@ -235,15 +235,15 @@ 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)
.hasNumberOfObservationsWithNameEqualToIgnoreCase("foo", 0)).isInstanceOf(AssertionError.class);
}

@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)
Expand Down