### Question:
NullEqualToIsParentOrIsGrandparentOfCurrentLocation implements Predicate<ComputeMetadata> { @Override public boolean apply(ComputeMetadata input) { Location current = locationSupplier.get(); if (current == null) return true; if (input.getLocation() == null) return true; Location parent = current.getParent(); checkArgument( parent != null || current.getScope() == LocationScope.PROVIDER, "only locations of scope PROVIDER can have a null parent; arg: %s", current); checkState( input.getLocation().getParent() != null || input.getLocation().getScope() == LocationScope.PROVIDER, "only locations of scope PROVIDER can have a null parent; input: %s", input.getLocation()); Builder<Predicate<Location>> predicates = ImmutableSet.builder(); predicates.add(equalTo(current)); if (parent != null) { predicates.add(equalTo(parent)); Location grandparent = parent.getParent(); if (grandparent != null) predicates.add(equalTo(grandparent)); } return Predicates.or(predicates.build()).apply(input.getLocation()); } NullEqualToIsParentOrIsGrandparentOfCurrentLocation(Supplier<Location> locationSupplier); @Override boolean apply(ComputeMetadata input); @Override String toString();  }### Answer:
@Test public void testReturnTrueWhenISpecifyARegionAndInputLocationIsProvider() { NullEqualToIsParentOrIsGrandparentOfCurrentLocation predicate = new NullEqualToIsParentOrIsGrandparentOfCurrentLocation(Suppliers.ofInstance(region)); Hardware md = new HardwareBuilder().id("foo").location(provider).build(); assertTrue(predicate.apply(md)); }
@Test public void testReturnFalseWhenISpecifyALocationWhichTheSameScopeByNotEqualToInputLocationAndParentsAreNull() { NullEqualToIsParentOrIsGrandparentOfCurrentLocation predicate = new NullEqualToIsParentOrIsGrandparentOfCurrentLocation(Suppliers.ofInstance(region)); Hardware md = new HardwareBuilder().id("foo").location(otherRegion).build(); assertFalse(predicate.apply(md)); }
@Test(expectedExceptions = IllegalStateException.class) public void testThrowIllegalStateExceptionWhenInputIsAnOrphanedRegion() { NullEqualToIsParentOrIsGrandparentOfCurrentLocation predicate = new NullEqualToIsParentOrIsGrandparentOfCurrentLocation(Suppliers.ofInstance(region)); Hardware md = new HardwareBuilder().id("foo").location(orphanedRegion).build(); predicate.apply(md); }
@Test(expectedExceptions = IllegalStateException.class) public void testThrowIllegalStateExceptionWhenInputIsAnOrphanedZone() { NullEqualToIsParentOrIsGrandparentOfCurrentLocation predicate = new NullEqualToIsParentOrIsGrandparentOfCurrentLocation(Suppliers.ofInstance(region)); Hardware md = new HardwareBuilder().id("foo").location(orphanedZone).build(); predicate.apply(md); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testThrowIllegalArgumentExceptionWhenWhenISpecifyAnOrphanedRegion() { NullEqualToIsParentOrIsGrandparentOfCurrentLocation predicate = new NullEqualToIsParentOrIsGrandparentOfCurrentLocation(Suppliers.ofInstance(orphanedRegion)); Hardware md = new HardwareBuilder().id("foo").location(region).build(); predicate.apply(md); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testThrowIllegalArgumentExceptionWhenWhenISpecifyAnOrphanedZone() { NullEqualToIsParentOrIsGrandparentOfCurrentLocation predicate = new NullEqualToIsParentOrIsGrandparentOfCurrentLocation(Suppliers.ofInstance(orphanedZone)); Hardware md = new HardwareBuilder().id("foo").location(region).build(); predicate.apply(md); }