@Test public void testMapSize() { Assert.assertTrue(CollectionFunctionals.<String>mapSizeEquals(2).apply(ImmutableMap.of("x", "1", "y", "2"))); Assert.assertFalse(CollectionFunctionals.<String>mapSizeEquals(2).apply(null)); Assert.assertTrue(CollectionFunctionals.mapSizeEquals(0).apply(ImmutableMap.of())); Assert.assertFalse(CollectionFunctionals.mapSizeEquals(0).apply(null)); }
public static <K> Predicate<Map<K,?>> mapEmptyOrNull() { return Predicates.<Map<K,?>>or(Predicates.isNull(), CollectionFunctionals.<K>mapSizeEquals(0)); }
@Test(groups={"Broken"}) public void testFiltersForRunningEntities() { app.start(ImmutableList.of(westChildWithLocation, eastChildWithLocationAndWithPrivateHostname)); publishSensors(2, true, true, true); TestEntity problemChild = Iterables.get(Entities.descendantsAndSelf(app, TestEntity.class), 0); assertAttributeEventually(geoDns, AbstractGeoDnsService.TARGETS, CollectionFunctionals.<String>mapSizeEquals(2)); problemChild.sensors().set(Attributes.SERVICE_STATE_ACTUAL, Lifecycle.ON_FIRE); assertAttributeEventually(geoDns, AbstractGeoDnsService.TARGETS, CollectionFunctionals.<String>mapSizeEquals(1)); problemChild.sensors().set(Attributes.SERVICE_STATE_ACTUAL, Lifecycle.RUNNING); assertAttributeEventually(geoDns, AbstractGeoDnsService.TARGETS, CollectionFunctionals.<String>mapSizeEquals(2)); }
@Test public void testGeoInfoOnParentLocation() { app.start( ImmutableList.of(westChild, eastChild) ); publishSensors(2, true, false, false); assertAttributeEventually(geoDns, AbstractGeoDnsService.TARGETS, CollectionFunctionals.<String>mapSizeEquals(2)); assertIsTarget("West child"); assertIsTarget("East child"); }
@Test public void testGeoInfoOnLocation() { app.start( ImmutableList.of(westChildWithLocation, eastChildWithLocationAndWithPrivateHostname) ); publishSensors(2, true, true, true); assertAttributeEventually(geoDns, AbstractGeoDnsService.TARGETS, CollectionFunctionals.<String>mapSizeEquals(2)); assertIsTarget("West child with location"); assertIsTarget("East child with location"); }
@Test public void testSubscribesToHostname() { geoDns.config().set(GeoDnsTestServiceImpl.ADD_ANYTHING, false); app.start( ImmutableList.of(westChild, eastChildWithLocationAndWithPrivateHostname) ); Assert.assertEquals(geoDns.getTargetHostsByName().size(), 0); publishSensors(2, true, true, true); assertAttributeEventually(geoDns, AbstractGeoDnsService.TARGETS, CollectionFunctionals.<String>mapSizeEquals(2)); Assert.assertEquals(geoDns.getTargetHostsByName().size(), 2); assertIsTarget("West child"); assertIsTarget("East child with location"); }
@Test public void testFiltersEntirelyPrivate() { geoDns.config().set(GeoDnsTestServiceImpl.ADD_ANYTHING, false); app.start( ImmutableList.of(westChild, eastChildWithLocationAndWithPrivateHostname, northChildWithLocation) ); Assert.assertEquals(geoDns.getTargetHostsByName().size(), 0); publishSensors(3, true, true, true); assertAttributeEventually(geoDns, AbstractGeoDnsService.TARGETS, CollectionFunctionals.<String>mapSizeEquals(2)); Assert.assertEquals(geoDns.getTargetHostsByName().size(), 2); assertIsTarget("West child"); assertIsTarget("East child with location"); assertIsNotTarget("North child"); }
@Test public void testChildAddedLate() { app.start( ImmutableList.of(westChild, eastChildWithLocationAndWithPrivateHostname) ); publishSensors(2, true, false, false); assertAttributeEventually(geoDns, AbstractGeoDnsService.TARGETS, CollectionFunctionals.<String>mapSizeEquals(2)); String id3 = fabric.addRegion("test:north"); publishSensors(3, true, false, false); try { assertAttributeEventually(geoDns, AbstractGeoDnsService.TARGETS, CollectionFunctionals.<String>mapSizeEquals(3)); } catch (Throwable e) { log.warn("Did not pick up third entity, targets are "+geoDns.getAttribute(AbstractGeoDnsService.TARGETS)+" (rethrowing): "+e); Exceptions.propagate(e); } assertIsTarget("North child"); log.info("targets: "+geoDns.getTargetHostsByName()); }
@Test public void testCanDisableFilterForRunningEntities() throws Exception { geoDns.config().set(AbstractGeoDnsService.FILTER_FOR_RUNNING, false); app.start(ImmutableList.of(westChildWithLocation, eastChildWithLocationAndWithPrivateHostname)); publishSensors(2, true, true, true); assertAttributeEventually(geoDns, AbstractGeoDnsService.TARGETS, CollectionFunctionals.<String>mapSizeEquals(2)); final Map<String, String> targets = ImmutableMap.copyOf(geoDns.sensors().get(AbstractGeoDnsService.TARGETS)); TestEntity problemChild = Iterables.get(Entities.descendantsAndSelf(app, TestEntity.class), 0); problemChild.sensors().set(Attributes.SERVICE_STATE_ACTUAL, Lifecycle.ON_FIRE); assertAttributeEqualsContinually(geoDns, AbstractGeoDnsService.TARGETS, targets); }
public static final EnricherSpec<?> newEnricherForServiceUpIfNotUpIndicatorsEmpty() { // The function means: if the serviceNotUpIndicators is not null, then serviceNotUpIndicators.size()==0; // otherwise return the default value. return Enrichers.builder() .transforming(SERVICE_NOT_UP_INDICATORS).<Object>publishing(Attributes.SERVICE_UP) .suppressDuplicates(true) .computing( Functionals.<Map<String,?>> ifNotEquals(null).<Object>apply(Functions.forPredicate(CollectionFunctionals.<String>mapSizeEquals(0))) .defaultValue(Entities.UNCHANGED) ) .uniqueTag(DEFAULT_ENRICHER_UNIQUE_TAG) .build(); }
protected void runStopProcessAndRestart(Effector<?> restartEffector, Map<String, ?> args) throws Exception { LocalhostMachineProvisioningLocation loc = app.newLocalhostProvisioningLocation(); SoftwareProcess entity = app.createAndManageChild(newEntitySpec()); // Start the app app.start(ImmutableList.of(loc)); EntityAsserts.assertAttributeEqualsEventually(entity, SoftwareProcess.SERVICE_UP, true); EntityAsserts.assertAttributeEqualsEventually(app, SoftwareProcess.SERVICE_UP, true); // Stop the process Entities.invokeEffector(app, entity, SoftwareProcess.STOP, ImmutableMap.of( StopSoftwareParameters.STOP_MACHINE_MODE.getName(), StopSoftwareParameters.StopMode.NEVER)) .get(); EntityAsserts.assertAttributeEqualsEventually(entity, SoftwareProcess.SERVICE_UP, false); EntityAsserts.assertAttributeEqualsEventually(entity, SoftwareProcess.SERVICE_STATE_ACTUAL, Lifecycle.STOPPED); EntityAsserts.assertAttributeEqualsEventually(entity, SoftwareProcess.SERVICE_PROCESS_IS_RUNNING, false); EntityAsserts.assertAttributeEventually(entity, ServiceStateLogic.SERVICE_NOT_UP_INDICATORS, CollectionFunctionals.<String>mapSizeEquals(1)); // Restart the process Entities.invokeEffector(app, entity, restartEffector, args).get(); EntityAsserts.assertAttributeEqualsEventually(entity, SoftwareProcess.SERVICE_UP, true); EntityAsserts.assertAttributeEqualsEventually(entity, SoftwareProcess.SERVICE_STATE_ACTUAL, Lifecycle.RUNNING); EntityAsserts.assertAttributeEqualsEventually(entity, SoftwareProcess.SERVICE_PROCESS_IS_RUNNING, true); EntityAsserts.assertAttributeEqualsEventually(entity, ServiceStateLogic.SERVICE_NOT_UP_INDICATORS, ImmutableMap.<String, Object>of()); EntityAsserts.assertAttributeEqualsEventually(app, SoftwareProcess.SERVICE_UP, true); EntityAsserts.assertAttributeEqualsEventually(app, SoftwareProcess.SERVICE_STATE_ACTUAL, Lifecycle.RUNNING); } }