From 51d9de02265d6cc9d6045d79497d2a987ae2f7f7 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 15 Nov 2016 14:21:27 +0300 Subject: core classes are now serializeable with JAXB (to use as babbler extension) --- src/test/java/com/juick/tests/ApiTests.java | 34 ++++---- src/test/java/com/juick/tests/JsonTests.java | 68 --------------- src/test/java/com/juick/tests/MessageTests.java | 6 +- .../java/com/juick/tests/SerializationTests.java | 96 ++++++++++++++++++++++ 4 files changed, 116 insertions(+), 88 deletions(-) delete mode 100644 src/test/java/com/juick/tests/JsonTests.java create mode 100644 src/test/java/com/juick/tests/SerializationTests.java (limited to 'src') diff --git a/src/test/java/com/juick/tests/ApiTests.java b/src/test/java/com/juick/tests/ApiTests.java index 02180888..b80b9eb9 100644 --- a/src/test/java/com/juick/tests/ApiTests.java +++ b/src/test/java/com/juick/tests/ApiTests.java @@ -59,7 +59,7 @@ public class ApiTests { public void messageTests() { int user_id = UserQueries.createUser(jdbc, "mmmme", "secret"); User user = UserQueries.getUserByUID(jdbc, user_id).orElse(new User()); - assertEquals("it should be me", "mmmme", user.getUName()); + assertEquals("it should be me", "mmmme", user.getName()); int mid = MessagesQueries.createMessage(jdbc, user_id, "yo", null, new ArrayList<>()); Message msg = MessagesQueries.getMessage(jdbc, mid); assertEquals("yo", msg.getText()); @@ -67,27 +67,27 @@ public class ApiTests { calendar.setTime(msg.getDate()); assertEquals(2016, calendar.get(Calendar.YEAR)); User me = msg.getUser(); - assertEquals("mmmme", me.getUName()); - assertEquals("mmmme", MessagesQueries.getMessageAuthor(jdbc, mid).getUName()); + assertEquals("mmmme", me.getName()); + assertEquals("mmmme", MessagesQueries.getMessageAuthor(jdbc, mid).getName()); int tagID = TagQueries.createTag(jdbc, "weather"); Tag tag = TagQueries.getTag(jdbc, tagID); List tagList = new ArrayList<>(); tagList.add(tag); int mid2 = MessagesQueries.createMessage(jdbc, user_id, "yo2", null, tagList); Message msg2 = MessagesQueries.getMessage(jdbc, mid2); - assertEquals(1, msg2.Tags.size()); + assertEquals(1, msg2.getTags().size()); assertEquals("we already have ugnich", -1, UserQueries.createUser(jdbc, "ugnich", "x")); int ugnich_id = UserQueries.createUser(jdbc, "hugnich", "x"); User ugnich = UserQueries.getUserByUID(jdbc, ugnich_id).orElse(new User()); - int rid = MessagesQueries.createReply(jdbc, msg2.getMID(), 0, ugnich.getUID(), "bla-bla", null); + int rid = MessagesQueries.createReply(jdbc, msg2.getMid(), 0, ugnich.getUid(), "bla-bla", null); assertEquals(1, rid); Message msg3 = MessagesQueries.getMessage(jdbc, mid2); assertEquals(1, msg3.Replies); - assertEquals("weather", msg3.Tags.get(0).getName()); - assertEquals(ugnich.getUID(), UserQueries.checkPassword(jdbc, ugnich.getUName(), "x")); - assertEquals(-1, UserQueries.checkPassword(jdbc, ugnich.getUName(), "xy")); - SubscriptionsQueries.subscribeMessage(jdbc, msg.getMID(), ugnich.getUID()); - assertEquals(1, SubscriptionsQueries.getUsersSubscribedToComments(jdbc, msg.getMID(), user.getUID()).size()); + assertEquals("weather", msg3.getTags().get(0).getName()); + assertEquals(ugnich.getUid(), UserQueries.checkPassword(jdbc, ugnich.getName(), "x")); + assertEquals(-1, UserQueries.checkPassword(jdbc, ugnich.getName(), "xy")); + SubscriptionsQueries.subscribeMessage(jdbc, msg.getMid(), ugnich.getUid()); + assertEquals(1, SubscriptionsQueries.getUsersSubscribedToComments(jdbc, msg.getMid(), user.getUid()).size()); MessagesQueries.deleteMessage(jdbc, user_id, mid); MessagesQueries.deleteMessage(jdbc, user_id, mid2); String htmlTagName = ">_<"; @@ -110,7 +110,7 @@ public class ApiTests { String expectedMessage = "New message posted"; assertEquals("should be message", true, protocol.getReply(user, "*yo yoyo").getDescription().startsWith(expectedMessage)); - int mid = MessagesQueries.getUserBlog(jdbc, user.getUID(), -1, 0).stream().reduce((first, second) -> second).get(); + int mid = MessagesQueries.getUserBlog(jdbc, user.getUid(), -1, 0).stream().reduce((first, second) -> second).get(); assertEquals("text should match", "yoyo", MessagesQueries.getMessage(jdbc, mid).getText()); assertEquals("tag should match", "yo", @@ -124,7 +124,7 @@ public class ApiTests { assertEquals("number of subscribed users should match", 1, SubscriptionsQueries.getUsersSubscribedToComments(jdbc, mid, uid).size()); assertEquals("should be subscribed", "Subscribed", - protocol.getReply(readerUser, "S @" + user.getUName()).getDescription()); + protocol.getReply(readerUser, "S @" + user.getName()).getDescription()); List friends = UserQueries.getUserFriends(jdbc, readerUid); assertEquals("number of friend users should match", 2, friends.size()); @@ -138,9 +138,9 @@ public class ApiTests { protocol.getReply(user, "#" + mid + " yoyo").getDescription()); assertEquals("should be second reply", expectedSecondReply, protocol.getReply(user, "#" + mid + "/1 yoyo").getDescription()); - Message reply = MessagesQueries.getReplies(jdbc, mid).stream().filter(m -> m.getRID() == 2).findFirst() + Message reply = MessagesQueries.getReplies(jdbc, mid).stream().filter(m -> m.getRid() == 2).findFirst() .orElse(new Message()); - assertEquals("should be reply to first comment", 1, reply.ReplyTo); + assertEquals("should be reply to first comment", 1, reply.getReplyto()); String jsonReply = protocol.getReply(user, "#" + mid).getJson().orElse(""); JSONArray jsonMessages = new JSONArray(jsonReply); Message receivedMsg = json.deserialize(jsonMessages.getJSONObject(0)); @@ -176,8 +176,8 @@ public class ApiTests { String readerUserFeed = readerFeed.getJson().orElse(""); JSONArray readerUserFeedMessages = new JSONArray(readerUserFeed); assertEquals("messages count should match", 1, readerUserFeedMessages.length()); - assertEquals("should be unsubscribed", "Unsubscribed from @" + user.getUName(), - protocol.getReply(readerUser, "U @" + user.getUName()).getDescription()); + assertEquals("should be unsubscribed", "Unsubscribed from @" + user.getName(), + protocol.getReply(readerUser, "U @" + user.getName()).getDescription()); assertEquals("number of readers should match", 0, UserQueries.getUserReaders(jdbc, uid).size()); assertEquals("number of friends should match", 1, @@ -190,7 +190,7 @@ public class ApiTests { protocol.getReply(readerUser, "D #" + mid).getDescription()); assertEquals("should be deleted", String.format("Message %s deleted", mid), protocol.getReply(user, "D #" + mid).getDescription()); - assertEquals("should not have messages", 0, MessagesQueries.getAll(jdbc, user.getUID(), 0).size()); + assertEquals("should not have messages", 0, MessagesQueries.getAll(jdbc, user.getUid(), 0).size()); String allFeed = protocol.getReply(readerUser, "#").getJson().orElse(""); JSONArray allFeedMessages = new JSONArray(allFeed); assertEquals("messages count should match", 0, allFeedMessages.length()); diff --git a/src/test/java/com/juick/tests/JsonTests.java b/src/test/java/com/juick/tests/JsonTests.java deleted file mode 100644 index 8378d7f1..00000000 --- a/src/test/java/com/juick/tests/JsonTests.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.juick.tests; - -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.juick.Message; -import com.juick.Tag; -import com.juick.User; -import com.juick.json.MessageSerializer; -import com.juick.json.UserSerializer; -import org.json.JSONObject; -import org.junit.Test; - -import java.io.IOException; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.TimeZone; - -import static org.junit.Assert.assertEquals; - -public class JsonTests { - @Test - public void DateTest() { - Message msg = new Message(); - SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - df.setTimeZone(TimeZone.getTimeZone("UTC")); - try { - msg.setDate(df.parse("2015-01-01 05:02:10")); - MessageSerializer serializer = new MessageSerializer(); - String json = serializer.serialize(msg).toString(); - assertEquals("{\"timestamp\":\"2015-01-01 05:02:10\"}", json); - } catch (ParseException e) { - e.printStackTrace(); - } - } - @Test - public void serializersTest() throws IOException { - User user = new User(); - user.setUName("ugnich"); - user.setUID(1); - user.setFullName("Anton Ugnich"); - UserSerializer userSerializer = new UserSerializer(); - JSONObject handmadeJson = userSerializer.serialize(user); - ObjectMapper mapper = new ObjectMapper(); - mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); - mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); - mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT); - User jacksonUser = mapper.readValue(handmadeJson.toString(), User.class); - assertEquals("jackson should deserialize ugnich json", user, jacksonUser); - String jacksonString = mapper.writeValueAsString(user); - JSONObject jacksonJson = new JSONObject(jacksonString); - assertEquals("jackson should serialize as ugnich", handmadeJson.length(), jacksonJson.length()); - Message msg = new Message(); - msg.setMID(1); - msg.setUser(user); - msg.setText("yo"); - msg.setDate(new Date()); - msg.Tags.add(new Tag("test")); - msg.Tags.add(new Tag("json")); - MessageSerializer messageSerializer = new MessageSerializer(); - JSONObject handmadeJsonMessage = messageSerializer.serialize(msg); - Message jacksonMessage = mapper.readValue(handmadeJsonMessage.toString(), Message.class); - assertEquals("jackson should deserialize ugnich json", msg, jacksonMessage); - String jacksonStringMessage = mapper.writeValueAsString(jacksonMessage); - JSONObject jacksonJsonMessage = new JSONObject(jacksonStringMessage); - assertEquals("jackson should serialize as ugnich", handmadeJsonMessage.length(), jacksonJsonMessage.length()); - } -} \ No newline at end of file diff --git a/src/test/java/com/juick/tests/MessageTests.java b/src/test/java/com/juick/tests/MessageTests.java index 3e391a67..45575ded 100644 --- a/src/test/java/com/juick/tests/MessageTests.java +++ b/src/test/java/com/juick/tests/MessageTests.java @@ -26,9 +26,9 @@ public class MessageTests { IOException, SAXException { Message msg = new Message(); msg.parseTags("test test" + (char) 0xA0 + "2 test3"); - assertEquals("First tag must be", "test", msg.Tags.get(0).getName()); - assertEquals("Third tag must be", "test3", msg.Tags.get(2).getName()); - assertEquals("Count of tags must be", 3, msg.Tags.size()); + 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()); Date currentDate = new Date(); msg.setDate(currentDate); MessageSerializer serializer = new MessageSerializer(); diff --git a/src/test/java/com/juick/tests/SerializationTests.java b/src/test/java/com/juick/tests/SerializationTests.java new file mode 100644 index 00000000..dd2812c7 --- /dev/null +++ b/src/test/java/com/juick/tests/SerializationTests.java @@ -0,0 +1,96 @@ +package com.juick.tests; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.juick.Message; +import com.juick.Tag; +import com.juick.User; +import com.juick.json.MessageSerializer; +import com.juick.json.UserSerializer; +import com.juick.xmpp.extensions.JuickMessage; +import org.json.JSONObject; +import org.junit.Test; +import org.w3c.dom.Document; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +import javax.xml.bind.*; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import java.io.IOException; +import java.io.StringReader; +import java.io.StringWriter; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +import static org.junit.Assert.assertEquals; + +public class SerializationTests { + @Test + public void DateTest() { + Message msg = new Message(); + SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + df.setTimeZone(TimeZone.getTimeZone("UTC")); + try { + msg.setDate(df.parse("2015-01-01 05:02:10")); + MessageSerializer serializer = new MessageSerializer(); + String json = serializer.serialize(msg).toString(); + assertEquals("{\"timestamp\":\"2015-01-01 05:02:10\"}", json); + } catch (ParseException e) { + e.printStackTrace(); + } + } + @Test + public void serializersTest() throws IOException, JAXBException, ParserConfigurationException, SAXException { + User user = new User(); + user.setName("ugnich"); + user.setUid(1); + user.setFullName("Anton Ugnich"); + UserSerializer userSerializer = new UserSerializer(); + JSONObject handmadeJson = userSerializer.serialize(user); + ObjectMapper mapper = new ObjectMapper(); + mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); + mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT); + User jacksonUser = mapper.readValue(handmadeJson.toString(), User.class); + assertEquals("jackson should deserialize ugnich json", user, jacksonUser); + String jacksonString = mapper.writeValueAsString(user); + JSONObject jacksonJson = new JSONObject(jacksonString); + assertEquals("jackson should serialize as ugnich", handmadeJson.length(), jacksonJson.length()); + Message msg = new Message(); + msg.setMid(1); + msg.setUser(user); + msg.setText("yo"); + msg.setDate(new Date()); + msg.getTags().add(new Tag("test")); + msg.getTags().add(new Tag("json")); + MessageSerializer messageSerializer = new MessageSerializer(); + JSONObject handmadeJsonMessage = messageSerializer.serialize(msg); + Message jacksonMessage = mapper.readValue(handmadeJsonMessage.toString(), Message.class); + assertEquals("jackson should deserialize ugnich json", msg, jacksonMessage); + String jacksonStringMessage = mapper.writeValueAsString(jacksonMessage); + JSONObject jacksonJsonMessage = new JSONObject(jacksonStringMessage); + assertEquals("jackson should serialize as ugnich", handmadeJsonMessage.length(), jacksonJsonMessage.length()); + + JuickMessage jmsg = new JuickMessage(msg); + String handmadeXml = jmsg.toString(); + + JAXBContext messageContext = JAXBContext.newInstance(Message.class); + Unmarshaller unmarshaller = messageContext.createUnmarshaller(); + Message jaxbMessage = (Message) unmarshaller.unmarshal(new StringReader(handmadeXml)); + assertEquals("jaxb should unmarshal ugnich xml", msg, jaxbMessage); + StringWriter sw = new StringWriter(); + Marshaller messageMarshaller = messageContext.createMarshaller(); + messageMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); + messageMarshaller.marshal(msg, sw); + String xmlString = sw.toString(); + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); + Document doc = dBuilder.parse(new InputSource(new StringReader(xmlString))); + assertEquals("jaxb should marshal as ugnich", 4, doc.getDocumentElement().getChildNodes().getLength()); + assertEquals("jaxb should marshal as ugnich", 7, doc.getDocumentElement().getAttributes().getLength()); + } +} \ No newline at end of file -- cgit v1.2.3