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 | }; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 46 | private static final PiHandle PI_HANDLE = |
| 47 | new PiHandle(DeviceId.NONE) { |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 48 | @Override |
| 49 | public PiEntityType entityType() { |
| 50 | return PI_ENTITY.piEntityType(); |
| 51 | } |
| 52 | |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 53 | @Override |
| 54 | public int hashCode() { |
| 55 | return HANDLE_HASH; |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public boolean equals(Object other) { |
| 60 | return other instanceof PiHandle && other.hashCode() == hashCode(); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public String toString() { |
| 65 | return String.valueOf(HANDLE_HASH); |
| 66 | } |
| 67 | }; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 68 | private static final PiEntity PI_ENTITY = new PiEntity() { |
| 69 | @Override |
| 70 | public PiEntityType piEntityType() { |
| 71 | return PiEntityType.TABLE_ENTRY; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public PiHandle handle(DeviceId deviceId) { |
| 76 | return PI_HANDLE; |
| 77 | } |
| 78 | }; |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 79 | private static final PiTranslatedEntity<PiTranslatable, PiEntity> TRANSLATED_ENTITY = |
| 80 | new PiTranslatedEntity<>(PI_TRANSLATABLE, PI_ENTITY, PI_HANDLE); |
| 81 | |
| 82 | /** |
| 83 | * Sets up the store and the storage service test harness. |
| 84 | */ |
| 85 | @Before |
| 86 | public void setUp() { |
| 87 | store = new AbstractDistributedPiTranslationStore<PiTranslatable, PiEntity>() { |
| 88 | @Override |
| 89 | protected String mapSimpleName() { |
| 90 | return "test"; |
| 91 | } |
| 92 | }; |
| 93 | store.storageService = new TestStorageService(); |
| 94 | store.setDelegate(event -> { |
| 95 | }); |
| 96 | store.activate(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Tests equality of key and value used in other tests. |
| 101 | */ |
| 102 | @Test |
| 103 | public void testEquality() { |
| 104 | assertEquals(PI_HANDLE, PI_HANDLE); |
| 105 | assertEquals(TRANSLATED_ENTITY, TRANSLATED_ENTITY); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Test for activate. |
| 110 | */ |
| 111 | @Test |
| 112 | public void activate() { |
| 113 | assertNotNull(store.storageService); |
| 114 | assertTrue("Store must have delegate", |
| 115 | store.hasDelegate()); |
| 116 | assertTrue("No value should be in the map", |
| 117 | Lists.newArrayList(store.getAll()).isEmpty()); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Test for deactivate. |
| 122 | */ |
| 123 | @Test(expected = NullPointerException.class) |
| 124 | public void deactivate() { |
| 125 | store.deactivate(); |
| 126 | store.getAll(); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Test of value add or update. |
| 131 | */ |
| 132 | @Test |
| 133 | public void addOrUpdate() { |
| 134 | store.addOrUpdate(PI_HANDLE, TRANSLATED_ENTITY); |
| 135 | assertTrue("Value should be in the map", |
| 136 | store.get(PI_HANDLE) != null); |
| 137 | assertTrue("Exactly 1 value should be in the map", |
| 138 | Lists.newArrayList(store.getAll()).size() == 1); |
| 139 | |
| 140 | // Add again, expect 1 value. |
| 141 | store.addOrUpdate(PI_HANDLE, TRANSLATED_ENTITY); |
| 142 | assertTrue("Exactly 1 value should be in the map", |
| 143 | Lists.newArrayList(store.getAll()).size() == 1); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Test of value lookup. |
| 148 | */ |
| 149 | @Test |
| 150 | public void lookup() throws Exception { |
| 151 | clear(); |
| 152 | addOrUpdate(); |
| 153 | assertEquals("Wrong value in the map", |
| 154 | store.get(PI_HANDLE), TRANSLATED_ENTITY); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Test of value removal. |
| 159 | */ |
| 160 | @Test |
| 161 | public void clear() { |
| 162 | store.remove(PI_HANDLE); |
| 163 | assertTrue("Value should NOT be in the map", |
| 164 | store.get(PI_HANDLE) == null); |
| 165 | assertTrue("No value should be in the map", |
| 166 | Lists.newArrayList(store.getAll()).isEmpty()); |
| 167 | } |
| 168 | } |