/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright 2011 Robin Collier
*
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.pubsub;
import static org.custommonkey.xmlunit.XMLAssert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.Reader;
import java.io.StringReader;
import org.jivesoftware.smack.ThreadedDummyConnection;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smackx.pubsub.packet.PubSubNamespace;
import org.jivesoftware.smackx.pubsub.provider.ItemsProvider;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.xmlpull.mxp1.MXParser;
import org.xmlpull.v1.XmlPullParser;
/**
*
* @author Robin Collier
*
*/
public class ItemValidationTest
{
private ThreadedDummyConnection connection;
@Before
public void setUp() throws Exception
{
// Uncomment this to enable debug output
// Connection.DEBUG_ENABLED = true;
connection = new ThreadedDummyConnection();
connection.connect();
connection.login("me", "secret");
}
@After
public void tearDown() throws Exception
{
if (connection != null)
connection.disconnect();
}
@Test
public void verifyBasicItem() throws Exception
{
Item simpleItem = new Item();
String simpleCtrl = " ";
assertXMLEqual(simpleCtrl, simpleItem.toXML());
Item idItem = new Item("uniqueid");
String idCtrl = " ";
assertXMLEqual(idCtrl, idItem.toXML());
Item itemWithNodeId = new Item("testId", "testNode");
String nodeIdCtrl = " ";
assertXMLEqual(nodeIdCtrl, itemWithNodeId.toXML());
}
@Test
public void verifyPayloadItem() throws Exception
{
SimplePayload payload = new SimplePayload(null, null, "This is the payload");
PayloadItem simpleItem = new PayloadItem(payload);
String simpleCtrl = "- " + payload.toXML() + "
";
assertXMLEqual(simpleCtrl, simpleItem.toXML());
PayloadItem idItem = new PayloadItem("uniqueid", payload);
String idCtrl = "- " + payload.toXML() + "
";
assertXMLEqual(idCtrl, idItem.toXML());
PayloadItem itemWithNodeId = new PayloadItem("testId", "testNode", payload);
String nodeIdCtrl = "- " + payload.toXML() + "
";
assertXMLEqual(nodeIdCtrl, itemWithNodeId.toXML());
}
@Test
public void parseBasicItem() throws Exception
{
XmlPullParser parser = TestUtils.getMessageParser(
"" +
"" +
"" +
" " +
"" +
"" +
"");
Packet message = PacketParserUtils.parseMessage(parser);
PacketExtension eventExt = message.getExtension(PubSubNamespace.EVENT.getXmlns());
assertTrue(eventExt instanceof EventElement);
EventElement event = (EventElement) eventExt;
assertEquals(EventElementType.items, event.getEventType());
assertEquals(1, event.getExtensions().size());
assertTrue(event.getExtensions().get(0) instanceof ItemsExtension);
assertEquals(1, ((ItemsExtension)event.getExtensions().get(0)).items.size());
PacketExtension itemExt = ((ItemsExtension)event.getExtensions().get(0)).items.get(0);
assertTrue(itemExt instanceof Item);
assertEquals("testid1", ((Item)itemExt).getId());
}
@Test
public void parseSimplePayloadItem() throws Exception
{
String itemContent = "Some text";
XmlPullParser parser = TestUtils.getMessageParser(
"" +
"" +
"" +
"- " +
itemContent +
"
" +
"" +
"" +
"");
Packet message = PacketParserUtils.parseMessage(parser);
PacketExtension eventExt = message.getExtension(PubSubNamespace.EVENT.getXmlns());
EventElement event = (EventElement) eventExt;
PacketExtension itemExt = ((ItemsExtension)event.getExtensions().get(0)).items.get(0);
assertTrue(itemExt instanceof PayloadItem>);
PayloadItem> item = (PayloadItem>)itemExt;
assertEquals("testid1", item.getId());
assertTrue(item.getPayload() instanceof SimplePayload);
SimplePayload payload = (SimplePayload) item.getPayload();
assertEquals("foo", payload.getElementName());
assertEquals("smack:test", payload.getNamespace());
assertXMLEqual(itemContent, payload.toXML());
}
@Test
public void parseComplexItem() throws Exception
{
String itemContent =
"" +
"Soliloquy" +
"" +
"To be, or not to be: that is the question:" +
"Whether 'tis nobler in the mind to suffer" +
"The slings and arrows of outrageous fortune," +
"Or to take arms against a sea of troubles," +
"And by opposing end them?" +
"" +
"" +
"tag:denmark.lit,2003:entry-32397" +
"2003-12-13T18:30:02Z" +
"2003-12-13T18:30:02Z" +
"";
XmlPullParser parser = TestUtils.getMessageParser(
"" +
"" +
"" +
"- " +
itemContent +
"
" +
"" +
"" +
"");
Packet message = PacketParserUtils.parseMessage(parser);
PacketExtension eventExt = message.getExtension(PubSubNamespace.EVENT.getXmlns());
EventElement event = (EventElement) eventExt;
PacketExtension itemExt = ((ItemsExtension)event.getExtensions().get(0)).items.get(0);
assertTrue(itemExt instanceof PayloadItem>);
PayloadItem> item = (PayloadItem>)itemExt;
assertEquals("testid1", item.getId());
assertTrue(item.getPayload() instanceof SimplePayload);
SimplePayload payload = (SimplePayload) item.getPayload();
assertEquals("entry", payload.getElementName());
assertEquals("http://www.w3.org/2005/Atom", payload.getNamespace());
assertXMLEqual(itemContent, payload.toXML());
}
@Test
public void parseEmptyTag() throws Exception
{
String itemContent = "";
XmlPullParser parser = TestUtils.getMessageParser(
"" +
"" +
"" +
"- " +
itemContent +
"
" +
"" +
"" +
"");
Packet message = PacketParserUtils.parseMessage(parser);
PacketExtension eventExt = message.getExtension(PubSubNamespace.EVENT.getXmlns());
assertTrue(eventExt instanceof EventElement);
EventElement event = (EventElement) eventExt;
assertEquals(EventElementType.items, event.getEventType());
assertEquals(1, event.getExtensions().size());
assertTrue(event.getExtensions().get(0) instanceof ItemsExtension);
assertEquals(1, ((ItemsExtension)event.getExtensions().get(0)).items.size());
PacketExtension itemExt = ((ItemsExtension)event.getExtensions().get(0)).items.get(0);
assertTrue(itemExt instanceof PayloadItem>);
PayloadItem> item = (PayloadItem>)itemExt;
assertEquals("testid1", item.getId());
assertTrue(item.getPayload() instanceof SimplePayload);
assertXMLEqual(itemContent, ((SimplePayload)item.getPayload()).toXML());
}
}