aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/com/juick/tests/ApiTests.java
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-12-19 13:41:23 +0300
committerGravatar Vitaly Takmazov2016-12-19 13:41:23 +0300
commit274de5ede59ed4023826a9db09ec60788bc950e1 (patch)
tree107dbca37f559666eb065989aa92fd81e79fef24 /src/test/java/com/juick/tests/ApiTests.java
parentacddd380e1868e28491eaf85f05e87d7a47a123f (diff)
all projects except juick-www are spring-managed now
Diffstat (limited to 'src/test/java/com/juick/tests/ApiTests.java')
-rw-r--r--src/test/java/com/juick/tests/ApiTests.java257
1 files changed, 163 insertions, 94 deletions
diff --git a/src/test/java/com/juick/tests/ApiTests.java b/src/test/java/com/juick/tests/ApiTests.java
index 1c13c1a7..f72bcc02 100644
--- a/src/test/java/com/juick/tests/ApiTests.java
+++ b/src/test/java/com/juick/tests/ApiTests.java
@@ -7,22 +7,33 @@ 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.helpers.TagStats;
import com.juick.server.protocol.JuickProtocol;
import com.juick.server.protocol.ProtocolReply;
+import com.juick.service.MessagesService;
+import com.juick.service.SubscriptionService;
+import com.juick.service.TagService;
+import com.juick.service.UserService;
+import com.juick.service.search.SearchService;
import com.juick.www.PageTemplates;
+import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.lang3.StringEscapeUtils;
import org.json.JSONArray;
-import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.DependsOn;
import org.springframework.jdbc.core.JdbcTemplate;
-import org.springframework.jdbc.datasource.DriverManagerDataSource;
+import org.springframework.jdbc.datasource.DataSourceTransactionManager;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.transaction.PlatformTransactionManager;
+import org.springframework.transaction.annotation.TransactionManagementConfigurer;
+import javax.inject.Inject;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.ArrayList;
@@ -36,68 +47,128 @@ import static org.junit.Assert.assertNotEquals;
/**
* Created by vt on 14.01.2016.
*/
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration
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();
+ @Configuration
+ @ComponentScan(basePackages = {"com.juick.service", "com.juick.server.protocol"})
+ static class Config implements TransactionManagementConfigurer {
+ @Bean
+ public BasicDataSource dataSource() {
+ try {
+ DB db = DB.newEmbeddedDB(33306);
+ db.start();
+ db.createDB("juick");
+ db.source("schema.sql");
+ BasicDataSource dataSource = new BasicDataSource();
+ dataSource.setDriverClassName("com.mysql.jdbc.Driver");
+ dataSource.setUrl("jdbc:mysql://localhost:33306/juick?autoReconnect=true&user=root");
+
+ dataSource.setValidationQuery("select 1");
+
+ return dataSource;
+ } catch (ManagedProcessException e) {
+ return null;
+ }
+ }
+
+ @Bean
+ public PlatformTransactionManager transactionManager() {
+ return new DataSourceTransactionManager(dataSource());
+ }
+
+ @Override
+ public PlatformTransactionManager annotationDrivenTransactionManager() {
+ return transactionManager();
+ }
+
+ @Bean
+ @DependsOn("dataSource")
+ public JdbcTemplate jdbcTemplate() {
+ return new JdbcTemplate(dataSource());
+ }
+ @Bean
+ public SearchService emptySearchService() {
+ return new SearchService() {
+ @Override
+ public void setMaxResult(int maxResult) {
+ }
+
+ @Override
+ public List<Integer> searchInAllMessages(String searchString, int messageIdBefore) {
+ return Collections.emptyList();
+ }
+
+ @Override
+ public List<Integer> searchByStringAndUser(String searchString, int userId, int messageIdBefore) {
+ return Collections.emptyList();
+ }
+ };
+ }
+ @Bean
+ public JuickProtocol juickProtocol() {
+ return new JuickProtocol("http://localhost:8080/");
+ }
}
-
+ @Inject
+ UserService userService;
+ @Inject
+ MessagesService messagesService;
+ @Inject
+ TagService tagService;
+ @Inject
+ SubscriptionService subscriptionService;
+ @Inject
+ JdbcTemplate jdbcTemplate;
+ @Inject
+ JuickProtocol juickProtocol;
+
+ @Before
+ public void setup() {
+ userService.createUser("ugnich", "secret");
+ userService.createUser("juick", "secret");
+ }
@Test
public void messageTests() {
- int user_id = UserQueries.createUser(jdbc, "mmmme", "secret");
- User user = UserQueries.getUserByUID(jdbc, user_id).orElse(new User());
+ int user_id = userService.createUser("mmmme", "secret");
+ User user = userService.getUserByUID(user_id).orElse(new User());
assertEquals("it should be me", "mmmme", user.getName());
- int mid = MessagesQueries.createMessage(jdbc, user_id, "yo", null, new ArrayList<>());
- Message msg = MessagesQueries.getMessage(jdbc, mid);
+ int mid = messagesService.createMessage(user_id, "yo", null, new ArrayList<>());
+ Message msg = messagesService.getMessage(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.getName());
- assertEquals("mmmme", MessagesQueries.getMessageAuthor(jdbc, mid).getName());
- int tagID = TagQueries.createTag(jdbc, "weather");
- Tag tag = TagQueries.getTag(jdbc, tagID);
+ assertEquals("mmmme", messagesService.getMessageAuthor(mid).getName());
+ int tagID = tagService.createTag("weather");
+ Tag tag = tagService.getTag(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);
+ int mid2 = messagesService.createMessage(user_id, "yo2", null, tagList);
+ Message msg2 = messagesService.getMessage(mid2);
assertEquals(1, msg2.getTags().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("we already have ugnich", -1, userService.createUser("ugnich", "x"));
+ int ugnich_id = userService.createUser("hugnich", "x");
+ User ugnich = userService.getUserByUID(ugnich_id).orElse(new User());
+ int rid = messagesService.createReply(msg2.getMid(), 0, ugnich.getUid(), "bla-bla", null);
assertEquals(1, rid);
- Message msg3 = MessagesQueries.getMessage(jdbc, mid2);
+ Message msg3 = messagesService.getMessage(mid2);
assertEquals(1, msg3.getReplies());
assertEquals("weather", msg3.getTags().get(0).getName());
- assertEquals(ugnich.getUid(), UserQueries.checkPassword(jdbc, ugnich.getName(), "x"));
- assertEquals(-1, UserQueries.checkPassword(jdbc, ugnich.getName(), "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);
+ assertEquals(ugnich.getUid(), userService.checkPassword(ugnich.getName(), "x"));
+ assertEquals(-1, userService.checkPassword(ugnich.getName(), "xy"));
+ subscriptionService.subscribeMessage(msg.getMid(), ugnich.getUid());
+ assertEquals(1, subscriptionService.getUsersSubscribedToComments(msg.getMid(), user.getUid()).size());
+ messagesService.deleteMessage(user_id, mid);
+ messagesService.deleteMessage(user_id, mid2);
String htmlTagName = ">_<";
- Tag htmlTag = TagQueries.getTag(jdbc, htmlTagName, true);
+ Tag htmlTag = tagService.getTag(htmlTagName, true);
TagStats htmlTagStats = new TagStats();
htmlTagStats.setTag(htmlTag);
- String dbTagName = jdbc.queryForObject("select name from tags where name=?", String.class, StringEscapeUtils.escapeHtml4(htmlTagName));
+ String dbTagName = jdbcTemplate.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",
@@ -106,97 +177,95 @@ public class ApiTests {
@Test
public void protocolTests() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, ParseException, JsonProcessingException {
- 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());
+ assertEquals("juick user should have uid 2", 2, userService.getUIDbyName("juick"));
+ int uid = userService.createUser("me", "secret");
+ User user = userService.getUserByUID(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();
+ juickProtocol.getReply(user, "*yo yoyo").getDescription().startsWith(expectedMessage));
+ int mid = messagesService.getUserBlog(user.getUid(), -1, 0).stream().reduce((first, second) -> second).get();
assertEquals("text should match", "yoyo",
- MessagesQueries.getMessage(jdbc, mid).getText());
+ messagesService.getMessage(mid).getText());
assertEquals("tag should match", "yo",
- MessagesQueries.getMessageTags(jdbc, mid).get(0).getTag().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());
+ tagService.getMessageTags(mid).get(0).getTag().getName());
+ assertNotEquals("should not be error", "Error", juickProtocol.getReply(user, "#" + mid).getDescription());
+ assertEquals("should be PONG", "PONG", juickProtocol.getReply(user, " ping \n ").getDescription());
+ int readerUid = userService.createUser("dummyReader", "dummySecret");
+ User readerUser = userService.getUserByUID(readerUid).orElse(new User());
assertEquals("should be subscribed", "Subscribed",
- protocol.getReply(readerUser, "S #" + mid).getDescription());
+ juickProtocol.getReply(readerUser, "S #" + mid).getDescription());
assertEquals("number of subscribed users should match", 1,
- SubscriptionsQueries.getUsersSubscribedToComments(jdbc, mid, uid).size());
+ subscriptionService.getUsersSubscribedToComments(mid, uid).size());
assertEquals("should be subscribed", "Subscribed",
- protocol.getReply(readerUser, "S @" + user.getName()).getDescription());
- List<User> friends = UserQueries.getUserFriends(jdbc, readerUid);
+ juickProtocol.getReply(readerUser, "S @" + user.getName()).getDescription());
+ List<User> friends = userService.getUserFriends(readerUid);
assertEquals("number of friend users should match", 2,
friends.size());
assertEquals("number of reader users should match", 1,
- UserQueries.getUserReaders(jdbc, uid).size());
+ userService.getUserReaders(uid).size());
String expectedReply = "Reply posted.\n#" + mid + "/1 "
- +baseUri + mid + "/1";
+ + juickProtocol.getBaseUri() + mid + "/1";
String expectedSecondReply = "Reply posted.\n#" + mid + "/2 "
- + baseUri + mid + "/2";
+ + juickProtocol.getBaseUri() + mid + "/2";
assertEquals("should be reply", expectedReply,
- protocol.getReply(user, "#" + mid + " yoyo").getDescription());
+ juickProtocol.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()
+ juickProtocol.getReply(user, "#" + mid + "/1 yoyo").getDescription());
+ Message reply = messagesService.getReplies(mid).stream().filter(m -> m.getRid() == 2).findFirst()
.orElse(new Message());
assertEquals("should be reply to first comment", 1, reply.getReplyto());
- String jsonReply = protocol.getReply(user, "#" + mid).getJson().orElse("");
+ String jsonReply = juickProtocol.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("");
+ jsonReply = juickProtocol.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());
+ juickProtocol.getReply(readerUser, "#" + mid + " *yo *there").getDescription());
assertEquals("tags should be updated", "Tags are updated",
- protocol.getReply(user, "#" + mid + " *there").getDescription());
+ juickProtocol.getReply(user, "#" + mid + " *there").getDescription());
assertEquals("number of tags should match", 2,
- MessagesQueries.getMessageTags(jdbc, mid).size());
+ tagService.getMessageTags(mid).size());
assertEquals("tags should be updated", "Tags are updated",
- protocol.getReply(user, "#" + mid + " *there").getDescription());
+ juickProtocol.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());
+ tagService.getMessageTags(mid).size());
+ int taggerUid = userService.createUser("dummyTagger", "dummySecret");
+ User taggerUser = userService.getUserByUID(taggerUid).orElse(new User());
assertEquals("should be subscribed", "Subscribed",
- protocol.getReply(taggerUser, "S *yo").getDescription());
+ juickProtocol.getReply(taggerUser, "S *yo").getDescription());
assertEquals("number of subscribed users should match", 2,
- SubscriptionsQueries.getSubscribedUsers(jdbc, uid, mid).size());
+ subscriptionService.getSubscribedUsers(uid, mid).size());
assertEquals("should be unsubscribed", "Unsubscribed from yo",
- protocol.getReply(taggerUser, "U *yo").getDescription());
+ juickProtocol.getReply(taggerUser, "U *yo").getDescription());
assertEquals("number of subscribed users should match", 1,
- SubscriptionsQueries.getSubscribedUsers(jdbc, uid, mid).size());
+ subscriptionService.getSubscribedUsers(uid, mid).size());
assertEquals("number of readers should match", 1,
- UserQueries.getUserReaders(jdbc, uid).size());
- ProtocolReply readerFeed = protocol.getReply(readerUser, "#");
+ userService.getUserReaders(uid).size());
+ ProtocolReply readerFeed = juickProtocol.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.getName(),
- protocol.getReply(readerUser, "U @" + user.getName()).getDescription());
+ juickProtocol.getReply(readerUser, "U @" + user.getName()).getDescription());
assertEquals("number of readers should match", 0,
- UserQueries.getUserReaders(jdbc, uid).size());
+ userService.getUserReaders(uid).size());
assertEquals("number of friends should match", 1,
- UserQueries.getUserFriends(jdbc, uid).size());
+ userService.getUserFriends(uid).size());
assertEquals("should be unsubscribed", "Unsubscribed from #" + mid,
- protocol.getReply(readerUser, "u #" + mid).getDescription());
+ juickProtocol.getReply(readerUser, "u #" + mid).getDescription());
assertEquals("number of subscribed users should match", 0,
- SubscriptionsQueries.getUsersSubscribedToComments(jdbc, mid, uid).size());
+ subscriptionService.getUsersSubscribedToComments(mid, uid).size());
assertNotEquals("should NOT be deleted", String.format("Message %s deleted", mid),
- protocol.getReply(readerUser, "D #" + mid).getDescription());
+ juickProtocol.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("");
+ juickProtocol.getReply(user, "D #" + mid).getDescription());
+ assertEquals("should not have messages", 0, messagesService.getAll(user.getUid(), 0).size());
+ String allFeed = juickProtocol.getReply(readerUser, "#").getJson().orElse("");
JSONArray allFeedMessages = new JSONArray(allFeed);
assertEquals("messages count should match", 0, allFeedMessages.length());
}