Fix TestDistributedSet to behave more like real one.

- fix test relying on incorrect behavior

Change-Id: Ib80bf9789a312c185794273d916d91989d2ae5cd
diff --git a/core/api/src/test/java/org/onosproject/store/service/TestDistributedSet.java b/core/api/src/test/java/org/onosproject/store/service/TestDistributedSet.java
index 79c108b..49f28f2 100644
--- a/core/api/src/test/java/org/onosproject/store/service/TestDistributedSet.java
+++ b/core/api/src/test/java/org/onosproject/store/service/TestDistributedSet.java
@@ -70,18 +70,24 @@
 
     @Override
     public CompletableFuture<Boolean> add(E element) {
-        SetEvent<E> event =
-                new SetEvent<>(setName, SetEvent.Type.ADD, element);
-        notifyListeners(event);
-        return CompletableFuture.completedFuture(set.add(element));
+        boolean updated = set.add(element);
+        if (updated) {
+            SetEvent<E> event =
+                    new SetEvent<>(setName, SetEvent.Type.ADD, element);
+            notifyListeners(event);
+        }
+        return CompletableFuture.completedFuture(updated);
     }
 
     @Override
     public CompletableFuture<Boolean> remove(E element) {
-        SetEvent<E> event =
-                new SetEvent<>(setName, SetEvent.Type.REMOVE, element);
-        notifyListeners(event);
-        return CompletableFuture.completedFuture(set.remove(element));
+        boolean updated = set.remove(element);
+        if (updated) {
+            SetEvent<E> event =
+                    new SetEvent<>(setName, SetEvent.Type.REMOVE, element);
+            notifyListeners(event);
+        }
+        return CompletableFuture.completedFuture(updated);
     }
 
     @Override
@@ -107,8 +113,10 @@
 
     @Override
     public CompletableFuture<Boolean> addAll(Collection<? extends E> c) {
-        c.forEach(this::add);
-        return CompletableFuture.completedFuture(true);
+        return c.stream()
+                .map(this::add)
+                .reduce(CompletableFuture.completedFuture(false),
+                        (l, r) -> l.thenCombine(r, Boolean::logicalOr));
     }
 
     @Override
@@ -118,15 +126,19 @@
 
     @Override
     public CompletableFuture<Boolean> retainAll(Collection<? extends E> c) {
-        Set notInSet2;
-        notInSet2 = Sets.difference(set, (Set<?>) c);
+        Set<? extends E> s = (c instanceof Set) ?
+                             (Set<? extends E>) c :
+                             ImmutableSet.copyOf(c);
+        Set<E> notInSet2 = Sets.difference(set, s);
         return removeAll(ImmutableSet.copyOf(notInSet2));
     }
 
     @Override
     public CompletableFuture<Boolean> removeAll(Collection<? extends E> c) {
-        c.forEach(this::remove);
-        return CompletableFuture.completedFuture(true);
+        return c.stream()
+                .map(this::remove)
+                .reduce(CompletableFuture.completedFuture(false),
+                        (l, r) -> l.thenCombine(r, Boolean::logicalOr));
     }
 
     @Override
@@ -154,8 +166,8 @@
      *
      * @return Builder
      **/
-    public static Builder builder() {
-        return new Builder();
+    public static <E> Builder<E> builder() {
+        return new Builder<>();
     }
 
     /**
@@ -166,7 +178,7 @@
     public static class Builder<E> extends DistributedSetBuilder<E> {
         @Override
         public AsyncDistributedSet<E> build() {
-            return new TestDistributedSet(name());
+            return new TestDistributedSet<>(name());
         }
     }
 }
diff --git a/incubator/net/src/test/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkManagerTest.java b/incubator/net/src/test/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkManagerTest.java
index 9ea8916..1f1624e 100644
--- a/incubator/net/src/test/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkManagerTest.java
+++ b/incubator/net/src/test/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkManagerTest.java
@@ -167,8 +167,7 @@
         assertTrue("The tenantId set should be empty.", tenantIdCollection.isEmpty());
 
         // Validate that the events were all received in the correct order.
-        validateEvents(VirtualNetworkEvent.Type.TENANT_UNREGISTERED,
-                       VirtualNetworkEvent.Type.TENANT_REGISTERED,
+        validateEvents(VirtualNetworkEvent.Type.TENANT_REGISTERED,
                        VirtualNetworkEvent.Type.TENANT_REGISTERED,
                        VirtualNetworkEvent.Type.TENANT_UNREGISTERED,
                        VirtualNetworkEvent.Type.TENANT_UNREGISTERED);
@@ -302,7 +301,7 @@
         assertTrue("The virtual device set should be empty.", virtualDevices1.isEmpty());
 
         // Validate that the events were all received in the correct order.
-        validateEvents((Enum[]) expectedEventTypes.toArray(
+        validateEvents(expectedEventTypes.toArray(
                 new VirtualNetworkEvent.Type[expectedEventTypes.size()]));
     }
 
@@ -631,7 +630,7 @@
         assertTrue("The virtual port set should be empty.", virtualPorts.isEmpty());
 
         // Validate that the events were all received in the correct order.
-        validateEvents((Enum[]) expectedEventTypes.toArray(
+        validateEvents(expectedEventTypes.toArray(
                 new VirtualNetworkEvent.Type[expectedEventTypes.size()]));
     }