blob: ab2e53ac4543594c84de872762937aea5d34377a [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.fasterxml.aalto.AsyncByteArrayFeeder;
20import com.fasterxml.aalto.AsyncXMLStreamReader;
21import com.fasterxml.aalto.stax.InputFactoryImpl;
22import com.google.common.collect.Lists;
23import org.codehaus.stax2.ri.evt.Stax2EventAllocatorImpl;
24import org.dom4j.Element;
25import org.hamcrest.Matchers;
26import org.junit.Before;
27import org.junit.Test;
28import org.onosproject.xmpp.core.ctl.ChannelHandlerContextAdapter;
29
30import javax.xml.stream.XMLStreamException;
31import javax.xml.stream.events.XMLEvent;
32import java.io.UnsupportedEncodingException;
33import java.util.List;
34
35import static org.hamcrest.CoreMatchers.is;
36import static org.hamcrest.MatcherAssert.assertThat;
37import static org.hamcrest.Matchers.instanceOf;
38import static org.hamcrest.Matchers.notNullValue;
39import static org.junit.Assert.assertNotNull;
40import static org.junit.Assert.fail;
41
42/**
43 * Test class for XmlMerger class.
44 */
45public class XmlMergerTest {
46
47 List<Object> streamOpenXmlEventList;
48 List<Object> streamCloseXmlEventList;
49 List<Object> subscribeMsgEventList;
50 List<Object> publishMsgEventList;
51
52 XmlMerger xmlMerger;
53
54 private String streamOpenMsg = String.format("<stream:stream to='%s' %s %s %s %s %s>", "xmpp.onosproject.org",
55 "from='test@xmpp.org'",
56 "xmlns:stream='http://etherx.jabber.org/streams'",
57 "xmlns='jabber:client'", "xml:lang='en'", "version='1.0'");
58
59 private String streamCloseMsg = "</stream:stream>";
60
61 private String publishMsg = "<iq type='set'\n" +
62 " from='test@xmpp.org'\n" +
63 " to='xmpp.onosproject.org'\n" +
64 " id='request1'>\n" +
65 " <pubsub xmlns='http://jabber.org/protocol/pubsub'>\n" +
66 " <publish node='test'>\n" +
67 " <item id=\"test\">\n" +
68 " <entry xmlns='http://ietf.org/protocol/bgpvpn'>\n" +
69 " <nlri af='1'>10.0.0.1</nlri>\n" +
70 " <next-hop af='1'>169.1.1.1</next-hop>\n" +
71 " <version id='1'/>\n" +
72 " <label>10000</label>\n" +
73 " </entry> \n" +
74 " </item>\n" +
75 " </publish>\n" +
76 " </pubsub>\n" +
77 "</iq>\n";
78
79 private String subscribeMsg = "<iq type='set'\n" +
80 " from='test@xmpp.org'\n" +
81 " to='xmpp.onosproject.org/other-peer'\n" +
82 " id='sub1'>\n" +
83 " <pubsub xmlns='http://jabber.org/protocol/pubsub'>\n" +
84 " <subscribe node='test'/>\n" +
85 " </pubsub>\n" +
86 "</iq>";
87
88 AsyncXMLStreamReader<AsyncByteArrayFeeder> streamReader =
89 new InputFactoryImpl().createAsyncForByteArray();
90 Stax2EventAllocatorImpl allocator = new Stax2EventAllocatorImpl();
91
92 @Before
93 public void setUp() throws Exception {
94 xmlMerger = new XmlMerger();
95 streamOpenXmlEventList = Lists.newArrayList();
96 streamCloseXmlEventList = Lists.newArrayList();
97 subscribeMsgEventList = Lists.newArrayList();
98 publishMsgEventList = Lists.newArrayList();
99 initXmlEventList(streamOpenXmlEventList, streamOpenMsg);
100 initXmlEventList(subscribeMsgEventList, subscribeMsg);
101 initXmlEventList(publishMsgEventList, publishMsg);
102 initXmlEventList(streamCloseXmlEventList, streamCloseMsg);
103 streamReader.closeCompletely();
104 }
105
106 private void initXmlEventList(List<Object> xmlEventList, String xmlMessage)
107 throws XMLStreamException, UnsupportedEncodingException {
108 AsyncByteArrayFeeder streamFeeder = streamReader.getInputFeeder();
109 byte[] buffer = xmlMessage.getBytes("UTF-8");
110 streamFeeder.feedInput(buffer, 0, buffer.length);
111 while (streamReader.hasNext() && streamReader.next() != AsyncXMLStreamReader.EVENT_INCOMPLETE) {
112 xmlEventList.add(allocator.allocate(streamReader));
113 }
114 }
115
116 @Test
117 public void testInit() throws Exception {
118 assertThat(xmlMerger.docBuilder, is(notNullValue()));
119 assertThat(xmlMerger.document, is(notNullValue()));
120 assertThat(xmlMerger.result, is(notNullValue()));
121 assertThat(xmlMerger.writer, is(notNullValue()));
122 assertThat(xmlMerger.xmlOutputFactory, is(notNullValue()));
123 }
124
125 @Test
126 public void testMergeStreamOpen() throws Exception {
127 List<Object> list = Lists.newArrayList();
128 streamOpenXmlEventList.forEach(xmlEvent -> {
129 try {
130 xmlMerger.decode(new ChannelHandlerContextAdapter(), xmlEvent, list);
131 } catch (Exception e) {
132 fail();
133 }
134 });
135 // StreamOpen should not be merged, should be passed as XMLEvent
136 assertThat(list.size(), Matchers.is(1));
137 assertThat(list.get(0), Matchers.is(instanceOf(XMLEvent.class)));
138 assertThat(((XMLEvent) list.get(0)).isStartElement(), Matchers.is(true));
139 }
140
141 @Test
142 public void testMergeSubscribeMsg() throws Exception {
143 List<Object> list = Lists.newArrayList();
144 xmlMerger.depth = 1;
145 subscribeMsgEventList.forEach(xmlEvent -> {
146 try {
147 xmlMerger.decode(new ChannelHandlerContextAdapter(), xmlEvent, list);
148 } catch (Exception e) {
149 fail();
150 }
151 });
152 assertThat("Output list should have size of 1", list.size(), Matchers.is(1));
153 assertThat("Output object should be of type org.dom4j.Element",
154 list.get(0), Matchers.is(instanceOf(Element.class)));
155 Element root = (Element) list.get(0);
156 assertThat("Top level element should be of type IQ",
157 root.getQName().getName(), Matchers.is("iq"));
158 assertThat(root.attributes().size(), Matchers.is(4));
159 assertThat(root.attribute("type").getValue(), Matchers.is("set"));
160 assertNotNull("<pubsub> element should be accessible", root.element("pubsub"));
161 assertThat(root.element("pubsub").getNamespaceURI(), Matchers.is("http://jabber.org/protocol/pubsub"));
162 assertNotNull("<subscribe> element should be accessible",
163 root.element("pubsub").element("subscribe"));
164
165 }
166
167 @Test
168 public void testMergePublishMsg() throws Exception {
169 List<Object> list = Lists.newArrayList();
170 xmlMerger.depth = 1;
171 publishMsgEventList.forEach(xmlEvent -> {
172 try {
173 xmlMerger.decode(new ChannelHandlerContextAdapter(), xmlEvent, list);
174 } catch (Exception e) {
175 fail();
176 }
177 });
178 Element root = (Element) list.get(0);
179 assertThat("Top level element should be of type IQ",
180 root.getQName().getName(), Matchers.is("iq"));
181 assertThat(root.attributes().size(), Matchers.is(4));
182 assertNotNull("<pubsub> element should be accessible", root.element("pubsub"));
183 assertNotNull("<publish> element should be accessible",
184 root.element("pubsub").element("publish"));
185 assertThat(root.element("pubsub").getNamespaceURI(), Matchers.is("http://jabber.org/protocol/pubsub"));
186 assertThat(root.element("pubsub").element("publish").attribute("node").getValue(),
187 Matchers.is("test"));
188 }
189
190 @Test
191 public void testMergeStreamClose() throws Exception {
192 List<Object> list = Lists.newArrayList();
193 streamCloseXmlEventList.forEach(xmlEvent -> {
194 try {
195 xmlMerger.decode(new ChannelHandlerContextAdapter(), xmlEvent, list);
196 } catch (Exception e) {
197 fail();
198 }
199 });
200 // StreamClose should not be merged, should be passed as XMLEvent
201 assertThat(list.size(), Matchers.is(1));
202 assertThat(list.get(0), Matchers.is(instanceOf(XMLEvent.class)));
203 assertThat(((XMLEvent) list.get(0)).isEndElement(), Matchers.is(true));
204
205 }
206
207
208
209
210
211}