aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/com/juick/tests/ApiTests.java
blob: 7ab04708bee0fdcd7c7df46e6645ea48fd8e0f03 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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());
    }

}