From dd4b8ff44e11a5b4734e428e3a7153eb1b89a825 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 13 Dec 2016 15:41:37 +0300 Subject: juick-rss: webmvc port WIP --- .../test/java/com/juick/rss/tests/RSSTests.java | 148 +++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 juick-rss/src/test/java/com/juick/rss/tests/RSSTests.java (limited to 'juick-rss/src/test') diff --git a/juick-rss/src/test/java/com/juick/rss/tests/RSSTests.java b/juick-rss/src/test/java/com/juick/rss/tests/RSSTests.java new file mode 100644 index 00000000..8f10f9c3 --- /dev/null +++ b/juick-rss/src/test/java/com/juick/rss/tests/RSSTests.java @@ -0,0 +1,148 @@ +package com.juick.rss.tests; + +import com.juick.Message; +import com.juick.Tag; +import com.juick.User; +import com.juick.configuration.DataConfiguration; +import com.juick.rss.configuration.RssAppConfiguration; +import com.juick.rss.configuration.RssMvcConfiguration; +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; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.Primary; +import org.springframework.http.MediaType; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import javax.inject.Inject; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +/** + * Created by vitalyster on 13.12.2016. + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +@WebAppConfiguration +public class RSSTests { + @Configuration + @Import(value = {RssMvcConfiguration.class, RssAppConfiguration.class, DataConfiguration.class}) + static class Config { + @Bean + @Primary + MessagesService messagesService() { + return Mockito.mock(MessagesService.class); + } + + @Bean + @Primary + UserService userService() { + return Mockito.mock(UserService.class); + } + + @Bean + @Primary + TagService tagService() { + return Mockito.mock(TagService.class); + } + } + + private MockMvc mockMvc; + @Inject + private WebApplicationContext webApplicationContext; + + @Inject + 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(); + + msg.setMid(1); + msg.setUser(user); + msg.setText(messageText == null ? RandomStringUtils.randomAlphanumeric(24) : messageText); + msg.setTags(Collections.singletonList(new Tag(RandomStringUtils.randomAlphabetic(4)))); + + return msg; + } + + private static User getUser(final int uid, final String name, final String password) { + User user = new User(); + + user.setName(name); + user.setUid(uid); + user.setCredentials(password); + user.setBanned(false); + + return user; + } + + @Before + public void setUp() { + mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext) + .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 lastMessagesTest() throws Exception { + String msgText = "Привет, я - Угнич"; + + Message msg = getMessage(ugnich, msgText); + + when(messagesService.getMyFeed(1, 0)) + .thenReturn(Collections.singletonList(1)); + when(messagesService.getMessages(Collections.singletonList(1))) + .thenReturn(Collections.singletonList(msg)); + + mockMvc.perform( + get("/")) + .andExpect(status().isOk()) + .andExpect(content().contentType("application/rss+xml")); + } +} -- cgit v1.2.3