EntityResource extends AbstractBrooklynRestResource implements EntityApi { @Override public EntitySummary get(String application, String entityName) { Entity entity = brooklyn().getEntity(application, entityName); if (Entitlements.isEntitled(mgmt().getEntitlementManager(), Entitlements.SEE_ENTITY, entity)) { return EntityTransformer.entitySummary(entity, ui.getBaseUriBuilder()); } throw WebResourceUtils.forbidden("User '%s' is not authorized to get entity '%s'", Entitlements.getEntitlementContext().user(), entity); }  @Override List<EntitySummary> list(final String application); @Override EntitySummary get(String application, String entityName); @Override List<EntitySummary> getChildren(final String application, final String entity); @Override Response addChildren(String applicationToken, String entityToken, Boolean start, String timeoutS, String yaml); @Override List<TaskSummary> listTasks(String applicationId, String entityId, int limit, Boolean recurse); @Override @Deprecated List<TaskSummary> listTasks(String applicationId, String entityId); @Override TaskSummary getTask(final String application, final String entityToken, String taskId); @SuppressWarnings("unchecked") @Override List<Object> listTags(String applicationId, String entityId); @Override Response getIcon(String applicationId, String entityId); @Override Response rename(String application, String entity, String newName); @Override Response expunge(String application, String entity, boolean release); @Override List<EntitySummary> getDescendants(String application, String entity, String typeRegex); @Override Map<String, Object> getDescendantsSensor(String application, String entity, String sensor, String typeRegex); @Override List<LocationSummary> getLocations(String application, String entity); @Override String getSpec(String applicationToken, String entityToken);  }
@Test public void testTagsSanity() throws Exception { entity.tags().addTag("foo"); Response response = client().path(entityEndpoint + "/tags") .accept(MediaType.APPLICATION_JSON_TYPE) .get(); String data = response.readEntity(String.class); try { List<Object> tags = new ObjectMapper().readValue(data, new TypeReference<List<Object>>() {}); Assert.assertTrue(tags.contains("foo")); Assert.assertFalse(tags.contains("bar")); } catch (Exception e) { Exceptions.propagateIfFatal(e); throw new IllegalStateException("Error with deserialization of tags list: "+e+"\n"+data, e); } }
@Test public void testTagsDoNotSerializeTooMuch() throws Exception { entity.tags().addTag("foo"); entity.tags().addTag(entity.getParent()); Response response = client().path(entityEndpoint + "/tags") .accept(MediaType.APPLICATION_JSON) .get(); String raw = response.readEntity(String.class); log.info("TAGS raw: "+raw); HttpAsserts.assertHealthyStatusCode(response.getStatus()); Assert.assertTrue(raw.contains(entity.getParent().getId()), "unexpected app tag, does not include ID: "+raw); Assert.assertTrue(raw.length() < 1000, "unexpected app tag, includes too much mgmt info (len "+raw.length()+"): "+raw); Assert.assertFalse(raw.contains(entity.getManagementContext().getManagementNodeId()), "unexpected app tag, includes too much mgmt info: "+raw); Assert.assertFalse(raw.contains("managementContext"), "unexpected app tag, includes too much mgmt info: "+raw); Assert.assertFalse(raw.contains("localhost"), "unexpected app tag, includes too much mgmt info: "+raw); Assert.assertFalse(raw.contains("catalog"), "unexpected app tag, includes too much mgmt info: "+raw); @SuppressWarnings("unchecked") List<Object> tags = mapper().readValue(raw, List.class); log.info("TAGS are: "+tags); Assert.assertEquals(tags.size(), 2, "tags are: "+tags); Assert.assertTrue(tags.contains("foo")); Assert.assertFalse(tags.contains("bar")); MutableList<Object> appTags = MutableList.copyOf(tags); appTags.remove("foo"); Object appTag = Iterables.getOnlyElement( appTags ); Assert.assertTrue(appTag instanceof Map, "Should have deserialized an entity: "+appTag); appTag = mapper().readValue(mapper().writeValueAsString(appTag), Entity.class); Assert.assertTrue(appTag instanceof Entity, "Should have deserialized an entity: "+appTag); Assert.assertEquals( ((Entity)appTag).getId(), entity.getApplicationId(), "Wrong ID: "+appTag); Assert.assertTrue(appTag instanceof BasicApplication, "Should have deserialized BasicApplication: "+appTag); }