Skip to content

Commit

Permalink
Added overloaded methods for tags from common module; fixes gh-3102
Browse files Browse the repository at this point in the history
  • Loading branch information
marcingrzejszczak committed Mar 29, 2022
1 parent 672840c commit 419f779
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.stream.Collectors;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.Timer;
import org.assertj.core.api.AbstractAssert;
Expand Down Expand Up @@ -123,6 +124,24 @@ public MeterRegistryAssert hasTimerWithNameAndTags(String timerName, Tags tags)
return this;
}

/**
* Verifies that a timer with given name and key-value tags exists in the provided {@link MeterRegistry}.
*
* @param timerName name of the timer
* @param tags key-value pairs of tags
* @return this
* @throws AssertionError if the actual value is {@code null}.
* @throws AssertionError if there is no timer registered under given name with given tags.
*/
public MeterRegistryAssert hasTimerWithNameAndTags(String timerName, io.micrometer.common.Tags tags) {
return hasTimerWithNameAndTags(timerName, toMicrometerTags(tags));
}

private Tags toMicrometerTags(io.micrometer.common.Tags tags) {
Tag[] array = tags.stream().map(tag -> Tag.of(tag.getKey(), tag.getValue())).toArray(Tag[]::new);
return Tags.of(array);
}

/**
* Verifies that a timer with given name and key-value tags does not exist in the provided {@link MeterRegistry}.
*
Expand All @@ -139,6 +158,18 @@ public MeterRegistryAssert doesNotHaveTimerWithNameAndTags(String timerName, Tag
}
return this;
}

/**
* Verifies that a timer with given name and key-value tags does not exist in the provided {@link MeterRegistry}.
*
* @param timerName name of the timer
* @param tags key-value pairs of tags
* @return this
* @throws AssertionError if there is a timer registered under given name with given tags.
*/
public MeterRegistryAssert doesNotHaveTimerWithNameAndTags(String timerName, io.micrometer.common.Tags tags) {
return doesNotHaveTimerWithNameAndTags(timerName, toMicrometerTags(tags));
}

/**
* Verifies that a timer with given name and tag keys exists in the provided {@link MeterRegistry}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ void assertionErrorThrownWhenTimerPresentWithTagValue() {
.isInstanceOf(AssertionError.class)
.hasMessageContaining("Expected no timer with name <matching-metric-name> and tags <[tag(matching-tag=matching-value)]> but found one");
}

@Test
void assertionErrorThrownWhenTimerPresentWithCommonTagValue() {
Timer.start(this.simpleMeterRegistry).stop(Timer.builder("matching-metric-name").tag("matching-tag", "matching-value").register(this.simpleMeterRegistry));

assertThatThrownBy(() -> meterRegistryAssert.doesNotHaveTimerWithNameAndTags("matching-metric-name", io.micrometer.common.Tags.of("matching-tag", "matching-value")))
.isInstanceOf(AssertionError.class)
.hasMessageContaining("Expected no timer with name <matching-metric-name> and tags <[tag(matching-tag=matching-value)]> but found one");
}

@Test
void noAssertionErrorThrownWhenNoMetricsRegistered() {
Expand Down Expand Up @@ -135,6 +144,14 @@ void noAssertionErrorThrownWhenTimerWithTagPresent() {
.doesNotThrowAnyException();
}

@Test
void noAssertionErrorThrownWhenTimerWithCommonTagPresent() {
Timer.start(this.simpleMeterRegistry).stop(Timer.builder("matching-metric-name").tag("matching-tag", "matching-value").register(this.simpleMeterRegistry));

assertThatCode(() -> meterRegistryAssert.hasTimerWithNameAndTags("matching-metric-name", io.micrometer.common.Tags.of("matching-tag", "matching-value")))
.doesNotThrowAnyException();
}

@Test
void noAssertionErrorThrownWhenTimerMissing() {
assertThatCode(() -> meterRegistryAssert.doesNotHaveTimerWithName("foo"))
Expand All @@ -147,6 +164,12 @@ void noAssertionErrorThrownWhenTimerWithTagsMissing() {
.doesNotThrowAnyException();
}

@Test
void noAssertionErrorThrownWhenTimerWithCommonTagsMissing() {
assertThatCode(() -> meterRegistryAssert.doesNotHaveTimerWithNameAndTags("foo", io.micrometer.common.Tags.of(io.micrometer.common.Tag.of("bar", "baz"))))
.doesNotThrowAnyException();
}

@Test
void noAssertionErrorThrownWhenTimerWithTagKeysMissing() {
assertThatCode(() -> meterRegistryAssert.doesNotHaveTimerWithNameAndTagKeys("foo", "bar"))
Expand Down

0 comments on commit 419f779

Please sign in to comment.