UsageResource extends AbstractBrooklynRestResource implements UsageApi { @Override public UsageStatistics getApplicationUsage(String application, String start, String end) { log.debug("REST call to get application usage for application {}: dates {} -> {}", new Object[] {application, start, end}); if (!Entitlements.isEntitled(mgmt().getEntitlementManager(), Entitlements.USAGE, null)) throw WebResourceUtils.forbidden("User '%s' is not authorized to perform this operation", Entitlements.getEntitlementContext().user()); Date startDate = parseDate(start, new Date(0)); Date endDate = parseDate(end, new Date()); checkDates(startDate, endDate); ApplicationUsage usage = mgmtInternal().getUsageManager().getApplicationUsage(application); if (usage != null) { List<UsageStatistic> statistics = retrieveApplicationUsage(usage, startDate, endDate); return new UsageStatistics(statistics, ImmutableMap.<String,URI>of()); } else { throw notFound("Application '%s' not found", application); } }  @Override List<UsageStatistics> listApplicationsUsage(@Nullable String start, @Nullable String end); @Override UsageStatistics getApplicationUsage(String application, String start, String end); @Override List<UsageStatistics> listMachinesUsage(final String application, final String start, final String end); @Override UsageStatistics getMachineUsage(final String machine, final String start, final String end);  }
@Test public void testGetApplicationUsage() throws Exception { Calendar preStart = new GregorianCalendar(); String appId = createApp(simpleSpec); Calendar postStart = new GregorianCalendar(); Response response = client().path("/usage/applications/" + appId).get(); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); UsageStatistics usage = response.readEntity(new GenericType<UsageStatistics>() {}); assertAppUsage(usage, appId, ImmutableList.of(Status.ACCEPTED, Status.STARTING, Status.RUNNING), roundDown(preStart), postStart); response = client().path("/usage/applications/" + appId).query("start", "1970-01-01T00:00:00-0100").get(); usage = response.readEntity(new GenericType<UsageStatistics>() {}); assertAppUsage(usage, appId, ImmutableList.of(Status.ACCEPTED, Status.STARTING, Status.RUNNING), roundDown(preStart), postStart); response = client().path("/usage/applications/" + appId).query("start", "9999-01-01T00:00:00+0100").get(); assertTrue(response.getStatus() >= 400, "end defaults to NOW, so future start should fail, instead got code "+response.getStatus()); response = client().path("/usage/applications/" + appId).query("start", "9999-01-01T00:00:00%2B0100").query("end", "9999-01-02T00:00:00%2B0100").get(); usage = response.readEntity(new GenericType<UsageStatistics>() {}); assertTrue(usage.getStatistics().isEmpty()); response = client().path("/usage/applications/" + appId).query("end", "9999-01-01T00:00:00+0100").get(); usage = response.readEntity(new GenericType<UsageStatistics>() {}); assertAppUsage(usage, appId, ImmutableList.of(Status.ACCEPTED, Status.STARTING, Status.RUNNING), roundDown(preStart), postStart); response = client().path("/usage/applications/" + appId).query("start", "9999-01-01T00:00:00+0100").query("end", "9999-02-01T00:00:00+0100").get(); usage = response.readEntity(new GenericType<UsageStatistics>() {}); assertTrue(usage.getStatistics().isEmpty()); response = client().path("/usage/applications/" + appId).query("start", "1970-01-01T00:00:00-0100").query("end", "9999-01-01T00:00:00+0100").get(); usage = response.readEntity(new GenericType<UsageStatistics>() {}); assertAppUsage(usage, appId, ImmutableList.of(Status.ACCEPTED, Status.STARTING, Status.RUNNING), roundDown(preStart), postStart); response = client().path("/usage/applications/" + appId).query("end", "1970-01-01T00:00:00-0100").get(); usage = response.readEntity(new GenericType<UsageStatistics>() {}); assertTrue(usage.getStatistics().isEmpty()); Calendar preDelete = new GregorianCalendar(); deleteApp(appId); Calendar postDelete = new GregorianCalendar(); response = client().path("/usage/applications/" + appId).get(); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); usage = response.readEntity(new GenericType<UsageStatistics>() {}); assertAppUsage(usage, appId, ImmutableList.of(Status.ACCEPTED, Status.STARTING, Status.RUNNING, Status.DESTROYED), roundDown(preStart), postDelete); assertAppUsage(ImmutableList.copyOf(usage.getStatistics()).subList(3, 4), appId, ImmutableList.of(Status.DESTROYED), roundDown(preDelete), postDelete); long afterPostDelete = postDelete.getTime().getTime()+1; waitForFuture(afterPostDelete); response = client().path("/usage/applications/" + appId).query("start", afterPostDelete).get(); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); usage = response.readEntity(new GenericType<UsageStatistics>() {}); assertTrue(usage.getStatistics().isEmpty(), "usages="+usage); }