diff options
author | Vitaly Takmazov | 2016-11-15 14:21:27 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2016-11-15 14:21:27 +0300 |
commit | 51d9de02265d6cc9d6045d79497d2a987ae2f7f7 (patch) | |
tree | d7dd2e534452cab45b8ebdba1e52c498951c5035 /src/test/java/com | |
parent | 32a6feb195171e06d60117ad758cc8beb50e5ca3 (diff) |
core classes are now serializeable with JAXB (to use as babbler extension)
Diffstat (limited to 'src/test/java/com')
-rw-r--r-- | src/test/java/com/juick/tests/ApiTests.java | 34 | ||||
-rw-r--r-- | src/test/java/com/juick/tests/MessageTests.java | 6 | ||||
-rw-r--r-- | src/test/java/com/juick/tests/SerializationTests.java (renamed from src/test/java/com/juick/tests/JsonTests.java) | 42 |
3 files changed, 55 insertions, 27 deletions
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<Tag> 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<User> 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/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/JsonTests.java b/src/test/java/com/juick/tests/SerializationTests.java index 8378d7f1..dd2812c7 100644 --- a/src/test/java/com/juick/tests/JsonTests.java +++ b/src/test/java/com/juick/tests/SerializationTests.java @@ -7,10 +7,20 @@ 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; @@ -18,7 +28,7 @@ import java.util.TimeZone; import static org.junit.Assert.assertEquals; -public class JsonTests { +public class SerializationTests { @Test public void DateTest() { Message msg = new Message(); @@ -34,10 +44,10 @@ public class JsonTests { } } @Test - public void serializersTest() throws IOException { + public void serializersTest() throws IOException, JAXBException, ParserConfigurationException, SAXException { User user = new User(); - user.setUName("ugnich"); - user.setUID(1); + user.setName("ugnich"); + user.setUid(1); user.setFullName("Anton Ugnich"); UserSerializer userSerializer = new UserSerializer(); JSONObject handmadeJson = userSerializer.serialize(user); @@ -51,12 +61,12 @@ public class JsonTests { JSONObject jacksonJson = new JSONObject(jacksonString); assertEquals("jackson should serialize as ugnich", handmadeJson.length(), jacksonJson.length()); Message msg = new Message(); - msg.setMID(1); + 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")); + 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); @@ -64,5 +74,23 @@ public class JsonTests { 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 |