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