Brian Stanke
Committed by Gerrit Code Review

ONOS-4075 TestDistributedSet implementation and Junit tests.

Change-Id: I77ea0beb5a1adbdbf8632c599b631d728455aa9a
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +package org.onosproject.store.service;
18 +
19 +import java.util.Collection;
20 +import java.util.Set;
21 +import java.util.concurrent.CompletableFuture;
22 +
23 +/**
24 + * Testing adapter for the distributed set.
25 + */
26 +public class DistributedSetAdapter<E> implements AsyncDistributedSet<E> {
27 + @Override
28 + public CompletableFuture<Void> addListener(SetEventListener<E> listener) {
29 + return null;
30 + }
31 +
32 + @Override
33 + public CompletableFuture<Void> removeListener(SetEventListener<E> listener) {
34 + return null;
35 + }
36 +
37 + @Override
38 + public CompletableFuture<Boolean> add(E element) {
39 + return null;
40 + }
41 +
42 + @Override
43 + public CompletableFuture<Boolean> remove(E element) {
44 + return null;
45 + }
46 +
47 + @Override
48 + public CompletableFuture<Integer> size() {
49 + return null;
50 + }
51 +
52 + @Override
53 + public CompletableFuture<Boolean> isEmpty() {
54 + return null;
55 + }
56 +
57 + @Override
58 + public CompletableFuture<Void> clear() {
59 + return null;
60 + }
61 +
62 + @Override
63 + public CompletableFuture<Boolean> contains(E element) {
64 + return null;
65 + }
66 +
67 + @Override
68 + public CompletableFuture<Boolean> addAll(Collection<? extends E> c) {
69 + return null;
70 + }
71 +
72 + @Override
73 + public CompletableFuture<Boolean> containsAll(Collection<? extends E> c) {
74 + return null;
75 + }
76 +
77 + @Override
78 + public CompletableFuture<Boolean> retainAll(Collection<? extends E> c) {
79 + return null;
80 + }
81 +
82 + @Override
83 + public CompletableFuture<Boolean> removeAll(Collection<? extends E> c) {
84 + return null;
85 + }
86 +
87 + @Override
88 + public CompletableFuture<? extends Set<E>> getAsImmutableSet() {
89 + return null;
90 + }
91 +
92 + @Override
93 + public String name() {
94 + return null;
95 + }
96 +}
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +package org.onosproject.store.service;
18 +
19 +import com.google.common.collect.Lists;
20 +import org.junit.After;
21 +import org.junit.Before;
22 +import org.junit.Test;
23 +import org.onlab.junit.TestTools;
24 +
25 +import java.util.HashSet;
26 +import java.util.List;
27 +import java.util.Set;
28 +
29 +import static org.junit.Assert.*;
30 +
31 +/**
32 + * Junit test class for TestDistributedSet.
33 + */
34 +public class DistributedSetTest {
35 +
36 + protected InternalSetListener listener = new InternalSetListener();
37 + private static final String SETNAME = "set1";
38 + private TestDistributedSet<String> set1;
39 + private Set<String> set2;
40 +
41 + /**
42 + * Test setup before each test.
43 + */
44 + @Before
45 + public void setup() {
46 + set1 = new TestDistributedSet<>(SETNAME);
47 + set2 = new HashSet<>();
48 +
49 + set1.addListener(listener);
50 + }
51 +
52 + /**
53 + * Test cleanup after each test.
54 + */
55 + @After
56 + public void teardown() {
57 + set1.removeListener(listener);
58 + }
59 +
60 + /**
61 + * Basic tests against TestDistributedSet.
62 + */
63 + @Test
64 + public void basicTests() {
65 + set1.add("item1");
66 + assertEquals("The set name should match.", SETNAME, set1.name());
67 + assertEquals("The set name should match.", DistributedPrimitive.Type.SET, set1.primitiveType());
68 +
69 + set1.add("item2");
70 + set1.add("item3");
71 + assertTrue("set1 should contain 3 items", set1.asDistributedSet().size() == 3);
72 + set1.remove("item1");
73 + set1.remove("item2");
74 + set1.remove("item3");
75 + assertTrue("set1 should be empty.", set1.asDistributedSet(0).isEmpty());
76 + validateEvents(SetEvent.Type.ADD, SetEvent.Type.ADD, SetEvent.Type.ADD,
77 + SetEvent.Type.REMOVE, SetEvent.Type.REMOVE, SetEvent.Type.REMOVE);
78 + }
79 +
80 + /**
81 + * TestDistributedSet clear method test.
82 + */
83 + @Test
84 + public void testClear() {
85 + set1.add("item1");
86 + set1.add("item2");
87 + set1.add("item3");
88 + set1.clear();
89 + assertTrue("set1 should be empty.", set1.asDistributedSet().isEmpty());
90 + validateEvents(SetEvent.Type.ADD, SetEvent.Type.ADD, SetEvent.Type.ADD,
91 + SetEvent.Type.REMOVE, SetEvent.Type.REMOVE, SetEvent.Type.REMOVE);
92 + }
93 +
94 + /**
95 + * TestDistributedSet addAll method test.
96 + */
97 + @Test
98 + public void testAddAll() {
99 + set2.add("item1");
100 + set2.add("item2");
101 + set2.add("item3");
102 + set1.addAll(set2);
103 + assertTrue("set1 should contain 3 items.", set1.asDistributedSet().size() == set2.size());
104 + validateEvents(SetEvent.Type.ADD, SetEvent.Type.ADD, SetEvent.Type.ADD);
105 + }
106 +
107 + /**
108 + * TestDistributedSet removeAll method test.
109 + */
110 + @Test
111 + public void testRemoveAll() {
112 + set1.add("item1");
113 + set1.add("item2");
114 + set1.add("item3");
115 + set2.add("item1");
116 + set1.removeAll(set2);
117 + assertTrue("set1 should contain 2 items.", set1.asDistributedSet().size() == 2);
118 + validateEvents(SetEvent.Type.ADD, SetEvent.Type.ADD, SetEvent.Type.ADD,
119 + SetEvent.Type.REMOVE);
120 + }
121 +
122 + /**
123 + * TestDistributedSet retainAll method test.
124 + */
125 + @Test
126 + public void testRetainAll() {
127 + set1.add("item1");
128 + set1.add("item2");
129 + set1.add("item3");
130 + set2.add("item4");
131 + set1.retainAll(set2);
132 + assertTrue("set1 should be empty.", set1.asDistributedSet().isEmpty());
133 + validateEvents(SetEvent.Type.ADD, SetEvent.Type.ADD, SetEvent.Type.ADD,
134 + SetEvent.Type.REMOVE, SetEvent.Type.REMOVE, SetEvent.Type.REMOVE);
135 +
136 + set1.add("item1");
137 + set1.add("item2");
138 + set1.add("item3");
139 + set2.add("item1");
140 + set1.retainAll(set2);
141 + assertTrue("set1 size should be 1.", set1.asDistributedSet().size() == 1);
142 + validateEvents(SetEvent.Type.ADD, SetEvent.Type.ADD, SetEvent.Type.ADD,
143 + SetEvent.Type.REMOVE, SetEvent.Type.REMOVE);
144 +
145 + }
146 +
147 + /**
148 + * TestDistributedSet contains method test.
149 + */
150 + @Test
151 + public void testContains() {
152 + set1.add("item1");
153 + set1.add("item2");
154 + set1.add("item3");
155 + assertTrue("set1 should contain item3.", set1.asDistributedSet().contains("item3"));
156 + }
157 +
158 + /**
159 + * TestDistributedSet containsAll method test.
160 + */
161 + @Test
162 + public void testContainsAll() {
163 + set1.add("item1");
164 + set1.add("item2");
165 + set1.add("item3");
166 + set2.add("item2");
167 + set2.add("item3");
168 + assertTrue("set1 should contain all of set2.", set1.asDistributedSet().containsAll(set2));
169 + }
170 +
171 + /**
172 + * TestDistributedSet getAsImmutableSet method test.
173 + */
174 + @Test
175 + public void testGetAsImmutableSet() {
176 + set1.add("item1");
177 + set1.add("item2");
178 + set1.add("item3");
179 + try {
180 + assertEquals("set1 size should be 3.", set1.getAsImmutableSet().get().size(),
181 + set1.asDistributedSet().size());
182 + } catch (Exception e) {
183 + fail("Expected exception. " + e.getMessage());
184 + }
185 + }
186 +
187 + /**
188 + * Method to validate that actual versus expected set events were
189 + * received correctly.
190 + *
191 + * @param types expected set events.
192 + */
193 + private void validateEvents(Enum... types) {
194 + TestTools.assertAfter(100, () -> {
195 + int i = 0;
196 + assertEquals("wrong events received", types.length, listener.events.size());
197 + for (SetEvent event : listener.events) {
198 + assertEquals("incorrect event type", types[i], event.type());
199 + i++;
200 + }
201 + listener.events.clear();
202 + });
203 + }
204 +
205 + /**
206 + * Listener class to test set events.
207 + */
208 + private class InternalSetListener implements SetEventListener<String> {
209 + protected List<SetEvent> events = Lists.newArrayList();
210 +
211 + @Override
212 + public void event(SetEvent event) {
213 + events.add(event);
214 + }
215 + }
216 +}
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +package org.onosproject.store.service;
18 +
19 +import com.google.common.collect.ImmutableSet;
20 +import com.google.common.collect.Sets;
21 +import org.onosproject.store.primitives.DefaultDistributedSet;
22 +
23 +import java.util.Collection;
24 +import java.util.HashSet;
25 +import java.util.LinkedList;
26 +import java.util.List;
27 +import java.util.Set;
28 +import java.util.concurrent.CompletableFuture;
29 +
30 +/**
31 + * Test implementation of the distributed set.
32 + */
33 +public final class TestDistributedSet<E> extends DistributedSetAdapter<E> {
34 + private final List<SetEventListener<E>> listeners;
35 + private final Set<E> set;
36 + private final String setName;
37 +
38 + /**
39 + * Public constructor.
40 + *
41 + * @param setName name to be assigned to this set
42 + */
43 + public TestDistributedSet(String setName) {
44 + set = new HashSet<>();
45 + listeners = new LinkedList<>();
46 + this.setName = setName;
47 + }
48 +
49 + /**
50 + * Notify all listeners of a set event.
51 + *
52 + * @param event the SetEvent
53 + */
54 + private void notifyListeners(SetEvent<E> event) {
55 + listeners.forEach(
56 + listener -> listener.event(event)
57 + );
58 + }
59 +
60 + @Override
61 + public CompletableFuture<Void> addListener(SetEventListener<E> listener) {
62 + listeners.add(listener);
63 + return CompletableFuture.completedFuture(null);
64 + }
65 +
66 + @Override
67 + public CompletableFuture<Void> removeListener(SetEventListener<E> listener) {
68 + listeners.remove(listener);
69 + return CompletableFuture.completedFuture(null);
70 + }
71 +
72 + @Override
73 + public CompletableFuture<Boolean> add(E element) {
74 + SetEvent<E> event =
75 + new SetEvent<>(setName, SetEvent.Type.ADD, element);
76 + notifyListeners(event);
77 + return CompletableFuture.completedFuture(set.add(element));
78 + }
79 +
80 + @Override
81 + public CompletableFuture<Boolean> remove(E element) {
82 + SetEvent<E> event =
83 + new SetEvent<>(setName, SetEvent.Type.REMOVE, element);
84 + notifyListeners(event);
85 + return CompletableFuture.completedFuture(set.remove(element));
86 + }
87 +
88 + @Override
89 + public CompletableFuture<Integer> size() {
90 + return CompletableFuture.completedFuture(set.size());
91 + }
92 +
93 + @Override
94 + public CompletableFuture<Boolean> isEmpty() {
95 + return CompletableFuture.completedFuture(set.isEmpty());
96 + }
97 +
98 + @Override
99 + public CompletableFuture<Void> clear() {
100 + removeAll(ImmutableSet.copyOf(set));
101 + return CompletableFuture.completedFuture(null);
102 + }
103 +
104 + @Override
105 + public CompletableFuture<Boolean> contains(E element) {
106 + return CompletableFuture.completedFuture(set.contains(element));
107 + }
108 +
109 + @Override
110 + public CompletableFuture<Boolean> addAll(Collection<? extends E> c) {
111 + c.forEach(this::add);
112 + return CompletableFuture.completedFuture(true);
113 + }
114 +
115 + @Override
116 + public CompletableFuture<Boolean> containsAll(Collection<? extends E> c) {
117 + return CompletableFuture.completedFuture(set.containsAll(c));
118 + }
119 +
120 + @Override
121 + public CompletableFuture<Boolean> retainAll(Collection<? extends E> c) {
122 + Set notInSet2;
123 + notInSet2 = Sets.difference(set, (Set<?>) c);
124 + return removeAll(ImmutableSet.copyOf(notInSet2));
125 + }
126 +
127 + @Override
128 + public CompletableFuture<Boolean> removeAll(Collection<? extends E> c) {
129 + c.forEach(this::remove);
130 + return CompletableFuture.completedFuture(true);
131 + }
132 +
133 + @Override
134 + public CompletableFuture<? extends Set<E>> getAsImmutableSet() {
135 + return CompletableFuture.completedFuture(ImmutableSet.copyOf(set));
136 + }
137 +
138 + @Override
139 + public String name() {
140 + return this.setName;
141 + }
142 +
143 + @Override
144 + public Type primitiveType() {
145 + return super.primitiveType();
146 + }
147 +
148 + @Override
149 + public DistributedSet<E> asDistributedSet() {
150 + return new DefaultDistributedSet<>(this, 0);
151 + }
152 +
153 + @Override
154 + public DistributedSet<E> asDistributedSet(long timeoutMillis) {
155 + return new DefaultDistributedSet<>(this, timeoutMillis);
156 + }
157 +
158 + /**
159 + * Returns a new Builder instance.
160 + *
161 + * @return Builder
162 + **/
163 + public static Builder builder() {
164 + return new Builder();
165 + }
166 +
167 + /**
168 + * Builder constructor that instantiates a TestDistributedSet.
169 + *
170 + * @param <E>
171 + */
172 + public static class Builder<E> extends DistributedSetBuilder<E> {
173 + @Override
174 + public AsyncDistributedSet<E> build() {
175 + return new TestDistributedSet(name());
176 + }
177 + }
178 +}
...@@ -30,7 +30,7 @@ public class TestStorageService extends StorageServiceAdapter { ...@@ -30,7 +30,7 @@ public class TestStorageService extends StorageServiceAdapter {
30 30
31 @Override 31 @Override
32 public <E> DistributedSetBuilder<E> setBuilder() { 32 public <E> DistributedSetBuilder<E> setBuilder() {
33 - throw new UnsupportedOperationException("setBuilder"); 33 + return TestDistributedSet.builder();
34 } 34 }
35 35
36 @Override 36 @Override
......