From 3d7f60c1c0b48dad2f58bffe3352d1c5c1b93f00 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 1 Dec 2016 13:56:53 +0300 Subject: juick-api: Tags controller from perl, refactoring --- .../java/com/juick/api/tests/MessagesTests.java | 81 +++++++++++++++------- 1 file changed, 57 insertions(+), 24 deletions(-) (limited to 'juick-api/src/test/java/com/juick/api') diff --git a/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java b/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java index 05744a61..e0f9b840 100644 --- a/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java +++ b/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java @@ -7,7 +7,9 @@ import com.juick.api.configuration.ApiAppConfiguration; import com.juick.api.configuration.ApiMvcConfiguration; import com.juick.api.configuration.ApiSecurityConfig; import com.juick.configuration.DataConfiguration; +import com.juick.server.helpers.TagStats; import com.juick.service.MessagesService; +import com.juick.service.TagService; import com.juick.service.UserService; import org.apache.commons.lang3.RandomStringUtils; import org.junit.Before; @@ -59,6 +61,12 @@ public class MessagesTests { UserService userService() { return Mockito.mock(UserService.class); } + + @Bean + @Primary + TagService tagService() { + return Mockito.mock(TagService.class); + } } private MockMvc mockMvc; @@ -69,6 +77,11 @@ public class MessagesTests { private MessagesService messagesService; @Inject private UserService userService; + @Inject + private TagService tagService; + + private User ugnich, freefd; + String ugnichName, ugnichPassword, freefdName, freefdPassword; private static Message getMessage(final User user, final String messageText) { Message msg = new Message(); @@ -98,12 +111,30 @@ public class MessagesTests { .apply(SecurityMockMvcConfigurers.springSecurity()) .dispatchOptions(true) .build(); + ugnichName = "ugnich"; + ugnichPassword = "MyPassw0rd!"; + freefdName = "freefd"; + freefdPassword = "MyPassw0rd!"; + + ugnich = getUser(1, ugnichName, ugnichPassword); + freefd = getUser(2, freefdName, freefdPassword); + + List users = new ArrayList<>(2); + users.add(ugnichName); + users.add(freefdName); + + when(userService.getUsersByName(users)) + .thenReturn(Arrays.asList(ugnich, freefd)); + when(userService.getUserByName(ugnichName)) + .thenReturn(ugnich); + when(userService.getFullyUserByName(ugnichName)) + .thenReturn(ugnich); + when(userService.getUserByName(null)) + .thenReturn(new User()); } @Test public void testAllUnAuthorized() throws Exception { - when(userService.getUserByName(null)) - .thenReturn(new User()); mockMvc.perform(get("/")) .andExpect(status().is4xxClientError()); @@ -120,17 +151,11 @@ public class MessagesTests { @Test public void homeTestWithMessages() throws Exception { - String ugnichName = "ugnich"; - String uginchPassword = "MyPassw0rd!"; String msgText = "Привет, я - Угнич"; - User user = getUser(1, ugnichName, uginchPassword); - Message msg = getMessage(user, msgText); + Message msg = getMessage(ugnich, msgText); + - when(userService.getUserByName(ugnichName)) - .thenReturn(user); - when(userService.getFullyUserByName(ugnichName)) - .thenReturn(user); when(messagesService.getMyFeed(1, 0)) .thenReturn(Collections.singletonList(1)); when(messagesService.getMessages(Collections.singletonList(1))) @@ -138,7 +163,7 @@ public class MessagesTests { mockMvc.perform( get("/home") - .with(httpBasic(ugnichName, uginchPassword))) + .with(httpBasic(ugnichName, ugnichPassword))) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) .andExpect(jsonPath("$", hasSize(1))) @@ -233,20 +258,7 @@ public class MessagesTests { @Test public void anonymousApis() throws Exception { - String ugnichName = "ugnich"; - String uginchPassword = "MyPassw0rd!"; - String freefdName = "freefd"; - String freefdPassword = "MyPassw0rd!"; - - User ugnich = getUser(1, ugnichName, uginchPassword); - User freefd = getUser(2, freefdName, freefdPassword); - - List users = new ArrayList<>(2); - users.add(ugnichName); - users.add(freefdName); - when(userService.getUsersByName(users)) - .thenReturn(Arrays.asList(ugnich, freefd)); mockMvc.perform(get("/messages")) .andExpect(status().isOk()); @@ -258,4 +270,25 @@ public class MessagesTests { .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) .andExpect(jsonPath("$", hasSize(2))); } + @Test + public void tags() throws Exception { + Tag weather = new Tag("weather"); + TagStats sw = new TagStats(); + sw.setTag(weather); + sw.setUsageCount(15); + Tag yo = new Tag("yo"); + TagStats sy = new TagStats(); + sy.setTag(yo); + sy.setUsageCount(5); + when(tagService.getUserTagStats(1)).thenReturn(Collections.singletonList(sy)); + when(tagService.getTagStats()).thenReturn(Arrays.asList(sy, sw)); + mockMvc.perform(get("/tags")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$", hasSize(2))); + mockMvc.perform(get("/tags") + .param("user_id", "1")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$", hasSize(1))) + .andExpect(jsonPath("$[0].messages", is(5))); + } } -- cgit v1.2.3