aboutsummaryrefslogtreecommitdiff
path: root/juick-www/src/test/java
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2017-10-17 22:43:27 +0300
committerGravatar Vitaly Takmazov2017-10-18 00:22:44 +0300
commita86b5ada5b07ec18671983b0c772063815cac334 (patch)
tree80e62339b857cd699db6f1f087cf2221df655d96 /juick-www/src/test/java
parent5b6ac36436bbed6da7937f0fc83eb734ad929910 (diff)
tests refatoring
now api and www tests uses embedded db
Diffstat (limited to 'juick-www/src/test/java')
-rw-r--r--juick-www/src/test/java/com/juick/www/WebAppTests.java172
1 files changed, 58 insertions, 114 deletions
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<Message> 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))));
}
}