Tomek OsiĆski | 7a1db18 | 2018-02-03 17:15:06 +0100 | [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 | |
| 17 | package org.onosproject.xmpp.pubsub.ctl; |
| 18 | |
| 19 | import com.google.common.collect.Lists; |
| 20 | import org.dom4j.Document; |
| 21 | import org.dom4j.Element; |
| 22 | import org.dom4j.Namespace; |
| 23 | import org.dom4j.tree.DefaultElement; |
| 24 | import org.junit.Before; |
| 25 | import org.junit.Test; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.xmpp.core.XmppController; |
| 28 | import org.onosproject.xmpp.core.XmppDevice; |
| 29 | import org.onosproject.xmpp.core.XmppDeviceId; |
| 30 | import org.onosproject.xmpp.core.XmppDeviceListener; |
| 31 | import org.onosproject.xmpp.core.XmppIqListener; |
| 32 | import org.onosproject.xmpp.core.XmppMessageListener; |
| 33 | import org.onosproject.xmpp.core.XmppPresenceListener; |
| 34 | import org.onosproject.xmpp.core.XmppSession; |
| 35 | import org.onosproject.xmpp.pubsub.XmppPubSubConstants; |
| 36 | import org.onosproject.xmpp.pubsub.XmppPublishEventsListener; |
| 37 | import org.onosproject.xmpp.pubsub.XmppSubscribeEventsListener; |
| 38 | import org.onosproject.xmpp.pubsub.model.XmppEventNotification; |
| 39 | import org.onosproject.xmpp.pubsub.model.XmppPubSubError; |
| 40 | import org.onosproject.xmpp.pubsub.model.XmppPublish; |
| 41 | import org.onosproject.xmpp.pubsub.model.XmppRetract; |
| 42 | import org.onosproject.xmpp.pubsub.model.XmppSubscribe; |
| 43 | import org.onosproject.xmpp.pubsub.model.XmppUnsubscribe; |
| 44 | import org.xmpp.packet.IQ; |
| 45 | import org.xmpp.packet.Packet; |
| 46 | import org.xmpp.packet.PacketError; |
| 47 | |
| 48 | import java.net.InetSocketAddress; |
| 49 | import java.util.List; |
| 50 | |
| 51 | import static org.hamcrest.CoreMatchers.is; |
| 52 | import static org.hamcrest.CoreMatchers.notNullValue; |
| 53 | import static org.hamcrest.CoreMatchers.nullValue; |
| 54 | import static org.hamcrest.MatcherAssert.assertThat; |
| 55 | import static org.onosproject.xmpp.pubsub.XmppPubSubConstants.PubSubApplicationCondition.ITEM_NOT_FOUND; |
| 56 | |
| 57 | /** |
| 58 | * Test class for XmppPubSubController class. |
| 59 | */ |
| 60 | public class XmppPubSubControllerTest { |
| 61 | |
| 62 | private String nodeAttribute = "test"; |
| 63 | private String node = "node"; |
| 64 | private String toJid = "xmpp@onosproject.org"; |
| 65 | private String fromJid = "test@xmpp.org"; |
| 66 | private String pubSub = "pubsub"; |
| 67 | private String publish = "publish"; |
| 68 | private String retract = "retract"; |
| 69 | private String subscribe = "subscribe"; |
| 70 | private String unsubscribe = "unsubscribe"; |
| 71 | private String item = "item"; |
| 72 | private String id = "id"; |
| 73 | private String itemId = "id-000"; |
| 74 | private String entry = "entry"; |
| 75 | private String testNamespace = "jabber:test:item"; |
| 76 | |
| 77 | |
| 78 | XmppPubSubControllerImpl pubSubController; |
| 79 | XmppControllerAdapter xmppControllerAdapter; |
| 80 | XmppDeviceAdapter testDevice; |
| 81 | |
| 82 | TestXmppPublishEventsListener testXmppPublishEventsListener; |
| 83 | TestXmppSubscribeEventsListener testXmppSubscribeEventsListener; |
| 84 | |
| 85 | static class TestXmppPublishEventsListener implements XmppPublishEventsListener { |
| 86 | |
| 87 | final List<XmppPublish> handledPublishMsgs = Lists.newArrayList(); |
| 88 | final List<XmppRetract> handledRetractMsgs = Lists.newArrayList(); |
| 89 | |
| 90 | @Override |
| 91 | public void handlePublish(XmppPublish publishEvent) { |
| 92 | handledPublishMsgs.add(publishEvent); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public void handleRetract(XmppRetract retractEvent) { |
| 97 | handledRetractMsgs.add(retractEvent); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | static class TestXmppSubscribeEventsListener implements XmppSubscribeEventsListener { |
| 102 | |
| 103 | final List<XmppSubscribe> handledSubscribeMsgs = Lists.newArrayList(); |
| 104 | final List<XmppUnsubscribe> handledUnsubscribeMsgs = Lists.newArrayList(); |
| 105 | |
| 106 | |
| 107 | @Override |
| 108 | public void handleSubscribe(XmppSubscribe subscribeEvent) { |
| 109 | handledSubscribeMsgs.add(subscribeEvent); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public void handleUnsubscribe(XmppUnsubscribe unsubscribeEvent) { |
| 114 | handledUnsubscribeMsgs.add(unsubscribeEvent); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | @Before |
| 119 | public void setUp() { |
| 120 | testDevice = new XmppDeviceAdapter(); |
| 121 | xmppControllerAdapter = new XmppControllerAdapter(); |
| 122 | pubSubController = new XmppPubSubControllerImpl(); |
| 123 | pubSubController.xmppController = xmppControllerAdapter; |
| 124 | testXmppPublishEventsListener = new TestXmppPublishEventsListener(); |
| 125 | testXmppSubscribeEventsListener = new TestXmppSubscribeEventsListener(); |
| 126 | pubSubController.activate(); |
| 127 | } |
| 128 | |
| 129 | @Test |
| 130 | public void testActivate() { |
| 131 | assertThat(xmppControllerAdapter.iqListener, is(notNullValue())); |
| 132 | } |
| 133 | |
| 134 | @Test |
| 135 | public void testDeactivate() { |
| 136 | pubSubController.deactivate(); |
| 137 | assertThat(xmppControllerAdapter.iqListener, is(nullValue())); |
| 138 | } |
| 139 | |
| 140 | @Test |
| 141 | public void testAddRemoveListeners() { |
| 142 | pubSubController.addXmppPublishEventsListener(testXmppPublishEventsListener); |
| 143 | assertThat(pubSubController.xmppPublishEventsListeners.size(), is(1)); |
| 144 | pubSubController.addXmppSubscribeEventsListener(testXmppSubscribeEventsListener); |
| 145 | assertThat(pubSubController.xmppSubscribeEventsListeners.size(), is(1)); |
| 146 | pubSubController.removeXmppPublishEventsListener(testXmppPublishEventsListener); |
| 147 | assertThat(pubSubController.xmppPublishEventsListeners.size(), is(0)); |
| 148 | pubSubController.removeXmppSubscribeEventsListener(testXmppSubscribeEventsListener); |
| 149 | assertThat(pubSubController.xmppSubscribeEventsListeners.size(), is(0)); |
| 150 | } |
| 151 | |
| 152 | @Test |
| 153 | public void testNotifyEvent() { |
| 154 | XmppEventNotification eventNotification = new XmppEventNotification(nodeAttribute, |
| 155 | new DefaultElement(nodeAttribute)); |
| 156 | pubSubController.notify(DeviceId.NONE, eventNotification); |
| 157 | assertThat(testDevice.sentPackets.size(), is(1)); |
| 158 | assertThat(testDevice.sentPackets.get(0), is(eventNotification)); |
| 159 | } |
| 160 | |
| 161 | @Test |
| 162 | public void testNotifyError() { |
| 163 | XmppPubSubError xmppPubSubError = |
| 164 | new XmppPubSubError(ITEM_NOT_FOUND); |
| 165 | pubSubController.notifyError(DeviceId.NONE, xmppPubSubError); |
| 166 | assertThat(testDevice.sentErrors.size(), is(1)); |
| 167 | } |
| 168 | |
| 169 | @Test |
| 170 | public void testHandlePubSubMessages() { |
| 171 | pubSubController.addXmppPublishEventsListener(testXmppPublishEventsListener); |
| 172 | pubSubController.addXmppSubscribeEventsListener(testXmppSubscribeEventsListener); |
| 173 | XmppSubscribe xmppSubscribe = buildXmppSubscribe(); |
| 174 | xmppControllerAdapter.iqListener.handleIqStanza(xmppSubscribe); |
| 175 | assertThat(testXmppSubscribeEventsListener.handledSubscribeMsgs.size(), is(1)); |
| 176 | XmppUnsubscribe xmppUnsubscribe = buildXmppUnsubscribe(); |
| 177 | xmppControllerAdapter.iqListener.handleIqStanza(xmppUnsubscribe); |
| 178 | assertThat(testXmppSubscribeEventsListener.handledUnsubscribeMsgs.size(), is(1)); |
| 179 | XmppPublish xmppPublish = buildXmppPublish(); |
| 180 | xmppControllerAdapter.iqListener.handleIqStanza(xmppPublish); |
| 181 | assertThat(testXmppPublishEventsListener.handledPublishMsgs.size(), is(1)); |
| 182 | XmppRetract xmppRetract = buildXmppRetract(); |
| 183 | xmppControllerAdapter.iqListener.handleIqStanza(xmppRetract); |
| 184 | assertThat(testXmppPublishEventsListener.handledRetractMsgs.size(), is(1)); |
| 185 | } |
| 186 | |
| 187 | private XmppSubscribe buildXmppSubscribe() { |
| 188 | IQ iq = new IQ(IQ.Type.set); |
| 189 | iq.setTo(toJid); |
| 190 | iq.setFrom(fromJid); |
| 191 | Element element = new DefaultElement(pubSub, Namespace.get(XmppPubSubConstants.PUBSUB_NAMESPACE)); |
| 192 | Element childElement = new DefaultElement(subscribe); |
| 193 | childElement.addAttribute(node, nodeAttribute); |
| 194 | element.add(childElement); |
| 195 | iq.setChildElement(element); |
| 196 | XmppSubscribe xmppSubscribe = new XmppSubscribe(iq); |
| 197 | return xmppSubscribe; |
| 198 | } |
| 199 | |
| 200 | private XmppUnsubscribe buildXmppUnsubscribe() { |
| 201 | IQ iq = new IQ(IQ.Type.set); |
| 202 | iq.setTo(toJid); |
| 203 | iq.setFrom(fromJid); |
| 204 | Element element = new DefaultElement(pubSub, Namespace.get(XmppPubSubConstants.PUBSUB_NAMESPACE)); |
| 205 | Element childElement = new DefaultElement(unsubscribe); |
| 206 | childElement.addAttribute(node, nodeAttribute); |
| 207 | element.add(childElement); |
| 208 | iq.setChildElement(element); |
| 209 | XmppUnsubscribe xmppUnsubscribe = new XmppUnsubscribe(iq); |
| 210 | return xmppUnsubscribe; |
| 211 | } |
| 212 | |
| 213 | private XmppPublish buildXmppPublish() { |
| 214 | IQ iq = new IQ(IQ.Type.set); |
| 215 | iq.setTo(toJid); |
| 216 | iq.setFrom(fromJid); |
| 217 | Element element = new DefaultElement(pubSub, Namespace.get(XmppPubSubConstants.PUBSUB_NAMESPACE)); |
| 218 | Element publishElement = new DefaultElement(publish).addAttribute(node, nodeAttribute); |
| 219 | Element itemElement = new DefaultElement(item).addAttribute(id, itemId); |
| 220 | Element entryElement = new DefaultElement(entry, Namespace.get(testNamespace)); |
| 221 | itemElement.add(entryElement); |
| 222 | publishElement.add(itemElement); |
| 223 | element.add(publishElement); |
| 224 | iq.setChildElement(element); |
| 225 | XmppPublish xmppPublish = new XmppPublish(iq); |
| 226 | return xmppPublish; |
| 227 | } |
| 228 | |
| 229 | private XmppRetract buildXmppRetract() { |
| 230 | IQ iq = new IQ(IQ.Type.set); |
| 231 | iq.setTo(toJid); |
| 232 | iq.setFrom(fromJid); |
| 233 | Element element = new DefaultElement(pubSub, Namespace.get(XmppPubSubConstants.PUBSUB_NAMESPACE)); |
| 234 | Element retractElement = new DefaultElement(retract).addAttribute(node, nodeAttribute); |
| 235 | Element itemElement = new DefaultElement(item).addAttribute(id, itemId); |
| 236 | retractElement.add(itemElement); |
| 237 | element.add(retractElement); |
| 238 | iq.setChildElement(element); |
| 239 | XmppRetract xmppRetract = new XmppRetract(iq); |
| 240 | return xmppRetract; |
| 241 | } |
| 242 | |
| 243 | |
| 244 | private class XmppControllerAdapter implements XmppController { |
| 245 | |
| 246 | XmppIqListener iqListener; |
| 247 | |
| 248 | @Override |
| 249 | public XmppDevice getDevice(XmppDeviceId xmppDeviceId) { |
| 250 | return testDevice; |
| 251 | } |
| 252 | |
| 253 | @Override |
| 254 | public void addXmppDeviceListener(XmppDeviceListener deviceListener) { |
| 255 | |
| 256 | } |
| 257 | |
| 258 | @Override |
| 259 | public void removeXmppDeviceListener(XmppDeviceListener deviceListener) { |
| 260 | |
| 261 | } |
| 262 | |
| 263 | @Override |
| 264 | public void addXmppIqListener(XmppIqListener iqListener, String namespace) { |
| 265 | this.iqListener = iqListener; |
| 266 | } |
| 267 | |
| 268 | @Override |
| 269 | public void removeXmppIqListener(XmppIqListener iqListener, String namespace) { |
| 270 | this.iqListener = null; |
| 271 | } |
| 272 | |
| 273 | @Override |
| 274 | public void addXmppMessageListener(XmppMessageListener messageListener) { |
| 275 | |
| 276 | } |
| 277 | |
| 278 | @Override |
| 279 | public void removeXmppMessageListener(XmppMessageListener messageListener) { |
| 280 | |
| 281 | } |
| 282 | |
| 283 | @Override |
| 284 | public void addXmppPresenceListener(XmppPresenceListener presenceListener) { |
| 285 | |
| 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public void removeXmppPresenceListener(XmppPresenceListener presenceListener) { |
| 290 | |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | private class XmppDeviceAdapter implements XmppDevice { |
| 295 | |
| 296 | final List<Packet> sentPackets = Lists.newArrayList(); |
| 297 | final List<PacketError> sentErrors = Lists.newArrayList(); |
| 298 | |
| 299 | @Override |
| 300 | public XmppSession getSession() { |
| 301 | return null; |
| 302 | } |
| 303 | |
| 304 | @Override |
| 305 | public InetSocketAddress getIpAddress() { |
| 306 | return null; |
| 307 | } |
| 308 | |
| 309 | @Override |
| 310 | public void registerConnectedDevice() { |
| 311 | |
| 312 | } |
| 313 | |
| 314 | @Override |
| 315 | public void disconnectDevice() { |
| 316 | |
| 317 | } |
| 318 | |
| 319 | @Override |
| 320 | public void sendPacket(Packet packet) { |
| 321 | sentPackets.add(packet); |
| 322 | } |
| 323 | |
| 324 | @Override |
| 325 | public void writeRawXml(Document document) { |
| 326 | |
| 327 | } |
| 328 | |
| 329 | @Override |
| 330 | public void handlePacket(Packet packet) { |
| 331 | |
| 332 | } |
| 333 | |
| 334 | @Override |
| 335 | public void sendError(PacketError packetError) { |
| 336 | sentErrors.add(packetError); |
| 337 | } |
| 338 | |
| 339 | } |
| 340 | |
| 341 | } |