aboutsummaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/com/juick/tests/ApiTests.java74
1 files changed, 74 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());
+ }
+
+}