private Object invokeEffectorNamed(String effectorName, ConfigBag params) { LOG.info("{} invoking effector on {}, effector={}, parameters={}", new Object[]{this, entity(), effectorName, params}); Maybe<Effector<?>> effector = entity().getEntityType().getEffectorByName(effectorName); if (effector.isAbsent()) { throw new IllegalStateException("Cannot find effector " + effectorName); } return entity().invoke(effector.get(), params.getAllConfig()).getUnchecked(); }
@Test public void testEffectorBodyAddedImplicitlyButBodylessSignatureInvoked() throws Exception { EntityInternal doubler = app.createAndManageChild(EntitySpec.create(TestEntity.class)); // add it doubler.getMutableEntityType().addEffector(DOUBLE_1); // invoke it, but using something with equivalent name (and signature -- though only name is used currently) // ensures that the call picks up the body by looking in the actual entity Assert.assertEquals(doubler.invoke(DOUBLE_BODYLESS, MutableMap.of("numberToDouble", 3)).get(), (Integer)6); }
@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 testEffectorBodyAdded() throws Exception { EntityInternal doubler = app.createAndManageChild(EntitySpec.create(TestEntity.class)); // not yet present Assert.assertNull( doubler.getEffector("double") ); // add it doubler.getMutableEntityType().addEffector(DOUBLE_BODYLESS, new EffectorBody<Integer>() { @Override public Integer call(ConfigBag parameters) { int input = (Integer)parameters.getStringKey("numberToDouble"); return queue(times(input, 2)).getUnchecked(); } }); // now it is present Assert.assertNotNull( doubler.getEffector("double") ); Assert.assertEquals(doubler.invoke(DOUBLE_BODYLESS, MutableMap.of("numberToDouble", 3)).get(), (Integer)6); }
private void execRelease(final JcloudsLocation loc, final JcloudsSshMachineLocation machine) throws Exception { EffectorBody<Void> effectorBody = new EffectorBody<Void>() { @Override public Void call(ConfigBag parameters) { loc.releasePortForwarding(machine); return null; } }; Effector<Void> effector = new EffectorAndBody<Void>("myeffector", Void.class, ImmutableList.<ParameterType<?>>of(), "", new EffectorBodyTaskFactory<Void>(effectorBody)); EntityInternal entity = (EntityInternal) app.createAndManageChild(EntitySpec.create(BasicEntity.class)); entity.getMutableEntityType().addEffector(effector); entity.invoke(effector, ImmutableMap.<String, Object>of()).get(); } }
@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); } }