Skip to content

Commit

Permalink
Added assertions for handled contexts; fixes gh-3274
Browse files Browse the repository at this point in the history
  • Loading branch information
marcingrzejszczak committed Jul 7, 2022
1 parent e83d8c7 commit e2a919b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.micrometer.observation.tck;

import io.micrometer.observation.Observation;
import org.assertj.core.api.ThrowingConsumer;

import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -104,8 +105,8 @@ private String observationNames(List<TestObservationRegistry.TestObservationCont
}

/**
* Verifies that there's only one {@link Observation} with a given name (ignoring
* case) and continues assertions for it.
* Verifies that there's at least one {@link Observation} with a given name (ignoring
* case) and continues assertions for the first found one.
* @return this
* @throws AssertionError if there is no matching observation
*/
Expand All @@ -125,6 +126,11 @@ public That hasObservationWithNameEqualToIgnoringCase(String name) {
return new That(testObservationContext, this);
}

/**
* Verifies that there are no observations registered.
* @return this
* @throws AssertionError if there are any registered observations
*/
public void doesNotHaveAnyObservation() {
List<TestObservationRegistry.TestObservationContext> contexts = this.actual.getContexts();
if (!contexts.isEmpty()) {
Expand All @@ -133,6 +139,19 @@ public void doesNotHaveAnyObservation() {
}
}

/**
* Verifies that all handled contexts satisfy the provided lambda.
* @param contextConsumer lambda to assert all handled contexts
* @return this
*/
public TestObservationRegistryAssert hasHandledContextsThatSatisfy(
ThrowingConsumer<List<Observation.Context>> contextConsumer) {
isNotNull();
contextConsumer.accept(actual.getContexts().stream()
.map(TestObservationRegistry.TestObservationContext::getContext).collect(Collectors.toList()));
return this;
}

/**
* Provides assertions for {@link Observation} and allows coming back to
* {@link TestObservationRegistryAssert}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationRegistry;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

import static io.micrometer.observation.tck.TestObservationRegistryAssert.assertThat;
Expand Down Expand Up @@ -111,6 +112,23 @@ void should_not_fail_when_observation_with_name_ignoring_case_found() {
.hasObservationWithNameEqualToIgnoringCase("foo").that().hasBeenStarted());
}

@Test
void should_fail_when_no_contexts_satisfy_the_assertion() {
Observation.createNotStarted("foo", registry).start().stop();

thenThrownBy(() -> TestObservationRegistryAssert.assertThat(registry)
.hasHandledContextsThatSatisfy(contexts -> Assertions.assertThat(contexts).hasSize(2)))
.isInstanceOf(AssertionError.class);
}

@Test
void should_not_fail_when_contexts_satisfy_the_assertions() {
Observation.createNotStarted("FOO", registry).start().stop();

thenNoException().isThrownBy(() -> TestObservationRegistryAssert.assertThat(registry)
.hasHandledContextsThatSatisfy(contexts -> Assertions.assertThat(contexts).hasSize(1)));
}

@Test
void should_jump_to_and_back_from_context_assert() {
new Example(registry).run();
Expand Down

0 comments on commit e2a919b

Please sign in to comment.