blob: fe355cc91526cd0a794ba2fee6133020d0c87a35 [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.handlers;
18
19import com.google.common.collect.Lists;
20import io.netty.channel.ChannelHandlerContext;
21import org.codehaus.stax2.ri.evt.AttributeEventImpl;
22import org.codehaus.stax2.ri.evt.NamespaceEventImpl;
23import org.codehaus.stax2.ri.evt.StartElementEventImpl;
24import org.dom4j.Element;
25import org.dom4j.tree.DefaultElement;
26import org.easymock.EasyMock;
27import org.junit.Before;
28import org.junit.Test;
29import org.onosproject.xmpp.core.ctl.exception.UnsupportedStanzaTypeException;
30import org.onosproject.xmpp.core.stream.XmppStreamOpen;
31import org.xmpp.packet.IQ;
32import org.xmpp.packet.JID;
33import org.xmpp.packet.Message;
34import org.xmpp.packet.Packet;
35import org.xmpp.packet.Presence;
36
37import javax.xml.namespace.QName;
38import javax.xml.stream.Location;
39import javax.xml.stream.events.Attribute;
40import javax.xml.stream.events.Namespace;
41import javax.xml.stream.events.XMLEvent;
42
43import java.util.List;
44
45import static org.hamcrest.Matchers.instanceOf;
46import static org.hamcrest.Matchers.is;
47import static org.hamcrest.MatcherAssert.assertThat;
48import static org.hamcrest.Matchers.notNullValue;
49
50/**
51 * Test class for XmppDecoder testing.
52 */
53public 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}