From 842e903d5b987dd23e82a423bddae75acdea0c9c Mon Sep 17 00:00:00 2001 From: mykhailo.dubovskyi Date: Wed, 18 Apr 2018 15:57:53 +0300 Subject: Like -> Reaction --- .../java/com/juick/server/tests/ServerTests.java | 84 +++++++++++++++++++--- 1 file changed, 75 insertions(+), 9 deletions(-) (limited to 'juick-server/src/test/java/com/juick') diff --git a/juick-server/src/test/java/com/juick/server/tests/ServerTests.java b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java index 17c7e35c..6b31906d 100644 --- a/juick-server/src/test/java/com/juick/server/tests/ServerTests.java +++ b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java @@ -19,10 +19,7 @@ package com.juick.server.tests; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import com.juick.ExternalToken; -import com.juick.Message; -import com.juick.Tag; -import com.juick.User; +import com.juick.*; import com.juick.server.*; import com.juick.server.component.MessageEvent; import com.juick.server.helpers.AnonymousUser; @@ -34,10 +31,9 @@ import com.juick.util.DateFormattersHolder; import com.juick.util.MessageUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; +import org.hamcrest.Matchers; +import org.junit.*; +import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; @@ -77,6 +73,7 @@ import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; /** @@ -261,11 +258,14 @@ public class ServerTests { List replies2 = messagesService.getReplies(user, msg2.getMid()); assertThat(replies2.size(), equalTo(2)); assertThat(replies2.get(1), equalTo(reply2)); + Message msg3 = messagesService.getMessage(mid2); + assertEquals(2, msg3.getReplies()); assertEquals("weather", msg3.getTags().get(0).getName()); assertEquals(ugnich.getUid(), userService.checkPassword(ugnich.getName(), "x")); assertEquals(-1, userService.checkPassword(ugnich.getName(), "xy")); + subscriptionService.subscribeMessage(msg, user); subscriptionService.subscribeMessage(msg, ugnich); int reply_id = messagesService.createReply(msg.getMid(), 0, ugnich, "comment", null); @@ -286,6 +286,28 @@ public class ServerTests { assertEquals("tags string should be empty", StringUtils.EMPTY, MessageUtils.getTagsString(msg4)); messagesService.deleteMessage(user_id, mid4); } + + @Rule + public ExpectedException exception = ExpectedException.none(); + + @Test + public void likeTypeStatsTests(){ + int user_id = userService.createUser("dsdss", "secret"); + final int freefdId = freefd.getUid(); + int mid = messagesService.createMessage(user_id, "yo", null, new ArrayList<>()); + messagesService.likeMessage(mid, freefdId , 2); + messagesService.likeMessage(mid, freefdId,2); + messagesService.likeMessage(mid, freefdId,3); + exception.expect(IllegalArgumentException.class); + messagesService.likeMessage(mid, freefdId,1); + + Message msg4 = messagesService.getMessage(mid); + + Assert.assertEquals(2, msg4.getReactions().stream().filter(r -> r.getId() == 2) + .findFirst().orElseThrow(IllegalStateException::new).getCount()); + Assert.assertEquals(1,msg4.getReactions().stream().filter(r -> r.getId() == 3) + .findFirst().orElseThrow(IllegalStateException::new).getCount()); + } @Test public void lastJidShouldNotBeDeleted() { int ugnich_id = userService.createUser("hugnich2", "x"); @@ -404,6 +426,29 @@ public class ServerTests { .andExpect(jsonPath("$", hasSize(2))); } + @Test + public void messagesUrlTest() throws Exception { + int user_id = userService.createUser("dsds4345", "secret"); + + String freefdHash = userService.getHashByUID(freefd.getUid()); + System.out.println("user_id"+ user_id); + String userIdHash = userService.getHashByUID(user_id); + final int freefdId = freefd.getUid(); + int mid = messagesService.createMessage(user_id, "yo", null, new ArrayList<>()); + messagesService.likeMessage(mid, freefdId, 2 ); + messagesService.likeMessage(mid, freefdId, 2 ); + messagesService.likeMessage(mid, freefdId, 3 ); + + mockMvc.perform(get("/messages?"+ "hash=" + userIdHash)) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) + .andExpect((jsonPath("$[0].reactions[?(@.id == 3)].count", + Matchers.is(Collections.singletonList(1))))) + .andExpect((jsonPath("$[0].reactions[?(@.id == 2)].count", + Matchers.is(Collections.singletonList(2))))); + } + @Test public void tags() throws Exception { Tag weather = tagService.getTag("weather", true); @@ -745,16 +790,37 @@ public class ServerTests { } @Test - public void likesTests() throws Exception { + public void recommendTests() throws Exception { + int mid = messagesService.createMessage(ugnich.getUid(), "to be liked", null, null); String freefdHash = userService.getHashByUID(freefd.getUid()); int freefdMid = messagesService.createMessage(freefd.getUid(), "to be not liked", null, null); + mockMvc.perform(post("/like?mid=" + mid + "&hash=" + freefdHash)) .andExpect(status().isOk()) .andExpect(jsonPath("$.status", is("Message is added to your recommendations"))); + mockMvc.perform(post("/like?mid=" + freefdMid + "&hash=" + freefdHash)) .andExpect(status().isForbidden()); } + + @Test + public void likesTests() throws Exception{ + int user_id = userService.createUser("dsds", "secret"); + String freefdHash = userService.getHashByUID(freefd.getUid()); + System.out.println("user_id"+ user_id); + String userIdHash = userService.getHashByUID(user_id); + int mid1 = messagesService.createMessage(user_id, "yo", null, new ArrayList<>()); + + mockMvc.perform(post("/react?mid=" + mid1 + "&hash=" + freefdHash+ "&reactionId=2")) + .andExpect(status().isOk()); + + Message msg4 = messagesService.getMessage(mid1); + System.out.println("msg4"+msg4); + Assert.assertEquals(1, msg4.getReactions().stream().filter(r -> r.getId() == 2) + .findFirst().orElseThrow(IllegalStateException::new).getCount()); + } + @Test public void telegramTests() { jdbcTemplate.update("INSERT INTO telegram(user_id, tg_id) VALUES(?,1)", ugnich.getUid()); -- cgit v1.2.3