From 9237f1a4061ce3121ce6e821532c473caf37a509 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Tue, 1 Jan 2013 22:16:28 -0800 Subject: [PATCH] eliminated errors when multiple groups have the same name --- .../org/jclouds/ec2/domain/SecurityGroup.java | 6 +-- .../services/SecurityGroupClientLiveTest.java | 48 ++++++++++++------- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/domain/SecurityGroup.java b/apis/ec2/src/main/java/org/jclouds/ec2/domain/SecurityGroup.java index a52ed5b900..34cade9369 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/domain/SecurityGroup.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/domain/SecurityGroup.java @@ -204,7 +204,7 @@ public class SecurityGroup extends ForwardingSet { @Override public int hashCode() { - return Objects.hashCode(region, id, name, ownerId, description, ipPermissions); + return Objects.hashCode(region, id, name, ownerId); } @Override @@ -217,9 +217,7 @@ public class SecurityGroup extends ForwardingSet { return Objects.equal(this.region, that.region) && Objects.equal(this.id, that.id) && Objects.equal(this.name, that.name) - && Objects.equal(this.ownerId, that.ownerId) - && Objects.equal(this.description, that.description) - && Objects.equal(this.ipPermissions, that.ipPermissions); + && Objects.equal(this.ownerId, that.ownerId); } protected ToStringHelper string() { diff --git a/apis/ec2/src/test/java/org/jclouds/ec2/services/SecurityGroupClientLiveTest.java b/apis/ec2/src/test/java/org/jclouds/ec2/services/SecurityGroupClientLiveTest.java index 3cfa7738e2..9bb129bd14 100644 --- a/apis/ec2/src/test/java/org/jclouds/ec2/services/SecurityGroupClientLiveTest.java +++ b/apis/ec2/src/test/java/org/jclouds/ec2/services/SecurityGroupClientLiveTest.java @@ -18,10 +18,15 @@ */ package org.jclouds.ec2.services; +import static com.google.common.base.Predicates.compose; +import static com.google.common.base.Predicates.in; +import static com.google.common.collect.Iterables.all; +import static com.google.common.collect.Iterables.getLast; +import static com.google.common.collect.Iterables.getOnlyElement; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertTrue; -import java.util.Iterator; import java.util.Set; import org.jclouds.compute.internal.BaseComputeServiceContextLiveTest; @@ -34,10 +39,11 @@ import org.jclouds.ec2.domain.UserIdGroupPair; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; +import com.google.common.base.Function; import com.google.common.base.Predicate; +import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Iterables; /** * Tests behavior of {@code SecurityGroupClient} @@ -67,11 +73,19 @@ public class SecurityGroupClientLiveTest extends BaseComputeServiceContextLiveTe Set allResults = client.describeSecurityGroupsInRegion(region); assertNotNull(allResults); if (allResults.size() >= 1) { - SecurityGroup group = Iterables.getLast(allResults); - Set result = client.describeSecurityGroupsInRegion(region, group.getName()); - assertNotNull(result); - SecurityGroup compare = Iterables.getLast(result); - assertEquals(compare, group); + final SecurityGroup group = getLast(allResults); + // in case there are multiple groups with the same name, which is the case with VPC + ImmutableSet expected = FluentIterable.from(allResults) + .filter(new Predicate() { + @Override + public boolean apply(SecurityGroup in) { + return group.getName().equals(in.getName()); + } + }).toSet(); + ImmutableSet result = ImmutableSet.copyOf(client.describeSecurityGroupsInRegion(region, + group.getName())); + // the above command has a chance of returning less groups than the original + assertTrue(expected.containsAll(result)); } } } @@ -197,7 +211,7 @@ public class SecurityGroupClientLiveTest extends BaseComputeServiceContextLiveTe public void run() { try { Set oneResult = client.describeSecurityGroupsInRegion(null, group); - assert Iterables.all(Iterables.getOnlyElement(oneResult).getIpPermissions(), permission) : permission + assert all(getOnlyElement(oneResult), permission) : permission + ": " + oneResult; } catch (Exception e) { throw new AssertionError(e); @@ -220,7 +234,7 @@ public class SecurityGroupClientLiveTest extends BaseComputeServiceContextLiveTe assertNotNull(oneResult); assertEquals(oneResult.size(), 1); SecurityGroup listPair = oneResult.iterator().next(); - assertEquals(listPair.getIpPermissions().size(), 0); + assertEquals(listPair.size(), 0); } catch (Exception e) { throw new AssertionError(e); } @@ -230,15 +244,15 @@ public class SecurityGroupClientLiveTest extends BaseComputeServiceContextLiveTe protected void ensureGroupsExist(String group1Name, String group2Name) { Set twoResults = client.describeSecurityGroupsInRegion(null, group1Name, group2Name); assertNotNull(twoResults); - assertEquals(twoResults.size(), 2); - Iterator iterator = twoResults.iterator(); - SecurityGroup listPair1 = iterator.next(); - assertEquals(listPair1.getName(), group1Name); - assertEquals(listPair1.getDescription(), group1Name); + assertTrue(twoResults.size() >= 2);// in VPC could be multiple groups with the same name - SecurityGroup listPair2 = iterator.next(); - assertEquals(listPair2.getName(), group2Name); - assertEquals(listPair2.getDescription(), group2Name); + assertTrue(all(twoResults, compose(in(ImmutableSet.of(group1Name, group2Name)), + new Function() { + @Override + public String apply(SecurityGroup in) { + return in.getName(); + } + }))); } private static final int INCONSISTENCY_WINDOW = 5000;