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 ++ .../java/com/juick/service/MessageServiceTest.java | 2 +- .../java/com/juick/service/MockImagesService.java | 3 - .../server/configuration/BaseWebConfiguration.java | 6 +- .../server/configuration/StorageConfiguration.java | 14 ++ .../juick/www/configuration/WwwInitializer.java | 4 +- .../src/test/java/com/juick/www/WebAppTests.java | 172 ++++++---------- .../main/java/com/juick/components/XMPPBot.java | 13 +- .../configuration/BotAppConfiguration.java | 2 +- .../src/test/java/com/juick/xmpp/XMPPTests.java | 5 + 12 files changed, 167 insertions(+), 292 deletions(-) create mode 100644 juick-api/src/test/java/com/juick/api/tests/configuration/MockStorageConfiguration.java create mode 100644 juick-server-web/src/main/java/com/juick/server/configuration/StorageConfiguration.java 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(); + } +} diff --git a/juick-server-jdbc/src/test/java/com/juick/service/MessageServiceTest.java b/juick-server-jdbc/src/test/java/com/juick/service/MessageServiceTest.java index f5701b0c..0577e009 100644 --- a/juick-server-jdbc/src/test/java/com/juick/service/MessageServiceTest.java +++ b/juick-server-jdbc/src/test/java/com/juick/service/MessageServiceTest.java @@ -59,7 +59,7 @@ public class MessageServiceTest extends AbstractJUnit4SpringContextTests { @Inject private PMQueriesService pmQueriesService; - int ugnichId, freefdId, mid; + static int ugnichId, freefdId, mid; private static boolean setUpIsDone = false; diff --git a/juick-server-jdbc/src/test/java/com/juick/service/MockImagesService.java b/juick-server-jdbc/src/test/java/com/juick/service/MockImagesService.java index 6f47de91..33cc1243 100644 --- a/juick-server-jdbc/src/test/java/com/juick/service/MockImagesService.java +++ b/juick-server-jdbc/src/test/java/com/juick/service/MockImagesService.java @@ -5,9 +5,6 @@ import com.juick.Message; import com.juick.Photo; import org.springframework.util.StringUtils; -import java.io.File; -import java.nio.file.Paths; - public class MockImagesService implements ImagesService { @Override public void setAttachmentMetadata(String imgDir, String baseUrl, Message msg) throws Exception { diff --git a/juick-server-web/src/main/java/com/juick/server/configuration/BaseWebConfiguration.java b/juick-server-web/src/main/java/com/juick/server/configuration/BaseWebConfiguration.java index abf8482c..27f4d55c 100644 --- a/juick-server-web/src/main/java/com/juick/server/configuration/BaseWebConfiguration.java +++ b/juick-server-web/src/main/java/com/juick/server/configuration/BaseWebConfiguration.java @@ -43,7 +43,6 @@ import java.util.concurrent.Executors; /** * Created by vitalyster on 28.06.2016. */ -@Import(DataConfiguration.class) @Configuration public class BaseWebConfiguration implements WebMvcConfigurer, SchedulingConfigurer { @@ -76,10 +75,7 @@ public class BaseWebConfiguration implements WebMvcConfigurer, SchedulingConfigu resolver.setMaxUploadSize(10000000); return resolver; } - @Bean - public ImagesService imagesService() { - return new ImagesServiceImpl(); - } + @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { taskRegistrar.setScheduler(taskExecutor()); diff --git a/juick-server-web/src/main/java/com/juick/server/configuration/StorageConfiguration.java b/juick-server-web/src/main/java/com/juick/server/configuration/StorageConfiguration.java new file mode 100644 index 00000000..94b23037 --- /dev/null +++ b/juick-server-web/src/main/java/com/juick/server/configuration/StorageConfiguration.java @@ -0,0 +1,14 @@ +package com.juick.server.configuration; + +import com.juick.service.ImagesService; +import com.juick.service.ImagesServiceImpl; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class StorageConfiguration { + @Bean + public ImagesService imagesService() { + return new ImagesServiceImpl(); + } +} 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 c621c450..36de7a6d 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 @@ -17,6 +17,7 @@ package com.juick.www.configuration; +import com.juick.configuration.DataConfiguration; import com.juick.configuration.SearchConfiguration; import org.apache.commons.codec.CharEncoding; import org.springframework.web.filter.CharacterEncodingFilter; @@ -38,7 +39,8 @@ public class WwwInitializer extends AbstractAnnotationConfigDispatcherServletIni WwwAppConfiguration.class, SearchConfiguration.class, SapeConfiguration.class, - WebSecurityConfig.class + WebSecurityConfig.class, + DataConfiguration.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 index c2f6b0d9..73750cb7 100644 --- a/juick-www/src/test/java/com/juick/www/WebAppTests.java +++ b/juick-www/src/test/java/com/juick/www/WebAppTests.java @@ -25,10 +25,11 @@ import com.gargoylesoftware.htmlunit.html.HtmlPage; import com.juick.Message; import com.juick.Tag; import com.juick.User; -import com.juick.configuration.MockDataConfiguration; +import com.juick.configuration.RepositoryConfiguration; +import com.juick.service.ImagesService; import com.juick.service.MessagesService; +import com.juick.service.MockImagesService; import com.juick.service.UserService; -import com.juick.test.util.MockUtils; import com.juick.util.MessageUtils; import com.juick.www.configuration.SapeConfiguration; import com.juick.www.configuration.WwwAppConfiguration; @@ -37,13 +38,13 @@ import com.mitchellbosecke.pebble.PebbleEngine; import com.mitchellbosecke.pebble.error.PebbleException; import com.mitchellbosecke.pebble.template.PebbleTemplate; import org.apache.commons.text.StringEscapeUtils; -import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +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.jdbc.core.JdbcTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; @@ -56,87 +57,82 @@ import java.io.IOException; import java.io.StringWriter; import java.io.Writer; import java.util.Collections; -import java.util.List; -import java.util.Optional; -import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.StreamSupport; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.startsWith; -import static org.mockito.Mockito.when; /** * Created by vitalyster on 12.01.2017. */ @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration -@ContextConfiguration +@ContextConfiguration(classes = { + WwwServletConfiguration.class, WwwAppConfiguration.class, SapeConfiguration.class, + RepositoryConfiguration.class +}) public class WebAppTests { @Configuration @EnableWebMvc @ComponentScan(basePackages = "com.juick.www.controllers") - @Import(value = { - WwwServletConfiguration.class, WwwAppConfiguration.class, SapeConfiguration.class, - MockDataConfiguration.class - }) - static class Config {} + static class Config { + @Bean + public ImagesService imagesService() { + return new MockImagesService(); + } + } @Inject private WebApplicationContext wac; @Inject private WebApp webApp; - private WebClient webClient; + private static WebClient webClient; @Inject UserService userService; @Inject MessagesService messagesService; + @Inject + JdbcTemplate jdbcTemplate; @Inject PebbleEngine pebbleEngine; + private static User ugnich, freefd; + private static String ugnichName, ugnichPassword, freefdName, freefdPassword; + + private static boolean isSetUp = false; + @Before public void setup() { - webClient = MockMvcWebClientBuilder.webAppContextSetup(this.wac).build(); - webClient.getOptions().setJavaScriptEnabled(false); - webClient.getOptions().setCssEnabled(false); - } - @After - public void teardown() { - webClient.close(); + if (!isSetUp) { + webClient = MockMvcWebClientBuilder.webAppContextSetup(this.wac).build(); + webClient.getOptions().setJavaScriptEnabled(false); + webClient.getOptions().setCssEnabled(false); + webClient.getOptions().setThrowExceptionOnFailingStatusCode(false); + + 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); + + isSetUp = true; + } } @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, false)) - .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"); + int mid = messagesService.createMessage(ugnich.getUid(), msgText, null, null); + HtmlPage threadPage = webClient.getPage(String.format("http://localhost:8080/ugnich/%d", mid)); assertThat(threadPage.getTitleText(), equalTo("ugnich:")); } @Test @@ -146,51 +142,21 @@ public class WebAppTests { String msgText = "автор этого поста был забанен"; String hash = "12345678"; - User user = MockUtils.mockUser(2, userName, userPassword); - user.setBanned(true); - Message msg = MockUtils.mockMessage(2, user, msgText); - - when(userService.getUIDbyName(userName)) - .thenReturn(2); - when(userService.getUserByName(userName)) - .thenReturn(user); - when(userService.getUserByUID(2)) - .thenReturn(Optional.of(user)); - when(userService.getFullyUserByName(userName)) - .thenReturn(user); - when(messagesService.getMyFeed(2, 0, false)) - .thenReturn(Collections.singletonList(2)); - when(messagesService.getMessages(Collections.singletonList(2))) - .thenReturn(Collections.singletonList(msg)); - when(userService.getUIDbyHash(hash)) - .thenReturn(1); - when(messagesService.getMessageAuthor(2)).thenReturn(user); - when(messagesService.canViewThread(2, 0)).thenReturn(true); - when(messagesService.getMessage(2)).thenReturn(msg); - webClient.getOptions().setThrowExceptionOnFailingStatusCode(false); + User isilmine = userService.getUserByUID(userService.createUser(userName, userPassword)).orElseThrow(IllegalStateException::new); + int mid = messagesService.createMessage(isilmine.getUid(), msgText, null, null); + jdbcTemplate.update("UPDATE users SET banned=1 WHERE id=?", isilmine.getUid()); Page blogPage = webClient.getPage("http://localhost:8080/isilmine"); - Page threadPage = webClient.getPage("http://localhost:8080/isilmine/2"); + Page threadPage = webClient.getPage(String.format("http://localhost:8080/isilmine/%d", mid)); assertThat(blogPage.getWebResponse().getStatusCode(), equalTo(404)); assertThat(threadPage.getWebResponse().getStatusCode(), equalTo(404)); } @Test public void repliesTree() throws IOException { - User ugnich = MockUtils.mockUser(1, "ugnich", "secret"); - User freefd = MockUtils.mockUser(3, "freefd", "secret"); - Message hello = MockUtils.mockMessage(1, ugnich, null); - List replies = IntStream.range(1, 15).mapToObj(i -> - MockUtils.mockReply(hello.getMid(), i, freefd, i-1, null )).collect(Collectors.toList()); - - when(userService.getUIDbyName("ugnich")).thenReturn(1); - when(userService.getUIDbyName("freefd")).thenReturn(3); - when(userService.getUserByName("ugnich")).thenReturn(ugnich); - when(userService.getUserByName("freefd")).thenReturn(freefd); - - when(messagesService.canViewThread(1, 0)).thenReturn(true); - when(messagesService.getMessage(1)).thenReturn(hello); - when(messagesService.getReplies(1)).thenReturn(replies); + int mid = messagesService.createMessage(ugnich.getUid(), "hello", null, null); + IntStream.range(1, 15).forEach(i -> + messagesService.createReply(mid, i-1, freefd.getUid(), String.valueOf(i-1), null )); - HtmlPage threadPage = webClient.getPage("http://localhost:8080/ugnich/1"); + HtmlPage threadPage = webClient.getPage(String.format("http://localhost:8080/ugnich/%d", mid)); assertThat(threadPage.getWebResponse().getStatusCode(), equalTo(200)); Long visibleItems = StreamSupport.stream(threadPage.getHtmlElementById("replies") .getChildElements().spliterator(), false).filter(e -> { @@ -218,40 +184,18 @@ public class WebAppTests { } @Test public void testTwitterCards() 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, false)) - .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); + int mid = messagesService.createMessage(ugnich.getUid(), "without image", null, null); - assertThat(fetchMeta("http://localhost:8080/ugnich/1", "twitter:card") + assertThat(fetchMeta(String.format("http://localhost:8080/ugnich/%d", mid), "twitter:card") .getAttribute("content"), equalTo("summary")); - msg.setAttachmentType("png"); - assertThat(fetchMeta("http://localhost:8080/ugnich/1", "twitter:card") + int mid2 = messagesService.createMessage(ugnich.getUid(), "with image", "png", null); + Message message = messagesService.getMessage(mid2); + assertThat(fetchMeta(String.format("http://localhost:8080/ugnich/%d", mid2), "twitter:card") .getAttribute("content"), equalTo("summary_large_image")); - assertThat(fetchMeta("http://localhost:8080/ugnich/1", "og:description") + assertThat(fetchMeta(String.format("http://localhost:8080/ugnich/%d", mid2), "og:description") .getAttribute("content"), - startsWith(StringEscapeUtils.escapeHtml4(MessageUtils.getMessageHashTags(msg)))); + startsWith(StringEscapeUtils.escapeHtml4(MessageUtils.getMessageHashTags(message)))); } } diff --git a/juick-xmpp-wip/src/main/java/com/juick/components/XMPPBot.java b/juick-xmpp-wip/src/main/java/com/juick/components/XMPPBot.java index b551ff2b..fa097584 100644 --- a/juick-xmpp-wip/src/main/java/com/juick/components/XMPPBot.java +++ b/juick-xmpp-wip/src/main/java/com/juick/components/XMPPBot.java @@ -37,6 +37,7 @@ import rocks.xmpp.extensions.vcard.temp.model.VCard; import rocks.xmpp.extensions.version.SoftwareVersionManager; import rocks.xmpp.extensions.version.model.SoftwareVersion; +import javax.annotation.PostConstruct; import javax.inject.Inject; import java.io.IOException; import java.lang.reflect.InvocationTargetException; @@ -49,17 +50,21 @@ import java.util.List; public class XMPPBot implements AutoCloseable, ProtocolListener { private static final Logger logger = LoggerFactory.getLogger(XMPPBot.class); @Inject - UserService userService; + private UserService userService; @Inject - PMQueriesService pmQueriesService; + private PMQueriesService pmQueriesService; @Inject - JuickProtocol juickProtocol; + private JuickProtocol juickProtocol; + @Inject + private Environment env; Jid juickJid; private ExternalComponent component; - public XMPPBot(Environment env) { + @PostConstruct + @Inject + public void init() { component = ExternalComponent.create(env.getProperty("component_name", "juick.com"), env.getProperty("component_password", "secret"), env.getProperty("component_host", "localhost"), NumberUtils.toInt(env.getProperty("component_port", "5347"), 5347)); diff --git a/juick-xmpp-wip/src/main/java/com/juick/components/configuration/BotAppConfiguration.java b/juick-xmpp-wip/src/main/java/com/juick/components/configuration/BotAppConfiguration.java index 7009f2b7..1de0a696 100644 --- a/juick-xmpp-wip/src/main/java/com/juick/components/configuration/BotAppConfiguration.java +++ b/juick-xmpp-wip/src/main/java/com/juick/components/configuration/BotAppConfiguration.java @@ -39,7 +39,7 @@ public class BotAppConfiguration extends WebMvcConfigurationSupport { @Bean public XMPPBot xmpp() { - return new XMPPBot(env); + return new XMPPBot(); } @Bean public JuickProtocol juickProtocol() { diff --git a/juick-xmpp-wip/src/test/java/com/juick/xmpp/XMPPTests.java b/juick-xmpp-wip/src/test/java/com/juick/xmpp/XMPPTests.java index e513c5e7..971e849a 100644 --- a/juick-xmpp-wip/src/test/java/com/juick/xmpp/XMPPTests.java +++ b/juick-xmpp-wip/src/test/java/com/juick/xmpp/XMPPTests.java @@ -23,6 +23,7 @@ import com.juick.components.XMPPRouter; import com.juick.components.configuration.XmppAppConfiguration; import com.juick.configuration.MockDataConfiguration; import com.juick.server.configuration.BaseWebConfiguration; +import com.juick.service.PrivacyQueriesService; import com.juick.service.ShowQueriesService; import org.junit.Before; import org.junit.Test; @@ -65,6 +66,10 @@ public class XMPPTests { public ShowQueriesService showQueriesService() { return Mockito.mock(ShowQueriesService.class); } + @Bean + public PrivacyQueriesService privacyQueriesService() { + return Mockito.mock(PrivacyQueriesService.class); + } } @Inject -- cgit v1.2.3