diff --git a/README.adoc b/README.adoc index 5889eeb..c8e112c 100644 --- a/README.adoc +++ b/README.adoc @@ -307,9 +307,9 @@ the `localhost` host to the inventory. Next, the test case makes a connection to Then, it asserts whether the time that is needed to retrieve the system properties for localhost is less than 4 seconds. -* The [hotspot=testInventoryAccessCountMetric file=0]`testInventoryAccessCountMetric()` test case validates the [hotspot=countedForList file=1]`@Counted` metric. The test case sends a request to the -`\http://localhost:9080/inventory/systems` URL to retrieve the whole inventory, and then it asserts -that this endpoint is accessed at least once. +* The [hotspot=testInventoryAccessCountMetric file=0]`testInventoryAccessCountMetric()` test case validates the [hotspot=countedForList file=1]`@Counted` metric. +The test case obtains metric data before and after a request to the `\http://localhost:9080/inventory/systems` URL. +It then asserts that the metric was increased after the URL was accessed. * The [hotspot=testInventorySizeGaugeMetric file=0]`testInventorySizeGaugeMetric()` test case validates the [hotspot=gaugeForGetTotal file=1]`@Gauge` metric. The test case first ensures that the localhost is in the inventory, then looks for the [hotspot=gaugeForGetTotal file=1]`@Gauge` metric and asserts diff --git a/finish/src/test/java/it/io/openliberty/guides/metrics/MetricsIT.java b/finish/src/test/java/it/io/openliberty/guides/metrics/MetricsIT.java index 3c48e21..a53cc41 100644 --- a/finish/src/test/java/it/io/openliberty/guides/metrics/MetricsIT.java +++ b/finish/src/test/java/it/io/openliberty/guides/metrics/MetricsIT.java @@ -91,7 +91,7 @@ public void testPropertiesRequestTimeMetric() { for (String metric : metrics) { if (metric.startsWith( "application_inventoryProcessingTime_rate_per_second")) { - float seconds = Float.parseFloat(metric.split(" ")[1]); + float seconds = Float.parseFloat(metric.split("\\s+")[1]); assertTrue(4 > seconds); } } @@ -106,13 +106,17 @@ public void testPropertiesRequestTimeMetric() { // end::Order2[] // tag::testInventoryAccessCountMetric[] public void testInventoryAccessCountMetric() { + metrics = getMetrics(); + Map accessCountsBefore = getIntMetrics(metrics, + "application_inventoryAccessCount_total"); connectToEndpoint(baseHttpUrl + INVENTORY_HOSTS); metrics = getMetrics(); - for (String metric : metrics) { - if (metric.startsWith("application_inventoryAccessCount_total")) { - assertTrue( - 1 <= Integer.parseInt(metric.split(" ")[metric.split(" ").length - 1])); - } + Map accessCountsAfter = getIntMetrics(metrics, + "application_inventoryAccessCount_total"); + for (String key : accessCountsBefore.keySet()) { + Integer accessCountBefore = accessCountsBefore.get(key); + Integer accessCountAfter = accessCountsAfter.get(key); + assertTrue(accessCountAfter > accessCountBefore); } } // end::testInventoryAccessCountMetric[] @@ -126,11 +130,10 @@ public void testInventoryAccessCountMetric() { // tag::testInventorySizeGaugeMetric[] public void testInventorySizeGaugeMetric() { metrics = getMetrics(); - for (String metric : metrics) { - if (metric.startsWith("application_inventorySizeGauge")) { - assertTrue( - 1 <= Character.getNumericValue(metric.charAt(metric.length() - 1))); - } + Map inventorySizeGauges = getIntMetrics(metrics, + "application_inventorySizeGauge"); + for (Integer value : inventorySizeGauges.values()) { + assertTrue(1 <= value); } } // end::testInventorySizeGaugeMetric[] @@ -198,5 +201,18 @@ private Response getResponse(String url) { private void assertResponse(String url, Response response) { assertEquals(200, response.getStatus(), "Incorrect response code from " + url); } + + private Map getIntMetrics(List metrics, String metricName) { + Map output = new HashMap(); + for (String metric : metrics) { + if (metric.startsWith(metricName)) { + String[] mSplit = metric.split("\\s+"); + String key = mSplit[0]; + Integer value = Integer.parseInt(mSplit[mSplit.length - 1]); + output.put(key, value); + } + } + return output; + } } // end::MetricsTest[]