FindSecurityGroupOrCreate extends CacheLoader<ZoneAndName, SecurityGroupInZone> { @Override public SecurityGroupInZone load(ZoneAndName in) { AtomicReference<ZoneAndName> securityGroupInZoneRef = Atomics.newReference(checkNotNull(in, "zoneSecurityGroupNameAndPorts")); if (returnSecurityGroupExistsInZone.apply(securityGroupInZoneRef)) { return returnExistingSecurityGroup(securityGroupInZoneRef); } else { return createNewSecurityGroup(in); } } @Inject  FindSecurityGroupOrCreate(
            @Named(TIMEOUT_SECURITYGROUP_PRESENT) Predicate<AtomicReference<ZoneAndName>> returnSecurityGroupExistsInZone,
            Function<ZoneSecurityGroupNameAndPorts, SecurityGroupInZone> groupCreator); @Override SecurityGroupInZone load(ZoneAndName in); @Override String toString();  }
@Test public void testWhenNotFoundCreatesANewSecurityGroup() throws Exception { Predicate<AtomicReference<ZoneAndName>> returnSecurityGroupExistsInZone = Predicates.alwaysFalse(); SecurityGroupInZone securityGroupInZone = createMock(SecurityGroupInZone.class); ZoneSecurityGroupNameAndPorts input = new ZoneSecurityGroupNameAndPorts("zone", "groupName", ImmutableSet .<Integer> of(22, 8080)); Function<ZoneSecurityGroupNameAndPorts, SecurityGroupInZone> groupCreator = Functions.forMap(ImmutableMap .<ZoneSecurityGroupNameAndPorts, SecurityGroupInZone> of(input, securityGroupInZone)); FindSecurityGroupOrCreate parser = new FindSecurityGroupOrCreate( returnSecurityGroupExistsInZone, groupCreator); assertEquals(parser.load(input), securityGroupInZone); }
@Test public void testWhenFoundReturnsSecurityGroupFromAtomicReferenceValueUpdatedDuringPredicateCheck() throws Exception { final SecurityGroupInZone securityGroupInZone = createMock(SecurityGroupInZone.class); Predicate<AtomicReference<ZoneAndName>> returnSecurityGroupExistsInZone = new Predicate<AtomicReference<ZoneAndName>>(){ @Override public boolean apply(AtomicReference<ZoneAndName> input) { input.set(securityGroupInZone); return true; } }; ZoneAndName input = ZoneAndName.fromZoneAndName("zone", "groupName"); Function<ZoneSecurityGroupNameAndPorts, SecurityGroupInZone> groupCreator = new Function<ZoneSecurityGroupNameAndPorts, SecurityGroupInZone>() { @Override public SecurityGroupInZone apply(ZoneSecurityGroupNameAndPorts input) { fail(); return null; } }; FindSecurityGroupOrCreate parser = new FindSecurityGroupOrCreate( returnSecurityGroupExistsInZone, groupCreator); assertEquals(parser.load(input), securityGroupInZone); }
@Test(expectedExceptions = IllegalStateException.class) public void testWhenFoundPredicateMustUpdateAtomicReference() throws Exception { Predicate<AtomicReference<ZoneAndName>> returnSecurityGroupExistsInZone = Predicates.alwaysTrue(); ZoneAndName input = ZoneAndName.fromZoneAndName("zone", "groupName"); Function<ZoneSecurityGroupNameAndPorts, SecurityGroupInZone> groupCreator = new Function<ZoneSecurityGroupNameAndPorts, SecurityGroupInZone>() { @Override public SecurityGroupInZone apply(ZoneSecurityGroupNameAndPorts input) { fail(); return null; } }; FindSecurityGroupOrCreate parser = new FindSecurityGroupOrCreate( returnSecurityGroupExistsInZone, groupCreator); parser.load(input); }
@Test(expectedExceptions = IllegalStateException.class) public void testWhenNotFoundInputMustBeZoneSecurityGroupNameAndPorts() throws Exception { Predicate<AtomicReference<ZoneAndName>> returnSecurityGroupExistsInZone = Predicates.alwaysFalse(); ZoneAndName input = ZoneAndName.fromZoneAndName("zone", "groupName"); Function<ZoneSecurityGroupNameAndPorts, SecurityGroupInZone> groupCreator = new Function<ZoneSecurityGroupNameAndPorts, SecurityGroupInZone>() { @Override public SecurityGroupInZone apply(ZoneSecurityGroupNameAndPorts input) { fail(); return null; } }; FindSecurityGroupOrCreate parser = new FindSecurityGroupOrCreate( returnSecurityGroupExistsInZone, groupCreator); parser.load(input); }