Skip to content

Commit

Permalink
Merge pull request #114 from OpenLiberty/improve-test-coverage
Browse files Browse the repository at this point in the history
Improve Tests
  • Loading branch information
gkwan-ibm authored Dec 8, 2020
2 parents 19b4057 + 2f3cdd9 commit e9073ab
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
6 changes: 3 additions & 3 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -106,13 +106,17 @@ public void testPropertiesRequestTimeMetric() {
// end::Order2[]
// tag::testInventoryAccessCountMetric[]
public void testInventoryAccessCountMetric() {
metrics = getMetrics();
Map<String, Integer> 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<String, Integer> 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[]
Expand All @@ -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<String, Integer> inventorySizeGauges = getIntMetrics(metrics,
"application_inventorySizeGauge");
for (Integer value : inventorySizeGauges.values()) {
assertTrue(1 <= value);
}
}
// end::testInventorySizeGaugeMetric[]
Expand Down Expand Up @@ -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<String, Integer> getIntMetrics(List<String> metrics, String metricName) {
Map<String, Integer> output = new HashMap<String, Integer>();
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[]

0 comments on commit e9073ab

Please sign in to comment.