aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/com/juick/tests/ApiTests.java
blob: 83afb62e00073d2e4d0c714b6c764d7ffc412cc6 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package com.juick.tests;

import com.juick.Message;
import com.juick.Tag;
import com.juick.User;
import com.juick.server.MessagesQueries;
import com.juick.server.TagQueries;
import com.juick.server.UserQueries;
import junit.framework.Assert;
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.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() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("org.h2.Driver");
        dataSource.setUrl("jdbc:h2:~/test");
        jdbc = new JdbcTemplate(dataSource);
        jdbc.execute("CREATE TABLE bl_users (user_id int(10) unsigned NOT NULL, " +
                "bl_user_id int(10) unsigned NOT NULL, ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP )");
        jdbc.execute("CREATE TABLE messages (" +
                "message_id int(10) unsigned NOT NULL AUTO_INCREMENT," +
                "user_id int(10) unsigned NOT NULL," +
                "place_id int(10) unsigned DEFAULT NULL," +
                "lat decimal(10,7) DEFAULT NULL," +
                "lon decimal(10,7) DEFAULT NULL," +
                "ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP," +
                "replies smallint(5) unsigned NOT NULL DEFAULT 0," +
                "maxreplyid smallint(5) unsigned NOT NULL DEFAULT 0," +
                "privacy tinyint(4) NOT NULL DEFAULT '1'," +
                "attach nchar(3) check (attach in ('jpg', 'mp4', 'png'))," +
                "readonly tinyint(1) NOT NULL DEFAULT 0," +
                "likes smallint(6) NOT NULL DEFAULT 0," +
                "hidden tinyint(3) unsigned NOT NULL DEFAULT 0" +
                ")");
        jdbc.execute("CREATE TABLE messages_tags (" +
                "message_id int(10) unsigned NOT NULL," +
                "tag_id int(10) unsigned NOT NULL" +
                ")");
        jdbc.execute("CREATE TABLE users (" +
                "id int(10) unsigned NOT NULL AUTO_INCREMENT," +
                "nick char(64) NOT NULL," +
                "passw char(32) NOT NULL," +
                "banned tinyint(3) unsigned NOT NULL DEFAULT 0)");
        jdbc.execute("CREATE TABLE useroptions (" +
                "user_id int(10) unsigned NOT NULL," +
                "jnotify tinyint(1) NOT NULL DEFAULT 1," +
                "subscr_active tinyint(1) NOT NULL DEFAULT 1)");
        jdbc.execute("CREATE TABLE subscr_users (" +
                "user_id int(10) unsigned NOT NULL," +
                "suser_id int(10) unsigned NOT NULL," +
                "jid char(64) DEFAULT NULL," +
                "active tinyint(1) NOT NULL DEFAULT 1," +
                "ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)");
        jdbc.execute("CREATE TABLE messages_txt (" +
                "message_id int(10) unsigned NOT NULL," +
                "tags varchar(255)," +
                "repliesby varchar(96)," +
                "txt TEXT)");
        jdbc.execute("CREATE TABLE tags(" +
                "tag_id int(10) unsigned NOT NULL AUTO_INCREMENT," +
                "synonym_id int(10) unsigned DEFAULT NULL," +
                "name char(48) NOT NULL," +
                "top tinyint(1) unsigned NOT NULL DEFAULT 0," +
                "stat_messages int(10) unsigned NOT NULL DEFAULT 0," +
                "stat_users  smallint(5) unsigned NOT NULL DEFAULT 0)");
        jdbc.execute("CREATE TABLE replies (" +
                "message_id int(10) unsigned NOT NULL," +
                "reply_id smallint(5) unsigned NOT NULL," +
                "user_id int(10) unsigned NOT NULL," +
                "replyto smallint(5) unsigned NOT NULL DEFAULT 0," +
                "ts timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP," +
                "attach nchar(3) check (attach in ('jpg', 'mp4', 'png'))," +
                "txt text)");
    }
    @After
    public void tearDown() {
        jdbc.execute("DROP ALL OBJECTS DELETE FILES");
    }

    
    @Test 
    public void messageTests() {
        int user_id = UserQueries.createUser(jdbc, "me", "secret");
        User user = UserQueries.getUserByUID(jdbc, user_id);
        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);
        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"));
    }

}