From 304143e30f383b9c3db59afb049abdb08e468a5d Mon Sep 17 00:00:00 2001 From: Doug Hoard Date: Tue, 20 Jun 2023 11:35:57 -0400 Subject: [PATCH] MetricsAssertion support for multiple labels (#815) Signed-off-by: dhoard * Added MetricAssertion support for multiple labels Signed-off-by: dhoard Signed-off-by: Doug Hoard --- .../jmx/test/support/MetricAssertion.java | 100 ++++++++++++++---- 1 file changed, 77 insertions(+), 23 deletions(-) diff --git a/integration_test_suite/integration_tests/src/main/java/io/prometheus/jmx/test/support/MetricAssertion.java b/integration_test_suite/integration_tests/src/main/java/io/prometheus/jmx/test/support/MetricAssertion.java index 3cd644c8..44884f1c 100644 --- a/integration_test_suite/integration_tests/src/main/java/io/prometheus/jmx/test/support/MetricAssertion.java +++ b/integration_test_suite/integration_tests/src/main/java/io/prometheus/jmx/test/support/MetricAssertion.java @@ -19,8 +19,13 @@ import io.prometheus.jmx.test.Metric; import org.opentest4j.AssertionFailedError; +import java.util.ArrayList; import java.util.Collection; +import java.util.List; import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.function.Consumer; /** * Class to implement a MetricAssertion @@ -29,8 +34,7 @@ public class MetricAssertion { private final Collection metrics; private String name; - private String label; - private String labelValue; + private List labelTuples; private Double value; /** @@ -40,6 +44,7 @@ public class MetricAssertion { */ public MetricAssertion(Collection metrics) { this.metrics = metrics; + this.labelTuples = new ArrayList<>(); } /** @@ -54,15 +59,14 @@ public MetricAssertion withName(String name) { } /** - * Method to set the metric label and value (only 1 pair is supported) + * Method to add a metric label and value * * @param label label * @param value value * @return the return value */ public MetricAssertion withLabel(String label, String value) { - this.label = label; - this.labelValue = value; + labelTuples.add(new LabelTuple(label, value)); return this; } @@ -103,13 +107,12 @@ public MetricAssertion exists() { .stream() .filter(metric -> metric.getName().equals(name)) .filter(metric -> { - if (label == null) { + if (labelTuples.size() == 0) { return true; } - for (Map.Entry entry : metric.getLabels().entrySet()) { - if (entry.getKey().equals(label) && entry.getValue().equals(labelValue)) { - return true; - } + List labelTuples = toLabelTupleList(metric); + if (labelTuples.containsAll(this.labelTuples)) { + return true; } return false; }) @@ -124,13 +127,12 @@ public MetricAssertion exists() { metric -> { /* DO NOTHING */ }, () -> { String message; - if (label != null) { + if (labelTuples.size() > 0) { message = String.format( - "Metric [%s] with label [%s] = [%s] does not exist", + "Metric [%s] with labels / values %s does not exist", name, - label, - labelValue); + toLabelTupleString(labelTuples)); } else { message = String.format("Metric [%s] does not exist", name); @@ -151,13 +153,12 @@ public MetricAssertion doesNotExist() { .stream() .filter(metric -> metric.getName().equals(name)) .filter(metric -> { - if (label == null) { + if (labelTuples.size() == 0) { return true; } - for (Map.Entry entry : metric.getLabels().entrySet()) { - if (entry.getKey().equals(label) && entry.getValue().equals(labelValue)) { - return true; - } + List labelTuples = toLabelTupleList(metric); + if (labelTuples.containsAll(this.labelTuples)) { + return true; } return false; }) @@ -170,13 +171,12 @@ public MetricAssertion doesNotExist() { .findFirst() .ifPresent(metric -> { String message; - if (label != null) { + if (labelTuples.size() > 0) { message = String.format( - "Metric [%s] with label [%s] = [%s] should not exist", + "Metric [%s] with labels / values %s should not exist", name, - label, - labelValue); + toLabelTupleString(labelTuples)); } else { message = String.format("Metric [%s] should not exist", name); @@ -186,4 +186,58 @@ public MetricAssertion doesNotExist() { return this; } + + private static List toLabelTupleList(Metric metric) { + List labelTuples = new ArrayList<>(); + for (Map.Entry entry : metric.getLabels().entrySet()) { + labelTuples.add(new LabelTuple(entry.getKey(), entry.getValue())); + } + return labelTuples; + } + + private static String toLabelTupleString(List labelTuples) { + StringBuilder stringBuilder = new StringBuilder(); + labelTuples.forEach(labelTuple -> { + stringBuilder.append(labelTuple); + stringBuilder.append(", "); + }); + return stringBuilder.toString().substring(0, stringBuilder.length() - 2); + } + + private static class LabelTuple { + + private final String label; + private final String value; + + public LabelTuple(String label, String value) { + this.label = label; + this.value = value; + } + + public String getLabel() { + return label; + } + + public String getValue() { + return value; + } + + @Override + public String toString() { + return "[" + label + "] = [" + value + "]"; + } + + @Override + public int hashCode() { + return Objects.hash(label, value); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + LabelTuple that = (LabelTuple) o; + return Objects.equals(label, that.label) && Objects.equals(value, that.value); + } + } }