Jian Li | 8f64feb | 2018-07-24 13:20:16 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.openstacknetworking.impl; |
| 17 | |
| 18 | import org.junit.After; |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
Jian Li | 0ce2c41 | 2018-07-24 21:09:04 +0900 | [diff] [blame] | 21 | import org.onlab.junit.TestUtils; |
| 22 | import org.onosproject.core.ApplicationId; |
| 23 | import org.onosproject.core.CoreServiceAdapter; |
| 24 | import org.onosproject.core.DefaultApplicationId; |
Jian Li | 628a7cb | 2018-08-11 23:04:24 +0900 | [diff] [blame] | 25 | import org.onosproject.openstacknetworking.api.InstancePortAdminService; |
Jian Li | 0ce2c41 | 2018-07-24 21:09:04 +0900 | [diff] [blame] | 26 | import org.onosproject.store.service.TestStorageService; |
Jian Li | 8f64feb | 2018-07-24 13:20:16 +0900 | [diff] [blame] | 27 | |
| 28 | import static org.junit.Assert.assertEquals; |
| 29 | import static org.onosproject.openstacknetworking.api.OpenstackNetworkEvent.Type.OPENSTACK_PORT_PRE_REMOVE; |
| 30 | import static org.onosproject.openstacknetworking.api.OpenstackNetworkEvent.Type.OPENSTACK_PORT_PRE_UPDATE; |
| 31 | |
| 32 | /** |
| 33 | * Unit tests for pre-commit port manager. |
| 34 | */ |
| 35 | public class PreCommitPortManagerTest { |
| 36 | |
Jian Li | 0ce2c41 | 2018-07-24 21:09:04 +0900 | [diff] [blame] | 37 | private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test"); |
| 38 | |
Jian Li | 8f64feb | 2018-07-24 13:20:16 +0900 | [diff] [blame] | 39 | 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 Li | 0ce2c41 | 2018-07-24 21:09:04 +0900 | [diff] [blame] | 53 | TestUtils.setField(target, "coreService", new TestCoreService()); |
| 54 | TestUtils.setField(target, "storageService", new TestStorageService()); |
Jian Li | 8f64feb | 2018-07-24 13:20:16 +0900 | [diff] [blame] | 55 | target.activate(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Tears down this unit test. |
| 60 | */ |
| 61 | @After |
| 62 | public void tearDown() { |
| 63 | target.deactivate(); |
Jian Li | 0ce2c41 | 2018-07-24 21:09:04 +0900 | [diff] [blame] | 64 | target = null; |
Jian Li | 8f64feb | 2018-07-24 13:20:16 +0900 | [diff] [blame] | 65 | } |
| 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 Li | 628a7cb | 2018-08-11 23:04:24 +0900 | [diff] [blame] | 93 | 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 Li | 8f64feb | 2018-07-24 13:20:16 +0900 | [diff] [blame] | 97 | |
| 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 Li | 0ce2c41 | 2018-07-24 21:09:04 +0900 | [diff] [blame] | 114 | |
| 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 Li | 628a7cb | 2018-08-11 23:04:24 +0900 | [diff] [blame] | 125 | |
| 126 | private static class TestInstancePortAdminService extends InstancePortAdminServiceAdapter { |
| 127 | } |
Jian Li | 8f64feb | 2018-07-24 13:20:16 +0900 | [diff] [blame] | 128 | } |