blob: 3b32fc07e14c9a33e27f9cb0247550fd97933b24 [file] [log] [blame]
Jian Li8f64feb2018-07-24 13:20:16 +09001/*
2 * Copyright 2018-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 */
16package org.onosproject.openstacknetworking.impl;
17
18import org.junit.After;
19import org.junit.Before;
20import org.junit.Test;
Jian Li0ce2c412018-07-24 21:09:04 +090021import org.onlab.junit.TestUtils;
22import org.onosproject.core.ApplicationId;
23import org.onosproject.core.CoreServiceAdapter;
24import org.onosproject.core.DefaultApplicationId;
25import org.onosproject.store.service.TestStorageService;
Jian Li8f64feb2018-07-24 13:20:16 +090026
27import static org.junit.Assert.assertEquals;
28import static org.onosproject.openstacknetworking.api.OpenstackNetworkEvent.Type.OPENSTACK_PORT_PRE_REMOVE;
29import static org.onosproject.openstacknetworking.api.OpenstackNetworkEvent.Type.OPENSTACK_PORT_PRE_UPDATE;
30
31/**
32 * Unit tests for pre-commit port manager.
33 */
34public class PreCommitPortManagerTest {
35
Jian Li0ce2c412018-07-24 21:09:04 +090036 private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test");
37
Jian Li8f64feb2018-07-24 13:20:16 +090038 private static final String PORT_ID_1 = "port-1";
39 private static final String PORT_ID_2 = "port-2";
40
41 private static final String CLASS_NAME_1 = "class-1";
42 private static final String CLASS_NAME_2 = "class-2";
43
44 private PreCommitPortManager target;
45
46 /**
47 * Initializes this unit test.
48 */
49 @Before
50 public void setUp() {
51 target = new PreCommitPortManager();
Jian Li0ce2c412018-07-24 21:09:04 +090052 TestUtils.setField(target, "coreService", new TestCoreService());
53 TestUtils.setField(target, "storageService", new TestStorageService());
Jian Li8f64feb2018-07-24 13:20:16 +090054 target.activate();
55 }
56
57 /**
58 * Tears down this unit test.
59 */
60 @After
61 public void tearDown() {
62 target.deactivate();
Jian Li0ce2c412018-07-24 21:09:04 +090063 target = null;
Jian Li8f64feb2018-07-24 13:20:16 +090064 }
65
66 /**
67 * Tests subscribe pre-commit method.
68 */
69 @Test
70 public void testSubscribePreCommit() {
71
72 sampleSubscribe();
73
74 assertEquals(1, target.subscriberCountByEventType(PORT_ID_1, OPENSTACK_PORT_PRE_REMOVE));
75 assertEquals(2, target.subscriberCountByEventType(PORT_ID_2, OPENSTACK_PORT_PRE_REMOVE));
76
77 assertEquals(0, target.subscriberCountByEventType(PORT_ID_1, OPENSTACK_PORT_PRE_UPDATE));
78 assertEquals(1, target.subscriberCountByEventType(PORT_ID_2, OPENSTACK_PORT_PRE_UPDATE));
79
80 assertEquals(1, target.subscriberCount(PORT_ID_1));
81 assertEquals(3, target.subscriberCount(PORT_ID_2));
82 }
83
84 /**
85 * Tests unsubscribe pre-commit method.
86 */
87 @Test
88 public void testUnsubscribePreCommit() {
89
90 sampleSubscribe();
91
92 target.unsubscribePreCommit(PORT_ID_1, OPENSTACK_PORT_PRE_REMOVE, CLASS_NAME_1);
93 target.unsubscribePreCommit(PORT_ID_2, OPENSTACK_PORT_PRE_REMOVE, CLASS_NAME_2);
94
95 assertEquals(0, target.subscriberCountByEventType(PORT_ID_1, OPENSTACK_PORT_PRE_REMOVE));
96 assertEquals(1, target.subscriberCountByEventType(PORT_ID_2, OPENSTACK_PORT_PRE_REMOVE));
97
98 assertEquals(0, target.subscriberCount(PORT_ID_1));
99 assertEquals(2, target.subscriberCount(PORT_ID_2));
100 }
101
102 private void sampleSubscribe() {
103
104 target.subscribePreCommit(PORT_ID_1, OPENSTACK_PORT_PRE_REMOVE, CLASS_NAME_1);
105
106 target.subscribePreCommit(PORT_ID_2, OPENSTACK_PORT_PRE_REMOVE, CLASS_NAME_1);
107 target.subscribePreCommit(PORT_ID_2, OPENSTACK_PORT_PRE_REMOVE, CLASS_NAME_2);
108
109 target.subscribePreCommit(PORT_ID_2, OPENSTACK_PORT_PRE_UPDATE, CLASS_NAME_1);
110 }
Jian Li0ce2c412018-07-24 21:09:04 +0900111
112 /**
113 * Mocks CoreService.
114 */
115 private static class TestCoreService extends CoreServiceAdapter {
116
117 @Override
118 public ApplicationId registerApplication(String name) {
119 return TEST_APP_ID;
120 }
121 }
Jian Li8f64feb2018-07-24 13:20:16 +0900122}