X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdatabase%2Fmemory%2FMemoryDatabaseTest.java;h=ee576b2d24723ed2756c85bf13ae59a15eae2914;hb=f333f58180a7f112394cd768d86c95a3c9edf794;hp=f7b07a215fc15e6fab8a6554cc09f25dedaefab1;hpb=2524f4d47c56874a263f9e53ec5c4035f2baa7e0;p=Sone.git diff --git a/src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java b/src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java index f7b07a2..ee576b2 100644 --- a/src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java +++ b/src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java @@ -29,11 +29,12 @@ import static org.hamcrest.MatcherAssert.assertThat; 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.hasKey; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.nullValue; import static org.mockito.Matchers.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; @@ -44,23 +45,28 @@ import java.util.List; import java.util.Map; import java.util.Set; +import net.pterodactylus.sone.Matchers.IncompletePostMatcher; import net.pterodactylus.sone.TestAlbumBuilder; import net.pterodactylus.sone.TestImageBuilder; import net.pterodactylus.sone.TestPostBuilder; import net.pterodactylus.sone.TestPostReplyBuilder; import net.pterodactylus.sone.TestValue; import net.pterodactylus.sone.data.Album; -import net.pterodactylus.sone.data.impl.AlbumImpl; import net.pterodactylus.sone.data.Image; +import net.pterodactylus.sone.data.LocalSone; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.PostReply; import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.data.impl.AlbumImpl; 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.junit.Before; +import org.junit.Ignore; import org.junit.Test; +import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; @@ -76,10 +82,53 @@ public class MemoryDatabaseTest { private final Configuration configuration = mock(Configuration.class); private final MemoryDatabase memoryDatabase = new MemoryDatabase(null, configuration); private final Sone sone = mock(Sone.class); + private final LocalSone localSone = mock(LocalSone.class); + private final Map> stringValues = new HashMap>(); + private final Map> longValues = new HashMap>(); + + @Before + public void prepareConfigurationValues() { + when(configuration.getStringValue(anyString())).thenAnswer(new Answer>() { + @Override + public Value answer(InvocationOnMock invocation) throws Throwable { + final String key = (String) invocation.getArguments()[0]; + if (!stringValues.containsKey(key)) { + TestValue value = Mockito.spy(new TestValue(null) { + @Override + public void setValue(String newValue) throws ConfigurationException { + super.setValue(newValue); + stringValues.put(key, this); + } + }); + stringValues.put(key, value); + } + return stringValues.get(key); + } + }); + when(configuration.getLongValue(anyString())).thenAnswer(new Answer>() { + @Override + public Value answer(InvocationOnMock invocation) throws Throwable { + final String key = (String) invocation.getArguments()[0]; + if (!longValues.containsKey(key)) { + TestValue value = Mockito.spy(new TestValue(null) { + @Override + public void setValue(Long newValue) throws ConfigurationException { + super.setValue(newValue); + longValues.put(key, this); + } + }); + longValues.put(key, value); + } + return longValues.get(key); + } + }); + } @Before - public void setupSone() { + public void setupSones() { when(sone.getId()).thenReturn(SONE_ID); + when(localSone.getId()).thenReturn(SONE_ID); + when(localSone.isLocal()).thenReturn(true); } @Test @@ -215,7 +264,7 @@ public class MemoryDatabaseTest { @Test public void storedAndRemovedSoneIsNotAvailable() { - storedSoneIsMadeAvailable(); + storedSoneIsMadeAvailable(); memoryDatabase.removeSone(sone); assertThat(memoryDatabase.getSones(), empty()); } @@ -227,7 +276,7 @@ public class MemoryDatabaseTest { Post postWithoutRecipient = createPost(Optional.absent()); memoryDatabase.storePost(postWithoutRecipient); assertThat(memoryDatabase.getDirectedPosts(RECIPIENT_ID), - contains(postWithRecipient)); + contains(IncompletePostMatcher.matches().recipient(RECIPIENT_ID))); } private Post createPost(Optional recipient) { @@ -277,112 +326,163 @@ public class MemoryDatabaseTest { } private void initializeFriends() { - when(configuration.getStringValue("Sone/" + SONE_ID + "/Friends/0/ID")).thenReturn( - new TestValue("Friend1")); - when(configuration.getStringValue("Sone/" + SONE_ID + "/Friends/1/ID")).thenReturn( - new TestValue("Friend2")); - when(configuration.getStringValue("Sone/" + SONE_ID + "/Friends/2/ID")).thenReturn( - new TestValue(null)); + stringValues.put("Sone/" + SONE_ID + "/Friends/0/ID", + Mockito.spy(TestValue.from("Friend1"))); + stringValues.put("Sone/" + SONE_ID + "/Friends/1/ID", + Mockito.spy(TestValue.from("Friend2"))); + stringValues.put("Sone/" + SONE_ID + "/Friends/2/ID", + Mockito.spy(TestValue.from(null))); } @Test public void friendsAreReturnedCorrectly() { initializeFriends(); - when(sone.isLocal()).thenReturn(true); - Collection friends = memoryDatabase.getFriends(sone); + Collection friends = memoryDatabase.getFriends(localSone); assertThat(friends, containsInAnyOrder("Friend1", "Friend2")); } @Test public void friendsAreOnlyLoadedOnceFromConfiguration() { friendsAreReturnedCorrectly(); - memoryDatabase.getFriends(sone); + memoryDatabase.getFriends(localSone); verify(configuration).getStringValue("Sone/" + SONE_ID + "/Friends/0/ID"); } @Test - public void friendsAreOnlyReturnedForLocalSones() { - Collection 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)); + assertThat(memoryDatabase.isFriend(localSone, "Friend1"), is(true)); } @Test public void checkingForAFriendThatIsNotAFriendReturnsFalse() { initializeFriends(); when(sone.isLocal()).thenReturn(true); - assertThat(memoryDatabase.isFriend(sone, "FriendX"), is(false)); + assertThat(memoryDatabase.isFriend(localSone, "FriendX"), is(false)); } @Test - public void checkingForAFriendOfRemoteSoneReturnsFalse() { - initializeFriends(); - assertThat(memoryDatabase.isFriend(sone, "Friend1"), is(false)); + public void friendIsAddedCorrectlyToLocalSone() throws ConfigurationException { + when(sone.isLocal()).thenReturn(true); + memoryDatabase.addFriend(localSone, "Friend1"); + assertThat(stringValues.get("Sone/" + SONE_ID + "/Friends/0/ID").getValue(), + is("Friend1")); + assertThat(stringValues.get("Sone/" + SONE_ID + "/Friends/1/ID").getValue(), + nullValue()); } - private Map> prepareConfigurationValues() { - final Map> configurationValues = new HashMap>(); - when(configuration.getStringValue(anyString())).thenAnswer(new Answer>() { - @Override - public Value answer(InvocationOnMock invocation) throws Throwable { - Value stringValue = new TestValue(null); - configurationValues.put((String) invocation.getArguments()[0], stringValue); - return stringValue; - } - }); - return configurationValues; + @Test + public void configurationIsWrittenOnceIfFriendIsAddedTwice() throws ConfigurationException { + when(sone.isLocal()).thenReturn(true); + memoryDatabase.addFriend(localSone, "Friend1"); + memoryDatabase.addFriend(localSone, "Friend1"); + verify(configuration.getStringValue("Sone/" + SONE_ID + "/Friends/0/ID")).setValue( + anyString()); } @Test - public void friendIsAddedCorrectlyToLocalSone() { - Map> configurationValues = prepareConfigurationValues(); + public void friendIsRemovedCorrectlyFromLocalSone() throws ConfigurationException { when(sone.isLocal()).thenReturn(true); - memoryDatabase.addFriend(sone, "Friend1"); - assertThat(configurationValues.get("Sone/" + SONE_ID + "/Friends/0/ID"), - is(TestValue.from("Friend1"))); - assertThat(configurationValues.get("Sone/" + SONE_ID + "/Friends/1/ID"), - is(TestValue.from(null))); + memoryDatabase.addFriend(localSone, "Friend1"); + memoryDatabase.removeFriend(localSone, "Friend1"); + assertThat(stringValues.get("Sone/" + SONE_ID + "/Friends/0/ID").getValue(), + nullValue()); + assertThat(stringValues.get("Sone/" + SONE_ID + "/Friends/1/ID").getValue(), + nullValue()); } @Test - public void friendIsNotAddedToRemoteSone() { - memoryDatabase.addFriend(sone, "Friend1"); - verify(configuration, never()).getStringValue(anyString()); + public void configurationIsNotWrittenWhenANonFriendIsRemoved() throws ConfigurationException { + when(sone.isLocal()).thenReturn(true); + memoryDatabase.removeFriend(localSone, "Friend1"); + verify(stringValues.get("Sone/" + SONE_ID + "/Friends/0/ID"), never()).setValue( + anyString()); } @Test - public void configurationIsWrittenOnceIfFriendIsAddedTwice() { - prepareConfigurationValues(); - when(sone.isLocal()).thenReturn(true); - memoryDatabase.addFriend(sone, "Friend1"); - memoryDatabase.addFriend(sone, "Friend1"); - verify(configuration, times(2)).getStringValue(anyString()); + public void newDatabaseKnowsNoSones() { + memoryDatabase.startAndWait(); + assertThat(memoryDatabase.isSoneKnown(sone), is(false)); + assertThat(stringValues, hasKey("KnownSones/0/ID")); + assertThat(stringValues, not(hasKey("KnownSones/1/ID"))); } @Test - public void friendIsRemovedCorrectlyFromLocalSone() { - Map> 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.from(null))); - assertThat(configurationValues.get("Sone/" + SONE_ID + "/Friends/1/ID"), - is(TestValue.from(null))); + public void databaseLoadsKnownSonesCorrectly() { + stringValues.put("KnownSones/0/ID", TestValue.from(SONE_ID)); + memoryDatabase.startAndWait(); + assertThat(memoryDatabase.isSoneKnown(sone), is(true)); } @Test - public void configurationIsNotWrittenWhenANonFriendIsRemoved() { - when(sone.isLocal()).thenReturn(true); - memoryDatabase.removeFriend(sone, "Friend1"); - verify(configuration, never()).getStringValue(anyString()); + public void databaseStoresKnownSonesCorrectly() throws ConfigurationException { + memoryDatabase.setSoneKnown(sone); + assertThat(stringValues, hasKey("KnownSones/0/ID")); + assertThat(stringValues.get("KnownSones/0/ID").getValue(), is(SONE_ID)); + assertThat(stringValues, hasKey("KnownSones/1/ID")); + assertThat(stringValues.get("KnownSones/1/ID").getValue(), nullValue()); + assertThat(stringValues, not(hasKey("KnownSones/2/ID"))); + } + + @Test + public void stoppingTheDatabaseSavesTheKnownSones() throws ConfigurationException { + stringValues.put("KnownSones/0/ID", Mockito.spy(TestValue.from(SONE_ID))); + memoryDatabase.startAndWait(); + memoryDatabase.stopAndWait(); + verify(stringValues.get("KnownSones/0/ID")).setValue(SONE_ID); + verify(stringValues.get("KnownSones/1/ID")).setValue(null); + } + + @Test + public void soneFollowingTimesAreLoaded() { + stringValues.put("SoneFollowingTimes/0/Sone", TestValue.from(SONE_ID)); + longValues.put("SoneFollowingTimes/0/Time", TestValue.from(1000L)); + memoryDatabase.startAndWait(); + assertThat(memoryDatabase.getSoneFollowingTime(SONE_ID).get(), is(1000L)); + } + + @Test + public void soneFollowingTimeIsSetOnFirstFollowing() { + memoryDatabase.startAndWait(); + memoryDatabase.addFriend(localSone, "Friend1"); + assertThat(stringValues, hasKey("SoneFollowingTimes/0/Sone")); + assertThat(stringValues, hasKey("SoneFollowingTimes/1/Sone")); + assertThat(stringValues, not(hasKey("SoneFollowingTimes/2/Sone"))); + assertThat(longValues, hasKey("SoneFollowingTimes/0/Time")); + assertThat(longValues, not(hasKey("SoneFollowingTimes/1/Time"))); + } + + @Test + public void soneFollowingTimeIsNotSetOnSecondFollowing() throws ConfigurationException { + memoryDatabase.startAndWait(); + memoryDatabase.addFriend(localSone, "Friend1"); + LocalSone secondLocalSone = mock(LocalSone.class); + when(secondLocalSone.getId()).thenReturn("LocalSone2"); + long followingTime = longValues.get("SoneFollowingTimes/0/Time").getValue(); + memoryDatabase.addFriend(secondLocalSone, "Friend1"); + while (followingTime == System.currentTimeMillis()); + assertThat(longValues.get("SoneFollowingTimes/0/Time").getValue(), is(followingTime)); + } + + @Test + public void soneFollowingTimesAreRemovedWhenSoneIsUnfollowedByAll() + throws ConfigurationException { + stringValues.put("Sone/" + SONE_ID + "/Friends/0/ID", TestValue.from("Friend1")); + stringValues.put("SoneFollowingTimes/0/Sone", TestValue.from("Friend1")); + longValues.put("SoneFollowingTimes/0/Time", TestValue.from(1000L)); + memoryDatabase.startAndWait(); + memoryDatabase.removeFriend(localSone, "Friend1"); + assertThat(stringValues.get("SoneFollowingTimes/0/Sone").getValue(), nullValue()); + } + + @Test + @Ignore("enable once Sones are built by the database") + public void soneUpdateTimeIsRetained() { + memoryDatabase.storeSone(sone); + memoryDatabase.updateSoneTime(sone, 1000L); + Sone updatedSone = memoryDatabase.getSone(SONE_ID).get(); + assertThat(updatedSone.getTime(), is(1000L)); } }