/*
* Copyright (C) 2008-2017, Juick
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
package com.juick;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.juick.util.DateFormattersHolder;
import com.juick.util.MessageUtils;
import org.apache.commons.codec.CharEncoding;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
import rocks.xmpp.addr.Jid;
import rocks.xmpp.core.session.Extension;
import rocks.xmpp.core.session.XmppSession;
import rocks.xmpp.core.session.XmppSessionConfiguration;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.text.ParseException;
import java.time.Instant;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertEquals;
/**
* Created by aalexeev on 12/7/16.
*/
public class MessageTest {
@Test
public void equalsShouldBeReturnCorrectResult() {
Message message1 = new Message();
Message message2 = new Message();
message2.setMid(RandomUtils.nextInt());
Message message3 = message1;
assertThat(message1, equalTo(message3));
assertThat(message3, equalTo(message1));
assertThat(message1, not(equalTo(message2)));
assertThat(message2, not(equalTo(message1)));
int id = RandomUtils.nextInt();
message1.setMid(id);
message2.setMid(id);
id = RandomUtils.nextInt();
message1.setRid(id);
message2.setRid(id);
assertThat(message1, equalTo(message2));
assertThat(message2, equalTo(message1));
message1.setMid(RandomUtils.nextInt());
message1.setRid(RandomUtils.nextInt());
message2.setMid(RandomUtils.nextInt());
message2.setRid(RandomUtils.nextInt());
assertThat(message1, not(equalTo(message2)));
assertThat(message2, not(equalTo(message1)));
}
@Test
public void compareShouldBeReturnCorrectResult() {
Message message1 = new Message();
message1.setMid(RandomUtils.nextInt());
message1.setRid(RandomUtils.nextInt());
Message message2 = message1;
assertThat(message1.compareTo(message2), equalTo(0));
assertThat(message2.compareTo(message1), equalTo(0));
Message message3 = new Message();
message3.setMid(message1.getMid());
message3.setRid(message1.getRid());
assertThat(message1.compareTo(message3), equalTo(0));
assertThat(message3.compareTo(message1), equalTo(0));
}
@Test
public void messageWithGreaterMidShouldBeLessThenMesageWithLessMid() {
Message message1 = new Message();
message1.setMid(RandomUtils.nextInt());
message1.setRid(RandomUtils.nextInt());
Message message2 = new Message();
message2.setMid(message1.getMid() + RandomUtils.nextInt(1, 10000));
message2.setRid(RandomUtils.nextInt());
assertThat(message1.compareTo(message2), equalTo(1));
assertThat(message2.compareTo(message1), equalTo(-1));
}
@Test
public void messageWithGreaterRidAndEqualsMidShouldBeGreaterThenMessageWithLessRid() {
Message message1 = new Message();
message1.setMid(RandomUtils.nextInt());
message1.setRid(RandomUtils.nextInt());
Message message2 = new Message();
message2.setMid(message1.getMid());
message2.setRid(message1.getRid() + + RandomUtils.nextInt(1, 10000));
assertThat(message1.compareTo(message2), equalTo(-1));
assertThat(message2.compareTo(message1), equalTo(1));
}
@Test
public void tagsStringShouldBeEmptyIfNoTags() {
Message message = new Message();
assertThat(MessageUtils.getTagsString(message), isEmptyString());
message.FriendsOnly = true;
assertThat(MessageUtils.getTagsString(message), isEmptyString());
message.Hidden = true;
message.ReadOnly = true;
assertThat(MessageUtils.getTagsString(message), isEmptyString());
message.setTags(MessageUtils.parseTags(" "));
assertThat(MessageUtils.getTagsString(message), isEmptyString());
}
@Test
public void tagsStringShouldHasNoTagDuplicates() {
Message message = new Message();
message.setTags(MessageUtils.parseTags("test"));
assertThat(StringUtils.countMatches(MessageUtils.getTagsString(message), "*test"), equalTo(1));
message.setTags(MessageUtils.parseTags("test test"));
assertThat(StringUtils.countMatches(MessageUtils.getTagsString(message), "*test"), equalTo(1));
message.setTags(MessageUtils.parseTags("test test ab test"));
assertThat(StringUtils.countMatches(MessageUtils.getTagsString(message), "*test"), equalTo(1));
assertThat(StringUtils.countMatches(MessageUtils.getTagsString(message), "*ab"), equalTo(1));
}
@Test
public void tagsShouldBeDeserializedFromXml() throws JAXBException {
XmppSessionConfiguration configuration = XmppSessionConfiguration.builder()
.extensions(Extension.of(com.juick.Message.class))
.build();
XmppSession xmpp = new XmppSession("juick.com", configuration) {
@Override
public void connect(Jid from) {
}
@Override
public Jid getConnectedResource() {
return null;
}
};
String tag = "yo";
String xml = "yoyoyopeople";
Unmarshaller unmarshaller = xmpp.createUnmarshaller();
rocks.xmpp.core.stanza.model.Message xmppMessage = (rocks.xmpp.core.stanza.model.Message) unmarshaller.unmarshal(new StringReader(xml));
Tag xmlTag = (Tag) unmarshaller.unmarshal(new StringReader(tag));
assertThat(xmlTag.getName(), equalTo("yo"));
Message juickMessage = xmppMessage.getExtension(Message.class);
assertThat(juickMessage.getTags().get(0).getName(), equalTo("yo"));
}
@Test
public void messageParserSerializer() throws ParseException, ParserConfigurationException,
IOException, SAXException, JAXBException {
Message msg = new Message();
msg.setTags(MessageUtils.parseTags("test test" + (char) 0xA0 + "2 test3"));
assertEquals("First tag must be", "test", msg.getTags().get(0).getName());
assertEquals("Third tag must be", "test3", msg.getTags().get(2).getName());
assertEquals("Count of tags must be", 3, msg.getTags().size());
Instant currentDate = Instant.now();
msg.setTimestamp(currentDate);
ObjectMapper serializer = new ObjectMapper();
serializer.registerModule(new Jdk8Module());
serializer.registerModule(new JavaTimeModule());
String jsonMessage = serializer.writeValueAsString(msg);
JSONObject jsonObject = new JSONObject(jsonMessage);
assertEquals("date should be in timestamp field", DateFormattersHolder.getMessageFormatterInstance().format(currentDate),
jsonObject.getString("timestamp"));
JAXBContext context = JAXBContext
.newInstance(Message.class);
Marshaller m = context.createMarshaller();
StringWriter sw = new StringWriter();
m.marshal(msg, sw);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new ByteArrayInputStream(sw.toString().getBytes(CharEncoding.UTF_8)));
Node juickNode = doc.getElementsByTagName("juick").item(0);
NamedNodeMap attrs = juickNode.getAttributes();
assertEquals("date should be in ts field", DateFormattersHolder.getMessageFormatterInstance().format(currentDate),
attrs.getNamedItem("ts").getNodeValue());
}
}