Ray Milkey | 24e60b3 | 2015-08-12 11:39:54 -0700 | [diff] [blame] | 1 | /* |
Sbhat35 | ee3132c | 2017-07-05 14:17:34 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Laboratory |
Ray Milkey | 24e60b3 | 2015-08-12 11:39:54 -0700 | [diff] [blame] | 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.flowobjective.impl; |
| 17 | |
| 18 | import org.junit.After; |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
| 21 | import org.onosproject.net.behaviour.DefaultNextGroup; |
| 22 | import org.onosproject.net.behaviour.NextGroup; |
| 23 | import org.onosproject.net.flowobjective.FlowObjectiveStore; |
| 24 | import org.onosproject.store.service.TestStorageService; |
| 25 | |
| 26 | import com.google.common.base.Charsets; |
Sbhat35 | ee3132c | 2017-07-05 14:17:34 -0700 | [diff] [blame] | 27 | |
| 28 | import java.util.Collections; |
| 29 | |
Ray Milkey | 24e60b3 | 2015-08-12 11:39:54 -0700 | [diff] [blame] | 30 | import static org.hamcrest.MatcherAssert.assertThat; |
Sbhat35 | ee3132c | 2017-07-05 14:17:34 -0700 | [diff] [blame] | 31 | import static org.junit.Assert.assertEquals; |
Ray Milkey | 24e60b3 | 2015-08-12 11:39:54 -0700 | [diff] [blame] | 32 | import static org.hamcrest.Matchers.*; |
Sbhat35 | ee3132c | 2017-07-05 14:17:34 -0700 | [diff] [blame] | 33 | import static org.junit.Assert.assertFalse; |
| 34 | import static org.junit.Assert.assertTrue; |
Ray Milkey | 24e60b3 | 2015-08-12 11:39:54 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Unit tests for distributed flow objective store. |
| 38 | */ |
| 39 | public class DistributedFlowObjectiveStoreTest { |
| 40 | DistributedFlowObjectiveStore storeImpl; |
| 41 | FlowObjectiveStore store; |
| 42 | |
| 43 | @Before |
| 44 | public void setUp() { |
| 45 | storeImpl = new DistributedFlowObjectiveStore(); |
| 46 | storeImpl.storageService = new TestStorageService(); |
| 47 | storeImpl.activate(); |
| 48 | store = storeImpl; |
| 49 | } |
| 50 | |
| 51 | @After |
| 52 | public void tearDown() { |
| 53 | storeImpl.deactivate(); |
| 54 | } |
| 55 | |
| 56 | @Test |
| 57 | public void testFlowObjectiveStore() { |
Sbhat35 | ee3132c | 2017-07-05 14:17:34 -0700 | [diff] [blame] | 58 | NextGroup group1 = new DefaultNextGroup("1".getBytes(Charsets.US_ASCII)); |
Ray Milkey | 24e60b3 | 2015-08-12 11:39:54 -0700 | [diff] [blame] | 59 | NextGroup group2 = new DefaultNextGroup("2".getBytes(Charsets.US_ASCII)); |
| 60 | int group1Id = store.allocateNextId(); |
| 61 | int group2Id = store.allocateNextId(); |
| 62 | |
| 63 | NextGroup group1add = store.getNextGroup(group1Id); |
| 64 | assertThat(group1add, nullValue()); |
Sbhat35 | ee3132c | 2017-07-05 14:17:34 -0700 | [diff] [blame] | 65 | NextGroup dif = store.getNextGroup(3); |
| 66 | assertThat(dif, is(nullValue())); |
Ray Milkey | 24e60b3 | 2015-08-12 11:39:54 -0700 | [diff] [blame] | 67 | |
Sbhat35 | ee3132c | 2017-07-05 14:17:34 -0700 | [diff] [blame] | 68 | |
| 69 | store.putNextGroup(group1Id, group1); |
Ray Milkey | 24e60b3 | 2015-08-12 11:39:54 -0700 | [diff] [blame] | 70 | store.putNextGroup(group2Id, group2); |
| 71 | NextGroup group2Query = store.getNextGroup(group2Id); |
| 72 | assertThat(group2Query.data(), is(group2.data())); |
Sbhat35 | ee3132c | 2017-07-05 14:17:34 -0700 | [diff] [blame] | 73 | assertTrue(store.getAllGroups().containsKey(group2Id)); |
| 74 | assertTrue(store.getAllGroups().containsKey(group1Id)); |
| 75 | |
| 76 | store.removeNextGroup(group2Id); |
| 77 | assertTrue(store.getAllGroups().containsKey(group1Id)); |
| 78 | assertFalse(store.getAllGroups().containsKey(group2Id)); |
| 79 | store.removeNextGroup(group1Id); |
| 80 | assertEquals(store.getAllGroups(), Collections.emptyMap()); |
Ray Milkey | 24e60b3 | 2015-08-12 11:39:54 -0700 | [diff] [blame] | 81 | } |
Sbhat35 | ee3132c | 2017-07-05 14:17:34 -0700 | [diff] [blame] | 82 | } |