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; |
| 25 | import org.onosproject.store.service.TestStorageService; |
Jian Li | 8f64feb | 2018-07-24 13:20:16 +0900 | [diff] [blame] | 26 | |
| 27 | import static org.junit.Assert.assertEquals; |
| 28 | import static org.onosproject.openstacknetworking.api.OpenstackNetworkEvent.Type.OPENSTACK_PORT_PRE_REMOVE; |
| 29 | import static org.onosproject.openstacknetworking.api.OpenstackNetworkEvent.Type.OPENSTACK_PORT_PRE_UPDATE; |
| 30 | |
| 31 | /** |
| 32 | * Unit tests for pre-commit port manager. |
| 33 | */ |
| 34 | public class PreCommitPortManagerTest { |
| 35 | |
Jian Li | 0ce2c41 | 2018-07-24 21:09:04 +0900 | [diff] [blame] | 36 | private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test"); |
| 37 | |
Jian Li | 8f64feb | 2018-07-24 13:20:16 +0900 | [diff] [blame] | 38 | 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 Li | 0ce2c41 | 2018-07-24 21:09:04 +0900 | [diff] [blame] | 52 | TestUtils.setField(target, "coreService", new TestCoreService()); |
| 53 | TestUtils.setField(target, "storageService", new TestStorageService()); |
Jian Li | 8f64feb | 2018-07-24 13:20:16 +0900 | [diff] [blame] | 54 | target.activate(); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Tears down this unit test. |
| 59 | */ |
| 60 | @After |
| 61 | public void tearDown() { |
| 62 | target.deactivate(); |
Jian Li | 0ce2c41 | 2018-07-24 21:09:04 +0900 | [diff] [blame] | 63 | target = null; |
Jian Li | 8f64feb | 2018-07-24 13:20:16 +0900 | [diff] [blame] | 64 | } |
| 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 Li | 0ce2c41 | 2018-07-24 21:09:04 +0900 | [diff] [blame] | 111 | |
| 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 Li | 8f64feb | 2018-07-24 13:20:16 +0900 | [diff] [blame] | 122 | } |