Search in sources :

Example 1 with ContainerFactory

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));
        }
    }
}
Also used : RrdDef(org.rrd4j.core.RrdDef) Set(java.util.Set) Matchers.anyString(org.mockito.Matchers.anyString) LinkedList(java.util.LinkedList) LinkedHashMap(java.util.LinkedHashMap) Response(javax.ws.rs.core.Response) Iterator(java.util.Iterator) Matchers.anyLong(org.mockito.Matchers.anyLong) ContainerFactory(org.json.simple.parser.ContainerFactory) JSONParser(org.json.simple.parser.JSONParser) File(java.io.File) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) UriInfo(javax.ws.rs.core.UriInfo) Test(org.junit.Test)

Aggregations

File (java.io.File)1 Iterator (java.util.Iterator)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Set (java.util.Set)1 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)1 Response (javax.ws.rs.core.Response)1 UriInfo (javax.ws.rs.core.UriInfo)1 ContainerFactory (org.json.simple.parser.ContainerFactory)1 JSONParser (org.json.simple.parser.JSONParser)1 Test (org.junit.Test)1 Matchers.anyLong (org.mockito.Matchers.anyLong)1 Matchers.anyString (org.mockito.Matchers.anyString)1 RrdDef (org.rrd4j.core.RrdDef)1