OrphanedGroupsByZoneId implements Function<Set<? extends NodeMetadata>, Multimap<String, String>> { public Multimap<String, String> apply(Set<? extends NodeMetadata> deadNodes) { Iterable<? extends NodeMetadata> nodesWithGroup = filter(deadNodes, NodePredicates.hasGroup()); Set<ZoneAndName> zoneAndGroupNames = ImmutableSet.copyOf(filter(transform(nodesWithGroup, new Function<NodeMetadata, ZoneAndName>() { @Override public ZoneAndName apply(NodeMetadata input) { String zoneId = input.getLocation().getScope() == LocationScope.HOST ? input.getLocation() .getParent().getId() : input.getLocation().getId(); return ZoneAndName.fromZoneAndName(zoneId, input.getGroup()); } }), allNodesInGroupTerminated)); Multimap<String, String> zoneToZoneAndGroupNames = Multimaps.transformValues(Multimaps.index(zoneAndGroupNames, ZoneAndName.ZONE_FUNCTION), ZoneAndName.NAME_FUNCTION); return zoneToZoneAndGroupNames; } @Inject protected  OrphanedGroupsByZoneId(ComputeService computeService); @VisibleForTesting  OrphanedGroupsByZoneId(Predicate<ZoneAndName> allNodesInGroupTerminated); Multimap<String, String> apply(Set<? extends NodeMetadata> deadNodes);  }
@Test public void testWhenComputeServiceSaysAllNodesAreDeadBothGroupsAreReturned() { ServerInZone withoutHost = new ServerInZone(new ServerInZoneToNodeMetadataTest().expectedServer(), "az-1.region-a.geo-1"); ServerInZone withHost = new ServerInZone(new ParseServerTest().expected(), "az-1.region-a.geo-1"); ServerInZoneToNodeMetadata converter = new ServerInZoneToNodeMetadata( NovaComputeServiceContextModule.toPortableNodeStatus, locationIndex, Suppliers .<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of()), Suppliers .<Set<? extends Hardware>> ofInstance(ImmutableSet.<Hardware> of()), namingConvention); Set<? extends NodeMetadata> set = ImmutableSet.of(converter.apply(withHost), converter.apply(withoutHost)); assertEquals(new OrphanedGroupsByZoneId(Predicates.<ZoneAndName> alwaysTrue()).apply(set), ImmutableMultimap .<String, String> builder().putAll("az-1.region-a.geo-1", "sample", "test").build()); }
@Test public void testWhenComputeServiceSaysAllNodesAreDeadNoGroupsAreReturned() { ServerInZone withoutHost = new ServerInZone(new ServerInZoneToNodeMetadataTest().expectedServer(), "az-1.region-a.geo-1"); ServerInZone withHost = new ServerInZone(new ParseServerTest().expected(), "az-1.region-a.geo-1"); ServerInZoneToNodeMetadata converter = new ServerInZoneToNodeMetadata( NovaComputeServiceContextModule.toPortableNodeStatus, locationIndex, Suppliers .<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of()), Suppliers .<Set<? extends Hardware>> ofInstance(ImmutableSet.<Hardware> of()), namingConvention); Set<? extends NodeMetadata> set = ImmutableSet.of(converter.apply(withHost), converter.apply(withoutHost)); assertEquals(new OrphanedGroupsByZoneId(Predicates.<ZoneAndName> alwaysFalse()).apply(set), ImmutableMultimap .<String, String> of()); }