From cc90bf909294467bd44c9870e9c050381e2655f2 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sun, 15 Nov 2020 23:35:59 +0300 Subject: Run tests with JUnit 5 --- build.gradle | 4 + src/test/java/com/juick/FormatterTest.java | 2 +- src/test/java/com/juick/MessageTest.java | 2 +- src/test/java/com/juick/UserTest.java | 8 +- .../java/com/juick/server/tests/ServerTests.java | 146 ++++++++++----------- 5 files changed, 83 insertions(+), 79 deletions(-) diff --git a/build.gradle b/build.gradle index 0673d164..593bf024 100644 --- a/build.gradle +++ b/build.gradle @@ -178,6 +178,10 @@ dependencies { testCompile 'org.bouncycastle:bcpkix-jdk15on:1.67' } +test { + useJUnitPlatform() +} + bootJar { launchScript() } diff --git a/src/test/java/com/juick/FormatterTest.java b/src/test/java/com/juick/FormatterTest.java index 519d70a8..8e6bde4c 100644 --- a/src/test/java/com/juick/FormatterTest.java +++ b/src/test/java/com/juick/FormatterTest.java @@ -19,7 +19,7 @@ package com.juick; import com.juick.util.DateFormattersHolder; import org.apache.commons.lang3.RandomUtils; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.time.Instant; import java.util.Calendar; diff --git a/src/test/java/com/juick/MessageTest.java b/src/test/java/com/juick/MessageTest.java index 9de3163b..cb4f4e45 100644 --- a/src/test/java/com/juick/MessageTest.java +++ b/src/test/java/com/juick/MessageTest.java @@ -25,7 +25,7 @@ import com.overzealous.remark.Options; import com.overzealous.remark.Remark; import org.apache.commons.lang3.RandomUtils; import org.apache.commons.lang3.StringUtils; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.List; diff --git a/src/test/java/com/juick/UserTest.java b/src/test/java/com/juick/UserTest.java index 1ae24e6d..9af67467 100644 --- a/src/test/java/com/juick/UserTest.java +++ b/src/test/java/com/juick/UserTest.java @@ -20,11 +20,12 @@ package com.juick; import com.fasterxml.jackson.databind.ObjectMapper; import com.juick.model.User; import com.juick.test.util.MockUtils; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.io.IOException; +import static org.junit.jupiter.api.Assertions.assertEquals; + public class UserTest { @Test public void userEqualityTest() throws IOException { @@ -32,6 +33,7 @@ public class UserTest { String jsonUser = "{\"uid\" : 1, \"uname\": \"ugnich\"}"; ObjectMapper jsonMapper = new ObjectMapper(); User jsonUgnich = jsonMapper.readValue(jsonUser, User.class); - Assert.assertEquals(ugnich, jsonUgnich); + + assertEquals(ugnich, jsonUgnich); } } diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java index aea9447f..3e883756 100644 --- a/src/test/java/com/juick/server/tests/ServerTests.java +++ b/src/test/java/com/juick/server/tests/ServerTests.java @@ -70,11 +70,10 @@ import org.bouncycastle.jce.spec.ECPublicKeySpec; import org.bouncycastle.math.ec.ECPoint; import org.bouncycastle.openssl.PEMParser; import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.Mockito; @@ -91,6 +90,7 @@ import org.springframework.http.*; import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.client.MockRestServiceServer; import org.springframework.test.web.servlet.MockMvc; @@ -141,11 +141,12 @@ import java.util.stream.IntStream; import java.util.stream.StreamSupport; import static com.juick.www.api.activity.model.Context.ACTIVITY_MEDIA_TYPE; -import static junit.framework.TestCase.assertTrue; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic; +import static org.springframework.test.util.AssertionErrors.assertNotEquals; +import static org.springframework.test.util.AssertionErrors.assertTrue; import static org.springframework.test.web.client.ExpectedCount.times; import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus; @@ -157,7 +158,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. /** * Created by vitalyster on 25.11.2016. */ -@RunWith(SpringRunner.class) +@ExtendWith(SpringExtension.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) @TestPropertySource(properties = { "ios_app_id=12345678.com.juick.ExampleApp" @@ -268,7 +269,7 @@ public class ServerTests { private static boolean isSetUp = false; - @Before + @BeforeEach public void setUp() throws Exception { FileSystemUtils.deleteRecursively(Paths.get(imgDir, "p")); FileSystemUtils.deleteRecursively(Paths.get(imgDir, "photos-1024")); @@ -300,7 +301,7 @@ public class ServerTests { MockitoAnnotations.initMocks(this); } - @After + @AfterEach public void teardown() throws IOException { FileSystemUtils.deleteRecursively(Paths.get(imgDir, "p")); FileSystemUtils.deleteRecursively(Paths.get(imgDir, "photos-1024")); @@ -370,7 +371,7 @@ public class ServerTests { @Test public void messageTests() { User user = userService.createUser("mmmme", "secret").orElseThrow(IllegalStateException::new); - assertEquals("it should be me", "mmmme", user.getName()); + assertEquals("mmmme", user.getName()); int mid = messagesService.createMessage(user.getUid(), "yo", null, new ArrayList<>()); Message msg = messagesService.getMessage(mid).get(); assertEquals("yo", msg.getText()); @@ -425,10 +426,10 @@ public class ServerTests { TagStats htmlTagStats = new TagStats(); htmlTagStats.setTag(htmlTag); String dbTagName = jdbcTemplate.queryForObject("select name from tags where name=?", String.class, htmlTagName); - assertEquals("db tags should not be escaped", dbTagName, htmlTag.getName()); + assertEquals( dbTagName, htmlTag.getName()); int mid4 = messagesService.createMessage(user.getUid(), "yoyoyo", null, null); Message msg4 = messagesService.getMessage(mid4).get(); - assertEquals("tags string should be empty", StringUtils.EMPTY, MessageUtils.getTagsString(msg4)); + assertEquals(StringUtils.EMPTY, MessageUtils.getTagsString(msg4)); messagesService.deleteMessage(user.getUid(), mid4); } @@ -445,9 +446,9 @@ public class ServerTests { Message msg4 = messagesService.getMessage(mid).get(); assertThat(msg4.getLikes(), equalTo(1)); - Assert.assertEquals(2, msg4.getReactions().stream().filter(r -> r.getId() == 2) + assertEquals(2, msg4.getReactions().stream().filter(r -> r.getId() == 2) .findFirst().orElseThrow(IllegalStateException::new).getCount()); - Assert.assertEquals(1, msg4.getReactions().stream().filter(r -> r.getId() == 3) + assertEquals(1, msg4.getReactions().stream().filter(r -> r.getId() == 3) .findFirst().orElseThrow(IllegalStateException::new).getCount()); } @@ -786,107 +787,106 @@ public class ServerTests { assertThat(msg.getAttachmentType(), is("png")); Message msgreply = commandsManager.processCommand(user, "#" + msg.getMid() + " yyy", HttpUtils.downloadImage(URI.create("https://static.juick.com/settings/xmpp.png").toURL(), tmpDir)).getNewMessage().get(); assertThat(msgreply.getAttachmentType(), equalTo("png")); - assertEquals("text should match", "yoyo", + assertEquals( "yoyo", messagesService.getMessage(msg.getMid()).get().getText()); - assertEquals("tag should match", "yo", + assertEquals( "yo", tagService.getMessageTags(msg.getMid()).get(0).getTag().getName()); CommandResult yoyoMsg = commandsManager.processCommand(user, "*yo", URI.create("https://static.juick.com/settings/facebook.png")); - assertTrue(yoyoMsg.getNewMessage().isPresent()); + assertThat(yoyoMsg.getNewMessage().isPresent(), is(true)); assertThat(yoyoMsg.getNewMessage().get().getTags().stream().findFirst().get(), is(yo)); Message msg2 = yoyoMsg.getNewMessage().get(); int mid = msg2.getMid(); Timestamp last = jdbcTemplate.queryForObject("SELECT lastmessage FROM users WHERE id=?", Timestamp.class, user.getUid()); assertThat(last.toInstant(), equalTo(yoyoMsg.getNewMessage().get().getCreated())); - assertEquals("should be message", true, + assertEquals(true, commandsManager.processCommand(user, String.format("#%d", mid), emptyUri).getText().startsWith("@me")); User readerUser = userService.createUser("dummyReader", "dummySecret").orElseThrow(IllegalStateException::new); assertThat(commandsManager.processCommand(readerUser, "s", emptyUri).getText().startsWith("You are subscribed to"), is(true)); assertThat(commandsManager.processCommand(readerUser, "S", emptyUri).getText().startsWith("You are subscribed to"), is(true)); - assertEquals("should be subscribed", "Subscribed", + assertEquals("Subscribed", commandsManager.processCommand(readerUser, "S #" + mid, emptyUri).getText()); - assertEquals("should be favorited", "Message is added to your recommendations", + assertEquals( "Message is added to your recommendations", commandsManager.processCommand(readerUser, "! #" + mid, emptyUri).getText()); int rid = messagesService.createReply(mid, 0, user, "comment", null); - assertEquals("number of subscribed users should match", 1, + assertEquals(1, subscriptionService.getUsersSubscribedToComments( messagesService.getMessage(mid).get(), messagesService.getReply(mid, rid)).size()); privacyQueriesService.blacklistUser(user, readerUser); - assertEquals("number of subscribed users should match", 0, + assertEquals(0, subscriptionService.getUsersSubscribedToComments( messagesService.getMessage(mid).get(), messagesService.getReply(mid, rid)).size()); - assertEquals("number of subscribed users should match", 1, + assertEquals(1, subscriptionService.getUsersSubscribedToComments( messagesService.getMessage(mid).get(), messagesService.getReply(mid, rid), true).size()); - assertEquals("should be subscribed", "Subscribed to @" + user.getName(), + assertEquals("Subscribed to @" + user.getName(), commandsManager.processCommand(readerUser, "S @" + user.getName(), emptyUri) .getText()); List friends = userService.getUserFriends(readerUser.getUid()); - assertEquals("number of friend users should match", 2, + assertEquals(2, friends.size()); - assertEquals("number of reader users should match", 1, + assertEquals(1, userService.getUserReaders(user.getUid()).size()); String expectedSecondReply = "Reply posted.\n#" + mid + "/2 " + "https://juick.com/m/" + mid + "#2"; String expectedThirdReply = "Reply posted.\n#" + mid + "/3 " + "https://juick.com/m/" + mid + "#3"; - assertEquals("should be second reply", expectedSecondReply, + assertEquals(expectedSecondReply, commandsManager.processCommand(user, "#" + mid + " yoyo", URI.create("https://static.juick.com/settings/facebook.png")).getText()); - assertEquals("should be third reply", expectedThirdReply, + assertEquals(expectedThirdReply, commandsManager.processCommand(user, " \t\n #" + mid + "/2 ", URI.create("https://static.juick.com/settings/facebook.png")).getText()); Message reply = messagesService.getReplies(user, mid).stream().filter(m -> m.getRid() == 3).findFirst() .orElse(new Message()); Timestamp lastreply = jdbcTemplate.queryForObject("SELECT lastmessage FROM users WHERE id=?", Timestamp.class, user.getUid()); assertThat(lastreply.toInstant(), equalTo(reply.getCreated())); - assertEquals("should be reply to second comment", 2, reply.getReplyto()); + assertEquals(2, reply.getReplyto()); assertThat(commandsManager.processCommand(readerUser, "#" + mid + " *yo *there", emptyUri) .getText(), startsWith("Reply posted")); - assertEquals("tags should be updated", "Tags are updated", + assertEquals("Tags are updated", commandsManager.processCommand(user, "#" + mid + " *there", emptyUri).getText()); - assertEquals("number of tags should match", 2, + assertEquals(2, tagService.getMessageTags(mid).size()); assertThat(messagesService.getMessage(mid).get().getTags().size(), is(2)); - assertEquals("should be blacklisted", "Tag added to your blacklist", + assertEquals("Tag added to your blacklist", commandsManager.processCommand(readerUser, "BL *there", emptyUri).getText()); - assertEquals("number of subscribed users should match", 0, + assertEquals(0, subscriptionService.getSubscribedUsers(user.getUid(), msg2).size()); - assertEquals("tags should be updated", "Tags are updated", + assertEquals("Tags are updated", commandsManager.processCommand(user, "#" + mid + " *there", emptyUri).getText()); - assertEquals("number of tags should match", 1, - tagService.getMessageTags(mid).size()); + assertEquals(1, tagService.getMessageTags(mid).size()); User taggerUser = userService.createUser("dummyTagger", "dummySecret").orElseThrow(IllegalStateException::new); - assertEquals("should be subscribed", "Subscribed", + assertEquals("Subscribed", commandsManager.processCommand(taggerUser, "S *yo", emptyUri).getText()); - assertEquals("number of subscribed users should match", 2, + assertEquals(2, subscriptionService.getSubscribedUsers(user.getUid(), msg2).size()); - assertEquals("should be unsubscribed", "Unsubscribed from yo", + assertEquals("Unsubscribed from yo", commandsManager.processCommand(taggerUser, "U *yo", emptyUri).getText()); - assertEquals("number of subscribed users should match", 1, + assertEquals(1, subscriptionService.getSubscribedUsers(user.getUid(), msg2).size()); - assertEquals("number of readers should match", 1, + assertEquals(1, userService.getUserReaders(user.getUid()).size()); String readerFeed = commandsManager.processCommand(readerUser, "#", emptyUri).getText(); - assertTrue("description should match", readerFeed.startsWith("Your feed")); - assertEquals("should be unsubscribed", "Unsubscribed from @" + user.getName(), + assertThat(readerFeed.startsWith("Your feed"), is(true)); + assertEquals("Unsubscribed from @" + user.getName(), commandsManager.processCommand(readerUser, "U @" + user.getName(), emptyUri) .getText()); - assertEquals("number of readers should match", 0, + assertEquals(0, userService.getUserReaders(user.getUid()).size()); - assertEquals("number of friends should match", 1, + assertEquals(1, userService.getUserFriends(user.getUid()).size()); - assertEquals("should be unsubscribed", "Unsubscribed from #" + mid, + assertEquals("Unsubscribed from #" + mid, commandsManager.processCommand(readerUser, "u #" + mid, emptyUri).getText()); - assertEquals("number of subscribed users should match", 0, + assertEquals(0, subscriptionService.getUsersSubscribedToComments(messagesService.getMessage(mid).get(), messagesService.getReply(mid, rid)).size()); assertNotEquals("should NOT be deleted", String.format("Message %s deleted", mid), commandsManager.processCommand(readerUser, "D #" + mid, emptyUri).getText()); - assertEquals("should be deleted", "Message deleted", + assertEquals("Message deleted", commandsManager.processCommand(user, "D #" + mid, emptyUri).getText()); - assertEquals("should be not found", "Message not found", + assertEquals("Message not found", commandsManager.processCommand(user, "#" + mid, emptyUri).getText()); String expectedCodeMessage = "some smelly code goes here\n" + @@ -895,11 +895,11 @@ public class ServerTests { String codeAndTags = "*code\n" + expectedCodeMessage; Message codeAndTagsMessage = commandsManager.processCommand(user, codeAndTags, emptyUri).getNewMessage().get(); Set codeAndTagsTags = codeAndTagsMessage.getTags(); - assertEquals("expected single tag", 1, + assertEquals(1, codeAndTagsTags.size()); - assertEquals("the single tag should be the 'code'", "code", + assertEquals("code", codeAndTagsTags.stream().findFirst().get().getName()); - assertEquals("and the message should be with a C-code and without tags", expectedCodeMessage, + assertEquals(expectedCodeMessage, codeAndTagsMessage.getText()); CommandResult result = commandsManager.processCommand(user, "*one *two *three *four *five *six test", emptyUri); assertThat(result.getNewMessage(), is(Optional.empty())); @@ -912,7 +912,7 @@ public class ServerTests { assertThat(result.getNewMessage().isPresent(), is(true)); assertFalse(result.getNewMessage().get().getText().contains("VTYZkKV8FWkmu6g1")); result = commandsManager.processCommand(user, "*корм *juick_ppl *рационализм *? *мюсли а сколько микроморт в дневной порции сверхмюслей?", emptyUri); - assertTrue(result.getNewMessage().isPresent()); + assertThat(result.getNewMessage().isPresent(), is(true)); String tags = "*Juick *Google *Google Play"; String data = "Вчера отправлял *NSFW постинг в топ :)"; result = commandsManager.processCommand(user, String.format("%s %s", tags, data), emptyUri); @@ -1011,7 +1011,7 @@ public class ServerTests { Message msg4 = messagesService.getMessage(mid1).get(); assertThat(msg4.getLikes(), is(0)); assertThat(messagesService.getMessages(AnonymousUser.INSTANCE, Collections.singletonList(mid1)).get(0).getLikes(), is(0)); - Assert.assertEquals(1, msg4.getReactions().stream().filter(r -> r.getId() == 2) + assertEquals(1, msg4.getReactions().stream().filter(r -> r.getId() == 2) .findFirst().orElseThrow(IllegalStateException::new).getCount()); mockMvc.perform(post("/api/react?mid=" + mid1 + "&hash=" + freefdHash + "&reactionId=1")) .andExpect(status().isOk()); @@ -1068,31 +1068,31 @@ public class ServerTests { int mid = messagesService.createMessage(ugnich.getUid(), "yo", "jpg", Collections.singletonList(banned)); privacyQueriesService.blacklistTag(freefd, banned); - assertTrue(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getAll(freefd.getUid(), 0)) - .stream().noneMatch(m -> m.getTags().contains(banned))); + assertThat(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getAll(freefd.getUid(), 0)) + .stream().noneMatch(m -> m.getTags().contains(banned)), is(true)); assertFalse(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getAll(ugnich.getUid(), 0)) .stream().noneMatch(m -> m.getTags().contains(banned))); - assertTrue(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getPhotos(freefd.getUid(), 0)) - .stream().noneMatch(m -> m.getTags().contains(banned))); + assertThat(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getPhotos(freefd.getUid(), 0)) + .stream().noneMatch(m -> m.getTags().contains(banned)), is(true)); assertFalse(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getPhotos(ugnich.getUid(), 0)) .stream().noneMatch(m -> m.getTags().contains(banned))); jdbcTemplate.update("UPDATE messages SET popular=1 WHERE message_id=?", mid); - assertTrue(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getPopular(freefd.getUid(), 0)) - .stream().noneMatch(m -> m.getTags().contains(banned))); + assertThat(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getPopular(freefd.getUid(), 0)) + .stream().noneMatch(m -> m.getTags().contains(banned)), is(true)); assertFalse(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getPopular(ugnich.getUid(), 0)) .stream().noneMatch(m -> m.getTags().contains(banned))); - assertTrue(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getMyFeed(freefd.getUid(), 0, true)) - .stream().noneMatch(m -> m.getTags().contains(banned))); + assertThat(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getMyFeed(freefd.getUid(), 0, true)) + .stream().noneMatch(m -> m.getTags().contains(banned)), is(true)); User newUser1 = userService.createUser("newUser1", "12345").orElseThrow(IllegalStateException::new); int newMid = messagesService.createMessage(newUser1.getUid(), "people", null, Collections.singletonList(banned)); messagesService.recommendMessage(newMid, ugnich.getUid()); - assertTrue(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getMyFeed(freefd.getUid(), 0, true)) - .stream().noneMatch(m -> m.getTags().contains(banned))); + assertThat(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getMyFeed(freefd.getUid(), 0, true)) + .stream().noneMatch(m -> m.getTags().contains(banned)), is(true)); tagService.updateTags(newMid, Collections.singletonList(banned)); assertThat(messagesService.getMessage(newMid).get().getTags().size(), is(0)); privacyQueriesService.blacklistUser(freefd, newUser1); - assertTrue(messagesService.getMyFeed(freefd.getUid(), 0, true) - .stream().noneMatch(m -> m == newMid)); + assertThat(messagesService.getMyFeed(freefd.getUid(), 0, true) + .stream().noneMatch(m -> m == newMid), is(true)); } @Test @@ -1128,9 +1128,9 @@ public class ServerTests { Set tags = MessageUtils.parseTags(tagsString); List tagList = tags.stream().map(t -> tagService.getTag(t.getName(), true)) .collect(Collectors.toList()); - assertEquals("First tag must be", "test", tagList.get(0).getName()); - assertEquals("Third tag must be", "test 3", tagList.get(2).getName()); - assertEquals("Count of tags must be", 3, tagList.size()); + assertEquals("test", tagList.get(0).getName()); + assertEquals("test 3", tagList.get(2).getName()); + assertEquals(3, tagList.size()); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); MultiValueMap map = new LinkedMultiValueMap<>(); @@ -1145,8 +1145,7 @@ public class ServerTests { Message msg = result.getBody().getNewMessage().orElseThrow(); Instant currentDate = msg.getCreated(); String jsonMessage = jsonMapper.writeValueAsString(msg); - assertEquals("date should be in timestamp field", - DateFormattersHolder.getMessageFormatterInstance().format(currentDate), + assertEquals(DateFormattersHolder.getMessageFormatterInstance().format(currentDate), JsonPath.read(jsonMessage, "$.timestamp")); JAXBContext context = JAXBContext @@ -1161,8 +1160,7 @@ public class ServerTests { Document doc = db.parse(new ByteArrayInputStream(sw.toString().getBytes(StandardCharsets.UTF_8))); Node juickNode = doc.getDocumentElement(); NamedNodeMap attrs = juickNode.getAttributes(); - assertEquals("date should be in ts field", - DateFormattersHolder.getMessageFormatterInstance().format(currentDate), + assertEquals(DateFormattersHolder.getMessageFormatterInstance().format(currentDate), attrs.getNamedItem("ts").getNodeValue()); MvcResult apiResult = mockMvc.perform(get("/api/thread?mid=" + msg.getMid())) -- cgit v1.2.3