Brian Stanke
Committed by Gerrit Code Review

ONOS-4075 TestDistributedSet implementation and Junit tests.

Change-Id: I77ea0beb5a1adbdbf8632c599b631d728455aa9a
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.store.service;
import java.util.Collection;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
/**
* Testing adapter for the distributed set.
*/
public class DistributedSetAdapter<E> implements AsyncDistributedSet<E> {
@Override
public CompletableFuture<Void> addListener(SetEventListener<E> listener) {
return null;
}
@Override
public CompletableFuture<Void> removeListener(SetEventListener<E> listener) {
return null;
}
@Override
public CompletableFuture<Boolean> add(E element) {
return null;
}
@Override
public CompletableFuture<Boolean> remove(E element) {
return null;
}
@Override
public CompletableFuture<Integer> size() {
return null;
}
@Override
public CompletableFuture<Boolean> isEmpty() {
return null;
}
@Override
public CompletableFuture<Void> clear() {
return null;
}
@Override
public CompletableFuture<Boolean> contains(E element) {
return null;
}
@Override
public CompletableFuture<Boolean> addAll(Collection<? extends E> c) {
return null;
}
@Override
public CompletableFuture<Boolean> containsAll(Collection<? extends E> c) {
return null;
}
@Override
public CompletableFuture<Boolean> retainAll(Collection<? extends E> c) {
return null;
}
@Override
public CompletableFuture<Boolean> removeAll(Collection<? extends E> c) {
return null;
}
@Override
public CompletableFuture<? extends Set<E>> getAsImmutableSet() {
return null;
}
@Override
public String name() {
return null;
}
}
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.store.service;
import com.google.common.collect.Lists;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onlab.junit.TestTools;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static org.junit.Assert.*;
/**
* Junit test class for TestDistributedSet.
*/
public class DistributedSetTest {
protected InternalSetListener listener = new InternalSetListener();
private static final String SETNAME = "set1";
private TestDistributedSet<String> set1;
private Set<String> set2;
/**
* Test setup before each test.
*/
@Before
public void setup() {
set1 = new TestDistributedSet<>(SETNAME);
set2 = new HashSet<>();
set1.addListener(listener);
}
/**
* Test cleanup after each test.
*/
@After
public void teardown() {
set1.removeListener(listener);
}
/**
* Basic tests against TestDistributedSet.
*/
@Test
public void basicTests() {
set1.add("item1");
assertEquals("The set name should match.", SETNAME, set1.name());
assertEquals("The set name should match.", DistributedPrimitive.Type.SET, set1.primitiveType());
set1.add("item2");
set1.add("item3");
assertTrue("set1 should contain 3 items", set1.asDistributedSet().size() == 3);
set1.remove("item1");
set1.remove("item2");
set1.remove("item3");
assertTrue("set1 should be empty.", set1.asDistributedSet(0).isEmpty());
validateEvents(SetEvent.Type.ADD, SetEvent.Type.ADD, SetEvent.Type.ADD,
SetEvent.Type.REMOVE, SetEvent.Type.REMOVE, SetEvent.Type.REMOVE);
}
/**
* TestDistributedSet clear method test.
*/
@Test
public void testClear() {
set1.add("item1");
set1.add("item2");
set1.add("item3");
set1.clear();
assertTrue("set1 should be empty.", set1.asDistributedSet().isEmpty());
validateEvents(SetEvent.Type.ADD, SetEvent.Type.ADD, SetEvent.Type.ADD,
SetEvent.Type.REMOVE, SetEvent.Type.REMOVE, SetEvent.Type.REMOVE);
}
/**
* TestDistributedSet addAll method test.
*/
@Test
public void testAddAll() {
set2.add("item1");
set2.add("item2");
set2.add("item3");
set1.addAll(set2);
assertTrue("set1 should contain 3 items.", set1.asDistributedSet().size() == set2.size());
validateEvents(SetEvent.Type.ADD, SetEvent.Type.ADD, SetEvent.Type.ADD);
}
/**
* TestDistributedSet removeAll method test.
*/
@Test
public void testRemoveAll() {
set1.add("item1");
set1.add("item2");
set1.add("item3");
set2.add("item1");
set1.removeAll(set2);
assertTrue("set1 should contain 2 items.", set1.asDistributedSet().size() == 2);
validateEvents(SetEvent.Type.ADD, SetEvent.Type.ADD, SetEvent.Type.ADD,
SetEvent.Type.REMOVE);
}
/**
* TestDistributedSet retainAll method test.
*/
@Test
public void testRetainAll() {
set1.add("item1");
set1.add("item2");
set1.add("item3");
set2.add("item4");
set1.retainAll(set2);
assertTrue("set1 should be empty.", set1.asDistributedSet().isEmpty());
validateEvents(SetEvent.Type.ADD, SetEvent.Type.ADD, SetEvent.Type.ADD,
SetEvent.Type.REMOVE, SetEvent.Type.REMOVE, SetEvent.Type.REMOVE);
set1.add("item1");
set1.add("item2");
set1.add("item3");
set2.add("item1");
set1.retainAll(set2);
assertTrue("set1 size should be 1.", set1.asDistributedSet().size() == 1);
validateEvents(SetEvent.Type.ADD, SetEvent.Type.ADD, SetEvent.Type.ADD,
SetEvent.Type.REMOVE, SetEvent.Type.REMOVE);
}
/**
* TestDistributedSet contains method test.
*/
@Test
public void testContains() {
set1.add("item1");
set1.add("item2");
set1.add("item3");
assertTrue("set1 should contain item3.", set1.asDistributedSet().contains("item3"));
}
/**
* TestDistributedSet containsAll method test.
*/
@Test
public void testContainsAll() {
set1.add("item1");
set1.add("item2");
set1.add("item3");
set2.add("item2");
set2.add("item3");
assertTrue("set1 should contain all of set2.", set1.asDistributedSet().containsAll(set2));
}
/**
* TestDistributedSet getAsImmutableSet method test.
*/
@Test
public void testGetAsImmutableSet() {
set1.add("item1");
set1.add("item2");
set1.add("item3");
try {
assertEquals("set1 size should be 3.", set1.getAsImmutableSet().get().size(),
set1.asDistributedSet().size());
} catch (Exception e) {
fail("Expected exception. " + e.getMessage());
}
}
/**
* Method to validate that actual versus expected set events were
* received correctly.
*
* @param types expected set events.
*/
private void validateEvents(Enum... types) {
TestTools.assertAfter(100, () -> {
int i = 0;
assertEquals("wrong events received", types.length, listener.events.size());
for (SetEvent event : listener.events) {
assertEquals("incorrect event type", types[i], event.type());
i++;
}
listener.events.clear();
});
}
/**
* Listener class to test set events.
*/
private class InternalSetListener implements SetEventListener<String> {
protected List<SetEvent> events = Lists.newArrayList();
@Override
public void event(SetEvent event) {
events.add(event);
}
}
}
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.store.service;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import org.onosproject.store.primitives.DefaultDistributedSet;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
/**
* Test implementation of the distributed set.
*/
public final class TestDistributedSet<E> extends DistributedSetAdapter<E> {
private final List<SetEventListener<E>> listeners;
private final Set<E> set;
private final String setName;
/**
* Public constructor.
*
* @param setName name to be assigned to this set
*/
public TestDistributedSet(String setName) {
set = new HashSet<>();
listeners = new LinkedList<>();
this.setName = setName;
}
/**
* Notify all listeners of a set event.
*
* @param event the SetEvent
*/
private void notifyListeners(SetEvent<E> event) {
listeners.forEach(
listener -> listener.event(event)
);
}
@Override
public CompletableFuture<Void> addListener(SetEventListener<E> listener) {
listeners.add(listener);
return CompletableFuture.completedFuture(null);
}
@Override
public CompletableFuture<Void> removeListener(SetEventListener<E> listener) {
listeners.remove(listener);
return CompletableFuture.completedFuture(null);
}
@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));
}
@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));
}
@Override
public CompletableFuture<Integer> size() {
return CompletableFuture.completedFuture(set.size());
}
@Override
public CompletableFuture<Boolean> isEmpty() {
return CompletableFuture.completedFuture(set.isEmpty());
}
@Override
public CompletableFuture<Void> clear() {
removeAll(ImmutableSet.copyOf(set));
return CompletableFuture.completedFuture(null);
}
@Override
public CompletableFuture<Boolean> contains(E element) {
return CompletableFuture.completedFuture(set.contains(element));
}
@Override
public CompletableFuture<Boolean> addAll(Collection<? extends E> c) {
c.forEach(this::add);
return CompletableFuture.completedFuture(true);
}
@Override
public CompletableFuture<Boolean> containsAll(Collection<? extends E> c) {
return CompletableFuture.completedFuture(set.containsAll(c));
}
@Override
public CompletableFuture<Boolean> retainAll(Collection<? extends E> c) {
Set notInSet2;
notInSet2 = Sets.difference(set, (Set<?>) c);
return removeAll(ImmutableSet.copyOf(notInSet2));
}
@Override
public CompletableFuture<Boolean> removeAll(Collection<? extends E> c) {
c.forEach(this::remove);
return CompletableFuture.completedFuture(true);
}
@Override
public CompletableFuture<? extends Set<E>> getAsImmutableSet() {
return CompletableFuture.completedFuture(ImmutableSet.copyOf(set));
}
@Override
public String name() {
return this.setName;
}
@Override
public Type primitiveType() {
return super.primitiveType();
}
@Override
public DistributedSet<E> asDistributedSet() {
return new DefaultDistributedSet<>(this, 0);
}
@Override
public DistributedSet<E> asDistributedSet(long timeoutMillis) {
return new DefaultDistributedSet<>(this, timeoutMillis);
}
/**
* Returns a new Builder instance.
*
* @return Builder
**/
public static Builder builder() {
return new Builder();
}
/**
* Builder constructor that instantiates a TestDistributedSet.
*
* @param <E>
*/
public static class Builder<E> extends DistributedSetBuilder<E> {
@Override
public AsyncDistributedSet<E> build() {
return new TestDistributedSet(name());
}
}
}
......@@ -30,7 +30,7 @@ public class TestStorageService extends StorageServiceAdapter {
@Override
public <E> DistributedSetBuilder<E> setBuilder() {
throw new UnsupportedOperationException("setBuilder");
return TestDistributedSet.builder();
}
@Override
......