diff options
Diffstat (limited to 'src/test/java')
-rw-r--r-- | src/test/java/com/juick/tests/ApiTests.java | 74 | ||||
-rw-r--r-- | src/test/java/com/juick/tests/JsonTests.java | 29 | ||||
-rw-r--r-- | src/test/java/com/juick/tests/MessageTests.java | 18 |
3 files changed, 121 insertions, 0 deletions
diff --git a/src/test/java/com/juick/tests/ApiTests.java b/src/test/java/com/juick/tests/ApiTests.java new file mode 100644 index 00000000..7ab04708 --- /dev/null +++ b/src/test/java/com/juick/tests/ApiTests.java @@ -0,0 +1,74 @@ +package com.juick.tests; + +import ch.vorburger.exec.ManagedProcessException; +import ch.vorburger.mariadb4j.DB; +import com.juick.Message; +import com.juick.Tag; +import com.juick.User; +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 org.junit.Before; +import org.junit.Test; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.DriverManagerDataSource; + +import java.util.ArrayList; +import java.util.Calendar; +import java.util.List; + +/** + * Created by vt on 14.01.2016. + */ +public class ApiTests { + JdbcTemplate jdbc; + @Before + public void setupConnection() throws ManagedProcessException { + DB db = DB.newEmbeddedDB(3306); + db.start(); + db.createDB("juick"); + db.source("schema.sql"); + DriverManagerDataSource dataSource = new DriverManagerDataSource(); + dataSource.setDriverClassName("com.mysql.jdbc.Driver"); + dataSource.setUrl("jdbc:mysql://localhost/juick?autoReconnect=true&user=root"); + jdbc = new JdbcTemplate(dataSource); + } + + + @Test + public void messageTests() { + int user_id = UserQueries.createUser(jdbc, "me", "secret"); + User user = UserQueries.getUserByUID(jdbc, user_id).orElse(new User()); + Assert.assertEquals("it should be me", "me", 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()); + Calendar calendar = Calendar.getInstance(); + calendar.setTime(msg.getDate()); + org.junit.Assert.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()); + 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"); + 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); + 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")); + SubscriptionsQueries.subscribeMessage(jdbc, msg.getMID(), ugnich.getUID()); + Assert.assertEquals(1, SubscriptionsQueries.getUsersSubscribedToComments(jdbc, msg.getMID(), user.getUID()).size()); + } + +} diff --git a/src/test/java/com/juick/tests/JsonTests.java b/src/test/java/com/juick/tests/JsonTests.java new file mode 100644 index 00000000..babcc6c7 --- /dev/null +++ b/src/test/java/com/juick/tests/JsonTests.java @@ -0,0 +1,29 @@ +package com.juick.tests; + +import com.juick.Message; +import com.juick.json.MessageSerializer; +import org.junit.Test; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +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(); + } + + } +}
\ 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 new file mode 100644 index 00000000..0392c7b3 --- /dev/null +++ b/src/test/java/com/juick/tests/MessageTests.java @@ -0,0 +1,18 @@ +package com.juick.tests;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+import com.juick.Message;
+
+public class MessageTests {
+ @Test
+ public void messageTagsParser() {
+ Message msg = new Message();
+ msg.parseTags("test test" + (char)0xA0 + "2 test3");
+ assertEquals("First tag must be", "test", msg.Tags.get(0));
+ assertEquals("Third tag must be", "test3", msg.Tags.get(2));
+ assertEquals("Count of tags must be", 3, msg.Tags.size());
+ }
+}
\ No newline at end of file |