ApplicationResource extends AbstractBrooklynRestResource implements ApplicationApi { @Override public ApplicationSummary get(String application) { return ApplicationTransformer.summaryFromApplication(brooklyn().getApplication(application), ui.getBaseUriBuilder()); }  @Override List<EntityDetail> fetch(String entityIds, String extraSensorsS); @Override List<EntitySummary> details(String entityIds, boolean includeAllApps, String extraSensorsGlobsS, String extraConfigGlobsS, int depth); @Override List<ApplicationSummary> list(String typeRegex); @Override ApplicationSummary get(String application); @Override Response createFromYaml(String yaml); @Override Response createFromYamlWithAppId(String yaml, String appId); @Override Response createPoly(byte[] inputToAutodetectType); @Override Response createFromForm(String contents); @Override Response delete(String application); @Override List<EntitySummary> getDescendants(String application, String typeRegex); @Override Map<String, Object> getDescendantsSensor(String application, String sensor, String typeRegex); static Map<String, Object> getSensorMap(String sensor, Iterable<Entity> descs);  }
@Test public void testGetUndefinedApplication() { try { client().path("/applications/dummy-not-found").get(ApplicationSummary.class); } catch (WebApplicationException e) { assertEquals(e.getResponse().getStatus(), 404); } }
@Test(dependsOnMethods = { "testDeleteApplication" }) public void testDeployApplicationImpl() throws Exception { ApplicationSpec spec = ApplicationSpec.builder() .type(RestMockApp.class.getCanonicalName()) .name("simple-app-impl") .locations(ImmutableSet.of("localhost")) .build(); Response response = clientDeploy(spec); assertTrue(response.getStatus() / 100 == 2, "response is " + response); URI appUri = response.getLocation(); waitForApplicationToBeRunning(response.getLocation()); assertEquals(client().path(appUri).get(ApplicationSummary.class).getSpec().getName(), "simple-app-impl"); }
@Test(dependsOnMethods = { "testDeployApplication", "testLocatedLocation" }) public void testDeployApplicationFromInterface() throws Exception { ApplicationSpec spec = ApplicationSpec.builder() .type(BasicApplication.class.getCanonicalName()) .name("simple-app-interface") .locations(ImmutableSet.of("localhost")) .build(); Response response = clientDeploy(spec); assertTrue(response.getStatus() / 100 == 2, "response is " + response); URI appUri = response.getLocation(); waitForApplicationToBeRunning(response.getLocation()); assertEquals(client().path(appUri).get(ApplicationSummary.class).getSpec().getName(), "simple-app-interface"); }
@Test(dependsOnMethods = { "testDeployApplication", "testLocatedLocation" }) public void testDeployApplicationYaml() throws Exception { String yaml = "{ name: simple-app-yaml, location: localhost, services: [ { serviceType: "+BasicApplication.class.getCanonicalName()+" } ] }"; Response response = client().path("/applications") .post(Entity.entity(yaml, "application/x-yaml")); assertTrue(response.getStatus()/100 == 2, "response is "+response); URI appUri = response.getLocation(); waitForApplicationToBeRunning(response.getLocation()); assertEquals(client().path(appUri).get(ApplicationSummary.class).getSpec().getName(), "simple-app-yaml"); }
@Test(dependsOnMethods = "testDeployApplication") public void testListEntities() { Set<EntitySummary> entities = client().path("/applications/simple-app/entities") .get(new GenericType<Set<EntitySummary>>() {}); assertEquals(entities.size(), 2); EntitySummary entity = Iterables.find(entities, withName("simple-ent"), null); EntitySummary group = Iterables.find(entities, withName("simple-group"), null); Assert.assertNotNull(entity); Assert.assertNotNull(group); client().path(entity.getLinks().get("self")).get(); Set<EntitySummary> children = client().path(entity.getLinks().get("children")) .get(new GenericType<Set<EntitySummary>>() {}); assertEquals(children.size(), 0); }
@Test(dependsOnMethods = "testDeployApplication") public void testListApplications() { Set<ApplicationSummary> applications = client().path("/applications") .get(new GenericType<Set<ApplicationSummary>>() { }); log.info("Applications listed are: " + applications); for (ApplicationSummary app : applications) { if (simpleSpec.getName().equals(app.getSpec().getName())) return; } Assert.fail("simple-app not found in list of applications: "+applications); }
@Test(dependsOnMethods = "testDeployApplication") public void testGetApplicationOnFire() { Application app = Iterables.find(manager.getApplications(), EntityPredicates.displayNameEqualTo(simpleSpec.getName())); Lifecycle origState = app.getAttribute(Attributes.SERVICE_STATE_ACTUAL); ApplicationSummary summary = client().path("/applications/"+app.getId()) .get(ApplicationSummary.class); assertEquals(summary.getStatus(), Status.RUNNING); app.sensors().set(Attributes.SERVICE_STATE_ACTUAL, Lifecycle.ON_FIRE); try { ApplicationSummary summary2 = client().path("/applications/"+app.getId()) .get(ApplicationSummary.class); log.info("Application: " + summary2); assertEquals(summary2.getStatus(), Status.ERROR); } finally { app.sensors().set(Attributes.SERVICE_STATE_ACTUAL, origState); } }
@SuppressWarnings({ "rawtypes", "unchecked" }) @Test(dependsOnMethods = "testDeployApplication") public void testFetchApplicationsAndEntity() { Collection apps = client().path("/applications/fetch").get(Collection.class); log.info("Applications fetched are: " + apps); Map app = ((Collection<Map>)apps).stream().filter(m -> "simple-app".equals(m.get("name"))).findFirst().orElse(null); Assert.assertNotNull(app, "did not find 'simple-app'"); Asserts.assertThat((String) app.get("applicationId"), StringPredicates.isNonBlank(), "expected value for applicationId"); Asserts.assertThat((String) app.get("serviceState"), StringPredicates.isNonBlank(), "expected value for serviceState"); Asserts.assertThat(app.get("serviceUp"), Predicates.not(Predicates.isNull()), "expected non-null for serviceUp"); Collection children = (Collection) app.get("children"); Asserts.assertSize(children, 2); Map entitySummary = (Map) Iterables.find(children, withValueForKey("name", "simple-ent"), null); Map groupSummary = (Map) Iterables.find(children, withValueForKey("name", "simple-group"), null); Assert.assertNotNull(entitySummary); Assert.assertNotNull(groupSummary); Asserts.assertThat( ((Collection<Map>)apps).stream().filter(m -> ImmutableSet.of("simple-ent", "simple-group").contains(m.get("name"))).collect(Collectors.toList()), CollectionFunctionals.empty(), "Some entities should not be listed at high level"); String itemIds = app.get("id") + "," + entitySummary.get("id") + "," + groupSummary.get("id"); Collection entities = client().path("/applications/fetch") .query("items", itemIds) .query("sensors", "[ service.state.expected, \"host.address\" ]") .get(Collection.class); log.info("Applications+Entities fetched are: " + entities); Asserts.assertSize(entities, apps.size() + 2); Map entityDetails = (Map) Iterables.find(entities, withValueForKey("name", "simple-ent"), null); Map groupDetails = (Map) Iterables.find(entities, withValueForKey("name", "simple-group"), null); Assert.assertNotNull(entityDetails); Assert.assertNotNull(groupDetails); Assert.assertEquals(entityDetails.get("parentId"), app.get("id")); Assert.assertNull(entityDetails.get("children")); Assert.assertEquals(groupDetails.get("parentId"), app.get("id")); Assert.assertNull(groupDetails.get("children")); Collection entityGroupIds = (Collection) entityDetails.get("groupIds"); Assert.assertNotNull(entityGroupIds); Assert.assertEquals(entityGroupIds.size(), 1); Assert.assertEquals(entityGroupIds.iterator().next(), groupDetails.get("id")); Collection groupMembers = (Collection) groupDetails.get("members"); Assert.assertNotNull(groupMembers); for (Application appi : getManagementContext().getApplications()) { Entities.dumpInfo(appi); } log.info("MEMBERS: " + groupMembers); Assert.assertEquals(groupMembers.size(), 1); Map entityMemberDetails = (Map) Iterables.find(groupMembers, withValueForKey("name", "simple-ent"), null); Assert.assertNotNull(entityMemberDetails); Assert.assertEquals(entityMemberDetails.get("id"), entityDetails.get("id")); Map<String,Object> simpleEntSensors = Preconditions.checkNotNull(Map.class.cast(entityDetails.get("sensors")), "sensors"); org.apache.brooklyn.api.entity.Entity simpleEnt = Preconditions.checkNotNull(getManagementContext().<org.apache.brooklyn.api.entity.Entity>lookup( EntityPredicates.displayNameEqualTo("simple-ent"))); Assert.assertEquals(simpleEntSensors.get("host.address"), simpleEnt.sensors().get(Attributes.ADDRESS)); Transition expected = simpleEnt.sensors().get(Attributes.SERVICE_STATE_EXPECTED); Assert.assertEquals(simpleEntSensors.get("service.state.expected"), MutableMap.of("state", expected.getState().name(), "timestampUtc", expected.getTimestamp().getTime())); }
@SuppressWarnings({ "rawtypes", "unchecked" }) @Test(dependsOnMethods = "testDeployApplication") public void testApplicationDetailsAndEntity() { Collection apps = client().path("/applications/details").get(Collection.class); log.info("Application details are: " + apps); Map app = ((Collection<Map>)apps).stream().filter(m -> "simple-app".equals(m.get("name"))).findFirst().orElse(null); Assert.assertNotNull(app, "did not find 'simple-app'"); Asserts.assertThat((String) app.get("applicationId"), StringPredicates.isNonBlank(), "expected value for applicationId"); Asserts.assertThat((String) app.get("serviceState"), StringPredicates.isNonBlank(), "expected value for serviceState"); Asserts.assertThat(app.get("serviceUp"), Predicates.not(Predicates.isNull()), "expected non-null for serviceUp"); Collection children = (Collection) app.get("children"); Asserts.assertSize(children, 2); Map entitySummary = (Map) Iterables.find(children, withValueForKey("name", "simple-ent"), null); Map groupSummary = (Map) Iterables.find(children, withValueForKey("name", "simple-group"), null); Assert.assertNotNull(entitySummary); Assert.assertNotNull(groupSummary); Asserts.assertThat( ((Collection<Map>)apps).stream().filter(m -> ImmutableSet.of("simple-ent", "simple-group").contains(m.get("name"))).collect(Collectors.toList()), CollectionFunctionals.empty(), "Some entities should not be listed at high level"); String itemIds = entitySummary.get("id") + "," + groupSummary.get("id"); Collection entities1 = client().path("/applications/details") .query("items", itemIds) .query("includeAllApps", false) .query("sensors", "[ service.state.expected, \"host.address\" ]") .query("config", "*") .get(Collection.class); log.info("Applications+Entities details are: " + entities1); Predicate<Collection> check = (entities) -> { Asserts.assertSize(entities, 3); Map entityDetails = (Map) Iterables.find(entities, withValueForKey("name", "simple-ent"), null); Map groupDetails = (Map) Iterables.find(entities, withValueForKey("name", "simple-group"), null); Assert.assertNotNull(entityDetails); Assert.assertNotNull(groupDetails); Assert.assertEquals(entityDetails.get("parentId"), app.get("id")); Assert.assertNull(entityDetails.get("children")); Assert.assertEquals(groupDetails.get("parentId"), app.get("id")); Assert.assertNull(groupDetails.get("children")); Collection entityGroupIds = (Collection) entityDetails.get("groupIds"); Assert.assertNotNull(entityGroupIds); Assert.assertEquals(entityGroupIds.size(), 1); Assert.assertEquals(entityGroupIds.iterator().next(), groupDetails.get("id")); Collection groupMembers = (Collection) groupDetails.get("members"); Assert.assertNotNull(groupMembers); log.info("MEMBERS: " + groupMembers); Assert.assertEquals(groupMembers.size(), 1); Map entityMemberDetails = (Map) Iterables.find(groupMembers, withValueForKey("name", "simple-ent"), null); Assert.assertNotNull(entityMemberDetails); Assert.assertEquals(entityMemberDetails.get("id"), entityDetails.get("id")); Map<String,Object> simpleEntSensors = Preconditions.checkNotNull(Map.class.cast(entityDetails.get("sensors")), "sensors"); org.apache.brooklyn.api.entity.Entity simpleEnt = Preconditions.checkNotNull(getManagementContext().<org.apache.brooklyn.api.entity.Entity>lookup( EntityPredicates.displayNameEqualTo("simple-ent"))); Assert.assertEquals(simpleEntSensors.get("host.address"), simpleEnt.sensors().get(Attributes.ADDRESS)); Transition expected = simpleEnt.sensors().get(Attributes.SERVICE_STATE_EXPECTED); Assert.assertEquals(simpleEntSensors.get("service.state.expected"), MutableMap.of("state", expected.getState().name(), "timestampUtc", expected.getTimestamp().getTime())); Assert.assertEquals( ((Map)groupDetails.get("config")).get("namematchergroup.regex"), "simple-ent" ); Assert.assertEquals( ((Map)entityDetails.get("config")).get("namematchergroup.regex"), null ); return true; }; check.apply(entities1); Collection entities2 = client().path("/applications/details") .query("items", app.get("id")) .query("includeAllApps", false) .query("sensors", "[ service.state.expected, \"host.address\" ]") .query("config", "n*") .query("depth", 2) .get(Collection.class); log.info("Applications+Entities details 2 are: " + entities2); MutableList entities2b = MutableList.copyOf(entities2); entities2b.addAll((Iterable) ((Map)Iterables.getOnlyElement(entities2)).get("children")); check.apply(entities2b); }
@Test(dependsOnMethods = "testDeployApplication") public void testListSensors() { Set<SensorSummary> sensors = client().path("/applications/simple-app/entities/simple-ent/sensors") .get(new GenericType<Set<SensorSummary>>() { }); assertTrue(sensors.size() > 0); SensorSummary sample = Iterables.find(sensors, new Predicate<SensorSummary>() { @Override public boolean apply(SensorSummary sensorSummary) { return sensorSummary.getName().equals(RestMockSimpleEntity.SAMPLE_SENSOR.getName()); } }); assertEquals(sample.getType(), "java.lang.String"); }
@Test(dependsOnMethods = "testDeployApplication") public void testListConfig() { Set<EntityConfigSummary> config = client().path("/applications/simple-app/entities/simple-ent/config") .get(new GenericType<Set<EntityConfigSummary>>() { }); assertTrue(config.size() > 0); System.out.println(("CONFIG: " + config)); }
@Test(dependsOnMethods = "testListConfig") public void testListConfig2() { Set<EntityConfigSummary> config = client().path("/applications/simple-app/entities/simple-ent/config") .get(new GenericType<Set<EntityConfigSummary>>() {}); assertTrue(config.size() > 0); System.out.println(("CONFIG: " + config)); }
@Test(dependsOnMethods = "testDeployApplication") public void testListEffectors() { Set<EffectorSummary> effectors = client().path("/applications/simple-app/entities/simple-ent/effectors") .get(new GenericType<Set<EffectorSummary>>() {}); assertTrue(effectors.size() > 0); EffectorSummary sampleEffector = find(effectors, new Predicate<EffectorSummary>() { @Override public boolean apply(EffectorSummary input) { return input.getName().equals("sampleEffector"); } }); assertEquals(sampleEffector.getReturnType(), "java.lang.String"); }
@Test(dependsOnMethods = "testTriggerSampleEffector") public void testBatchSensorValues() { WebClient client = client().path("/applications/simple-app/entities/simple-ent/sensors/current-state"); Map<String, Object> sensors = client.get(new GenericType<Map<String, Object>>() {}); assertTrue(sensors.size() > 0); assertEquals(sensors.get(RestMockSimpleEntity.SAMPLE_SENSOR.getName()), "foo4"); }
@Test(dependsOnMethods = "testBatchSensorValues") public void testReadEachSensor() { Set<SensorSummary> sensors = client().path("/applications/simple-app/entities/simple-ent/sensors") .get(new GenericType<Set<SensorSummary>>() {}); Map<String, String> readings = Maps.newHashMap(); for (SensorSummary sensor : sensors) { try { readings.put(sensor.getName(), client().path(sensor.getLinks().get("self")).accept(MediaType.TEXT_PLAIN).get(String.class)); } catch (WebApplicationException uie) { if (uie.getResponse().getStatus() == 204) { readings.put(sensor.getName(), null); } else { Exceptions.propagate(uie); } } } assertEquals(readings.get(RestMockSimpleEntity.SAMPLE_SENSOR.getName()), "foo4"); }
@Test(dependsOnMethods = "testTriggerSampleEffector") public void testPolicyWhichCapitalizes() throws Exception { String policiesEndpoint = "/applications/simple-app/entities/simple-ent/policies"; Set<PolicySummary> policies = client().path(policiesEndpoint).get(new GenericType<Set<PolicySummary>>(){}); assertEquals(policies.size(), 0); Response response = client().path(policiesEndpoint) .query("type", CapitalizePolicy.class.getCanonicalName()) .type(MediaType.APPLICATION_JSON_TYPE) .post(toJsonEntity(ImmutableMap.of())); assertEquals(response.getStatus(), 200); PolicySummary policy = response.readEntity(PolicySummary.class); assertNotNull(policy.getId()); String newPolicyId = policy.getId(); log.info("POLICY CREATED: " + newPolicyId); policies = client().path(policiesEndpoint).get(new GenericType<Set<PolicySummary>>() {}); assertEquals(policies.size(), 1); Lifecycle status = client().path(policiesEndpoint + "/" + newPolicyId).get(Lifecycle.class); log.info("POLICY STATUS: " + status); response = client().path(policiesEndpoint+"/"+newPolicyId+"/start") .post(null); assertEquals(response.getStatus(), 204); status = client().path(policiesEndpoint + "/" + newPolicyId).get(Lifecycle.class); assertEquals(status, Lifecycle.RUNNING); response = client().path(policiesEndpoint+"/"+newPolicyId+"/stop") .post(null); assertEquals(response.getStatus(), 204); status = client().path(policiesEndpoint + "/" + newPolicyId).get(Lifecycle.class); assertEquals(status, Lifecycle.STOPPED); response = client().path(policiesEndpoint+"/"+newPolicyId+"/destroy") .post(null); assertEquals(response.getStatus(), 204); response = client().path(policiesEndpoint+"/"+newPolicyId).get(); log.info("POLICY STATUS RESPONSE AFTER DESTROY: " + response.getStatus()); assertEquals(response.getStatus(), 404); policies = client().path(policiesEndpoint).get(new GenericType<Set<PolicySummary>>() {}); assertEquals(0, policies.size()); }
@SuppressWarnings({ "rawtypes" }) @Test(dependsOnMethods = "testDeployApplication") public void testLocatedLocation() { log.info("starting testLocatedLocations"); testListApplications(); LocationInternal l = (LocationInternal) getManagementContext().getApplications().iterator().next().getLocations().iterator().next(); if (l.config().getLocalRaw(LocationConfigKeys.LATITUDE).isAbsent()) { log.info("Supplying fake locations for localhost because could not be autodetected"); ((AbstractLocation) l).setHostGeoInfo(new HostGeoInfo("localhost", "localhost", 50, 0)); } Map result = client().path("/locations/usage/LocatedLocations") .get(Map.class); log.info("LOCATIONS: " + result); Assert.assertEquals(result.size(), 1); Map details = (Map) result.values().iterator().next(); assertEquals(details.get("leafEntityCount"), 2); }