@Override public BrooklynObject apply(BrooklynObject obj) { // entities must be deproxied if (obj instanceof Entity) obj = Entities.deproxy((Entity)obj); return obj; } }));
private Entity deproxyIfNecessary(Entity e) { return (e instanceof AbstractEntity) ? e : Entities.deproxy(e); }
private Map<String, EntityMemento> toMementos(Iterable<? extends Entity> entities) { Map<String, EntityMemento> result = Maps.newLinkedHashMap(); for (Entity entity : entities) { result.put(entity.getId(), (EntityMemento) MementosGenerators.newBasicMemento(Entities.deproxy(entity))); } return result; } }
@Test public void testFindMaster() { List<Entity> nodes = makeTwoNodes(); Assert.assertEquals(((BrooklynClusterImpl)Entities.deproxy(cluster)).findMasterChild(), nodes.get(0)); }
@Test public void testAddConfigKeyThroughEntity() throws Exception{ Entities.deproxy(entity).configure(ImmutableList.<ConfigKey<?>>of(NEW_CONFIG)); assertEquals(entity.getEntityType().getConfigKeys(), ImmutableSet.builder().addAll(DEFAULT_CONFIG_KEYS).add(NEW_CONFIG).build()); assertEventuallyListenerEventsEqual(ImmutableList.of(BasicSensorEvent.ofUnchecked(AbstractEntity.CONFIG_KEY_ADDED, entity, NEW_CONFIG))); }
@Test public void testCreatesProxy() throws Exception { EntitySpec<Application> spec = EntitySpec.create(Application.class).impl(TestApplicationImpl.class); Application app = factory.createEntity(spec, Optional.absent()); Application proxy = factory.createEntityProxy(spec, app); TestApplicationImpl deproxied = (TestApplicationImpl) Entities.deproxy(proxy); assertTrue(app instanceof TestApplicationImpl, "app="+app); assertFalse(proxy instanceof TestApplicationImpl, "proxy="+proxy); assertTrue(proxy instanceof EntityProxy, "proxy="+proxy); assertTrue(proxy instanceof Application, "proxy="+proxy); assertTrue(deproxied instanceof TestApplicationImpl, "deproxied="+deproxied); assertFalse(deproxied instanceof EntityProxy, "deproxied="+deproxied); }
/** * Stop the given underlying entity, but without our entity instance being told! */ protected void killEntityBehindBack(Entity tokill) throws Exception { ((SoftwareProcessImpl) Entities.deproxy(tokill)).getDriver().stop(); // old method of doing this did some dodgy legacy rebind and failed due to too many dangling refs; above is better in any case // but TODO we should have some rebind tests for these! } /**
/** * Stop the given underlying entity, but without our entity instance being told! */ protected void killEntityBehindBack(Entity tokill) throws Exception { ((SoftwareProcessDriver)((DriverDependentEntity<?>) Entities.deproxy(tokill)).getDriver()).stop(); // old method of doing this did some dodgy legacy rebind and failed due to too many dangling refs; above is better in any case // but TODO we should have some rebind tests for these! }
private String getPidFile(VanillaSoftwareProcess proc) { VanillaSoftwareProcessImpl impl = (VanillaSoftwareProcessImpl)Entities.deproxy(proc); return ((VanillaSoftwareProcessSshDriver)impl.getDriver()).getPidFile(); }
@Test public void testRebindChildSimple() throws Exception { TestEntity c1 = origApp.addChild(EntitySpec.create(TestEntity.class)); AbstractEntity c1r = Entities.deproxy(c1); doPartialRebindOfIds(c1.getId()); BrooklynObject c2 = origManagementContext.lookup(c1.getId()); AbstractEntity c2r = Entities.deproxy((Entity)c2); Assert.assertTrue(c2 == c1, "Proxy instance should be the same: "+c1+" / "+c2); Assert.assertFalse(c2r == c1r, "Real instance should NOT be the same: "+c1r+" / "+c2r); }
@BeforeMethod(alwaysRun=true) @Override public void setUp() throws Exception { super.setUp(); entity = app.addChild(EntitySpec.create(TestEntity.class)); entityImpl = (TestEntityImpl) Entities.deproxy(entity); }
@BeforeMethod(alwaysRun=true) public void setUp() { app = TestApplication.Factory.newManagedInstanceForTests(); TestEntity entity = app.addChild(EntitySpec.create(TestEntity.class)); entityImpl = (TestEntityImpl) Entities.deproxy(entity); map = new AttributeMap(entityImpl, Collections.synchronizedMap(MutableMap.<Collection<String>,Object>of())); executor = Executors.newCachedThreadPool(); }
@Test public void stoppingTheClusterStopsTheEntity() throws Exception { DynamicCluster cluster = app.createAndManageChild(EntitySpec.create(DynamicCluster.class) .configure("memberSpec", EntitySpec.create(BlockingEntity.class)) .configure("initialSize", 1)); cluster.start(ImmutableList.of(loc)); TestEntity entity = (TestEntity) Iterables.get(cluster.getMembers(), 0); TestEntityImpl deproxiedEntity = (TestEntityImpl) Entities.deproxy(entity); assertEquals(entity.getCounter().get(), 1); cluster.stop(); // Need to use deproxiedEntity, because entity-proxy would intercept method call, and it // would fail because the entity has been unmanaged. assertEquals(deproxiedEntity.getCounter().get(), 0); }
@Test public void testRebindParentSimple() throws Exception { TestEntity c1 = origApp.addChild(EntitySpec.create(TestEntity.class)); AbstractEntity origAppr = Entities.deproxy(origApp); doPartialRebindOfIds(origApp.getId()); BrooklynObject app2 = origManagementContext.lookup(origApp.getId()); AbstractEntity app2r = Entities.deproxy((Entity)app2); Assert.assertTrue(app2 == origApp, "Proxy instance should be the same: "+app2+" / "+origApp); Assert.assertFalse(app2r == origAppr, "Real instance should NOT be the same: "+app2r+" / "+origAppr); Assert.assertTrue(c1.getManagementSupport().isDeployed()); // check that child of parent is not a new unmanaged entity Entity c1b = origApp.getChildren().iterator().next(); Assert.assertTrue(c1.getManagementSupport().isDeployed()); Assert.assertTrue( ((EntityInternal)c1b).getManagementSupport().isDeployed(), "Not deployed: "+c1b ); }
@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); }
private void runTestGeneratesCorrectDownloadUrl(String version, String expectedUrl) throws Exception { // TODO Using BrooklynNodeImpl directly, because want to instantiate a BroolynNodeSshDriver. // Really want to make that easier to test, without going through "wrong" code path for creating entity. BrooklynNode entity = app.addChild(EntitySpec.create(BrooklynNode.class) .configure(BrooklynNode.SUGGESTED_VERSION, version)); BrooklynNodeImpl entityImpl = (BrooklynNodeImpl) Entities.deproxy(entity); ConfigToAttributes.apply(entity); BrooklynNodeSshDriver driver = new BrooklynNodeSshDriver(entityImpl, loc); DownloadResolver resolver = Entities.newDownloader(driver); List<String> urls = resolver.getTargets(); System.out.println("urls=" + urls); assertTrue(urls.contains(expectedUrl), "urls=" + urls); }
private MyEntity newDeproxiedEntity(Map<?, ?> config) { Entity result = app.addChild(EntitySpec.create(Entity.class).impl(MyEntity.class) .configure(config)); return (MyEntity) Entities.deproxy(result); }
@Test public void testHappyPath() { TestEntity a = app.createAndManageChild(EntitySpec.create(TestEntity.class)); ProxyEffector proxyEffector = new ProxyEffector(ImmutableMap.of( AddEffector.EFFECTOR_NAME, "proxy-effector", ProxyEffector.TARGET_ENTITY, a, ProxyEffector.TARGET_EFFECTOR_NAME, "identityEffector")); // BasicEntity doesn't have an identityEffector. EntityInternal b = Entities.deproxy(app.createAndManageChild(EntitySpec.create(BasicEntity.class) .addInitializer(proxyEffector))); Object output = b.invoke(b.getEffector("proxy-effector"), ImmutableMap.of("arg", "value")) .getUnchecked(Duration.ONE_MINUTE); assertEquals(output, "value"); }
@Test public void testThrowsIfTargetEffectorDoesntExist() { TestEntity a = app.createAndManageChild(EntitySpec.create(TestEntity.class)); ProxyEffector proxyEffector = new ProxyEffector(ImmutableMap.of( AddEffector.EFFECTOR_NAME, "proxy-effector", ProxyEffector.TARGET_ENTITY, a, ProxyEffector.TARGET_EFFECTOR_NAME, "kajnfksjdnfkjsdnf")); EntityInternal b = Entities.deproxy(app.createAndManageChild(EntitySpec.create(BasicEntity.class) .addInitializer(proxyEffector))); try { b.invoke(b.getEffector("proxy-effector"), ImmutableMap.of("arg", "value")) .getUnchecked(Duration.ONE_MINUTE); Asserts.shouldHaveFailedPreviously("expected exception when invoking effector that does not exist"); } catch (Exception e) { Asserts.expectedFailureOfType(e, NoSuchElementException.class); } }
protected void runBuildCreateRoles(Map<String, Map<String, ?>> roles, String expected) throws Exception { stubbedMachine = app.getManagementContext().getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class) .configure("sshToolClass", RecordingSshTool.class.getName()) .configure("address", "1.2.3.4")); PostgreSqlNode pgsql = app.createAndManageChild(EntitySpec.create(PostgreSqlNode.class) .impl(PostgreSqlNodeForTestingImpl.class) .configure(PostgreSqlNode.ROLES, roles) .location(stubbedMachine)); PostgreSqlNodeForTestingImpl impl = (PostgreSqlNodeForTestingImpl) Entities.deproxy(pgsql); PostgreSqlSshDriver driver = (PostgreSqlSshDriver) impl.doInitDriver(stubbedMachine); String query = driver.buildCreateRolesQuery(); assertEquals(query, expected); }