+ private void initializeFriends() {
+ 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.<String>from(null)));
+ }
+
+ @Test
+ public void friendsAreReturnedCorrectly() {
+ initializeFriends();
+ Collection<String> friends = memoryDatabase.getFriends(localSone);
+ assertThat(friends, containsInAnyOrder("Friend1", "Friend2"));
+ }
+
+ @Test
+ public void friendsAreOnlyLoadedOnceFromConfiguration() {
+ friendsAreReturnedCorrectly();
+ memoryDatabase.getFriends(localSone);
+ verify(configuration).getStringValue("Sone/" + SONE_ID + "/Friends/0/ID");
+ }
+
+ @Test
+ public void checkingForAFriendReturnsTrue() {
+ initializeFriends();
+ when(sone.isLocal()).thenReturn(true);
+ assertThat(memoryDatabase.isFriend(localSone, "Friend1"), is(true));
+ }
+
+ @Test
+ public void checkingForAFriendThatIsNotAFriendReturnsFalse() {
+ initializeFriends();
+ when(sone.isLocal()).thenReturn(true);
+ assertThat(memoryDatabase.isFriend(localSone, "FriendX"), is(false));
+ }
+
+ @Test
+ 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());
+ }
+
+ @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 friendIsRemovedCorrectlyFromLocalSone() throws ConfigurationException {
+ when(sone.isLocal()).thenReturn(true);
+ 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 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 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 databaseLoadsKnownSonesCorrectly() {
+ stringValues.put("KnownSones/0/ID", TestValue.from(SONE_ID));
+ memoryDatabase.startAndWait();
+ assertThat(memoryDatabase.isSoneKnown(sone), is(true));
+ }
+
+ @Test
+ 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));
+ }
+