blob: 75780e58d3f006c7508575db25c7d60a0fb7ee58 [file] [log] [blame]
Tomek Osińskie9ccf412018-01-13 19:44:11 +01001/*
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
17package org.onosproject.xmpp.core.ctl;
18
19import java.util.List;
20import java.util.ArrayList;
21import java.util.Dictionary;
22import java.util.Hashtable;
23
24import com.google.common.collect.ImmutableSet;
Tomek Osiński7a1db182018-02-03 17:15:06 +010025import org.dom4j.Element;
26import org.dom4j.Namespace;
27import org.dom4j.tree.DefaultElement;
Tomek Osińskie9ccf412018-01-13 19:44:11 +010028import org.easymock.EasyMock;
29import org.junit.After;
30import org.junit.Before;
31import org.junit.Test;
32
33import static org.easymock.EasyMock.*;
34import static org.hamcrest.MatcherAssert.assertThat;
35import static org.hamcrest.Matchers.hasItems;
36import static org.hamcrest.Matchers.hasSize;
37import static org.hamcrest.Matchers.is;
38
39import org.onosproject.cfg.ComponentConfigService;
40import org.onosproject.core.CoreService;
41import org.onosproject.xmpp.core.XmppDevice;
42import org.onosproject.xmpp.core.XmppDeviceId;
43import org.onosproject.xmpp.core.XmppDeviceListener;
44import org.onosproject.xmpp.core.XmppIqListener;
45import org.onosproject.xmpp.core.XmppMessageListener;
46import org.onosproject.xmpp.core.XmppPresenceListener;
47import org.onosproject.xmpp.core.XmppDeviceAgent;
48import org.osgi.service.component.ComponentContext;
49import org.xmpp.packet.IQ;
50import org.xmpp.packet.JID;
51import org.xmpp.packet.Message;
52import org.xmpp.packet.Packet;
53import org.xmpp.packet.Presence;
54
55
56/**
57 * Test class for XmppControllerImpl.
58 */
59public class XmppControllerImplTest {
60
61 XmppControllerImpl controller;
62 XmppDeviceAgent agent;
63 TestXmppDeviceListener testXmppDeviceListener;
64 TestXmppIqListener testXmppIqListener;
65 TestXmppMessageListener testXmppMessageListener;
66 TestXmppPresenceListener testXmppPresenceListener;
67
68 XmppDevice device1;
69 XmppDeviceId jid1;
70 XmppDevice device2;
71 XmppDeviceId jid2;
72 XmppDevice device3;
73 XmppDeviceId jid3;
74
Tomek Osiński7a1db182018-02-03 17:15:06 +010075 final String testNamespace = "testns";
76
Tomek Osińskie9ccf412018-01-13 19:44:11 +010077 /**
78 * Test harness for a device listener.
79 */
80 static class TestXmppDeviceListener implements XmppDeviceListener {
81 final List<XmppDeviceId> removedDevices = new ArrayList<>();
82 final List<XmppDeviceId> addedDevices = new ArrayList<>();
83
84 @Override
85 public void deviceConnected(XmppDeviceId deviceId) {
86 addedDevices.add(deviceId);
87 }
88
89 @Override
90 public void deviceDisconnected(XmppDeviceId deviceId) {
91 removedDevices.add(deviceId);
92 }
93 }
94
95 static class TestXmppIqListener implements XmppIqListener {
96 final List<IQ> handledIqs = new ArrayList<>();
97
98 @Override
99 public void handleIqStanza(IQ iq) {
100 handledIqs.add(iq);
101 }
102
103 }
104
105 static class TestXmppMessageListener implements XmppMessageListener {
106 final List<Message> handledMessages = new ArrayList<>();
107
108 @Override
109 public void handleMessageStanza(Message message) {
110 handledMessages.add(message);
111 }
112 }
113
114 static class TestXmppPresenceListener implements XmppPresenceListener {
115 final List<Presence> handledPresenceStanzas = new ArrayList<>();
116
117 @Override
118 public void handlePresenceStanza(Presence presence) {
119 handledPresenceStanzas.add(presence);
120 }
121 }
122
123 /**
124 * Sets up devices to use as data, mocks and launches a controller instance.
125 */
126 @Before
127 public void setUp() {
128 device1 = new XmppDeviceAdapter();
129 jid1 = new XmppDeviceId(new JID("agent1@testxmpp.org"));
130 device2 = new XmppDeviceAdapter();
131 jid2 = new XmppDeviceId(new JID("agent2@testxmpp.org"));
132 device3 = new XmppDeviceAdapter();
133 jid3 = new XmppDeviceId(new JID("agent3@testxmpp.org"));
134
135 controller = new XmppControllerImpl();
136 agent = controller.agent;
137
138 testXmppDeviceListener = new TestXmppDeviceListener();
139 controller.addXmppDeviceListener(testXmppDeviceListener);
140 testXmppIqListener = new TestXmppIqListener();
Tomek Osiński7a1db182018-02-03 17:15:06 +0100141 controller.addXmppIqListener(testXmppIqListener, testNamespace);
Tomek Osińskie9ccf412018-01-13 19:44:11 +0100142 testXmppMessageListener = new TestXmppMessageListener();
143 controller.addXmppMessageListener(testXmppMessageListener);
144 testXmppPresenceListener = new TestXmppPresenceListener();
145 controller.addXmppPresenceListener(testXmppPresenceListener);
146
147 CoreService mockCoreService =
148 EasyMock.createMock(CoreService.class);
149 controller.coreService = mockCoreService;
150
151 ComponentConfigService mockCfgService =
152 EasyMock.createMock(ComponentConfigService.class);
153 expect(mockCfgService.getProperties(anyObject())).andReturn(ImmutableSet.of());
154 mockCfgService.registerProperties(controller.getClass());
155 expectLastCall();
156 mockCfgService.unregisterProperties(controller.getClass(), false);
157 expectLastCall();
158 expect(mockCfgService.getProperties(anyObject())).andReturn(ImmutableSet.of());
159 controller.cfgService = mockCfgService;
160 replay(mockCfgService);
161
162 ComponentContext mockContext = EasyMock.createMock(ComponentContext.class);
163 Dictionary<String, Object> properties = new Hashtable<>();
164 properties.put("xmppPort",
165 "5269");
166 expect(mockContext.getProperties()).andReturn(properties);
167 replay(mockContext);
168 controller.activate(mockContext);
169 }
170
171 @After
172 public void tearDown() {
173 controller.removeXmppDeviceListener(testXmppDeviceListener);
Tomek Osiński7a1db182018-02-03 17:15:06 +0100174 controller.removeXmppIqListener(testXmppIqListener, testNamespace);
Tomek Osińskie9ccf412018-01-13 19:44:11 +0100175 controller.removeXmppMessageListener(testXmppMessageListener);
176 controller.removeXmppPresenceListener(testXmppPresenceListener);
177 controller.deactivate();
178 }
179
180 /**
181 * Tests adding and removing connected devices.
182 */
183 @Test
184 public void testAddRemoveConnectedDevice() {
185 // test adding connected devices
186 boolean add1 = agent.addConnectedDevice(jid1, device1);
187 assertThat(add1, is(true));
188 assertThat(testXmppDeviceListener.addedDevices, hasSize(1));
189 boolean add2 = agent.addConnectedDevice(jid2, device2);
190 assertThat(add2, is(true));
191 assertThat(testXmppDeviceListener.addedDevices, hasSize(2));
192 boolean add3 = agent.addConnectedDevice(jid3, device3);
193 assertThat(add3, is(true));
194 assertThat(testXmppDeviceListener.addedDevices, hasSize(3));
195
196 assertThat(testXmppDeviceListener.addedDevices, hasItems(jid1, jid2, jid3));
197
198 // Test adding a device twice - it should fail
199 boolean addError1 = agent.addConnectedDevice(jid1, device1);
200 assertThat(addError1, is(false));
201
202 assertThat(controller.connectedDevices.size(), is(3));
203
204 // test querying the individual device
205 XmppDevice queriedDevice = controller.getDevice(jid1);
206 assertThat(queriedDevice, is(device1));
207
208 // test removing device
209 agent.removeConnectedDevice(jid3);
210 assertThat(controller.connectedDevices.size(), is(2));
211
212 // Make sure the listener delete callbacks fired
213 assertThat(testXmppDeviceListener.removedDevices, hasSize(1));
214 assertThat(testXmppDeviceListener.removedDevices, hasItems(jid3));
215 }
216
217 /**
218 * Tests adding, removing IQ listeners and handling IQ stanzas.
219 */
220 @Test
221 public void handlePackets() {
222 // IQ packets
Tomek Osiński7a1db182018-02-03 17:15:06 +0100223 IQ iq = new IQ();
224 Element element = new DefaultElement("pubsub", Namespace.get(testNamespace));
225 iq.setChildElement(element);
Tomek Osińskie9ccf412018-01-13 19:44:11 +0100226 agent.processUpstreamEvent(jid1, iq);
227 assertThat(testXmppIqListener.handledIqs, hasSize(1));
228 agent.processUpstreamEvent(jid2, iq);
229 assertThat(testXmppIqListener.handledIqs, hasSize(2));
230 // Message packets
231 Packet message = new Message();
232 agent.processUpstreamEvent(jid1, message);
233 assertThat(testXmppMessageListener.handledMessages, hasSize(1));
234 agent.processUpstreamEvent(jid2, message);
235 assertThat(testXmppMessageListener.handledMessages, hasSize(2));
236 Packet presence = new Presence();
237 agent.processUpstreamEvent(jid1, presence);
238 assertThat(testXmppPresenceListener.handledPresenceStanzas, hasSize(1));
239 agent.processUpstreamEvent(jid2, presence);
240 assertThat(testXmppPresenceListener.handledPresenceStanzas, hasSize(2));
241 }
242
243
244}