From a86b5ada5b07ec18671983b0c772063815cac334 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 17 Oct 2017 22:43:27 +0300 Subject: tests refatoring now api and www tests uses embedded db --- .../juick/api/configuration/ApiInitializer.java | 6 +- .../java/com/juick/api/tests/MessagesTests.java | 218 ++++++--------------- .../configuration/MockStorageConfiguration.java | 14 ++ 3 files changed, 75 insertions(+), 163 deletions(-) create mode 100644 juick-api/src/test/java/com/juick/api/tests/configuration/MockStorageConfiguration.java (limited to 'juick-api/src') diff --git a/juick-api/src/main/java/com/juick/api/configuration/ApiInitializer.java b/juick-api/src/main/java/com/juick/api/configuration/ApiInitializer.java index 8f9cb4ed..59c69926 100644 --- a/juick-api/src/main/java/com/juick/api/configuration/ApiInitializer.java +++ b/juick-api/src/main/java/com/juick/api/configuration/ApiInitializer.java @@ -17,6 +17,8 @@ package com.juick.api.configuration; +import com.juick.configuration.DataConfiguration; +import com.juick.server.configuration.StorageConfiguration; import org.apache.commons.codec.CharEncoding; import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; @@ -31,7 +33,9 @@ public class ApiInitializer extends AbstractAnnotationConfigDispatcherServletIni @Override protected Class[] getRootConfigClasses() { return new Class[]{ - ApiSecurityConfig.class + ApiSecurityConfig.class, + DataConfiguration.class, + StorageConfiguration.class }; } 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 518e3762..6fddba11 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 @@ -21,46 +21,34 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.juick.Message; import com.juick.Tag; import com.juick.User; -import com.juick.api.ApiServer; -import com.juick.api.TelegramBotManager; -import com.juick.server.configuration.BaseWebConfiguration; -import com.juick.api.configuration.ApiSecurityConfig; -import com.juick.api.configuration.MessengerConfiguration; -import com.juick.configuration.MockDataConfiguration; -import com.juick.server.helpers.TagStats; -import com.juick.service.*; -import com.juick.test.util.MockUtils; +import com.juick.api.configuration.ApiAppConfiguration; +import com.juick.api.tests.configuration.MockStorageConfiguration; +import com.juick.configuration.RepositoryConfiguration; +import com.juick.service.ImagesService; +import com.juick.service.MessagesService; +import com.juick.service.TagService; +import com.juick.service.UserService; import com.juick.util.DateFormattersHolder; -import org.junit.After; 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.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Import; import org.springframework.http.MediaType; import org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers; import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; 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.MvcResult; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; -import org.springframework.web.servlet.config.annotation.EnableWebMvc; -import org.springframework.web.socket.client.WebSocketConnectionManager; import javax.inject.Inject; -import java.time.Instant; -import java.util.*; +import java.util.Arrays; +import java.util.Collections; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.is; -import static org.mockito.Mockito.when; +import static org.hamcrest.Matchers.*; 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.MockMvcResultMatchers.*; @@ -69,32 +57,9 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. * Created by vitalyster on 25.11.2016. */ @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration +@ContextConfiguration(classes = {RepositoryConfiguration.class, ApiAppConfiguration.class, MockStorageConfiguration.class}) @WebAppConfiguration -public class MessagesTests { - @Configuration - @EnableWebMvc - @ComponentScan(basePackages = "com.juick.api.controllers") - @Import(value = {BaseWebConfiguration.class, ApiSecurityConfig.class, - MockDataConfiguration.class, MessengerConfiguration.class}) - static class Config { - @Bean - WebSocketConnectionManager connectionManager() { - return Mockito.mock(WebSocketConnectionManager.class); - } - @Bean - TelegramBotManager tgBot() { - return Mockito.mock(TelegramBotManager.class); - } - @Bean - ApiServer apiServer() { - return Mockito.mock(ApiServer.class); - } - @Bean - ImagesService imagesService() { - return new MockImagesService(); - } - } +public class MessagesTests extends AbstractJUnit4SpringContextTests { private MockMvc mockMvc; @Inject @@ -111,8 +76,11 @@ public class MessagesTests { @Inject private ImagesService imagesService; - private User ugnich, freefd; - String ugnichName, ugnichPassword, freefdName, freefdPassword; + private static User ugnich, freefd; + static String ugnichName, ugnichPassword, freefdName, freefdPassword; + static Message msg; + + private static boolean isSetUp = false; @Before @@ -121,33 +89,22 @@ public class MessagesTests { .apply(SecurityMockMvcConfigurers.springSecurity()) .dispatchOptions(true) .build(); - ugnichName = "ugnich"; - ugnichPassword = "MyPassw0rd!"; - freefdName = "freefd"; - freefdPassword = "MyPassw0rd!"; - - ugnich = MockUtils.mockUser(1, ugnichName, ugnichPassword); - freefd = MockUtils.mockUser(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()); - } - - @After - public void resetMocks() { - Mockito.reset(userService); - Mockito.reset(messagesService); - Mockito.reset(tagService); + if (!isSetUp) { + ugnichName = "ugnich"; + ugnichPassword = "MyPassw0rd!"; + freefdName = "freefd"; + freefdPassword = "MyPassw0rd!"; + + int ugnichId = userService.createUser(ugnichName, ugnichPassword); + ugnich = userService.getUserByUID(ugnichId).orElseThrow(IllegalStateException::new); + int freefdId = userService.createUser(freefdName, freefdPassword); + freefd = userService.getUserByUID(freefdId).orElseThrow(IllegalStateException::new); + String msgText = "Привет, я - Угнич"; + + int mid = messagesService.createMessage(ugnich.getUid(), msgText, "png", null); + msg = messagesService.getMessage(mid); + isSetUp = true; + } } @Test @@ -171,96 +128,45 @@ public class MessagesTests { @Test public void homeTestWithMessages() throws Exception { - String msgText = "Привет, я - Угнич"; - - Message msg = MockUtils.mockMessage(1, ugnich, msgText); - Instant now = Instant.now(); - msg.setTimestamp(now); - msg.setAttachmentType("png"); - imagesService.setAttachmentMetadata("", "http://localhost/", msg); - when(messagesService.getMyFeed(1, 0, true)) - .thenReturn(Collections.singletonList(1)); - when(messagesService.getMessages(Collections.singletonList(1))) - .thenReturn(Collections.singletonList(msg)); - mockMvc.perform( get("/home") .with(httpBasic(ugnichName, ugnichPassword))) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) - .andExpect(jsonPath("$", hasSize(1))) - .andExpect(jsonPath("$[0].mid", is(1))) - .andExpect(jsonPath("$[0].timestamp", is(DateFormattersHolder.getMessageFormatterInstance().format(now)))) - .andExpect(jsonPath("$[0].body", is(msgText))) - .andExpect(jsonPath("$[0].attachment.url", is("http://localhost/p/1.png"))) - .andExpect(jsonPath("$[0].attachment.small.url", is("http://localhost/photos-512/1.png"))); + .andExpect(jsonPath("$[-1].mid", is(msg.getMid()))) + .andExpect(jsonPath("$[-1].timestamp", + is(DateFormattersHolder.getMessageFormatterInstance().format(msg.getTimestamp())))) + .andExpect(jsonPath("$[-1].body", is(msg.getText()))) + .andExpect(jsonPath("$[-1].attachment.url", is("https://i.juick.com/p/1.png"))) + .andExpect(jsonPath("$[-1].attachment.small.url", is("https://i.juick.com/photos-512/1.png"))); } @Test public void homeTestWithMessagesAndRememberMe() throws Exception { - String msgText = "Привет, я - Угнич"; - String hash = "12345678"; - - User user = MockUtils.mockUser(1, ugnichName, ugnichPassword); - Message msg = MockUtils.mockMessage(1, user, msgText); - - when(userService.getUIDbyName(ugnichName)) - .thenReturn(1); - when(userService.getUserByName(ugnichName)) - .thenReturn(user); - when(userService.getUserByUID(1)) - .thenReturn(Optional.of(user)); - when(userService.getFullyUserByName(ugnichName)) - .thenReturn(user); - when(messagesService.getMyFeed(1, 0, true)) - .thenReturn(Collections.singletonList(1)); - when(messagesService.getMessages(Collections.singletonList(1))) - .thenReturn(Collections.singletonList(msg)); - when(userService.getUIDbyHash(hash)) - .thenReturn(1); - + String ugnichHash = userService.getHashByUID(ugnich.getUid()); mockMvc.perform( get("/home") .with(httpBasic(ugnichName, ugnichPassword))) .andExpect(status().isOk()) .andReturn(); - when(userService.getUserByHash(hash)) - .thenReturn(user); - mockMvc.perform(get("/home") - .param("hash", hash)) + .param("hash", ugnichHash)) .andExpect(status().isOk()); } @Test public void homeTestWithMessagesAndSimpleCors() throws Exception { - User user = MockUtils.mockUser(1, ugnichName, ugnichPassword); - Message msg = MockUtils.mockMessage(1, user, null); - - when(userService.getFullyUserByName(ugnichName)) - .thenReturn(user); - when(userService.getUserByName(ugnichName)) - .thenReturn(user); - when(messagesService.getMyFeed(1, 0, false)) - .thenReturn(Collections.singletonList(1)); - when(messagesService.getMessages(Collections.singletonList(1))) - .thenReturn(Collections.singletonList(msg)); - mockMvc.perform( get("/home") .with(httpBasic(ugnichName, ugnichPassword)) .header("Origin", "http://api.example.net")) - .andExpect(status().isNotFound()) + .andExpect(status().isOk()) .andExpect(header().string("Access-Control-Allow-Origin", "*")); } @Test public void homeTestWithPreflightCors() throws Exception { - User user = MockUtils.mockUser(1, ugnichName, ugnichPassword); - when(userService.getFullyUserByName(ugnichName)) - .thenReturn(user); - mockMvc.perform( options("/home") .with(httpBasic(ugnichName, ugnichPassword)) @@ -290,24 +196,19 @@ public class MessagesTests { @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)); + Tag weather = tagService.getTag("weather", true); + Tag yo = tagService.getTag("yo", true); + messagesService.createMessage(ugnich.getUid(), "text", null, Arrays.asList(yo, weather)); + messagesService.createMessage(freefd.getUid(), "text2", null, Collections.singletonList(yo)); mockMvc.perform(get("/tags")) .andExpect(status().isOk()) - .andExpect(jsonPath("$", hasSize(2))); + .andExpect(jsonPath("$", hasSize(2))) + .andExpect(jsonPath("$[0].messages", is(2))); mockMvc.perform(get("/tags") - .param("user_id", "1")) + .param("user_id", String.valueOf(ugnich.getUid()))) .andExpect(status().isOk()) - .andExpect(jsonPath("$", hasSize(1))) - .andExpect(jsonPath("$[0].messages", is(5))); + .andExpect(jsonPath("$", hasSize(2))) + .andExpect(jsonPath("$[0].messages", is(1))); } @Test @@ -324,14 +225,7 @@ public class MessagesTests { } @Test public void performRequestsWithIssuedToken() throws Exception { - User user = MockUtils.mockUser(1, ugnichName, ugnichPassword); - String testHash = "12345"; - when(userService.getFullyUserByName(ugnichName)) - .thenReturn(user); - when(userService.getUserByName(ugnichName)) - .thenReturn(user); - when(userService.getUserByHash(testHash)).thenReturn(user); - when(userService.getHashByUID(user.getUid())).thenReturn(testHash); + String ugnichHash = userService.getHashByUID(ugnich.getUid()); mockMvc.perform(get("/home")).andExpect(status().isUnauthorized()); mockMvc.perform(get("/auth")) .andExpect(status().isUnauthorized()); @@ -340,8 +234,8 @@ public class MessagesTests { MvcResult result = mockMvc.perform(get("/auth").with(httpBasic(ugnichName, ugnichPassword))) .andExpect(status().isOk()) .andReturn(); - String hash = jsonMapper.readValue(result.getResponse().getContentAsString(), String.class); - assertThat(hash, equalTo(testHash)); - mockMvc.perform(get("/home").param("hash", hash)).andExpect(status().isNotFound()); + String authHash = jsonMapper.readValue(result.getResponse().getContentAsString(), String.class); + assertThat(authHash, equalTo(ugnichHash)); + mockMvc.perform(get("/home").param("hash", ugnichHash)).andExpect(status().isOk()); } } diff --git a/juick-api/src/test/java/com/juick/api/tests/configuration/MockStorageConfiguration.java b/juick-api/src/test/java/com/juick/api/tests/configuration/MockStorageConfiguration.java new file mode 100644 index 00000000..374bec0a --- /dev/null +++ b/juick-api/src/test/java/com/juick/api/tests/configuration/MockStorageConfiguration.java @@ -0,0 +1,14 @@ +package com.juick.api.tests.configuration; + +import com.juick.service.ImagesService; +import com.juick.service.MockImagesService; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class MockStorageConfiguration { + @Bean + public ImagesService imagesService() { + return new MockImagesService(); + } +} -- cgit v1.2.3