@Override protected T customReadBody(String type, Map<Object, Object> values, JsonParser jp, DeserializationContext ctxt) throws IOException { Optional<String> entityId = Optional.fromNullable((String) values.get("entityId")); Optional<Entity> entity = Optional.fromNullable(entityId.isPresent() ? null: mgmt.getEntityManager().getEntity(entityId.get())); String id = (String) values.get("id"); return getInstanceFromId(entity, id); } protected Optional<String> getEntityId(T value) {
@Override @SuppressWarnings("unchecked") public <T extends BrooklynObject> T lookup(String id, Class<T> type) { Object result; result = getEntityManager().getEntity(id); if (result!=null && type.isInstance(result)) return (T)result; result = getLocationManager().getLocation(id); if (result!=null && type.isInstance(result)) return (T)result; // TODO policies, enrichers, feeds; bundles? return null; }
@Override public Entity getEntity(String id) { if (isInitialManagementContextReal()) { return initialManagementContext.getEntityManager().getEntity(id); } else { return null; } }
@Override public InetAddress getNextAgentAddress(String agentId) { Entity agent = getManagementContext().getEntityManager().getEntity(agentId); String address = agent.sensors().get(CalicoNode.DOCKER_HOST).sensors().get(Attributes.SUBNET_ADDRESS); try { return InetAddress.getByName(address); } catch (UnknownHostException uhe) { throw Exceptions.propagate(uhe); } }
@Override public InetAddress getNextAgentAddress(String agentId) { Entity agent = getManagementContext().getEntityManager().getEntity(agentId); String address = agent.sensors().get(OverlayPlugin.DOCKER_HOST).sensors().get(Attributes.SUBNET_ADDRESS); try { return InetAddress.getByName(address); } catch (UnknownHostException uhe) { throw Exceptions.propagate(uhe); } }
@Test public void testEntityManagerQueriesGiveProxies() { EntityManager entityManager = mgmt.getEntityManager(); Application retrievedApp = (Application) entityManager.getEntity(app.getId()); TestEntity retrievedEntity = (TestEntity) entityManager.getEntity(entity.getId()); assertIsProxy(retrievedApp); assertIsProxy(retrievedEntity); Collection<Entity> entities = entityManager.getEntities(); for (Entity e : entities) { assertIsProxy(e); } assertEquals(ImmutableSet.copyOf(entities), ImmutableSet.of(app, entity)); }
@Test public void testAutoCheckpointsOnUnmanageEntity() throws Exception { Entities.unmanage(origE); newApp = rebind(); // Assert does not container unmanaged entity assertEquals(ImmutableList.copyOf(newApp.getChildren()), Collections.emptyList()); assertNull(((EntityInternal)newApp).getManagementContext().getEntityManager().getEntity(origE.getId())); }
@SuppressWarnings("unchecked") protected <T extends Entity> T rebind(T entity) throws Exception { rebind(); Entity result = mgmt().getEntityManager().getEntity(entity.getId()); assertNotNull(result, "no entity found after rebind with id " + entity.getId()); return (T) result; }
@Test public void testReplacesMember() throws Exception { DynamicCluster cluster = app.createAndManageChild(EntitySpec.create(DynamicCluster.class) .configure("initialSize", 1) .configure("memberSpec", EntitySpec.create(TestEntity.class))); cluster.start(ImmutableList.of(loc)); Entity member = Iterables.get(cluster.getMembers(), 0); String replacementId = cluster.replaceMember(member.getId()); Entity replacement = app.getManagementContext().getEntityManager().getEntity(replacementId); assertEquals(cluster.getMembers().size(), 1); assertFalse(cluster.getMembers().contains(member)); assertFalse(cluster.getChildren().contains(member)); assertNotNull(replacement, "replacementId="+replacementId); assertTrue(cluster.getMembers().contains(replacement), "replacement="+replacement+"; members="+cluster.getMembers()); assertTrue(cluster.getChildren().contains(replacement), "replacement="+replacement+"; children="+cluster.getChildren()); }
@Test public void testDslAttributeWhenReady_2016_07() throws Exception { String entityId = "klcueb1ide"; doAddEntityMemento("2016-07", entityId); rebind(); Entity newEntity = mgmt().getEntityManager().getEntity(entityId); String val = newEntity.config().get(attributeWhenReady1); assertEquals(val, "myval"); }
@Test public void testDslConfigSupplier_2016_07() throws Exception { String entityId = "klcueb1ide"; doAddEntityMemento("2016-07", entityId); rebind(); Entity newEntity = mgmt().getEntityManager().getEntity(entityId); String val = newEntity.config().get(configSupplier1); assertEquals(val, "myval"); }
@Test public void testDslConfigSupplier() throws Exception { final Entity app = createAndStartApplication( "services:", "- type: " + BasicApplication.class.getName(), " brooklyn.config:", " myConfigKeyName: myval", " " + configSupplier1.getName() + ": $brooklyn:config(\"myConfigKeyName\")"); rebind(); Entity newEntity = mgmt().getEntityManager().getEntity(app.getId()); String val = newEntity.config().get(configSupplier1); assertEquals(val, "myval"); }
@Test public void testDslSensorSupplier_2016_07() throws Exception { String entityId = "klcueb1ide"; doAddEntityMemento("2016-07", entityId); rebind(); Entity newEntity = mgmt().getEntityManager().getEntity(entityId); Sensor<?> sensor = newEntity.config().get(sensorSupplier1); assertEquals(sensor.getName(), "mySensorName"); }
@Test public void testUsingDeprecatedName() throws Exception { MyApp entity = app().addChild(EntitySpec.create(MyApp.class) .configure("oldKey1", "myval")); assertEquals(entity.config().get(MyApp.KEY_1), "myval"); rebind(); Entity newEntity = mgmt().getEntityManager().getEntity(entity.getId()); assertEquals(newEntity.config().get(MyApp.KEY_1), "myval"); // Expect the persisted state of the entity to have used the non-deprecated name. String allLines = getPersistanceFileContents(BrooklynObjectType.ENTITY, newEntity.getId()); assertFalse(allLines.contains("oldKey1"), "contains 'oldKey1', allLines="+allLines); assertTrue(allLines.contains("key1"), "contains 'key1', allLines="+allLines); }
@Test public void testDslSensorSupplier() throws Exception { final Entity app = createAndStartApplication( "services:", "- type: " + BasicApplication.class.getName(), " brooklyn.config:", " " + sensorSupplier1.getName() + ": $brooklyn:sensor(\"mySensorName\")"); rebind(); Entity newEntity = mgmt().getEntityManager().getEntity(app.getId()); Sensor<?> sensor = newEntity.config().get(sensorSupplier1); assertEquals(sensor.getName(), "mySensorName"); }
@Test public void testCreateEntitiesWithDuplicateIdFails() { TestApplication origApp = app; Entity origDeproxiedApp = Entities.deproxy(app); try { TestApplication app2 = ((EntityManagerInternal)entityManager).createEntity(EntitySpec.create(TestApplication.class), Optional.of(app.getId())); Asserts.shouldHaveFailedPreviously("app2="+app2); } catch (IdAlreadyExistsException e) { // success } // Should not have affected the existing app! Entity postApp = entityManager.getEntity(app.getId()); assertSame(postApp, origApp); assertSame(Entities.deproxy(postApp), origDeproxiedApp); }
@Test public void testRebindToMachineEntity() throws Exception { MachineEntity entity = origApp.createAndManageChild(EntitySpec.create(MachineEntity.class) .configure(BrooklynConfigKeys.SKIP_ON_BOX_BASE_DIR_RESOLUTION, true)); origApp.start(ImmutableList.of(origMachine)); EntityAsserts.assertAttributeEqualsEventually(entity, Attributes.SERVICE_STATE_ACTUAL, Lifecycle.RUNNING); rebind(); Entity newEntity = mgmt().getEntityManager().getEntity(entity.getId()); EntityAsserts.assertAttributeEqualsEventually(newEntity, Attributes.SERVICE_STATE_ACTUAL, Lifecycle.RUNNING); }
@Test public void testCreateAndManageChild() { TestEntity result = entity.createAndManageChild(EntitySpec.create(TestEntity.class)); assertIsProxy(result); assertIsProxy(Iterables.get(entity.getChildren(), 0)); assertIsProxy(result.getParent()); assertIsProxy(mgmt.getEntityManager().getEntity(result.getId())); }
@Test(groups={"Integration", "WIP", "Broken"}) public void testJavaWebWithMemberSpecRebind() throws Exception { Reader input = Streams.reader(new ResourceUtils(this).getResourceFromUrl("test-java-web-app-spec-and-db-with-function.yaml")); AssemblyTemplate at = platform.pdp().registerDeploymentPlan(input); Assembly assembly = at.getInstantiator().newInstance().instantiate(at, platform); final Application app = (Application) mgmt().getEntityManager().getEntity(assembly.getId()); Set<Task<?>> tasks = BrooklynTaskTags.getTasksInEntityContext(mgmt().getExecutionManager(), app); for (Task<?> t: tasks) t.blockUntilEnded(); Entities.dumpInfo(app); Application app2 = rebind(app); Assert.assertEquals(app2.getChildren().size(), 2); }
/** as {@link JavaWebAppsIntegrationTest#testWithDbDeploy()} but with rebind */ @Test(groups={"Integration", "WIP", "Broken"}) public void testJavaWebAppDeployAndRebind() throws Exception { Reader input = Streams.reader(new ResourceUtils(this).getResourceFromUrl("java-web-app-and-db-with-function.yaml")); AssemblyTemplate at = platform.pdp().registerDeploymentPlan(input); Assembly assembly = at.getInstantiator().newInstance().instantiate(at, platform); final Application app = (Application) mgmt().getEntityManager().getEntity(assembly.getId()); Set<Task<?>> tasks = BrooklynTaskTags.getTasksInEntityContext(mgmt().getExecutionManager(), app); for (Task<?> t: tasks) t.blockUntilEnded(); Entities.dumpInfo(app); Application app2 = rebind(app); Assert.assertEquals(app2.getChildren().size(), 2); }