Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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 | |
| 17 | package org.onosproject.store.pi.impl; |
| 18 | |
| 19 | import com.google.common.collect.Lists; |
| 20 | import org.apache.commons.lang3.RandomUtils; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.pi.runtime.PiEntity; |
| 25 | import org.onosproject.net.pi.runtime.PiEntityType; |
| 26 | import org.onosproject.net.pi.runtime.PiHandle; |
| 27 | import org.onosproject.net.pi.service.PiTranslatable; |
| 28 | import org.onosproject.net.pi.service.PiTranslatedEntity; |
| 29 | import org.onosproject.store.service.TestStorageService; |
| 30 | |
| 31 | import static org.junit.Assert.assertEquals; |
| 32 | import static org.junit.Assert.assertNotNull; |
| 33 | import static org.junit.Assert.assertTrue; |
| 34 | |
| 35 | /** |
| 36 | * Test for {@link AbstractDistributedPiTranslationStore}. |
| 37 | */ |
| 38 | public class DistributedPiTranslationStoreTest { |
| 39 | |
| 40 | private AbstractDistributedPiTranslationStore<PiTranslatable, PiEntity> store; |
| 41 | |
| 42 | private static final int HANDLE_HASH = RandomUtils.nextInt(); |
| 43 | private static final PiTranslatable PI_TRANSLATABLE = |
| 44 | new PiTranslatable() { |
| 45 | }; |
| 46 | private static final PiEntity PI_ENTITY = () -> PiEntityType.TABLE_ENTRY; |
| 47 | private static final PiHandle<PiEntity> PI_HANDLE = |
| 48 | new PiHandle<PiEntity>(DeviceId.NONE, PI_ENTITY) { |
| 49 | @Override |
| 50 | public int hashCode() { |
| 51 | return HANDLE_HASH; |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public boolean equals(Object other) { |
| 56 | return other instanceof PiHandle && other.hashCode() == hashCode(); |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public String toString() { |
| 61 | return String.valueOf(HANDLE_HASH); |
| 62 | } |
| 63 | }; |
| 64 | private static final PiTranslatedEntity<PiTranslatable, PiEntity> TRANSLATED_ENTITY = |
| 65 | new PiTranslatedEntity<>(PI_TRANSLATABLE, PI_ENTITY, PI_HANDLE); |
| 66 | |
| 67 | /** |
| 68 | * Sets up the store and the storage service test harness. |
| 69 | */ |
| 70 | @Before |
| 71 | public void setUp() { |
| 72 | store = new AbstractDistributedPiTranslationStore<PiTranslatable, PiEntity>() { |
| 73 | @Override |
| 74 | protected String mapSimpleName() { |
| 75 | return "test"; |
| 76 | } |
| 77 | }; |
| 78 | store.storageService = new TestStorageService(); |
| 79 | store.setDelegate(event -> { |
| 80 | }); |
| 81 | store.activate(); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Tests equality of key and value used in other tests. |
| 86 | */ |
| 87 | @Test |
| 88 | public void testEquality() { |
| 89 | assertEquals(PI_HANDLE, PI_HANDLE); |
| 90 | assertEquals(TRANSLATED_ENTITY, TRANSLATED_ENTITY); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Test for activate. |
| 95 | */ |
| 96 | @Test |
| 97 | public void activate() { |
| 98 | assertNotNull(store.storageService); |
| 99 | assertTrue("Store must have delegate", |
| 100 | store.hasDelegate()); |
| 101 | assertTrue("No value should be in the map", |
| 102 | Lists.newArrayList(store.getAll()).isEmpty()); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Test for deactivate. |
| 107 | */ |
| 108 | @Test(expected = NullPointerException.class) |
| 109 | public void deactivate() { |
| 110 | store.deactivate(); |
| 111 | store.getAll(); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Test of value add or update. |
| 116 | */ |
| 117 | @Test |
| 118 | public void addOrUpdate() { |
| 119 | store.addOrUpdate(PI_HANDLE, TRANSLATED_ENTITY); |
| 120 | assertTrue("Value should be in the map", |
| 121 | store.get(PI_HANDLE) != null); |
| 122 | assertTrue("Exactly 1 value should be in the map", |
| 123 | Lists.newArrayList(store.getAll()).size() == 1); |
| 124 | |
| 125 | // Add again, expect 1 value. |
| 126 | store.addOrUpdate(PI_HANDLE, TRANSLATED_ENTITY); |
| 127 | assertTrue("Exactly 1 value should be in the map", |
| 128 | Lists.newArrayList(store.getAll()).size() == 1); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Test of value lookup. |
| 133 | */ |
| 134 | @Test |
| 135 | public void lookup() throws Exception { |
| 136 | clear(); |
| 137 | addOrUpdate(); |
| 138 | assertEquals("Wrong value in the map", |
| 139 | store.get(PI_HANDLE), TRANSLATED_ENTITY); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Test of value removal. |
| 144 | */ |
| 145 | @Test |
| 146 | public void clear() { |
| 147 | store.remove(PI_HANDLE); |
| 148 | assertTrue("Value should NOT be in the map", |
| 149 | store.get(PI_HANDLE) == null); |
| 150 | assertTrue("No value should be in the map", |
| 151 | Lists.newArrayList(store.getAll()).isEmpty()); |
| 152 | } |
| 153 | } |