aboutsummaryrefslogtreecommitdiff
path: root/juick-server/src/test/java/com/juick/server
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-04-25 13:54:25 +0300
committerGravatar Vitaly Takmazov2018-04-25 19:04:07 +0300
commit2bf63fc8d2a4b84e051d28e670c7c74b039e2545 (patch)
treeafceff82288a45649156b0a8428999a00464ba59 /juick-server/src/test/java/com/juick/server
parent003ff7221782c4f0cf4a5660832e40c7ac386618 (diff)
www: no more xmpp
Diffstat (limited to 'juick-server/src/test/java/com/juick/server')
-rw-r--r--juick-server/src/test/java/com/juick/server/tests/ServerTests.java113
1 files changed, 109 insertions, 4 deletions
diff --git a/juick-server/src/test/java/com/juick/server/tests/ServerTests.java b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java
index 4ea212f1..9b9722b2 100644
--- a/juick-server/src/test/java/com/juick/server/tests/ServerTests.java
+++ b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java
@@ -19,7 +19,12 @@ package com.juick.server.tests;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
-import com.juick.*;
+import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
+import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
+import com.juick.ExternalToken;
+import com.juick.Message;
+import com.juick.Tag;
+import com.juick.User;
import com.juick.server.*;
import com.juick.server.component.MessageEvent;
import com.juick.server.helpers.AnonymousUser;
@@ -29,10 +34,15 @@ import com.juick.server.util.HttpUtils;
import com.juick.service.*;
import com.juick.util.DateFormattersHolder;
import com.juick.util.MessageUtils;
+import org.apache.commons.codec.CharEncoding;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
-import org.hamcrest.Matchers;
-import org.junit.*;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
@@ -41,23 +51,44 @@ import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ConfigurableApplicationContext;
-import org.springframework.http.MediaType;
+import org.springframework.http.*;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.util.FileSystemUtils;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
+import org.springframework.web.client.HttpClientErrorException;
+import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
+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 rocks.xmpp.core.stanza.model.StanzaError;
import rocks.xmpp.core.stanza.model.client.ClientMessage;
import rocks.xmpp.core.stanza.model.errors.Condition;
import javax.inject.Inject;
+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.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;
@@ -898,4 +929,78 @@ public class ServerTests {
.stream().noneMatch(m -> m.getTags().contains(banned)));
}
+ @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 = "<tag xmlns='http://juick.com/message'>yo</tag>";
+ String xml = "<message xmlns='jabber:client' from='juick@juick.com' type='chat'><body>yo</body><juick mid='1' ts='2017-09-14' uid='1' uname='ugnich' xmlns='http://juick.com/message'><body>yo</body><user uid='1' uname='ugnich' xmlns='http://juick.com/user'/><tag>yo</tag><tag>people</tag></juick></message>";
+ 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 ParserConfigurationException,
+ IOException, SAXException, JAXBException, JSONException {
+ 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());
+ }
+ @Test
+ public void restTemplateTests() {
+ HttpHeaders headers = new HttpHeaders();
+ headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
+ MultiValueMap<String, String> map= new LinkedMultiValueMap<>();
+ HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
+
+ map.add("body", "yo");
+ map.add("hash", userService.getHashByUID(ugnich.getUid()));
+ RestTemplate rest = new RestTemplate();
+ ResponseEntity<CommandResult> result = rest.postForEntity(
+ "http://localhost:8080/post",
+ request, CommandResult.class);
+ assertThat(result.getStatusCode(), is(HttpStatus.OK));
+ }
}