+ assertThat(memoryDatabase.getAlbum(newAlbum.getId()), nullValue());
+ }
+
+ private void initializeFriends() {
+ when(configuration.getStringValue("Sone/" + SONE_ID + "/Friends/0/ID")).thenReturn(
+ TestValue.from("Friend1"));
+ when(configuration.getStringValue("Sone/" + SONE_ID + "/Friends/1/ID")).thenReturn(
+ TestValue.from("Friend2"));
+ when(configuration.getStringValue("Sone/" + SONE_ID + "/Friends/2/ID")).thenReturn(
+ TestValue.<String>from(null));
+ }
+
+ @Test
+ public void friendsAreReturnedCorrectly() {
+ initializeFriends();
+ when(sone.isLocal()).thenReturn(true);
+ Collection<String> friends = memoryDatabase.getFriends(sone);
+ assertThat(friends, containsInAnyOrder("Friend1", "Friend2"));
+ }
+
+ @Test
+ public void friendsAreOnlyLoadedOnceFromConfiguration() {
+ friendsAreReturnedCorrectly();
+ memoryDatabase.getFriends(sone);
+ verify(configuration).getStringValue("Sone/" + SONE_ID + "/Friends/0/ID");
+ }
+
+ @Test
+ public void friendsAreOnlyReturnedForLocalSones() {
+ Collection<String> friends = memoryDatabase.getFriends(sone);
+ assertThat(friends, emptyIterable());
+ verify(configuration, never()).getStringValue("Sone/" + SONE_ID + "/Friends/0/ID");
+ }
+
+ @Test
+ public void checkingForAFriendReturnsTrue() {
+ initializeFriends();
+ when(sone.isLocal()).thenReturn(true);
+ assertThat(memoryDatabase.isFriend(sone, "Friend1"), is(true));
+ }
+
+ @Test
+ public void checkingForAFriendThatIsNotAFriendReturnsFalse() {
+ initializeFriends();
+ when(sone.isLocal()).thenReturn(true);
+ assertThat(memoryDatabase.isFriend(sone, "FriendX"), is(false));
+ }
+
+ @Test
+ public void checkingForAFriendOfRemoteSoneReturnsFalse() {
+ initializeFriends();
+ assertThat(memoryDatabase.isFriend(sone, "Friend1"), is(false));
+ }
+
+ 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<?> 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;
+ }
+
+ @Test
+ public void friendIsAddedCorrectlyToLocalSone() {
+ Map<String, Value<?>> configurationValues = prepareConfigurationValues();
+ when(sone.isLocal()).thenReturn(true);
+ memoryDatabase.addFriend(sone, "Friend1");
+ assertThat(configurationValues.get("Sone/" + SONE_ID + "/Friends/0/ID"),
+ CoreMatchers.<Value<?>>is(TestValue.from("Friend1")));
+ assertThat(configurationValues.get("Sone/" + SONE_ID + "/Friends/1/ID"),
+ CoreMatchers.<Value<?>>is(TestValue.<String>from(null)));
+ }
+
+ @Test
+ public void friendIsNotAddedToRemoteSone() {
+ memoryDatabase.addFriend(sone, "Friend1");
+ verify(configuration, never()).getStringValue(anyString());
+ }
+
+ @Test
+ public void friendIsRemovedCorrectlyFromLocalSone() {
+ 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"),
+ CoreMatchers.<Value<?>>is(TestValue.<String>from(null)));
+ assertThat(configurationValues.get("Sone/" + SONE_ID + "/Friends/1/ID"),
+ CoreMatchers.<Value<?>>is(TestValue.<String>from(null)));
+ }
+
+ @Test
+ public void configurationIsNotWrittenWhenANonFriendIsRemoved() {
+ prepareConfigurationValues();
+ when(sone.isLocal()).thenReturn(true);
+ memoryDatabase.removeFriend(sone, "Friend1");
+ 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));