### Question:
AWSEC2CreateSecurityGroupIfNeeded extends CacheLoader<RegionAndName, String> { @Override public String load(RegionAndName from) { RegionNameAndIngressRules realFrom = RegionNameAndIngressRules.class.cast(from); return createSecurityGroupInRegion(from.getRegion(), from.getName(), realFrom.getVpcId(), realFrom.getPorts()); } @Inject  AWSEC2CreateSecurityGroupIfNeeded(AWSEC2Api ec2Api,
                                            @Named("SECGROUP_NAME_TO_ID") Function<String, String> groupNameToId,
                                            @Named("SECURITY") Predicate<RegionAndName> securityGroupEventualConsistencyDelay);  AWSEC2CreateSecurityGroupIfNeeded(AWSSecurityGroupApi securityApi,
                                            @Named("SECGROUP_NAME_TO_ID") Function<String, String> groupNameToId,
                                            @Named("SECURITY") Predicate<RegionAndName> securityGroupEventualConsistencyDelay); @Override String load(RegionAndName from);  }### Answer:
@SuppressWarnings("unchecked") @Test public void testWhenPort22AndToItselfAuthorizesIngressOnce() throws ExecutionException { AWSSecurityGroupApi client = createMock(AWSSecurityGroupApi.class); Predicate<RegionAndName> tester = Predicates.alwaysTrue(); SecurityGroup group = createNiceMock(SecurityGroup.class); Set<SecurityGroup> groups = ImmutableSet.<SecurityGroup> of(group); EC2SecurityGroupIdFromName groupIdFromName = createMock(EC2SecurityGroupIdFromName.class); ImmutableSet.Builder<IpPermission> permissions = ImmutableSet.builder(); permissions.add(IpPermission.builder() .fromPort(22) .toPort(22) .ipProtocol(IpProtocol.TCP) .cidrBlock("0.0.0.0/0") .build()); permissions.add(IpPermission.builder() .fromPort(0) .toPort(65535) .ipProtocol(IpProtocol.TCP) .tenantIdGroupNamePair("ownerId", "sg-123456") .build()); permissions.add(IpPermission.builder() .fromPort(0) .toPort(65535) .ipProtocol(IpProtocol.UDP) .tenantIdGroupNamePair("ownerId", "sg-123456") .build()); expect( client.createSecurityGroupInRegionAndReturnId("region", "group", "group", new CreateSecurityGroupOptions().vpcId("vpc"))).andReturn("sg-123456"); expect(group.getOwnerId()).andReturn("ownerId"); client.authorizeSecurityGroupIngressInRegion("region", "sg-123456", permissions.build()); expect(client.describeSecurityGroupsInRegionById("region", "sg-123456")) .andReturn(Set.class.cast(groups)); replay(client); replay(group); replay(groupIdFromName); AWSEC2CreateSecurityGroupIfNeeded function = new AWSEC2CreateSecurityGroupIfNeeded(client, groupIdFromName, tester); assertEquals("sg-123456", function.load(new RegionNameAndIngressRules("region", "group", new int[] { 22 }, true, "vpc"))); verify(client); verify(group); verify(groupIdFromName); }