diff options
author | Vitaly Takmazov | 2016-07-11 11:28:25 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2016-07-11 11:28:25 +0300 |
commit | ec589172a627aee760a07e4830499cebc1943c35 (patch) | |
tree | fd1fd11fd3a63365fdb9d1a2b8d393b77d2ccfe2 /src/test/java/com/juick/tests | |
parent | 3daf98ea8ee13a75fa5819fa90f24a7e095ed769 (diff) |
backport tests (WIP)
Diffstat (limited to 'src/test/java/com/juick/tests')
-rw-r--r-- | src/test/java/com/juick/tests/ApiTests.java | 151 |
1 files changed, 135 insertions, 16 deletions
diff --git a/src/test/java/com/juick/tests/ApiTests.java b/src/test/java/com/juick/tests/ApiTests.java index 7ab04708..5f919c76 100644 --- a/src/test/java/com/juick/tests/ApiTests.java +++ b/src/test/java/com/juick/tests/ApiTests.java @@ -5,28 +5,38 @@ import ch.vorburger.mariadb4j.DB; import com.juick.Message; import com.juick.Tag; import com.juick.User; +import com.juick.json.MessageSerializer; import com.juick.server.MessagesQueries; import com.juick.server.SubscriptionsQueries; import com.juick.server.TagQueries; import com.juick.server.UserQueries; -import org.junit.Assert; +import com.juick.server.protocol.JuickProtocol; +import com.juick.server.protocol.ProtocolReply; +import org.json.JSONArray; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; +import java.lang.reflect.InvocationTargetException; +import java.text.ParseException; import java.util.ArrayList; import java.util.Calendar; import java.util.List; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + /** * Created by vt on 14.01.2016. */ public class ApiTests { JdbcTemplate jdbc; + DB db; @Before public void setupConnection() throws ManagedProcessException { - DB db = DB.newEmbeddedDB(3306); + db = DB.newEmbeddedDB(3306); db.start(); db.createDB("juick"); db.source("schema.sql"); @@ -34,41 +44,150 @@ public class ApiTests { dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost/juick?autoReconnect=true&user=root"); jdbc = new JdbcTemplate(dataSource); + UserQueries.createUser(jdbc, "ugnich", "secret"); + UserQueries.createUser(jdbc, "juick", "secret"); + } + @After + public void teardown() throws ManagedProcessException { + db.stop(); } @Test public void messageTests() { - int user_id = UserQueries.createUser(jdbc, "me", "secret"); + int user_id = UserQueries.createUser(jdbc, "mmmme", "secret"); User user = UserQueries.getUserByUID(jdbc, user_id).orElse(new User()); - Assert.assertEquals("it should be me", "me", user.getUName()); + assertEquals("it should be me", "mmmme", user.getUName()); int mid = MessagesQueries.createMessage(jdbc, user_id, "yo", null, new ArrayList<>()); Message msg = MessagesQueries.getMessage(jdbc, mid); - org.junit.Assert.assertEquals("yo", msg.getText()); + assertEquals("yo", msg.getText()); Calendar calendar = Calendar.getInstance(); calendar.setTime(msg.getDate()); - org.junit.Assert.assertEquals(2016, calendar.get(Calendar.YEAR)); + assertEquals(2016, calendar.get(Calendar.YEAR)); User me = msg.getUser(); - Assert.assertEquals("me", me.getUName()); - org.junit.Assert.assertEquals("me", MessagesQueries.getMessageAuthor(jdbc, mid).getUName()); + assertEquals("mmmme", me.getUName()); + assertEquals("mmmme", MessagesQueries.getMessageAuthor(jdbc, mid).getUName()); 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); - org.junit.Assert.assertEquals(1, msg2.Tags.size()); - int ugnich_id = UserQueries.createUser(jdbc, "ugnich", "x"); + assertEquals(1, msg2.Tags.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); - org.junit.Assert.assertEquals(1, rid); + assertEquals(1, rid); Message msg3 = MessagesQueries.getMessage(jdbc, mid2); - org.junit.Assert.assertEquals(1, msg3.Replies); - org.junit.Assert.assertEquals("weather", msg3.Tags.get(0)); - org.junit.Assert.assertEquals(ugnich.getUID(), UserQueries.checkPassword(jdbc, ugnich.getUName(), "x")); - org.junit.Assert.assertEquals(-1, UserQueries.checkPassword(jdbc, ugnich.getUName(), "xy")); + assertEquals(1, msg3.Replies); + assertEquals("weather", msg3.Tags.get(0)); + assertEquals(ugnich.getUID(), UserQueries.checkPassword(jdbc, ugnich.getUName(), "x")); + assertEquals(-1, UserQueries.checkPassword(jdbc, ugnich.getUName(), "xy")); SubscriptionsQueries.subscribeMessage(jdbc, msg.getMID(), ugnich.getUID()); - Assert.assertEquals(1, SubscriptionsQueries.getUsersSubscribedToComments(jdbc, msg.getMID(), user.getUID()).size()); + assertEquals(1, SubscriptionsQueries.getUsersSubscribedToComments(jdbc, msg.getMID(), user.getUID()).size()); + MessagesQueries.deleteMessage(jdbc, user_id, mid); + MessagesQueries.deleteMessage(jdbc, user_id, mid2); + } + + @Test + public void protocolTests() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, ParseException { + String baseUri = "http://localhost/"; + JuickProtocol protocol = new JuickProtocol(jdbc, baseUri); + MessageSerializer json = new MessageSerializer(); + int uid = UserQueries.createUser(jdbc, "me", "secret"); + User user = UserQueries.getUserByUID(jdbc, uid).orElse(new User()); + 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(); + assertEquals("text should match", "yoyo", + MessagesQueries.getMessage(jdbc, mid).getText()); + assertEquals("tag should match", "yo", + MessagesQueries.getMessageTags(jdbc, mid).get(0).Name); + assertNotEquals("should not be error", "Error", protocol.getReply(user, "#" + mid).getDescription()); + assertEquals("should be PONG", "PONG", protocol.getReply(user, " ping \n ").getDescription()); + int readerUid = UserQueries.createUser(jdbc, "dummyReader", "dummySecret"); + User readerUser = UserQueries.getUserByUID(jdbc, readerUid).orElse(new User()); + assertEquals("should be subscribed", "Subscribed", + protocol.getReply(readerUser, "S #" + mid).getDescription()); + 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()); + /*assertEquals("number of friend users should match", 2, + UserQueries.getUserFriends(jdbc, readerUid).size());*/ + assertEquals("number of reader users should match", 1, + UserQueries.getUserReaders(jdbc, uid).size()); + String expectedReply = "Reply posted.\n#" + mid + "/1 " + +baseUri + mid + "/1"; + String expectedSecondReply = "Reply posted.\n#" + mid + "/2 " + + baseUri + mid + "/2"; + assertEquals("should be reply", expectedReply, + 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() + .orElse(new Message()); + assertEquals("should be reply to first comment", 1, reply.ReplyTo); + String jsonReply = protocol.getReply(user, "#" + mid).getJson().orElse(""); + JSONArray jsonMessages = new JSONArray(jsonReply); + Message receivedMsg = json.deserialize(jsonMessages.getJSONObject(0)); + assertEquals("json should match text", "yoyo", receivedMsg.getText()); + assertEquals("array length should match", 1, jsonMessages.length()); + jsonReply = protocol.getReply(user, "#" + mid+"+").getJson().orElse(""); + jsonMessages = new JSONArray(jsonReply); + assertEquals("array length should match", 3, jsonMessages.length()); + assertNotEquals("tags should NOT be updated", "Tags are updated", + protocol.getReply(readerUser, "#" + mid + " *yo *there").getDescription()); + assertEquals("tags should be updated", "Tags are updated", + protocol.getReply(user, "#" + mid + " *there").getDescription()); + assertEquals("number of tags should match", 2, + MessagesQueries.getMessageTags(jdbc, mid).size()); + assertEquals("tags should be updated", "Tags are updated", + protocol.getReply(user, "#" + mid + " *there").getDescription()); + assertEquals("number of tags should match", 1, + MessagesQueries.getMessageTags(jdbc, mid).size()); + int taggerUid = UserQueries.createUser(jdbc, "dummyTagger", "dummySecret"); + User taggerUser = UserQueries.getUserByUID(jdbc, taggerUid).orElse(new User()); + assertEquals("should be subscribed", "Subscribed", + protocol.getReply(taggerUser, "S *yo").getDescription()); + assertEquals("number of subscribed users should match", 2, + SubscriptionsQueries.getSubscribedUsers(jdbc, uid, mid).size()); + assertEquals("should be unsubscribed", "Unsubscribed from yo", + protocol.getReply(taggerUser, "U *yo").getDescription()); + assertEquals("number of subscribed users should match", 1, + SubscriptionsQueries.getSubscribedUsers(jdbc, uid, mid).size()); + assertEquals("number of readers should match", 1, + UserQueries.getUserReaders(jdbc, uid).size()); + ProtocolReply readerFeed = protocol.getReply(readerUser, "#"); + assertEquals("description should match", true, readerFeed.getDescription().startsWith("Your feed")); + 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("number of readers should match", 0, + UserQueries.getUserReaders(jdbc, uid).size()); + assertEquals("number of friends should match", 1, + UserQueries.getUserFriends(jdbc, uid).size()); + assertEquals("should be unsubscribed", "Unsubscribed from #" + mid, + protocol.getReply(readerUser, "u #" + mid).getDescription()); + assertEquals("number of subscribed users should match", 0, + SubscriptionsQueries.getUsersSubscribedToComments(jdbc, mid, uid).size()); + assertNotEquals("should NOT be deleted", String.format("Message %s deleted", mid), + 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()); + String allFeed = protocol.getReply(readerUser, "#").getJson().orElse(""); + JSONArray allFeedMessages = new JSONArray(allFeed); + assertEquals("messages count should match", 0, allFeedMessages.length()); + //assertEquals("number of message events should match", 1, newMessages.size()); + /* TODO: add asserts + protocol.getReply(user, "BL"); + protocol.getReply(user, "@me"); + protocol.getReply(user, "OFF"); */ } } |