Sbhat35 | ffe0234 | 2017-08-09 16:31:17 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Foundation |
| 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 | package org.onosproject.store.primitives.impl; |
| 17 | |
| 18 | import org.junit.After; |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
| 21 | import org.onosproject.store.serializers.KryoNamespaces; |
| 22 | import org.onosproject.store.service.AsyncConsistentMap; |
| 23 | import org.onosproject.store.service.Serializer; |
| 24 | |
| 25 | import java.util.ArrayList; |
| 26 | import java.util.Collection; |
| 27 | import java.util.HashMap; |
| 28 | import java.util.HashSet; |
| 29 | import java.util.Map; |
| 30 | import java.util.Set; |
| 31 | |
| 32 | import static org.hamcrest.Matchers.is; |
| 33 | import static org.junit.Assert.assertFalse; |
| 34 | import static org.junit.Assert.assertThat; |
| 35 | import static org.junit.Assert.assertTrue; |
| 36 | |
| 37 | public class DefaultAsyncDistributedSetTest { |
| 38 | DefaultAsyncDistributedSet<String> defaultAsyncDistributedSet; |
| 39 | |
| 40 | private static String name; |
| 41 | private static Serializer serializer; |
| 42 | private static AsyncConsistentMap<String, Boolean> asyncConsistentMap; |
| 43 | private static AsyncConsistentMap<String, byte[]> baseMap; |
| 44 | private static Map<Integer, String> map; |
| 45 | private static Collection<String> collection; |
| 46 | private static Collection<String> collection1; |
| 47 | private static Set<String> set; |
| 48 | |
| 49 | private static final Boolean MAGENT = false; |
| 50 | private static final String TEST1 = "one"; |
| 51 | private static final String TEST2 = "two"; |
| 52 | private static final String TEST3 = "three"; |
| 53 | private static final String TEST4 = "four"; |
| 54 | private static final String TEST5 = "five"; |
| 55 | |
| 56 | @Before |
| 57 | public void setUp() throws Exception { |
| 58 | serializer = Serializer.using(KryoNamespaces.API); |
| 59 | asyncConsistentMap = new AsyncConsistentMapMock<>(); |
| 60 | name = "Default Name"; |
| 61 | map = new HashMap<>(); |
| 62 | collection = new ArrayList<>(); |
| 63 | set = new HashSet<>(); |
| 64 | |
| 65 | defaultAsyncDistributedSet = new DefaultAsyncDistributedSet<>(asyncConsistentMap, |
| 66 | name, MAGENT); |
| 67 | } |
| 68 | @After |
| 69 | public void clear() throws Exception { |
| 70 | defaultAsyncDistributedSet.clear().join(); |
| 71 | assertThat(defaultAsyncDistributedSet.size().join(), is(0)); |
| 72 | assertTrue(defaultAsyncDistributedSet.isEmpty().join()); |
| 73 | } |
| 74 | @Test |
| 75 | public void testProperties() { |
| 76 | assertThat(defaultAsyncDistributedSet.name(), is(name)); |
| 77 | assertTrue(defaultAsyncDistributedSet.isEmpty().join()); |
| 78 | |
| 79 | collection.add(TEST1); |
| 80 | collection.add(TEST2); |
| 81 | collection.add(TEST3); |
| 82 | set.add(TEST4); |
| 83 | set.add(TEST5); |
| 84 | |
| 85 | assertThat(defaultAsyncDistributedSet.size().join(), is(0)); |
| 86 | defaultAsyncDistributedSet.add(TEST1).join(); |
| 87 | defaultAsyncDistributedSet.add(TEST2).join(); |
| 88 | assertThat(defaultAsyncDistributedSet.size().join(), is(2)); |
| 89 | defaultAsyncDistributedSet.add(TEST3).join(); |
| 90 | assertThat(defaultAsyncDistributedSet.size().join(), is(3)); |
| 91 | |
| 92 | defaultAsyncDistributedSet.remove(TEST1); |
| 93 | assertThat(defaultAsyncDistributedSet.size().join(), is(2)); |
| 94 | assertFalse(defaultAsyncDistributedSet.contains(TEST1).join()); |
| 95 | assertTrue(defaultAsyncDistributedSet.contains(TEST2).join()); |
| 96 | |
| 97 | defaultAsyncDistributedSet.addAll(collection).join(); |
| 98 | assertTrue(defaultAsyncDistributedSet.containsAll(collection).join()); |
| 99 | assertFalse(defaultAsyncDistributedSet.retainAll(collection).join()); |
| 100 | |
| 101 | assertThat(defaultAsyncDistributedSet.size().join(), is(3)); |
| 102 | |
| 103 | defaultAsyncDistributedSet.addAll(set).join(); |
| 104 | assertThat(defaultAsyncDistributedSet.size().join(), is(5)); |
| 105 | assertTrue(defaultAsyncDistributedSet.contains(TEST4).join()); |
| 106 | defaultAsyncDistributedSet.retainAll(set); |
| 107 | |
| 108 | assertTrue(defaultAsyncDistributedSet.contains(TEST4).join()); |
| 109 | assertThat(defaultAsyncDistributedSet.contains(TEST1).join(), is(false)); |
| 110 | |
| 111 | assertThat(defaultAsyncDistributedSet.getAsImmutableSet().join().size(), |
| 112 | is(2)); |
| 113 | } |
| 114 | |
| 115 | } |