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