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

MeterRegistryAssert provides checks for all type of meters #3104

Merged
merged 1 commit into from
Apr 11, 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 @@ -18,6 +18,7 @@
import java.util.List;
import java.util.stream.Collectors;

import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags;
Expand Down Expand Up @@ -73,6 +74,24 @@ public MeterRegistryAssert hasNoMetrics() {
}
return this;
}

/**
* Verifies that a meter with given name exists in the provided {@link MeterRegistry}.
*
* @param meterName name of the meter
* @return this
* @throws AssertionError if the actual value is {@code null}.
* @throws AssertionError if there is no meter registered under given name.
*/
public MeterRegistryAssert hasMeterWithName(String meterName) {
isNotNull();
Meter foundMeter = actual.find(meterName).meter();
if (foundMeter == null) {
failWithMessage("Expected a meter with name <%s> but found none.\nFound following metrics %s", meterName, allMetrics());
}
return this;
}

/**
* Verifies that a timer with given name exists in the provided {@link MeterRegistry}.
*
Expand All @@ -90,6 +109,22 @@ public MeterRegistryAssert hasTimerWithName(String timerName) {
return this;
}

/**
* Verifies that a meter with given name does not exist in the provided {@link MeterRegistry}.
*
* @param meterName name of the meter that should not be present
* @return this
* @throws AssertionError if there is a meter registered under given name.
*/
public MeterRegistryAssert doesNotHaveMeterWithName(String meterName) {
isNotNull();
Meter foundMeter = actual.find(meterName).meter();
if (foundMeter != null) {
failWithMessage("Expected no meter with name <%s> but found one with tags <%s>", meterName, foundMeter.getId().getTags());
}
return this;
}

/**
* Verifies that a timer with given name does not exist in the provided {@link MeterRegistry}.
*
Expand All @@ -106,6 +141,24 @@ public MeterRegistryAssert doesNotHaveTimerWithName(String timerName) {
return this;
}

/**
* Verifies that a meter with given name and key-value tags exists in the provided {@link MeterRegistry}.
*
* @param meterName name of the meter
* @param tags key-value pairs of tags
* @return this
* @throws AssertionError if the actual value is {@code null}.
* @throws AssertionError if there is no meter registered under given name with given tags.
*/
public MeterRegistryAssert hasMeterWithNameAndTags(String meterName, Tags tags) {
isNotNull();
Meter foundMeter = actual.find(meterName).tags(tags).meter();
if (foundMeter == null) {
failWithMessage("Expected a meter with name <%s> and tags <%s> but found none.\nFound following metrics %s", meterName, tags, allMetrics());
}
return this;
}

/**
* Verifies that a timer with given name and key-value tags exists in the provided {@link MeterRegistry}.
*
Expand All @@ -124,6 +177,19 @@ public MeterRegistryAssert hasTimerWithNameAndTags(String timerName, Tags tags)
return this;
}

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

/**
* Verifies that a timer with given name and key-value tags exists in the provided {@link MeterRegistry}.
*
Expand All @@ -142,6 +208,23 @@ private Tags toMicrometerTags(io.micrometer.common.Tags tags) {
return Tags.of(array);
}

/**
* Verifies that a meter with given name and key-value tags does not exist in the provided {@link MeterRegistry}.
*
* @param meterName name of the meter
* @param tags key-value pairs of tags
* @return this
* @throws AssertionError if there is a meter registered under given name with given tags.
*/
public MeterRegistryAssert doesNotHaveMeterWithNameAndTags(String meterName, Tags tags) {
isNotNull();
Meter foundMeter = actual.find(meterName).tags(tags).meter();
if (foundMeter != null) {
failWithMessage("Expected no meter with name <%s> and tags <%s> but found one", meterName, tags);
}
return this;
}

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

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

/**
* Verifies that a timer with given name and key-value tags does not exist in the provided {@link MeterRegistry}.
*
Expand All @@ -170,7 +265,25 @@ public MeterRegistryAssert doesNotHaveTimerWithNameAndTags(String timerName, Tag
public MeterRegistryAssert doesNotHaveTimerWithNameAndTags(String timerName, io.micrometer.common.Tags tags) {
return doesNotHaveTimerWithNameAndTags(timerName, toMicrometerTags(tags));
}


/**
* Verifies that a meter with given name and tag keys exists in the provided {@link MeterRegistry}.
*
* @param meterName name of the meter
* @param tagKeys tag keys
* @return this
* @throws AssertionError if the actual value is {@code null}.
* @throws AssertionError if there is no meter registered under given name with given tag keys.
*/
public MeterRegistryAssert hasMeterWithNameAndTagKeys(String meterName, String... tagKeys) {
isNotNull();
Meter foundMeter = actual.find(meterName).tagKeys(tagKeys).meter();
if (foundMeter == null) {
failWithMessage("Expected a meter with name <%s> and tag keys <%s> but found none.\nFound following metrics %s", meterName, String.join(",", tagKeys), allMetrics());
}
return this;
}

/**
* Verifies that a timer with given name and tag keys exists in the provided {@link MeterRegistry}.
*
Expand All @@ -189,6 +302,23 @@ public MeterRegistryAssert hasTimerWithNameAndTagKeys(String timerName, String..
return this;
}

/**
* Verifies that a meter with given name and tag keys does not exist in the provided {@link MeterRegistry}.
*
* @param meterName name of the meter
* @param tagKeys tag keys
* @return this
* @throws AssertionError if there is a meter registered under given name with given tag keys.
*/
public MeterRegistryAssert doesNotHaveMeterWithNameAndTagKeys(String meterName, String... tagKeys) {
isNotNull();
Meter foundMeter = actual.find(meterName).tagKeys(tagKeys).meter();
if (foundMeter != null) {
failWithMessage("Expected a meter with name <%s> and tag keys <%s> but found one", meterName, String.join(",", tagKeys));
}
return this;
}

/**
* Verifies that a timer with given name and tag keys does not exist in the provided {@link MeterRegistry}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,135 @@ void noAssertionErrorThrownWhenTimerWithTagKeysMissing() {
.doesNotThrowAnyException();
}

@Test
void assertionErrorThrownWhenNoMeterUsingAssertThat() {
assertThatThrownBy(() -> MeterRegistryAssert.assertThat(simpleMeterRegistry).hasMeterWithName("foo"))
.isInstanceOf(AssertionError.class)
.hasMessageContaining("Expected a meter with name <foo> but found none");
}

@Test
void assertionErrorThrownWhenNoMeterUsingThen() {
assertThatThrownBy(() -> MeterRegistryAssert.then(simpleMeterRegistry).hasMeterWithName("foo"))
.isInstanceOf(AssertionError.class)
.hasMessageContaining("Expected a meter with name <foo> but found none");
}

@Test
void assertionErrorThrownWhenNoMeter() {
assertThatThrownBy(() -> meterRegistryAssert.hasMeterWithName("foo"))
.isInstanceOf(AssertionError.class)
.hasMessageContaining("Expected a meter with name <foo> but found none");
}

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

assertThatThrownBy(() -> meterRegistryAssert.hasMeterWithNameAndTagKeys("matching-metric-name", "non-existent-tag"))
.isInstanceOf(AssertionError.class)
.hasMessageContaining("Expected a meter with name <matching-metric-name> and tag keys <non-existent-tag> but found none");
}

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

assertThatThrownBy(() -> meterRegistryAssert.hasMeterWithNameAndTags("matching-metric-name", Tags.of("matching-tag", "some-value")))
.isInstanceOf(AssertionError.class)
.hasMessageContaining("Expected a meter with name <matching-metric-name> and tags <[tag(matching-tag=some-value)]> but found none");
}

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

assertThatThrownBy(() -> meterRegistryAssert.doesNotHaveMeterWithName("matching-metric-name"))
.isInstanceOf(AssertionError.class)
.hasMessageContaining("Expected no meter with name <matching-metric-name> but found one with tags <[]>");
}

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

assertThatThrownBy(() -> meterRegistryAssert.doesNotHaveMeterWithNameAndTagKeys("matching-metric-name", "matching-tag"))
.isInstanceOf(AssertionError.class)
.hasMessageContaining("Expected a meter with name <matching-metric-name> and tag keys <matching-tag> but found one");
}

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

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

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

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

@Test
void noAssertionErrorThrownWhenMeterPresent() {
Timer.start(this.simpleMeterRegistry).stop(Timer.builder("foo").register(this.simpleMeterRegistry));

assertThatCode(() -> meterRegistryAssert.hasMeterWithName("foo"))
.doesNotThrowAnyException();
}

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

assertThatCode(() -> meterRegistryAssert.hasMeterWithNameAndTagKeys("matching-metric-name", "matching-tag"))
.doesNotThrowAnyException();
}

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

assertThatCode(() -> meterRegistryAssert.hasMeterWithNameAndTags("matching-metric-name", Tags.of("matching-tag", "matching-value")))
.doesNotThrowAnyException();
}

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

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

@Test
void noAssertionErrorThrownWhenMeterMissing() {
assertThatCode(() -> meterRegistryAssert.doesNotHaveMeterWithName("foo"))
.doesNotThrowAnyException();
}

@Test
void noAssertionErrorThrownWhenMeterWithTagsMissing() {
assertThatCode(() -> meterRegistryAssert.doesNotHaveMeterWithNameAndTags("foo", Tags.of(Tag.of("bar", "baz"))))
.doesNotThrowAnyException();
}

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

@Test
void noAssertionErrorThrownWhenMeterWithTagKeysMissing() {
assertThatCode(() -> meterRegistryAssert.doesNotHaveMeterWithNameAndTagKeys("foo", "bar"))
.doesNotThrowAnyException();
}

}