From 0c1ef3ab4f352445df3c1ebf87fe9eac2f78df41 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 12 Jan 2017 14:59:11 +0300 Subject: tests refactoring --- juick-api/build.gradle | 7 +- .../java/com/juick/api/tests/MessagesTests.java | 66 +++------------- juick-server/build.gradle | 11 +++ .../juick/configuration/MockDataConfiguration.java | 51 ++++++++++++ .../test/java/com/juick/tests/util/MockUtils.java | 33 ++++++++ juick-www/build.gradle | 4 + .../www/configuration/WebAppConfiguration.java | 50 ------------ .../www/configuration/WwwAppConfiguration.java | 50 ++++++++++++ .../juick/www/configuration/WwwInitializer.java | 2 +- .../src/test/java/com/juick/www/WebAppTests.java | 90 ++++++++++++++++++++++ 10 files changed, 253 insertions(+), 111 deletions(-) create mode 100644 juick-server/src/test/java/com/juick/configuration/MockDataConfiguration.java create mode 100644 juick-server/src/test/java/com/juick/tests/util/MockUtils.java delete mode 100644 juick-www/src/main/java/com/juick/www/configuration/WebAppConfiguration.java create mode 100644 juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java create mode 100644 juick-www/src/test/java/com/juick/www/WebAppTests.java diff --git a/juick-api/build.gradle b/juick-api/build.gradle index 5202789a..64fc4d19 100644 --- a/juick-api/build.gradle +++ b/juick-api/build.gradle @@ -19,12 +19,7 @@ dependencies { compile 'net.coobird:thumbnailator:0.4.8' providedRuntime 'mysql:mysql-connector-java:5.1.40' - testCompile "junit:junit:${rootProject.junitVersion}" - testCompile "org.hamcrest:hamcrest-all:${rootProject.hamcrestVersion}" - testCompile "org.mockito:mockito-core:1.+" - testCompile "org.springframework:spring-test:${rootProject.springFrameworkVersion}" - testCompile "org.springframework.security:spring-security-test:${rootProject.springSecurityVersion}" - + testCompile project(path: ':juick-server', configuration: 'testArtifacts') testRuntime 'com.jayway.jsonpath:json-path:2.2.0' } 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 2a5dba18..61d1b008 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 @@ -6,20 +6,17 @@ import com.juick.User; 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.configuration.MockDataConfiguration; 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 com.juick.tests.util.MockUtils; 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.security.test.web.servlet.setup.SecurityMockMvcConfigurers; import org.springframework.test.context.ContextConfiguration; @@ -47,26 +44,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @WebAppConfiguration public class MessagesTests { @Configuration - @Import(value = {ApiMvcConfiguration.class, ApiAppConfiguration.class, ApiSecurityConfig.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); - } - } + @Import(value = {ApiMvcConfiguration.class, ApiAppConfiguration.class, ApiSecurityConfig.class, MockDataConfiguration.class}) + static class Config {} private MockMvc mockMvc; @Inject @@ -82,27 +61,6 @@ public class MessagesTests { 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() { @@ -115,8 +73,8 @@ public class MessagesTests { freefdName = "freefd"; freefdPassword = "MyPassw0rd!"; - ugnich = getUser(1, ugnichName, ugnichPassword); - freefd = getUser(2, freefdName, freefdPassword); + ugnich = MockUtils.mockUser(1, ugnichName, ugnichPassword); + freefd = MockUtils.mockUser(2, freefdName, freefdPassword); List users = new ArrayList<>(2); users.add(ugnichName); @@ -152,7 +110,7 @@ public class MessagesTests { public void homeTestWithMessages() throws Exception { String msgText = "Привет, я - Угнич"; - Message msg = getMessage(ugnich, msgText); + Message msg = MockUtils.mockMessage(1, ugnich, msgText); when(messagesService.getMyFeed(1, 0)) .thenReturn(Collections.singletonList(1)); @@ -174,8 +132,8 @@ public class MessagesTests { String msgText = "Привет, я - Угнич"; String hash = "12345678"; - User user = getUser(1, ugnichName, ugnichPassword); - Message msg = getMessage(user, msgText); + User user = MockUtils.mockUser(1, ugnichName, ugnichPassword); + Message msg = MockUtils.mockMessage(1, user, msgText); when(userService.getUIDbyName(ugnichName)) .thenReturn(1); @@ -208,8 +166,8 @@ public class MessagesTests { @Test public void homeTestWithMessagesAndSimpleCors() throws Exception { - User user = getUser(1, ugnichName, ugnichPassword); - Message msg = getMessage(user, null); + User user = MockUtils.mockUser(1, ugnichName, ugnichPassword); + Message msg = MockUtils.mockMessage(1, user, null); when(userService.getFullyUserByName(ugnichName)) .thenReturn(user); @@ -230,7 +188,7 @@ public class MessagesTests { @Test public void homeTestWithPreflightCors() throws Exception { - User user = getUser(1, ugnichName, ugnichPassword); + User user = MockUtils.mockUser(1, ugnichName, ugnichPassword); when(userService.getFullyUserByName(ugnichName)) .thenReturn(user); diff --git a/juick-server/build.gradle b/juick-server/build.gradle index ee77b572..2c140b12 100644 --- a/juick-server/build.gradle +++ b/juick-server/build.gradle @@ -62,3 +62,14 @@ compileJava.options.encoding = 'UTF-8' configurations { all*.exclude module: 'commons-logging' } + +configurations { + testArtifacts.extendsFrom testRuntime +} +task testJar(type: Jar) { + classifier "test" + from sourceSets.test.output +} +artifacts { + testArtifacts testJar +} diff --git a/juick-server/src/test/java/com/juick/configuration/MockDataConfiguration.java b/juick-server/src/test/java/com/juick/configuration/MockDataConfiguration.java new file mode 100644 index 00000000..be72f6ca --- /dev/null +++ b/juick-server/src/test/java/com/juick/configuration/MockDataConfiguration.java @@ -0,0 +1,51 @@ +package com.juick.configuration; + +import com.juick.service.*; +import org.mockito.Mockito; +import org.springframework.context.annotation.Bean; + +/** + * Created by vitalyster on 12.01.2017. + */ +public class MockDataConfiguration { + @Bean + MessagesService messagesService() { + return Mockito.mock(MessagesService.class); + } + @Bean + UserService userService() { + return Mockito.mock(UserService.class); + } + @Bean + TagService tagService() { + return Mockito.mock(TagService.class); + } + @Bean + PushQueriesService pushQueriesService() { + return Mockito.mock(PushQueriesService.class); + } + @Bean + SubscriptionService subscriptionService() { + return Mockito.mock(SubscriptionService.class); + } + @Bean + PMQueriesService pmQueriesService() { + return Mockito.mock(PMQueriesService.class); + } + @Bean + TelegramService telegramService() { + return Mockito.mock(TelegramService.class); + } + @Bean + AdsService adsService() { + return Mockito.mock(AdsService.class); + } + @Bean + CrosspostService crosspostService() { + return Mockito.mock(CrosspostService.class); + } + @Bean + EmailService emailService() { + return Mockito.mock(EmailService.class); + } +} diff --git a/juick-server/src/test/java/com/juick/tests/util/MockUtils.java b/juick-server/src/test/java/com/juick/tests/util/MockUtils.java new file mode 100644 index 00000000..c67b26ef --- /dev/null +++ b/juick-server/src/test/java/com/juick/tests/util/MockUtils.java @@ -0,0 +1,33 @@ +package com.juick.tests.util; + +import com.juick.Message; +import com.juick.User; +import org.apache.commons.lang3.RandomStringUtils; + +import java.util.Date; + +/** + * Created by vitalyster on 12.01.2017. + */ +public class MockUtils { + public static Message mockMessage(Integer mid, final User user, final String messageText) { + Message msg = new Message(); + + msg.setMid(mid); + msg.setUser(user); + msg.setText(messageText == null ? RandomStringUtils.randomAlphanumeric(24) : messageText); + msg.setDate(new Date()); + return msg; + } + + public static User mockUser(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; + } +} diff --git a/juick-www/build.gradle b/juick-www/build.gradle index 8d081515..26cf61da 100644 --- a/juick-www/build.gradle +++ b/juick-www/build.gradle @@ -30,6 +30,10 @@ dependencies { compile 'com.mitchellbosecke:pebble-spring4:2.3.0' providedCompile 'javax.servlet:javax.servlet-api:3.1.0' providedRuntime 'mysql:mysql-connector-java:5.1.40' + + testCompile project(path: ':juick-server', configuration: 'testArtifacts') + testCompile ('net.sourceforge.htmlunit:htmlunit:2.24') + } compileFrontend.dependsOn 'npmInstall' diff --git a/juick-www/src/main/java/com/juick/www/configuration/WebAppConfiguration.java b/juick-www/src/main/java/com/juick/www/configuration/WebAppConfiguration.java deleted file mode 100644 index cd681190..00000000 --- a/juick-www/src/main/java/com/juick/www/configuration/WebAppConfiguration.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.juick.www.configuration; - -import com.juick.service.TagService; -import com.juick.service.UserService; -import com.juick.www.HelpService; -import com.juick.www.controllers.PageTemplates; -import com.juick.www.WebApp; -import org.springframework.cache.annotation.EnableCaching; -import org.springframework.cache.guava.GuavaCacheManager; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.PropertySource; -import org.springframework.core.env.Environment; - -import javax.annotation.Resource; -import javax.inject.Inject; - -/** - * Created by aalexeev on 11/22/16. - */ -@Configuration -@PropertySource("classpath:juick.conf") -@EnableCaching -public class WebAppConfiguration { - @Resource - private Environment env; - @Inject - private UserService userService; - @Inject - private TagService tagService; - - @Bean - public WebApp webApp() { - return new WebApp(env, templates(), userService, tagService); - } - @Bean - public PageTemplates templates() { - return new PageTemplates(); - } - @Bean - public GuavaCacheManager cacheManager() { - return new GuavaCacheManager("help"); - } - - @Bean - public HelpService helpService() { - return new HelpService("help"); - } - -} diff --git a/juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java b/juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java new file mode 100644 index 00000000..7ffefb1d --- /dev/null +++ b/juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java @@ -0,0 +1,50 @@ +package com.juick.www.configuration; + +import com.juick.service.TagService; +import com.juick.service.UserService; +import com.juick.www.HelpService; +import com.juick.www.controllers.PageTemplates; +import com.juick.www.WebApp; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.cache.guava.GuavaCacheManager; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.core.env.Environment; + +import javax.annotation.Resource; +import javax.inject.Inject; + +/** + * Created by aalexeev on 11/22/16. + */ +@Configuration +@PropertySource("classpath:juick.conf") +@EnableCaching +public class WwwAppConfiguration { + @Resource + private Environment env; + @Inject + private UserService userService; + @Inject + private TagService tagService; + + @Bean + public WebApp webApp() { + return new WebApp(env, templates(), userService, tagService); + } + @Bean + public PageTemplates templates() { + return new PageTemplates(); + } + @Bean + public GuavaCacheManager cacheManager() { + return new GuavaCacheManager("help"); + } + + @Bean + public HelpService helpService() { + return new HelpService("help"); + } + +} diff --git a/juick-www/src/main/java/com/juick/www/configuration/WwwInitializer.java b/juick-www/src/main/java/com/juick/www/configuration/WwwInitializer.java index cd5429c2..0e185447 100644 --- a/juick-www/src/main/java/com/juick/www/configuration/WwwInitializer.java +++ b/juick-www/src/main/java/com/juick/www/configuration/WwwInitializer.java @@ -18,7 +18,7 @@ public class WwwInitializer extends AbstractAnnotationConfigDispatcherServletIni @Override protected Class[] getRootConfigClasses() { return new Class[]{ - WebAppConfiguration.class, + WwwAppConfiguration.class, DataConfiguration.class, SearchConfiguration.class }; diff --git a/juick-www/src/test/java/com/juick/www/WebAppTests.java b/juick-www/src/test/java/com/juick/www/WebAppTests.java new file mode 100644 index 00000000..b4fa14bb --- /dev/null +++ b/juick-www/src/test/java/com/juick/www/WebAppTests.java @@ -0,0 +1,90 @@ +package com.juick.www; + +import com.gargoylesoftware.htmlunit.WebClient; +import com.gargoylesoftware.htmlunit.html.HtmlPage; +import com.juick.Message; +import com.juick.User; +import com.juick.configuration.MockDataConfiguration; +import com.juick.service.MessagesService; +import com.juick.service.UserService; +import com.juick.tests.util.MockUtils; +import com.juick.www.configuration.WwwAppConfiguration; +import com.juick.www.configuration.WwwServletConfiguration; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +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.htmlunit.MockMvcWebClientBuilder; +import org.springframework.web.context.WebApplicationContext; + +import javax.inject.Inject; +import java.util.Collections; +import java.util.Optional; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; +import static org.mockito.Mockito.when; + +/** + * Created by vitalyster on 12.01.2017. + */ +@RunWith(SpringJUnit4ClassRunner.class) +@WebAppConfiguration +@ContextConfiguration +public class WebAppTests { + @Configuration + @Import(value = {WwwServletConfiguration.class, WwwAppConfiguration.class, MockDataConfiguration.class}) + static class Config {} + + @Inject + private WebApplicationContext wac; + @Inject + private WebApp webApp; + + private WebClient webClient; + + @Inject + UserService userService; + @Inject + MessagesService messagesService; + + @Before + public void setup() { + webClient = MockMvcWebClientBuilder.webAppContextSetup(this.wac).build(); + } + + @Test + public void postWithoutTagsShouldNotHaveAsteriskInTitle() throws Exception { + String ugnichName = "ugnich"; + String ugnichPassword = "MyPassw0rd!"; + 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)) + .thenReturn(Collections.singletonList(1)); + when(messagesService.getMessages(Collections.singletonList(1))) + .thenReturn(Collections.singletonList(msg)); + when(userService.getUIDbyHash(hash)) + .thenReturn(1); + when(messagesService.getMessageAuthor(1)).thenReturn(user); + when(messagesService.canViewThread(1, 0)).thenReturn(true); + when(messagesService.getMessage(1)).thenReturn(msg); + HtmlPage threadPage = webClient.getPage("http://localhost:8080/ugnich/1"); + assertThat(threadPage.getTitleText(), equalTo("ugnich:")); + } +} -- cgit v1.2.3