Tomek OsiĆski | e9ccf41 | 2018-01-13 19:44:11 +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.core.ctl.handlers; |
| 18 | |
| 19 | import com.google.common.collect.Lists; |
| 20 | import io.netty.channel.ChannelHandlerContext; |
| 21 | import org.codehaus.stax2.ri.evt.AttributeEventImpl; |
| 22 | import org.codehaus.stax2.ri.evt.NamespaceEventImpl; |
| 23 | import org.codehaus.stax2.ri.evt.StartElementEventImpl; |
| 24 | import org.dom4j.Element; |
| 25 | import org.dom4j.tree.DefaultElement; |
| 26 | import org.easymock.EasyMock; |
| 27 | import org.junit.Before; |
| 28 | import org.junit.Test; |
| 29 | import org.onosproject.xmpp.core.ctl.exception.UnsupportedStanzaTypeException; |
| 30 | import org.onosproject.xmpp.core.stream.XmppStreamOpen; |
| 31 | import org.xmpp.packet.IQ; |
| 32 | import org.xmpp.packet.JID; |
| 33 | import org.xmpp.packet.Message; |
| 34 | import org.xmpp.packet.Packet; |
| 35 | import org.xmpp.packet.Presence; |
| 36 | |
| 37 | import javax.xml.namespace.QName; |
| 38 | import javax.xml.stream.Location; |
| 39 | import javax.xml.stream.events.Attribute; |
| 40 | import javax.xml.stream.events.Namespace; |
| 41 | import javax.xml.stream.events.XMLEvent; |
| 42 | |
| 43 | import java.util.List; |
| 44 | |
| 45 | import static org.hamcrest.Matchers.instanceOf; |
| 46 | import static org.hamcrest.Matchers.is; |
| 47 | import static org.hamcrest.MatcherAssert.assertThat; |
| 48 | import static org.hamcrest.Matchers.notNullValue; |
| 49 | |
| 50 | /** |
| 51 | * Test class for XmppDecoder testing. |
| 52 | */ |
| 53 | public class XmppDecoderTest { |
| 54 | |
| 55 | XmppDecoder xmppDecoder; |
| 56 | Element xmppStanzaElement; |
| 57 | XMLEvent streamOpen; |
| 58 | |
| 59 | Element iqElement = new IQ().getElement(); |
| 60 | Element messageElement = new Message().getElement(); |
| 61 | Element presenceElement = new Presence().getElement(); |
| 62 | |
| 63 | ChannelHandlerContext mockChannelHandlerContext; |
| 64 | Location mockLocation; |
| 65 | |
| 66 | @Before |
| 67 | public void setUp() { |
| 68 | xmppDecoder = new XmppDecoder(); |
| 69 | mockChannelHandlerContext = EasyMock.createMock(ChannelHandlerContext.class); |
| 70 | mockLocation = EasyMock.createMock(Location.class); |
| 71 | buildXmppStanza(); |
| 72 | buildXmppStreamOpen(); |
| 73 | |
| 74 | } |
| 75 | |
| 76 | private void buildXmppStreamOpen() { |
| 77 | QName qName = new QName("http://etherx.jabber.org/streams", "stream", "stream"); |
| 78 | Attribute attrTo = new AttributeEventImpl(mockLocation, QName.valueOf("to"), "xmpp.onosproject.org", true); |
| 79 | Attribute attrFrom = new AttributeEventImpl(mockLocation, QName.valueOf("from"), "test@xmpp.org", true); |
| 80 | List<Attribute> attributes = Lists.newArrayList(); |
| 81 | attributes.add(attrTo); |
| 82 | attributes.add(attrFrom); |
| 83 | Namespace streamNs = NamespaceEventImpl.constructNamespace(mockLocation, "stream", |
| 84 | "http://etherx.jabber.org/streams"); |
| 85 | Namespace jabberNs = NamespaceEventImpl.constructDefaultNamespace(mockLocation, "jabber:client"); |
| 86 | List<Namespace> namespaces = Lists.newArrayList(); |
| 87 | namespaces.add(streamNs); |
| 88 | namespaces.add(jabberNs); |
| 89 | streamOpen = StartElementEventImpl.construct(mockLocation, qName, attributes.iterator(), |
| 90 | namespaces.iterator(), null); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | private void buildXmppStanza() { |
| 95 | xmppStanzaElement = new DefaultElement("iq"); |
| 96 | xmppStanzaElement.addAttribute("type", "set"); |
| 97 | xmppStanzaElement.addAttribute("from", "test@xmpp.org"); |
| 98 | xmppStanzaElement.addAttribute("to", "xmpp.onosproject.org"); |
| 99 | Element pubsub = new DefaultElement("pubsub", |
| 100 | new org.dom4j.Namespace("", "http://jabber.org/protocol/pubsub")); |
| 101 | Element subscribe = new DefaultElement("subscribe"); |
| 102 | subscribe.addAttribute("node", "test"); |
| 103 | pubsub.add(subscribe); |
| 104 | xmppStanzaElement.add(pubsub); |
| 105 | } |
| 106 | |
| 107 | @Test |
| 108 | public void testDecodeStream() throws Exception { |
| 109 | List<Object> out = Lists.newArrayList(); |
| 110 | xmppDecoder.decode(mockChannelHandlerContext, streamOpen, out); |
| 111 | assertThat(out.size(), is(1)); |
| 112 | assertThat(out.get(0), is(instanceOf(XmppStreamOpen.class))); |
| 113 | XmppStreamOpen stream = (XmppStreamOpen) out.get(0); |
| 114 | assertThat(stream.getElement(), is(notNullValue())); |
| 115 | assertThat(stream.getToJid(), is(new JID("xmpp.onosproject.org"))); |
| 116 | assertThat(stream.getFromJid(), is(new JID("test@xmpp.org"))); |
| 117 | } |
| 118 | |
| 119 | @Test |
| 120 | public void testDecodeXmppStanza() throws Exception { |
| 121 | // TODO: complete it |
| 122 | List<Object> out = Lists.newArrayList(); |
| 123 | xmppDecoder.decode(mockChannelHandlerContext, xmppStanzaElement, out); |
| 124 | assertThat(out.size(), is(1)); |
| 125 | assertThat(out.get(0), is(instanceOf(Packet.class))); |
| 126 | assertThat(out.get(0), is(instanceOf(IQ.class))); |
| 127 | IQ iq = (IQ) out.get(0); |
| 128 | assertThat(iq.getElement(), is(notNullValue())); |
| 129 | assertThat(iq.getFrom(), is(new JID("test@xmpp.org"))); |
| 130 | assertThat(iq.getTo(), is(new JID("xmpp.onosproject.org"))); |
| 131 | assertThat(iq.getType(), is(IQ.Type.set)); |
| 132 | } |
| 133 | |
| 134 | @Test |
| 135 | public void testRecognizePacket() throws Exception { |
| 136 | Packet iqPacket = xmppDecoder.recognizeAndReturnXmppPacket(iqElement); |
| 137 | assertThat(iqPacket, is(instanceOf(IQ.class))); |
| 138 | Packet messagePacket = xmppDecoder.recognizeAndReturnXmppPacket(messageElement); |
| 139 | assertThat(messagePacket, is(instanceOf(Message.class))); |
| 140 | Packet presencePacket = xmppDecoder.recognizeAndReturnXmppPacket(presenceElement); |
| 141 | assertThat(presencePacket, is(instanceOf(Presence.class))); |
| 142 | Element wrongElement = new DefaultElement("test"); |
| 143 | try { |
| 144 | xmppDecoder.recognizeAndReturnXmppPacket(wrongElement); |
| 145 | } catch (Exception e) { |
| 146 | assertThat(e, is(instanceOf(UnsupportedStanzaTypeException.class))); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | } |