blob: b6ba3f989bb4da4e13705042e30dac0a02076f92 [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;
21
22import static org.junit.Assert.assertEquals;
23import static org.onosproject.openstacknetworking.api.OpenstackNetworkEvent.Type.OPENSTACK_PORT_PRE_REMOVE;
24import static org.onosproject.openstacknetworking.api.OpenstackNetworkEvent.Type.OPENSTACK_PORT_PRE_UPDATE;
25
26/**
27 * Unit tests for pre-commit port manager.
28 */
29public class PreCommitPortManagerTest {
30
31 private static final String PORT_ID_1 = "port-1";
32 private static final String PORT_ID_2 = "port-2";
33
34 private static final String CLASS_NAME_1 = "class-1";
35 private static final String CLASS_NAME_2 = "class-2";
36
37 private PreCommitPortManager target;
38
39 /**
40 * Initializes this unit test.
41 */
42 @Before
43 public void setUp() {
44 target = new PreCommitPortManager();
45 target.activate();
46 }
47
48 /**
49 * Tears down this unit test.
50 */
51 @After
52 public void tearDown() {
53 target.deactivate();
54 }
55
56 /**
57 * Tests subscribe pre-commit method.
58 */
59 @Test
60 public void testSubscribePreCommit() {
61
62 sampleSubscribe();
63
64 assertEquals(1, target.subscriberCountByEventType(PORT_ID_1, OPENSTACK_PORT_PRE_REMOVE));
65 assertEquals(2, target.subscriberCountByEventType(PORT_ID_2, OPENSTACK_PORT_PRE_REMOVE));
66
67 assertEquals(0, target.subscriberCountByEventType(PORT_ID_1, OPENSTACK_PORT_PRE_UPDATE));
68 assertEquals(1, target.subscriberCountByEventType(PORT_ID_2, OPENSTACK_PORT_PRE_UPDATE));
69
70 assertEquals(1, target.subscriberCount(PORT_ID_1));
71 assertEquals(3, target.subscriberCount(PORT_ID_2));
72 }
73
74 /**
75 * Tests unsubscribe pre-commit method.
76 */
77 @Test
78 public void testUnsubscribePreCommit() {
79
80 sampleSubscribe();
81
82 target.unsubscribePreCommit(PORT_ID_1, OPENSTACK_PORT_PRE_REMOVE, CLASS_NAME_1);
83 target.unsubscribePreCommit(PORT_ID_2, OPENSTACK_PORT_PRE_REMOVE, CLASS_NAME_2);
84
85 assertEquals(0, target.subscriberCountByEventType(PORT_ID_1, OPENSTACK_PORT_PRE_REMOVE));
86 assertEquals(1, target.subscriberCountByEventType(PORT_ID_2, OPENSTACK_PORT_PRE_REMOVE));
87
88 assertEquals(0, target.subscriberCount(PORT_ID_1));
89 assertEquals(2, target.subscriberCount(PORT_ID_2));
90 }
91
92 private void sampleSubscribe() {
93
94 target.subscribePreCommit(PORT_ID_1, OPENSTACK_PORT_PRE_REMOVE, CLASS_NAME_1);
95
96 target.subscribePreCommit(PORT_ID_2, OPENSTACK_PORT_PRE_REMOVE, CLASS_NAME_1);
97 target.subscribePreCommit(PORT_ID_2, OPENSTACK_PORT_PRE_REMOVE, CLASS_NAME_2);
98
99 target.subscribePreCommit(PORT_ID_2, OPENSTACK_PORT_PRE_UPDATE, CLASS_NAME_1);
100 }
101}