blob: 833a288ab87710184d102a565b8907935a3f9bee [file] [log] [blame]
Ray Milkey24e60b32015-08-12 11:39:54 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Ray Milkey24e60b32015-08-12 11:39:54 -07003 *
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 */
16package org.onosproject.store.flowobjective.impl;
17
Sbhat35d975bdf2017-07-11 11:22:16 -070018import org.hamcrest.collection.IsMapContaining;
Ray Milkey24e60b32015-08-12 11:39:54 -070019import org.junit.After;
20import org.junit.Before;
21import org.junit.Test;
22import org.onosproject.net.behaviour.DefaultNextGroup;
23import org.onosproject.net.behaviour.NextGroup;
24import org.onosproject.net.flowobjective.FlowObjectiveStore;
25import org.onosproject.store.service.TestStorageService;
26
27import com.google.common.base.Charsets;
Sbhat35ee3132c2017-07-05 14:17:34 -070028
29import java.util.Collections;
30
Ray Milkey24e60b32015-08-12 11:39:54 -070031import static org.hamcrest.MatcherAssert.assertThat;
Sbhat35ee3132c2017-07-05 14:17:34 -070032import static org.junit.Assert.assertEquals;
Ray Milkey24e60b32015-08-12 11:39:54 -070033import static org.hamcrest.Matchers.*;
Sbhat35ee3132c2017-07-05 14:17:34 -070034import static org.junit.Assert.assertFalse;
35import static org.junit.Assert.assertTrue;
Ray Milkey24e60b32015-08-12 11:39:54 -070036
37/**
38 * Unit tests for distributed flow objective store.
39 */
40public 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() {
Sbhat35ee3132c2017-07-05 14:17:34 -070059 NextGroup group1 = new DefaultNextGroup("1".getBytes(Charsets.US_ASCII));
Ray Milkey24e60b32015-08-12 11:39:54 -070060 NextGroup group2 = new DefaultNextGroup("2".getBytes(Charsets.US_ASCII));
Sbhat35d975bdf2017-07-11 11:22:16 -070061 NextGroup group3 = new DefaultNextGroup("3".getBytes(Charsets.US_ASCII));
Ray Milkey24e60b32015-08-12 11:39:54 -070062 int group1Id = store.allocateNextId();
63 int group2Id = store.allocateNextId();
Sbhat35d975bdf2017-07-11 11:22:16 -070064 int group3Id = store.allocateNextId();
Ray Milkey24e60b32015-08-12 11:39:54 -070065
66 NextGroup group1add = store.getNextGroup(group1Id);
67 assertThat(group1add, nullValue());
Sbhat35ee3132c2017-07-05 14:17:34 -070068 NextGroup dif = store.getNextGroup(3);
69 assertThat(dif, is(nullValue()));
Ray Milkey24e60b32015-08-12 11:39:54 -070070
Sbhat35ee3132c2017-07-05 14:17:34 -070071
72 store.putNextGroup(group1Id, group1);
Ray Milkey24e60b32015-08-12 11:39:54 -070073 store.putNextGroup(group2Id, group2);
74 NextGroup group2Query = store.getNextGroup(group2Id);
75 assertThat(group2Query.data(), is(group2.data()));
Sbhat35ee3132c2017-07-05 14:17:34 -070076 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());
Sbhat35d975bdf2017-07-11 11:22:16 -070084
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 Milkey24e60b32015-08-12 11:39:54 -070093 }
Sbhat35ee3132c2017-07-05 14:17:34 -070094}