use of org.json.simple.parser.ContainerFactory in project ddf by codice.
the class MetricsEndpointTest method testGetMetricsList.
@SuppressWarnings("rawtypes")
@Test
public // @Ignore
void testGetMetricsList() throws Exception {
// Delete all .rrd files in test directory to ensure starting with clean directory
File testDir = new File(TEST_DIR);
File[] fileList = testDir.listFiles();
if (fileList != null) {
for (File file : fileList) {
if (file.isFile()) {
file.delete();
}
}
}
// Create RRD file that Metrics Endpoint will detect
rrdPath = TEST_DIR + "uptime.rrd";
RrdDef def = new RrdDef(rrdPath, 1);
def.addDatasource("uptime", DsType.COUNTER, 90, 0, Double.NaN);
def.addArchive(ConsolFun.TOTAL, 0.5, 1, 60);
rrdDb = RrdDbPool.getInstance().requestRrdDb(def);
UriInfo uriInfo = createUriInfo();
// Get the metrics list from the endpoint
MetricsEndpoint endpoint = getEndpoint();
endpoint.setMetricsDir(TEST_DIR);
Response response = endpoint.getMetricsList(uriInfo);
String metricsList = (String) response.getEntity();
LOGGER.debug("metricsList = {}", metricsList);
cleanupRrd();
// Useful class for simple JSON to handle collections when parsing
// (Called internally by the simple JSON parser.parse(...) method)
ContainerFactory containerFactory = new ContainerFactory() {
public List creatArrayContainer() {
return new LinkedList();
}
public Map createObjectContainer() {
return new LinkedHashMap();
}
};
// Parse the returned JSON text
JSONParser parser = new JSONParser();
Map json = (Map) parser.parse(metricsList, containerFactory);
Set<String> metricNames = (Set<String>) json.keySet();
assertThat(metricNames.size(), equalTo(1));
assertThat(metricNames, hasItem("uptime"));
Iterator metricsIter = json.entrySet().iterator();
// http://<host>:<port>/services/internal/metrics?dateOffset=3600
while (metricsIter.hasNext()) {
Map.Entry entry = (Map.Entry) metricsIter.next();
Map metricTimeRangeLinks = (Map) entry.getValue();
LOGGER.debug("metricTimeRangeLinks = {}", metricTimeRangeLinks);
// Verify each metric name, e.g., "uptime", has all of the time ranges represented
assertThat(metricTimeRangeLinks.containsKey("15m"), is(true));
assertThat(metricTimeRangeLinks.containsKey("1h"), is(true));
// assertThat(metricTimeRangeLinks.containsKey("4h"), is(true));
// assertThat(metricTimeRangeLinks.containsKey("12h"), is(true));
assertThat(metricTimeRangeLinks.containsKey("1d"), is(true));
// assertThat(metricTimeRangeLinks.containsKey("3d"), is(true));
assertThat(metricTimeRangeLinks.containsKey("1w"), is(true));
assertThat(metricTimeRangeLinks.containsKey("1M"), is(true));
assertThat(metricTimeRangeLinks.containsKey("3M"), is(true));
assertThat(metricTimeRangeLinks.containsKey("6M"), is(true));
assertThat(metricTimeRangeLinks.containsKey("1y"), is(true));
Iterator timeRangeLinksIter = metricTimeRangeLinks.entrySet().iterator();
// supported formats and that the correct dateOffset is specified in the hyperlinks
while (timeRangeLinksIter.hasNext()) {
Map.Entry timeRangeLinkEntry = (Map.Entry) timeRangeLinksIter.next();
String timeRange = (String) timeRangeLinkEntry.getKey();
Map<String, String> metricHyperlinks = (Map<String, String>) timeRangeLinkEntry.getValue();
Long dateOffset = MetricsEndpoint.TIME_RANGES.get(timeRange);
assertThat(metricHyperlinks.containsKey("PNG"), is(true));
assertThat(metricHyperlinks.get("PNG"), endsWith("dateOffset=" + dateOffset));
assertThat(metricHyperlinks.containsKey("CSV"), is(true));
assertThat(metricHyperlinks.get("CSV"), endsWith("dateOffset=" + dateOffset));
assertThat(metricHyperlinks.containsKey("XLS"), is(true));
assertThat(metricHyperlinks.get("XLS"), endsWith("dateOffset=" + dateOffset));
}
}
}
Aggregations