/** * * Copyright 2016 Fernando Ramirez * * 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.mam; import java.util.Date; import java.util.GregorianCalendar; import java.util.TimeZone; import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.util.PacketParserUtils; import org.jivesoftware.smackx.forward.packet.Forwarded; import org.jivesoftware.smackx.mam.element.MamElements.MamResultExtension; import org.jivesoftware.smackx.mam.provider.MamResultProvider; import org.junit.Assert; import org.junit.Test; import org.xmlpull.v1.XmlPullParser; public class MamResultProviderTest { private static final String exampleMamResultXml = "" + "" + "" + "" + "Call me but love, and I'll be new baptized; Henceforth I never will be Romeo." + "" + "" + ""; private static final String exampleResultMessage = "" + "" + "" + "" + "" + "Hail to thee" + "" + "" + "" + ""; @Test public void checkMamResultProvider() throws Exception { XmlPullParser parser = PacketParserUtils.getParserFor(exampleMamResultXml); MamResultExtension mamResultExtension = new MamResultProvider().parse(parser); Assert.assertEquals(mamResultExtension.getQueryId(), "f27"); Assert.assertEquals(mamResultExtension.getId(), "28482-98726-73623"); GregorianCalendar calendar = new GregorianCalendar(2010, 7 - 1, 10, 23, 8, 25); calendar.setTimeZone(TimeZone.getTimeZone("UTC")); Date date = calendar.getTime(); Forwarded forwarded = mamResultExtension.getForwarded(); Assert.assertEquals(forwarded.getDelayInformation().getStamp(), date); Message message = (Message) forwarded.getForwardedStanza(); Assert.assertEquals(message.getFrom().toString(), "romeo@montague.lit/orchard"); Assert.assertEquals(message.getTo().toString(), "juliet@capulet.lit/balcony"); Assert.assertEquals(message.getBody(), "Call me but love, and I'll be new baptized; Henceforth I never will be Romeo."); } @Test public void checkResultsParse() throws Exception { Message message = PacketParserUtils.parseStanza(exampleResultMessage); MamResultExtension mamResultExtension = MamResultExtension.from(message); Assert.assertEquals(mamResultExtension.getQueryId(), "f27"); Assert.assertEquals(mamResultExtension.getId(), "28482-98726-73623"); GregorianCalendar calendar = new GregorianCalendar(2010, 7 - 1, 10, 23, 8, 25); calendar.setTimeZone(TimeZone.getTimeZone("UTC")); Date date = calendar.getTime(); Forwarded forwarded = mamResultExtension.getForwarded(); Assert.assertEquals(forwarded.getDelayInformation().getStamp(), date); Message forwardedMessage = (Message) forwarded.getForwardedStanza(); Assert.assertEquals(forwardedMessage.getFrom().toString(), "witch@shakespeare.lit"); Assert.assertEquals(forwardedMessage.getTo().toString(), "macbeth@shakespeare.lit"); Assert.assertEquals(forwardedMessage.getBody(), "Hail to thee"); } }