From f56d7724c0ed7a87821fac0999c334540d3f797f Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 20 Jun 2020 14:16:36 +0200 Subject: [PATCH] =?utf8?q?=F0=9F=8E=A8=20Replace=20list=20notification=20f?= =?utf8?q?ilter=20test=20with=20Kotlin=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../sone/notify/ListNotificationFilterTest.java | 280 --------------------- .../sone/notify/ListNotificationFilterTest.kt | 228 +++++++++++++++++ 2 files changed, 228 insertions(+), 280 deletions(-) delete mode 100644 src/test/java/net/pterodactylus/sone/notify/ListNotificationFilterTest.java create mode 100644 src/test/kotlin/net/pterodactylus/sone/notify/ListNotificationFilterTest.kt diff --git a/src/test/java/net/pterodactylus/sone/notify/ListNotificationFilterTest.java b/src/test/java/net/pterodactylus/sone/notify/ListNotificationFilterTest.java deleted file mode 100644 index af6339b..0000000 --- a/src/test/java/net/pterodactylus/sone/notify/ListNotificationFilterTest.java +++ /dev/null @@ -1,280 +0,0 @@ -package net.pterodactylus.sone.notify; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.contains; -import static org.hamcrest.Matchers.emptyIterable; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.sameInstance; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import javax.annotation.Nullable; - -import net.pterodactylus.sone.data.Post; -import net.pterodactylus.sone.data.PostReply; -import net.pterodactylus.sone.data.Sone; -import net.pterodactylus.sone.data.SoneOptions; -import net.pterodactylus.sone.freenet.wot.OwnIdentity; -import net.pterodactylus.util.notify.Notification; - -import com.google.common.base.Predicate; -import com.google.common.base.Predicates; -import com.google.inject.Guice; -import com.google.inject.Injector; -import org.hamcrest.Matchers; -import org.junit.Test; -import org.mockito.ArgumentMatchers; - -/** - * Unit test for {@link ListNotificationFilterTest}. - */ -public class ListNotificationFilterTest { - - private static final String LOCAL_ID = "local-id"; - - private final PostVisibilityFilter postVisibilityFilter = mock(PostVisibilityFilter.class); - private final ReplyVisibilityFilter replyVisibilityFilter = mock(ReplyVisibilityFilter.class); - private final ListNotificationFilter listNotificationFilter = new ListNotificationFilter(postVisibilityFilter, replyVisibilityFilter); - - private final Sone localSone = mock(Sone.class); - private final SoneOptions soneOptions = mock(SoneOptions.class); - private final OwnIdentity localIdentity = mock(OwnIdentity.class); - private final List> newPostNotifications = Arrays.asList(createNewPostNotification()); - private final List> newReplyNotifications = Arrays.asList(createNewReplyNotification()); - private final List> mentionNotifications = Arrays.asList(createMentionNotification()); - - public ListNotificationFilterTest() { - when(localSone.getId()).thenReturn(LOCAL_ID); - when(localSone.isLocal()).thenReturn(true); - when(localSone.getIdentity()).thenReturn(localIdentity); - when(localIdentity.getId()).thenReturn(LOCAL_ID); - when(localSone.getOptions()).thenReturn(soneOptions); - } - - @Test - public void filterIsOnlyCreatedOnce() { - Injector injector = Guice.createInjector(); - ListNotificationFilter firstFilter = injector.getInstance(ListNotificationFilter.class); - ListNotificationFilter secondFilter = injector.getInstance(ListNotificationFilter.class); - assertThat(firstFilter, sameInstance(secondFilter)); - } - - @Test - public void newSoneNotificationsAreNotRemovedIfNotLoggedIn() { - List notifications = Arrays.asList(createNewSoneNotification()); - List filteredNotifications = listNotificationFilter.filterNotifications(notifications, null); - assertThat(filteredNotifications, contains(notifications.get(0))); - } - - private Notification createNewSoneNotification() { - ListNotification newSoneNotification = mock(ListNotification.class); - when(newSoneNotification.getId()).thenReturn("new-sone-notification"); - return newSoneNotification; - } - - @Test - public void newSoneNotificationsAreRemovedIfLoggedInAndNewSonesShouldNotBeShown() { - List notifications = Arrays.asList(createNewSoneNotification()); - List filteredNotifications = listNotificationFilter.filterNotifications(notifications, localSone); - assertThat(filteredNotifications, emptyIterable()); - } - - @Test - public void newSoneNotificationsAreNotRemovedIfLoggedInAndNewSonesShouldBeShown() { - List notifications = Arrays.asList(createNewSoneNotification()); - when(soneOptions.isShowNewSoneNotifications()).thenReturn(true); - List filteredNotifications = listNotificationFilter.filterNotifications(notifications, localSone); - assertThat(filteredNotifications, contains(notifications.get(0))); - } - - private ListNotification createNewPostNotification() { - ListNotification newSoneNotification = mock(ListNotification.class); - when(newSoneNotification.getElements()).thenReturn(new ArrayList()); - when(newSoneNotification.getId()).thenReturn("new-post-notification"); - return newSoneNotification; - } - - @Test - public void newPostNotificationIsNotShownIfOptionsSetAccordingly() { - List> notifications = Arrays.asList(createNewPostNotification()); - List filteredNotifications = listNotificationFilter.filterNotifications(notifications, localSone); - assertThat(filteredNotifications, hasSize(0)); - } - - private void activateNewPostNotifications() { - when(soneOptions.isShowNewPostNotifications()).thenReturn(true); - } - - private boolean addPostToPostNotification(List> notifications) { - return notifications.get(0).getElements().add(mock(Post.class)); - } - - private void setPostVisibilityPredicate(Predicate value) { - when(postVisibilityFilter.isVisible(ArgumentMatchers.any())).thenReturn(value); - } - - @Test - public void newPostNotificationIsNotShownIfNoNewPostsAreVisible() { - activateNewPostNotifications(); - addPostToPostNotification(newPostNotifications); - setPostVisibilityPredicate(Predicates.alwaysFalse()); - List filteredNotifications = listNotificationFilter.filterNotifications(newPostNotifications, localSone); - assertThat(filteredNotifications, hasSize(0)); - } - - @Test - public void newPostNotificationIsShownIfNewPostsAreVisible() { - activateNewPostNotifications(); - addPostToPostNotification(newPostNotifications); - setPostVisibilityPredicate(Predicates.alwaysTrue()); - List filteredNotifications = listNotificationFilter.filterNotifications(newPostNotifications, localSone); - assertThat(filteredNotifications, contains((Notification) newPostNotifications.get(0))); - } - - @Test - public void newPostNotificationIsNotShownIfNewPostsAreVisibleButLocalSoneIsNull() { - activateNewPostNotifications(); - addPostToPostNotification(newPostNotifications); - setPostVisibilityPredicate(Predicates.alwaysTrue()); - List filteredNotifications = listNotificationFilter.filterNotifications(newPostNotifications, null); - assertThat(filteredNotifications, Matchers.emptyIterable()); - } - - @Test - public void newPostNotificationContainsOnlyVisiblePosts() { - activateNewPostNotifications(); - addPostToPostNotification(newPostNotifications); - addPostToPostNotification(newPostNotifications); - setPostVisibilityPredicate(new Predicate() { - @Override - public boolean apply(@Nullable Post post) { - return post.equals(newPostNotifications.get(0).getElements().get(1)); - } - }); - List filteredNotifications = listNotificationFilter.filterNotifications(newPostNotifications, localSone); - assertThat(filteredNotifications, hasSize(1)); - assertThat(((ListNotification) filteredNotifications.get(0)).getElements().get(0), is(newPostNotifications.get(0).getElements().get(1))); - } - - private ListNotification createNewReplyNotification() { - ListNotification newReplyNotifications = mock(ListNotification.class); - when(newReplyNotifications.getElements()).thenReturn(new ArrayList()); - when(newReplyNotifications.getId()).thenReturn("new-reply-notification"); - return newReplyNotifications; - } - - private void activateNewReplyNotifications() { - when(soneOptions.isShowNewReplyNotifications()).thenReturn(true); - } - - private void addReplyToNewReplyNotification(List> notifications) { - notifications.get(0).getElements().add(mock(PostReply.class)); - } - - private void setReplyVisibilityPredicate(Predicate value) { - when(replyVisibilityFilter.isVisible(any(Sone.class))).thenReturn(value); - } - - @Test - public void newReplyNotificationContainsOnlyVisibleReplies() { - activateNewReplyNotifications(); - addReplyToNewReplyNotification(newReplyNotifications); - addReplyToNewReplyNotification(newReplyNotifications); - setReplyVisibilityPredicate(new Predicate() { - @Override - public boolean apply(@Nullable PostReply postReply) { - return postReply.equals(newReplyNotifications.get(0).getElements().get(1)); - } - }); - List filteredNotifications = listNotificationFilter.filterNotifications(newReplyNotifications, localSone); - assertThat(filteredNotifications, hasSize(1)); - assertThat(((ListNotification) filteredNotifications.get(0)).getElements().get(0), is(newReplyNotifications.get(0).getElements().get(1))); - } - - @Test - public void newReplyNotificationIsNotModifiedIfAllRepliesAreVisible() { - activateNewReplyNotifications(); - addReplyToNewReplyNotification(newReplyNotifications); - addReplyToNewReplyNotification(newReplyNotifications); - setReplyVisibilityPredicate(Predicates.alwaysTrue()); - List filteredNotifications = listNotificationFilter.filterNotifications(newReplyNotifications, localSone); - assertThat(filteredNotifications, hasSize(1)); - assertThat(filteredNotifications.get(0), is((Notification) newReplyNotifications.get(0))); - assertThat(((ListNotification) filteredNotifications.get(0)).getElements(), hasSize(2)); - } - - @Test - public void newReplyNotificationIsNotShownIfNoRepliesAreVisible() { - activateNewReplyNotifications(); - addReplyToNewReplyNotification(newReplyNotifications); - addReplyToNewReplyNotification(newReplyNotifications); - setReplyVisibilityPredicate(Predicates.alwaysFalse()); - List filteredNotifications = listNotificationFilter.filterNotifications(newReplyNotifications, localSone); - assertThat(filteredNotifications, hasSize(0)); - } - - @Test - public void newReplyNotificationIsNotShownIfDeactivatedInOptions() { - addReplyToNewReplyNotification(newReplyNotifications); - addReplyToNewReplyNotification(newReplyNotifications); - setReplyVisibilityPredicate(Predicates.alwaysTrue()); - List filteredNotifications = listNotificationFilter.filterNotifications(newReplyNotifications, localSone); - assertThat(filteredNotifications, hasSize(0)); - } - - @Test - public void newReplyNotificationIsNotShownIfCurrentSoneIsNull() { - addReplyToNewReplyNotification(newReplyNotifications); - addReplyToNewReplyNotification(newReplyNotifications); - setReplyVisibilityPredicate(Predicates.alwaysTrue()); - List filteredNotifications = listNotificationFilter.filterNotifications(newReplyNotifications, null); - assertThat(filteredNotifications, hasSize(0)); - } - - private ListNotification createMentionNotification() { - ListNotification newSoneNotification = mock(ListNotification.class); - when(newSoneNotification.getElements()).thenReturn(new ArrayList()); - when(newSoneNotification.getId()).thenReturn("mention-notification"); - return newSoneNotification; - } - - @Test - public void mentionNotificationContainsOnlyVisiblePosts() { - addPostToPostNotification(mentionNotifications); - addPostToPostNotification(mentionNotifications); - setPostVisibilityPredicate(new Predicate() { - @Override - public boolean apply(@Nullable Post post) { - return post.equals(mentionNotifications.get(0).getElements().get(1)); - } - }); - List filteredNotifications = listNotificationFilter.filterNotifications(mentionNotifications, localSone); - assertThat(filteredNotifications, hasSize(1)); - assertThat(((ListNotification) filteredNotifications.get(0)).getElements().get(0), is(mentionNotifications.get(0).getElements().get(1))); - } - - @Test - public void mentionNotificationIsNotShownIfNoPostsAreVisible() { - addPostToPostNotification(mentionNotifications); - addPostToPostNotification(mentionNotifications); - setPostVisibilityPredicate(Predicates.alwaysFalse()); - List filteredNotifications = listNotificationFilter.filterNotifications(mentionNotifications, localSone); - assertThat(filteredNotifications, hasSize(0)); - } - - @Test - public void unfilterableNotificationIsNotFiltered() { - Notification notification = mock(Notification.class); - when(notification.getId()).thenReturn("random-notification"); - List notifications = Arrays.asList(notification); - List filteredNotifications = listNotificationFilter.filterNotifications(notifications, null); - assertThat(filteredNotifications, contains(notification)); - } - -} diff --git a/src/test/kotlin/net/pterodactylus/sone/notify/ListNotificationFilterTest.kt b/src/test/kotlin/net/pterodactylus/sone/notify/ListNotificationFilterTest.kt new file mode 100644 index 0000000..e552ece --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/notify/ListNotificationFilterTest.kt @@ -0,0 +1,228 @@ +package net.pterodactylus.sone.notify + +import com.google.common.base.Predicate +import com.google.inject.Guice +import net.pterodactylus.sone.data.Post +import net.pterodactylus.sone.data.PostReply +import net.pterodactylus.sone.data.Sone +import net.pterodactylus.sone.test.createLocalSone +import net.pterodactylus.sone.test.createPost +import net.pterodactylus.sone.test.createPostReply +import net.pterodactylus.sone.test.verifySingletonInstance +import net.pterodactylus.util.notify.Notification +import net.pterodactylus.util.notify.TemplateNotification +import net.pterodactylus.util.template.Template +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.contains +import org.hamcrest.Matchers.emptyIterable +import org.hamcrest.Matchers.equalTo +import org.hamcrest.Matchers.hasSize +import org.junit.Test + +/** + * Unit test for [ListNotificationFilterTest]. + */ +class ListNotificationFilterTest { + + private val listNotificationFilter = ListNotificationFilter(showAllPosts, showAllReplies) + + @Test + fun `filter is only created once`() { + val injector = Guice.createInjector() + injector.verifySingletonInstance() + } + + @Test + fun `new sone notifications are not removed if not logged in`() { + val notification = createNewSoneNotification() + val filteredNotifications = listNotificationFilter.filterNotifications(listOf(notification), null) + assertThat(filteredNotifications, contains(notification)) + } + + @Test + fun `new sone notifications are removed if logged in and new sones should not be shown`() { + val notification = createNewSoneNotification() + val localSone = createLocalSone() + localSone.options.isShowNewSoneNotifications = false + val filteredNotifications = listNotificationFilter.filterNotifications(listOf(notification), localSone) + assertThat(filteredNotifications, emptyIterable()) + } + + @Test + fun `new sone notifications are not removed if logged in and new sones should be shown`() { + val notification = createNewSoneNotification() + val localSone = createLocalSone() + localSone.options.isShowNewSoneNotifications = true + val filteredNotifications = listNotificationFilter.filterNotifications(listOf(notification), localSone) + assertThat(filteredNotifications, contains(notification)) + } + + @Test + fun `new post notification is not shown if options set accordingly`() { + val newPostNotification = createNewPostNotification() + newPostNotification.add(createPost()) + val localSone = createLocalSone() + localSone.options.isShowNewPostNotifications = false + val filteredNotifications = listNotificationFilter.filterNotifications(listOf(newPostNotification), localSone) + assertThat(filteredNotifications, emptyIterable()) + } + + @Test + fun `new post notification is not shown if no new posts are visible`() { + val localSone = createLocalSone() + localSone.options.isShowNewPostNotifications = true + val newPostNotification = createNewPostNotification() + newPostNotification.add(createPost()) + val listNotificationFilter = ListNotificationFilter(showNoPosts, showAllReplies) + val filteredNotifications = listNotificationFilter.filterNotifications(listOf(newPostNotification), localSone) + assertThat(filteredNotifications, emptyIterable()) + } + + @Test + fun `new post notification is shown if new posts are visible`() { + val localSone = createLocalSone() + localSone.options.isShowNewPostNotifications = true + val newPostNotification = createNewPostNotification() + newPostNotification.add(createPost()) + val filteredNotifications = listNotificationFilter.filterNotifications(listOf(newPostNotification), localSone) + assertThat(filteredNotifications, contains(newPostNotification)) + } + + @Test + fun `new post notification is not shown if new posts are visible but local sone is null`() { + val localSone = createLocalSone() + localSone.options.isShowNewPostNotifications = true + val newPostNotification = createNewPostNotification() + newPostNotification.add(createPost()) + val filteredNotifications = listNotificationFilter.filterNotifications(listOf(newPostNotification), null) + assertThat(filteredNotifications, emptyIterable()) + } + + @Test + fun `new post notification contains only visible posts`() { + val localSone = createLocalSone() + localSone.options.isShowNewPostNotifications = true + val newPostNotification = createNewPostNotification() + newPostNotification.add(createPost()) + newPostNotification.add(createPost()) + val listNotificationFilter = ListNotificationFilter(matchThisPost(newPostNotification.elements[1]), showAllReplies) + val filteredNotifications = listNotificationFilter.filterNotifications(listOf(newPostNotification), localSone) + assertThat((filteredNotifications[0] as ListNotification).elements, contains(newPostNotification.elements[1])) + } + + @Test + fun `new reply notification contains only visible replies`() { + val localSone = createLocalSone() + localSone.options.isShowNewReplyNotifications = true + val newReplyNotification = createNewReplyNotification() + newReplyNotification.add(createPostReply()) + newReplyNotification.add(createPostReply()) + val listNotificationFilter = ListNotificationFilter(showAllPosts, matchThisReply(newReplyNotification.elements[1])) + val filteredNotifications = listNotificationFilter.filterNotifications(listOf(newReplyNotification), localSone) + assertThat(filteredNotifications, hasSize(1)) + assertThat((filteredNotifications[0] as ListNotification).elements[0], equalTo(newReplyNotification.elements[1])) + } + + @Test + fun `new reply notification is not modified if all replies are visible`() { + val localSone = createLocalSone() + localSone.options.isShowNewReplyNotifications = true + val newReplyNotification = createNewReplyNotification() + newReplyNotification.add(createPostReply()) + newReplyNotification.add(createPostReply()) + val filteredNotifications = listNotificationFilter.filterNotifications(listOf(newReplyNotification), localSone) + assertThat(filteredNotifications, contains(newReplyNotification)) + } + + @Test + fun `new reply notification is not shown if no replies are visible`() { + val localSone = createLocalSone() + localSone.options.isShowNewReplyNotifications = true + val newReplyNotification = createNewReplyNotification() + newReplyNotification.add(createPostReply()) + newReplyNotification.add(createPostReply()) + val listNotificationFilter = ListNotificationFilter(showAllPosts, showNoReplies) + val filteredNotifications = listNotificationFilter.filterNotifications(listOf(newReplyNotification), localSone) + assertThat(filteredNotifications, emptyIterable()) + } + + @Test + fun `new reply notification is not shown if deactivated in options`() { + val localSone = createLocalSone() + localSone.options.isShowNewReplyNotifications = false + val newReplyNotification = createNewReplyNotification() + newReplyNotification.add(createPostReply()) + newReplyNotification.add(createPostReply()) + val filteredNotifications = listNotificationFilter.filterNotifications(listOf(newReplyNotification), localSone) + assertThat(filteredNotifications, emptyIterable()) + } + + @Test + fun `new reply notification is not shown if current sone is null`() { + val newReplyNotification = createNewReplyNotification() + newReplyNotification.add(createPostReply()) + newReplyNotification.add(createPostReply()) + val filteredNotifications = listNotificationFilter.filterNotifications(listOf(newReplyNotification), null) + assertThat(filteredNotifications, emptyIterable()) + } + + @Test + fun `mention notification contains only visible posts`() { + val mentionNotification = createMentionNotification() + mentionNotification.add(createPost()) + mentionNotification.add(createPost()) + val listNotificationFilter = ListNotificationFilter(matchThisPost(mentionNotification.elements[1]), showAllReplies) + val filteredNotifications = listNotificationFilter.filterNotifications(listOf(mentionNotification), null) + assertThat(filteredNotifications, hasSize(1)) + assertThat((filteredNotifications[0] as ListNotification).elements[0], equalTo(mentionNotification.elements[1])) + } + + @Test + fun `mention notification is not shown if no posts are visible`() { + val mentionNotification = createMentionNotification() + mentionNotification.add(createPost()) + mentionNotification.add(createPost()) + val listNotificationFilter = ListNotificationFilter(showNoPosts, showAllReplies) + val filteredNotifications = listNotificationFilter.filterNotifications(listOf(mentionNotification), null) + assertThat(filteredNotifications, emptyIterable()) + } + + + @Test + fun `unfilterable notification is not filtered`() { + val notification: Notification = TemplateNotification("random-notification", Template()) + val filteredNotifications = listNotificationFilter.filterNotifications(listOf(notification), null) + assertThat(filteredNotifications, contains(notification)) + } + +} + +private fun createNewSoneNotification() = + ListNotification("new-sone-notification", "", Template()) + +private fun createNewPostNotification() = + ListNotification("new-post-notification", "", Template()) + +private fun createNewReplyNotification() = + ListNotification("new-reply-notification", "", Template()) + +private fun createMentionNotification() = + ListNotification("mention-notification", "", Template()) + +private fun matchThisPost(post: Post) = createPostVisibilityFilter { _, p -> p == post } +private val showAllPosts = createPostVisibilityFilter { _, _ -> true } +private val showNoPosts = createPostVisibilityFilter { _, _ -> false } + +private fun createPostVisibilityFilter(visible: (Sone?, Post) -> Boolean) = object : PostVisibilityFilter() { + override fun isPostVisible(sone: Sone?, post: Post) = visible(sone, post) + override fun isVisible(currentSone: Sone?) = Predicate { p -> p != null && isPostVisible(currentSone, p) } +} + +private fun matchThisReply(reply: PostReply) = createReplyVisibilityFilter(showAllPosts) { _, r -> r == reply } +private val showAllReplies = createReplyVisibilityFilter(showAllPosts) { _, _ -> true } +private val showNoReplies = createReplyVisibilityFilter(showAllPosts) { _, _ -> false } + +private fun createReplyVisibilityFilter(postVisibilityFilter: PostVisibilityFilter, visible: (Sone?, PostReply) -> Boolean) = object : ReplyVisibilityFilter(postVisibilityFilter) { + override fun isReplyVisible(sone: Sone?, reply: PostReply) = visible(sone, reply) + override fun isVisible(currentSone: Sone?) = Predicate { r -> r != null && isReplyVisible(currentSone, r) } +} -- 2.7.4