blob: 8575d7a3baefa7e0ea5530614bcde1224780bdfc [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;
Jian Li628a7cb2018-08-11 23:04:24 +090025import org.onosproject.openstacknetworking.api.InstancePortAdminService;
Jian Li0ce2c412018-07-24 21:09:04 +090026import org.onosproject.store.service.TestStorageService;
Jian Li8f64feb2018-07-24 13:20:16 +090027
28import static org.junit.Assert.assertEquals;
29import static org.onosproject.openstacknetworking.api.OpenstackNetworkEvent.Type.OPENSTACK_PORT_PRE_REMOVE;
30import static org.onosproject.openstacknetworking.api.OpenstackNetworkEvent.Type.OPENSTACK_PORT_PRE_UPDATE;
31
32/**
33 * Unit tests for pre-commit port manager.
34 */
35public class PreCommitPortManagerTest {
36
Jian Li0ce2c412018-07-24 21:09:04 +090037 private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test");
38
Jian Li8f64feb2018-07-24 13:20:16 +090039 private static final String PORT_ID_1 = "port-1";
40 private static final String PORT_ID_2 = "port-2";
41
42 private static final String CLASS_NAME_1 = "class-1";
43 private static final String CLASS_NAME_2 = "class-2";
44
45 private PreCommitPortManager target;
46
47 /**
48 * Initializes this unit test.
49 */
50 @Before
51 public void setUp() {
52 target = new PreCommitPortManager();
Jian Li0ce2c412018-07-24 21:09:04 +090053 TestUtils.setField(target, "coreService", new TestCoreService());
54 TestUtils.setField(target, "storageService", new TestStorageService());
Jian Li8f64feb2018-07-24 13:20:16 +090055 target.activate();
56 }
57
58 /**
59 * Tears down this unit test.
60 */
61 @After
62 public void tearDown() {
63 target.deactivate();
Jian Li0ce2c412018-07-24 21:09:04 +090064 target = null;
Jian Li8f64feb2018-07-24 13:20:16 +090065 }
66
67 /**
68 * Tests subscribe pre-commit method.
69 */
70 @Test
71 public void testSubscribePreCommit() {
72
73 sampleSubscribe();
74
75 assertEquals(1, target.subscriberCountByEventType(PORT_ID_1, OPENSTACK_PORT_PRE_REMOVE));
76 assertEquals(2, target.subscriberCountByEventType(PORT_ID_2, OPENSTACK_PORT_PRE_REMOVE));
77
78 assertEquals(0, target.subscriberCountByEventType(PORT_ID_1, OPENSTACK_PORT_PRE_UPDATE));
79 assertEquals(1, target.subscriberCountByEventType(PORT_ID_2, OPENSTACK_PORT_PRE_UPDATE));
80
81 assertEquals(1, target.subscriberCount(PORT_ID_1));
82 assertEquals(3, target.subscriberCount(PORT_ID_2));
83 }
84
85 /**
86 * Tests unsubscribe pre-commit method.
87 */
88 @Test
89 public void testUnsubscribePreCommit() {
90
91 sampleSubscribe();
92
Jian Li628a7cb2018-08-11 23:04:24 +090093 InstancePortAdminService service = new TestInstancePortAdminService();
94
95 target.unsubscribePreCommit(PORT_ID_1, OPENSTACK_PORT_PRE_REMOVE, service, CLASS_NAME_1);
96 target.unsubscribePreCommit(PORT_ID_2, OPENSTACK_PORT_PRE_REMOVE, service, CLASS_NAME_2);
Jian Li8f64feb2018-07-24 13:20:16 +090097
98 assertEquals(0, target.subscriberCountByEventType(PORT_ID_1, OPENSTACK_PORT_PRE_REMOVE));
99 assertEquals(1, target.subscriberCountByEventType(PORT_ID_2, OPENSTACK_PORT_PRE_REMOVE));
100
101 assertEquals(0, target.subscriberCount(PORT_ID_1));
102 assertEquals(2, target.subscriberCount(PORT_ID_2));
103 }
104
105 private void sampleSubscribe() {
106
107 target.subscribePreCommit(PORT_ID_1, OPENSTACK_PORT_PRE_REMOVE, CLASS_NAME_1);
108
109 target.subscribePreCommit(PORT_ID_2, OPENSTACK_PORT_PRE_REMOVE, CLASS_NAME_1);
110 target.subscribePreCommit(PORT_ID_2, OPENSTACK_PORT_PRE_REMOVE, CLASS_NAME_2);
111
112 target.subscribePreCommit(PORT_ID_2, OPENSTACK_PORT_PRE_UPDATE, CLASS_NAME_1);
113 }
Jian Li0ce2c412018-07-24 21:09:04 +0900114
115 /**
116 * Mocks CoreService.
117 */
118 private static class TestCoreService extends CoreServiceAdapter {
119
120 @Override
121 public ApplicationId registerApplication(String name) {
122 return TEST_APP_ID;
123 }
124 }
Jian Li628a7cb2018-08-11 23:04:24 +0900125
126 private static class TestInstancePortAdminService extends InstancePortAdminServiceAdapter {
127 }
Jian Li8f64feb2018-07-24 13:20:16 +0900128}