Add test for storing IDs on known post replies
[Sone.git] / src / test / java / net / pterodactylus / sone / database / memory / MemoryDatabaseTest.java
index 4d88429..60c8c53 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - MemoryDatabaseTest.java - Copyright © 2013–2016 David Roden
+ * Sone - MemoryDatabaseTest.java - Copyright © 2013–2019 David Roden
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -30,11 +30,12 @@ import static org.hamcrest.Matchers.contains;
 import static org.hamcrest.Matchers.containsInAnyOrder;
 import static org.hamcrest.Matchers.empty;
 import static org.hamcrest.Matchers.emptyIterable;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.lessThan;
 import static org.hamcrest.Matchers.nullValue;
 import static org.mockito.ArgumentMatchers.anyString;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
-import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
@@ -57,9 +58,11 @@ import net.pterodactylus.sone.test.TestPostBuilder;
 import net.pterodactylus.sone.test.TestPostReplyBuilder;
 import net.pterodactylus.sone.test.TestValue;
 import net.pterodactylus.util.config.Configuration;
+import net.pterodactylus.util.config.ConfigurationException;
 import net.pterodactylus.util.config.Value;
 
 import com.google.common.base.Optional;
+import org.hamcrest.CoreMatchers;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.invocation.InvocationOnMock;
@@ -67,8 +70,6 @@ import org.mockito.stubbing.Answer;
 
 /**
  * Tests for {@link MemoryDatabase}.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
 public class MemoryDatabaseTest {
 
@@ -119,7 +120,7 @@ public class MemoryDatabaseTest {
                                                .withTime(4000L)
                                                .withText("reply2")
                                                .build();
-               Set<PostReply> postReplies = new HashSet<PostReply>(
+               Set<PostReply> postReplies = new HashSet<>(
                                asList(firstPostFirstReply, firstPostSecondReply,
                                                secondPostReply));
                when(sone.getReplies()).thenReturn(postReplies);
@@ -138,6 +139,7 @@ public class MemoryDatabaseTest {
                                "album-description3").update();
                firstAlbum.addAlbum(thirdAlbum);
                Album rootAlbum = mock(Album.class);
+               when(rootAlbum.getId()).thenReturn("root");
                when(rootAlbum.getAlbums()).thenReturn(
                                asList(firstAlbum, secondAlbum));
                when(sone.getRootAlbum()).thenReturn(rootAlbum);
@@ -212,7 +214,7 @@ public class MemoryDatabaseTest {
 
        @Test
        public void storedAndRemovedSoneIsNotAvailable() {
-           storedSoneIsMadeAvailable();
+               storedSoneIsMadeAvailable();
                memoryDatabase.removeSone(sone);
                assertThat(memoryDatabase.getSones(), empty());
        }
@@ -324,14 +326,28 @@ public class MemoryDatabaseTest {
                assertThat(memoryDatabase.isFriend(sone, "Friend1"), is(false));
        }
 
-       private Map<String, Value<String>> prepareConfigurationValues() {
-               final Map<String, Value<String>> configurationValues = new HashMap<String, Value<String>>();
+       private Map<String, Value<?>> prepareConfigurationValues() {
+               final Map<String, Value<?>> configurationValues = new HashMap<>();
                when(configuration.getStringValue(anyString())).thenAnswer(new Answer<Value<String>>() {
                        @Override
                        public Value<String> answer(InvocationOnMock invocation) throws Throwable {
-                               Value<String> stringValue = TestValue.from(null);
-                               configurationValues.put((String) invocation.getArguments()[0], stringValue);
-                               return stringValue;
+                               Value<?> value = configurationValues.get(invocation.<String>getArgument(0));
+                               if (value == null) {
+                                       value = TestValue.from(null);
+                                       configurationValues.put(invocation.<String>getArgument(0), value);
+                               }
+                               return (Value<String>) value;
+                       }
+               });
+               when(configuration.getLongValue(anyString())).thenAnswer(new Answer<Value<Long>>() {
+                       @Override
+                       public Value<Long> answer(InvocationOnMock invocation) throws Throwable {
+                               Value<?> value = configurationValues.get(invocation.<String>getArgument(0));
+                               if (value == null) {
+                                       value = TestValue.from(null);
+                                       configurationValues.put(invocation.<String>getArgument(0), value);
+                               }
+                               return (Value<Long>) value;
                        }
                });
                return configurationValues;
@@ -339,13 +355,13 @@ public class MemoryDatabaseTest {
 
        @Test
        public void friendIsAddedCorrectlyToLocalSone() {
-               Map<String, Value<String>> configurationValues = prepareConfigurationValues();
+               Map<String, Value<?>> configurationValues = prepareConfigurationValues();
                when(sone.isLocal()).thenReturn(true);
                memoryDatabase.addFriend(sone, "Friend1");
                assertThat(configurationValues.get("Sone/" + SONE_ID + "/Friends/0/ID"),
-                               is(TestValue.from("Friend1")));
+                               CoreMatchers.<Value<?>>is(TestValue.from("Friend1")));
                assertThat(configurationValues.get("Sone/" + SONE_ID + "/Friends/1/ID"),
-                               is(TestValue.<String>from(null)));
+                               CoreMatchers.<Value<?>>is(TestValue.<String>from(null)));
        }
 
        @Test
@@ -355,24 +371,15 @@ public class MemoryDatabaseTest {
        }
 
        @Test
-       public void configurationIsWrittenOnceIfFriendIsAddedTwice() {
-               prepareConfigurationValues();
-               when(sone.isLocal()).thenReturn(true);
-               memoryDatabase.addFriend(sone, "Friend1");
-               memoryDatabase.addFriend(sone, "Friend1");
-               verify(configuration, times(3)).getStringValue(anyString());
-       }
-
-       @Test
        public void friendIsRemovedCorrectlyFromLocalSone() {
-               Map<String, Value<String>> configurationValues = prepareConfigurationValues();
+               Map<String, Value<?>> configurationValues = prepareConfigurationValues();
                when(sone.isLocal()).thenReturn(true);
                memoryDatabase.addFriend(sone, "Friend1");
                memoryDatabase.removeFriend(sone, "Friend1");
                assertThat(configurationValues.get("Sone/" + SONE_ID + "/Friends/0/ID"),
-                               is(TestValue.<String>from(null)));
+                               CoreMatchers.<Value<?>>is(TestValue.<String>from(null)));
                assertThat(configurationValues.get("Sone/" + SONE_ID + "/Friends/1/ID"),
-                               is(TestValue.<String>from(null)));
+                               CoreMatchers.<Value<?>>is(TestValue.<String>from(null)));
        }
 
        @Test
@@ -383,4 +390,90 @@ public class MemoryDatabaseTest {
                verify(configuration).getStringValue(anyString());
        }
 
+       @Test
+       public void soneFollowingTimeIsReturnedCorrectly() throws ConfigurationException {
+               prepareConfigurationValues();
+               configuration.getStringValue("SoneFollowingTimes/0/Sone").setValue("sone");
+               configuration.getLongValue("SoneFollowingTimes/0/Time").setValue(1000L);
+               assertThat(memoryDatabase.getFollowingTime("sone"), equalTo(1000L));
+       }
+
+       @Test
+       public void nullisReturnedWhenSoneIsNotFollowed() throws ConfigurationException {
+               prepareConfigurationValues();
+               configuration.getStringValue("SoneFollowingTimes/0/Sone").setValue("otherSone");
+               configuration.getLongValue("SoneFollowingTimes/0/Time").setValue(1000L);
+               assertThat(memoryDatabase.getFollowingTime("sone"), nullValue());
+       }
+
+       @Test
+       public void timeIsStoredInConfigurationWhenASoneIsFollowed() throws ConfigurationException {
+               prepareConfigurationValues();
+               when(sone.isLocal()).thenReturn(true);
+               memoryDatabase.addFriend(sone, "Friend");
+               assertThat(configuration.getStringValue("SoneFollowingTimes/0/Sone").getValue(), equalTo("Friend"));
+               assertThat(System.currentTimeMillis() - configuration.getLongValue("SoneFollowingTimes/0/Time").getValue(), lessThan(1000L));
+               assertThat(configuration.getStringValue("SoneFollowingTimes/1/Sone").getValue(), nullValue());
+       }
+
+       @Test
+       public void existingTimeIsNotOverwrittenWhenASoneIsFollowed() throws ConfigurationException {
+               prepareConfigurationValues();
+               configuration.getStringValue("SoneFollowingTimes/0/Sone").setValue("Friend");
+               configuration.getLongValue("SoneFollowingTimes/0/Time").setValue(1000L);
+               when(sone.isLocal()).thenReturn(true);
+               memoryDatabase.addFriend(sone, "Friend");
+               assertThat(configuration.getStringValue("SoneFollowingTimes/0/Sone").getValue(), equalTo("Friend"));
+               assertThat(configuration.getLongValue("SoneFollowingTimes/0/Time").getValue(), equalTo(1000L));
+               assertThat(configuration.getStringValue("SoneFollowingTimes/1/Sone").getValue(), nullValue());
+       }
+
+       @Test
+       public void unfollowingASoneRemovesTheFollowingTime() throws ConfigurationException {
+               prepareConfigurationValues();
+               configuration.getStringValue("Sone/sone/Friends/0/ID").setValue("Friend");
+               configuration.getStringValue("SoneFollowingTimes/0/Sone").setValue("Friend");
+               configuration.getLongValue("SoneFollowingTimes/0/Time").setValue(1000L);
+               when(sone.isLocal()).thenReturn(true);
+               memoryDatabase.removeFriend(sone, "Friend");
+               assertThat(configuration.getStringValue("SoneFollowingTimes/0/Sone").getValue(), nullValue());
+       }
+
+       @Test
+       public void unfollowingASoneDoesNotRemoveTheFollowingTimeIfAnotherLocalSoneFollowsIt() throws ConfigurationException {
+               prepareConfigurationValues();
+               configuration.getStringValue("Sone/sone/Friends/0/ID").setValue("Friend");
+               configuration.getStringValue("Sone/other-sone/Friends/0/ID").setValue("Friend");
+               configuration.getStringValue("SoneFollowingTimes/0/Sone").setValue("Friend");
+               configuration.getLongValue("SoneFollowingTimes/0/Time").setValue(1000L);
+               Sone otherSone = mock(Sone.class);
+               when(otherSone.isLocal()).thenReturn(true);
+               when(otherSone.getId()).thenReturn("other-sone");
+               memoryDatabase.getFriends(otherSone);
+               when(sone.isLocal()).thenReturn(true);
+               memoryDatabase.removeFriend(sone, "Friend");
+               assertThat(configuration.getStringValue("SoneFollowingTimes/0/Sone").getValue(), equalTo("Friend"));
+               assertThat(configuration.getLongValue("SoneFollowingTimes/0/Time").getValue(), equalTo(1000L));
+       }
+
+       @Test
+       public void markingAPostAsKnownSavesConfiguration() throws ConfigurationException {
+               prepareConfigurationValues();
+               Post post = mock(Post.class);
+               when(post.getId()).thenReturn("post-id");
+               memoryDatabase.setPostKnown(post, true);
+               assertThat(configuration.getStringValue("KnownPosts/0/ID").getValue(), equalTo("post-id"));
+               assertThat(configuration.getStringValue("KnownPosts/1/ID").getValue(), equalTo(null));
+       }
+
+       @Test
+       public void markingAPostReplyAsKnownSavesConfiguration() throws ConfigurationException {
+               prepareConfigurationValues();
+               PostReply postReply = mock(PostReply.class);
+               when(postReply.getId()).thenReturn("post-reply-id");
+               memoryDatabase.setPostReplyKnown(postReply, true);
+               assertThat(configuration.getStringValue("KnownReplies/0/ID").getValue(), equalTo("post-reply-id"));
+               assertThat(configuration.getStringValue("KnownReplies/1/ID").getValue(), equalTo(null));
+       }
+
 }