aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/com/juick/tests/ApiTests.java
blob: 021808880e2310415da83706ab899eb866a93ed6 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
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.json.MessageSerializer;
import com.juick.server.MessagesQueries;
import com.juick.server.SubscriptionsQueries;
import com.juick.server.TagQueries;
import com.juick.server.UserQueries;
import com.juick.server.protocol.JuickProtocol;
import com.juick.server.protocol.ProtocolReply;
import com.juick.www.PageTemplates;
import org.apache.commons.lang3.StringEscapeUtils;
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.newEmbeddedDB(33306);
        db.start();
        db.createDB("juick");
        db.source("schema.sql");
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:33306/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, "mmmme", "secret");
        User user = UserQueries.getUserByUID(jdbc, user_id).orElse(new User());
        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);
        assertEquals("yo", msg.getText());
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(msg.getDate());
        assertEquals(2016, calendar.get(Calendar.YEAR));
        User me = msg.getUser();
        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);
        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);
        assertEquals(1, rid);
        Message msg3 = MessagesQueries.getMessage(jdbc, mid2);
        assertEquals(1, msg3.Replies);
        assertEquals("weather", msg3.Tags.get(0).getName());
        assertEquals(ugnich.getUID(), UserQueries.checkPassword(jdbc, ugnich.getUName(), "x"));
        assertEquals(-1, UserQueries.checkPassword(jdbc, ugnich.getUName(), "xy"));
        SubscriptionsQueries.subscribeMessage(jdbc, msg.getMID(), ugnich.getUID());
        assertEquals(1, SubscriptionsQueries.getUsersSubscribedToComments(jdbc, msg.getMID(), user.getUID()).size());
        MessagesQueries.deleteMessage(jdbc, user_id, mid);
        MessagesQueries.deleteMessage(jdbc, user_id, mid2);
        String htmlTagName = ">_<";
        Tag htmlTag = TagQueries.getTag(jdbc, htmlTagName, true);
        String dbTagName = jdbc.queryForObject("select name from tags where name=?", String.class, StringEscapeUtils.escapeHtml4(htmlTagName));
        assertNotEquals("db tags should be escaped", dbTagName, htmlTag.getName());
        assertEquals("object tags should unescaped", htmlTag.getName(), StringEscapeUtils.unescapeHtml4(dbTagName));
        assertEquals("template should encode escaped tag in url and show escaped tag in name",
                "<a href=\"/tag/%3E_%3C\" rel=\"nofollow\">&gt;_&lt;</a>", PageTemplates.formatTags(new ArrayList<Tag>() {{ add(htmlTag); }} ));
    }

    @Test
    public void protocolTests() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, ParseException {
        String baseUri = "http://localhost/";
        JuickProtocol protocol = new JuickProtocol(jdbc, baseUri);
        MessageSerializer json = new MessageSerializer();
        assertEquals("juick user should have uid 2", 2, UserQueries.getUIDbyName(jdbc, "juick"));
        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).getName());
        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());
        List<User> friends = UserQueries.getUserFriends(jdbc, readerUid);
        assertEquals("number of friend users should match", 2,
                friends.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());
    }

}