Jian Li
Committed by Gerrit Code Review

Add more unit tests for control plane manager

- Add unit test for ControlMessageMetricMapper
- Add unit test for ControlMessageManager

Change-Id: I83e2eda8ca5ed3dabae2fb262a20b7d45de4c75d
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
15 */ 15 */
16 package org.onosproject.cpman.impl; 16 package org.onosproject.cpman.impl;
17 17
18 +import org.junit.After;
19 +import org.junit.Before;
18 import org.junit.Test; 20 import org.junit.Test;
19 import org.onosproject.core.CoreServiceAdapter; 21 import org.onosproject.core.CoreServiceAdapter;
20 import org.onosproject.cpman.impl.message.ControlMessageServiceAdaptor; 22 import org.onosproject.cpman.impl.message.ControlMessageServiceAdaptor;
...@@ -30,7 +32,7 @@ public class ControlPlaneManagerTest { ...@@ -30,7 +32,7 @@ public class ControlPlaneManagerTest {
30 /** 32 /**
31 * Sets up the services required by the CPMan application. 33 * Sets up the services required by the CPMan application.
32 */ 34 */
33 - //@Before 35 + @Before
34 public void setUp() { 36 public void setUp() {
35 cpMan = new ControlPlaneManager(); 37 cpMan = new ControlPlaneManager();
36 cpMan.coreService = new CoreServiceAdapter(); 38 cpMan.coreService = new CoreServiceAdapter();
...@@ -42,7 +44,7 @@ public class ControlPlaneManagerTest { ...@@ -42,7 +44,7 @@ public class ControlPlaneManagerTest {
42 /** 44 /**
43 * Tears down the CPMan application. 45 * Tears down the CPMan application.
44 */ 46 */
45 - //@After 47 + @After
46 public void tearDown() { 48 public void tearDown() {
47 cpMan.deactivate(); 49 cpMan.deactivate();
48 } 50 }
......
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
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.cpman.impl.message;
17 +
18 +import com.google.common.collect.Sets;
19 +import org.junit.After;
20 +import org.junit.Before;
21 +import org.junit.Test;
22 +import org.onosproject.cpman.ControlMessage;
23 +import org.onosproject.cpman.DefaultControlMessage;
24 +import org.onosproject.cpman.message.ControlMessageEvent;
25 +import org.onosproject.cpman.message.ControlMessageListener;
26 +import org.onosproject.cpman.message.ControlMessageProvider;
27 +import org.onosproject.cpman.message.ControlMessageProviderRegistry;
28 +import org.onosproject.cpman.message.ControlMessageProviderService;
29 +import org.onosproject.cpman.message.ControlMessageService;
30 +import org.onosproject.event.DefaultEventSinkRegistry;
31 +import org.onosproject.event.Event;
32 +import org.onosproject.event.EventDeliveryService;
33 +import org.onosproject.event.EventSink;
34 +import org.onosproject.net.DeviceId;
35 +import org.onosproject.net.provider.AbstractProvider;
36 +import org.onosproject.net.provider.ProviderId;
37 +
38 +import java.util.ArrayList;
39 +import java.util.List;
40 +import java.util.Set;
41 +
42 +import static com.google.common.base.Preconditions.checkState;
43 +import static org.junit.Assert.assertEquals;
44 +import static org.junit.Assert.assertFalse;
45 +import static org.junit.Assert.assertTrue;
46 +import static org.onosproject.net.DeviceId.deviceId;
47 +import static org.onosproject.net.NetTestTools.injectEventDispatcher;
48 +
49 +/**
50 + * Unit test for control message manager.
51 + */
52 +public class ControlMessageManagerTest {
53 +
54 + private static final ProviderId PID = new ProviderId("of", "foo");
55 + private static final DeviceId DID = deviceId("of:foo");
56 +
57 + private ControlMessageManager manager;
58 + private ControlMessageService service;
59 + private ControlMessageProviderRegistry registry;
60 + private ControlMessageProviderService providerService;
61 + protected TestProvider provider;
62 + protected TestListener listener = new TestListener();
63 +
64 + /**
65 + * Initializes all variables for unit test.
66 + */
67 + @Before
68 + public void setUp() {
69 + manager = new ControlMessageManager();
70 + service = manager;
71 + registry = manager;
72 + manager.store = new DefaultControlMessageStore();
73 + injectEventDispatcher(manager, new TestEventDispatcher());
74 + manager.activate();
75 +
76 + service.addListener(listener);
77 +
78 + provider = new TestProvider();
79 + providerService = registry.register(provider);
80 + assertTrue("provider should be registered",
81 + registry.getProviders().contains(provider.id()));
82 + }
83 +
84 + /**
85 + * Tear down the control message manager.
86 + */
87 + @After
88 + public void tearDown() {
89 + registry.unregister(provider);
90 + assertFalse("provider should not be registered",
91 + registry.getProviders().contains(provider.id()));
92 + service.removeListener(listener);
93 + manager.deactivate();
94 + }
95 +
96 + /**
97 + * Tests the updateStatsInfo method.
98 + */
99 + @Test
100 + public void updateStatsInfo() {
101 + Set<ControlMessage> cms = Sets.newHashSet();
102 + ControlMessage.Type in = ControlMessage.Type.INBOUND_PACKET;
103 + ControlMessage.Type out = ControlMessage.Type.OUTBOUND_PACKET;
104 + ControlMessage.Type mod = ControlMessage.Type.FLOW_MOD_PACKET;
105 + ControlMessage.Type rmv = ControlMessage.Type.FLOW_REMOVED_PACKET;
106 + ControlMessage.Type req = ControlMessage.Type.REQUEST_PACKET;
107 + ControlMessage.Type rep = ControlMessage.Type.REPLY_PACKET;
108 +
109 + cms.add(new DefaultControlMessage(in, DID, 0, 0, 0, 0));
110 + cms.add(new DefaultControlMessage(out, DID, 0, 0, 0, 0));
111 + cms.add(new DefaultControlMessage(mod, DID, 0, 0, 0, 0));
112 + cms.add(new DefaultControlMessage(rmv, DID, 0, 0, 0, 0));
113 + cms.add(new DefaultControlMessage(req, DID, 0, 0, 0, 0));
114 + cms.add(new DefaultControlMessage(rep, DID, 0, 0, 0, 0));
115 +
116 + providerService.updateStatsInfo(DID, cms);
117 + validateEvents(ControlMessageEvent.Type.STATS_UPDATE);
118 + cms.clear();
119 + }
120 +
121 + /**
122 + * Validates whether the manager receives the right events.
123 + *
124 + * @param types a set of types of control message event
125 + */
126 + protected void validateEvents(Enum... types) {
127 + int i = 0;
128 + assertEquals("wrong events received", types.length, listener.events.size());
129 + for (Event event : listener.events) {
130 + assertEquals("incorrect event type", types[i], event.type());
131 + i++;
132 + }
133 + listener.events.clear();
134 + }
135 +
136 + /**
137 + * A mock of control message provider.
138 + */
139 + private class TestProvider extends AbstractProvider implements ControlMessageProvider {
140 +
141 + public TestProvider() {
142 + super(PID);
143 + }
144 + }
145 +
146 + /**
147 + * A mock of control message listener.
148 + */
149 + private static class TestListener implements ControlMessageListener {
150 + final List<ControlMessageEvent> events = new ArrayList<>();
151 +
152 + @Override
153 + public void event(ControlMessageEvent event) {
154 + events.add(event);
155 + }
156 + }
157 +
158 + /**
159 + * A mock of event dispatcher.
160 + */
161 + private class TestEventDispatcher extends DefaultEventSinkRegistry
162 + implements EventDeliveryService {
163 + @Override
164 + @SuppressWarnings("unchecked")
165 + public synchronized void post(Event event) {
166 + EventSink sink = getSink(event.getClass());
167 + checkState(sink != null, "No sink for event %s", event);
168 + sink.process(event);
169 + }
170 +
171 + @Override
172 + public void setDispatchTimeLimit(long millis) {
173 + }
174 +
175 + @Override
176 + public long getDispatchTimeLimit() {
177 + return 0;
178 + }
179 + }
180 +}
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
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.cpman.impl.message;
17 +
18 +import org.junit.Test;
19 +import org.onosproject.cpman.ControlMessage;
20 +import org.onosproject.cpman.ControlMetricType;
21 +
22 +import static org.hamcrest.Matchers.is;
23 +import static org.junit.Assert.assertThat;
24 +import static org.onosproject.cpman.impl.ControlMessageMetricMapper.lookupControlMessageType;
25 +import static org.onosproject.cpman.impl.ControlMessageMetricMapper.lookupControlMetricType;
26 +
27 +/**
28 + * Unit test for control message metric mapper.
29 + */
30 +public final class ControlMessageMetricMapperTest {
31 +
32 + /**
33 + * Tests whether control message metric mapper returns right control metric type.
34 + */
35 + @Test
36 + public void testLookupControlMetricType() {
37 + assertThat(lookupControlMetricType(ControlMessage.Type.INBOUND_PACKET),
38 + is(ControlMetricType.INBOUND_PACKET));
39 +
40 + assertThat(lookupControlMetricType(ControlMessage.Type.OUTBOUND_PACKET),
41 + is(ControlMetricType.OUTBOUND_PACKET));
42 +
43 + assertThat(lookupControlMetricType(ControlMessage.Type.FLOW_MOD_PACKET),
44 + is(ControlMetricType.FLOW_MOD_PACKET));
45 +
46 + assertThat(lookupControlMetricType(ControlMessage.Type.FLOW_REMOVED_PACKET),
47 + is(ControlMetricType.FLOW_REMOVED_PACKET));
48 +
49 + assertThat(lookupControlMetricType(ControlMessage.Type.REQUEST_PACKET),
50 + is(ControlMetricType.REQUEST_PACKET));
51 +
52 + assertThat(lookupControlMetricType(ControlMessage.Type.REPLY_PACKET),
53 + is(ControlMetricType.REPLY_PACKET));
54 + }
55 +
56 + /**
57 + * Tests whether control message metric mapper returns right control message type.
58 + */
59 + @Test
60 + public void testLookupControlMessageType() {
61 + assertThat(lookupControlMessageType(ControlMetricType.INBOUND_PACKET),
62 + is(ControlMessage.Type.INBOUND_PACKET));
63 +
64 + assertThat(lookupControlMessageType(ControlMetricType.OUTBOUND_PACKET),
65 + is(ControlMessage.Type.OUTBOUND_PACKET));
66 +
67 + assertThat(lookupControlMessageType(ControlMetricType.FLOW_MOD_PACKET),
68 + is(ControlMessage.Type.FLOW_MOD_PACKET));
69 +
70 + assertThat(lookupControlMessageType(ControlMetricType.FLOW_REMOVED_PACKET),
71 + is(ControlMessage.Type.FLOW_REMOVED_PACKET));
72 +
73 + assertThat(lookupControlMessageType(ControlMetricType.REQUEST_PACKET),
74 + is(ControlMessage.Type.REQUEST_PACKET));
75 +
76 + assertThat(lookupControlMessageType(ControlMetricType.REPLY_PACKET),
77 + is(ControlMessage.Type.REPLY_PACKET));
78 + }
79 +}