From b17bf6508ef2b00fbd1ad04725ab457e0cf93831 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 17 May 2018 23:18:40 +0300 Subject: tests refactoring --- .../java/com/juick/server/tests/ServerTests.java | 40 ++++++++++++---------- juick-server/src/test/resources/data.sql | 1 + 2 files changed, 22 insertions(+), 19 deletions(-) (limited to 'juick-server/src/test') diff --git a/juick-server/src/test/java/com/juick/server/tests/ServerTests.java b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java index 924615d7..ebca8c6c 100644 --- a/juick-server/src/test/java/com/juick/server/tests/ServerTests.java +++ b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java @@ -160,8 +160,7 @@ public class ServerTests { private static User ugnich, freefd, juick; static String ugnichName, ugnichPassword, freefdName, freefdPassword, juickName, juickPassword; - static Message msg; - static int juickTagId; + URI emptyUri = URI.create(StringUtils.EMPTY); private static boolean isSetUp = false; @@ -184,13 +183,6 @@ public class ServerTests { freefd = userService.getUserByUID(freefdId).orElseThrow(IllegalStateException::new); int juickId = userService.createUser(juickName, juickPassword); juick = userService.getUserByUID(juickId).orElseThrow(IllegalStateException::new); - - String msgText = "Привет, я - Угнич"; - CommandResult result = commandsManager.processCommand(ugnich, msgText, URI.create("http://static.juick.com/settings/facebook.png")); - int mid = result.getNewMessage().get().getMid(); - msg = messagesService.getMessage(mid); - tagService.createTag("тест"); - juickTagId = tagService.createTag("juick"); isSetUp = true; } } @@ -384,17 +376,24 @@ public class ServerTests { @Test public void homeTestWithMessages() throws Exception { + String msgText = "Привет, я - Угнич"; + CommandResult result = commandsManager.processCommand(ugnich, msgText, URI.create("http://static.juick.com/settings/facebook.png")); + int mid = result.getNewMessage().get().getMid(); + Message msg = messagesService.getMessage(mid); + tagService.createTag("тест"); mockMvc.perform( get("/home") .with(httpBasic(ugnichName, ugnichPassword))) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) - .andExpect(jsonPath("$[-1].mid", is(msg.getMid()))) - .andExpect(jsonPath("$[-1].timestamp", + .andExpect(jsonPath("$[0].mid", is(msg.getMid()))) + .andExpect(jsonPath("$[0].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"))); + .andExpect(jsonPath("$[0].body", is(msg.getText()))) + .andExpect(jsonPath("$[0].attachment.url", + is(String.format("https://i.juick.com/p/%d.png", msg.getMid())))) + .andExpect(jsonPath("$[0].attachment.small.url", + is(String.format("https://i.juick.com/photos-512/%d.png", msg.getMid())))); } @Test @@ -564,10 +563,12 @@ public class ServerTests { } @Test public void notificationsSettingsAllowedOnlyForServiceUser() throws Exception { + CommandResult result = commandsManager.processCommand(ugnich, "yo", emptyUri); + String stringValueOfMid = String.valueOf(result.getNewMessage().get().getMid()); mockMvc.perform(get("/notifications").with(httpBasic(juickName, juickPassword)) - .param("mid", "1").param("uid", String.valueOf(ugnich.getUid()))).andExpect(status().isOk()); + .param("mid", stringValueOfMid).param("uid", String.valueOf(ugnich.getUid()))).andExpect(status().isOk()); mockMvc.perform(get("/notifications") - .param("mid", "1").param("uid", String.valueOf(ugnich.getUid()))).andExpect(status().isUnauthorized()); + .param("mid", stringValueOfMid).param("uid", String.valueOf(ugnich.getUid()))).andExpect(status().isUnauthorized()); } @Test public void topTest() { @@ -578,8 +579,10 @@ public class ServerTests { messagesService.createReply(topmid, 0, next, "yo", null); }); - assertThat(messagesService.getPopularCandidates().get(0), is(topmid)); - Tag juickTag = tagService.getTag(juickTagId); + List topCandidates = messagesService.getPopularCandidates(); + assertThat(topCandidates.size(), is(1)); + assertThat(topCandidates.get(0), is(topmid)); + Tag juickTag = tagService.getTag("juick", false); assertThat(juickTag.TID, is(2)); tagService.updateTags(topmid, Collections.singletonList(juickTag)); assertThat(messagesService.getPopularCandidates().isEmpty(), is(true)); @@ -654,7 +657,6 @@ public class ServerTests { } @Test public void botCommandsTests() throws Exception { - URI emptyUri = URI.create(StringUtils.EMPTY); assertThat(commandsManager.processCommand(AnonymousUser.INSTANCE, "PING", emptyUri).getText(), is("PONG")); // subscription commands have two lines, others have 1 assertThat(commandsManager.processCommand(AnonymousUser.INSTANCE, "help", emptyUri).getText().split("\n").length, is(32)); diff --git a/juick-server/src/test/resources/data.sql b/juick-server/src/test/resources/data.sql index 17421b16..102b11f4 100644 --- a/juick-server/src/test/resources/data.sql +++ b/juick-server/src/test/resources/data.sql @@ -1,3 +1,4 @@ +INSERT INTO tags(tag_id, name) VALUES(2, 'juick'); INSERT INTO reactions (like_id, description) VALUES (1, 'like'); INSERT INTO reactions (like_id, description) VALUES (2, 'love'); INSERT INTO reactions (like_id, description) VALUES (3, 'lol'); -- cgit v1.2.3