/** Tests the result of the rest api single subject single config GET when there is a config. */
  @Test
  public void testSingleSubjectSingleConfig() {
    setUpConfigData();
    final WebTarget wt = target();
    final String response =
        wt.path("network/configuration/devices/device1/basic").request().get(String.class);

    final JsonObject result = Json.parse(response).asObject();
    Assert.assertThat(result, notNullValue());

    Assert.assertThat(result.names(), hasSize(2));

    checkBasicAttributes(result);
  }
 /*
  * Obtain device names from a JSON table (e.g. from hm-manager)
  */
 private static void fetchDeviceNamesFromNameTable(String filename) {
   lastFetch = System.currentTimeMillis();
   try (FileReader f = new FileReader(filename)) {
     JsonObject table = Json.parse(f).asObject();
     int cnt = 0;
     synchronized (nameCache) {
       for (Member m : table) {
         nameCache.put(m.getName(), m.getValue().asString());
         cnt++;
       }
     }
     L.log(Level.INFO, "Read " + cnt + " entries from name table " + filename);
     couldFetchOnce = true;
     DeviceInfo.resolveNames();
   } catch (Exception e) {
     L.log(Level.WARNING, "Error reading device name table " + filename, e);
   }
 }
  /** Tests GetAllMetrics method. */
  @Test
  public void testGetAllMetrics() {
    Counter onosCounter = new Counter();
    onosCounter.inc();

    Meter onosMeter = new Meter();
    onosMeter.mark();

    Timer onosTimer = new Timer();
    onosTimer.update(1, TimeUnit.MILLISECONDS);

    ImmutableMap<String, Metric> metrics =
        new ImmutableMap.Builder<String, Metric>()
            .put("onosCounter", onosCounter)
            .put("onosMeter", onosMeter)
            .put("onosTimer", onosTimer)
            .build();

    expect(mockMetricsService.getMetrics()).andReturn(metrics).anyTimes();

    replay(mockMetricsService);

    WebTarget wt = target();
    String response = wt.path("metrics").request().get(String.class);
    assertThat(response, containsString("{\"metrics\":["));

    JsonObject result = Json.parse(response).asObject();
    assertThat(result, notNullValue());

    JsonArray jsonMetrics = result.get("metrics").asArray();
    assertThat(jsonMetrics, notNullValue());
    assertThat(jsonMetrics.size(), is(3));

    assertTrue(
        matchesMetric(metrics.get("onosCounter")).matchesSafely(jsonMetrics.get(0).asObject()));
    assertTrue(
        matchesMetric(metrics.get("onosMeter")).matchesSafely(jsonMetrics.get(1).asObject()));
    assertTrue(
        matchesMetric(metrics.get("onosTimer")).matchesSafely(jsonMetrics.get(2).asObject()));
  }
  /** Tests the result of the rest api GET when there is a config. */
  @Test
  public void testConfigs() {
    setUpConfigData();
    final WebTarget wt = target();
    final String response = wt.path("network/configuration").request().get(String.class);

    final JsonObject result = Json.parse(response).asObject();
    Assert.assertThat(result, notNullValue());

    Assert.assertThat(result.names(), hasSize(2));

    JsonValue devices = result.get("devices");
    Assert.assertThat(devices, notNullValue());

    JsonValue device1 = devices.asObject().get("device1");
    Assert.assertThat(device1, notNullValue());

    JsonValue basic = device1.asObject().get("basic");
    Assert.assertThat(basic, notNullValue());

    checkBasicAttributes(basic);
  }