package net.pterodactylus.sone.database.memory;
import static com.google.common.base.Optional.of;
+import static java.util.Arrays.asList;
import static java.util.UUID.randomUUID;
+import static net.pterodactylus.sone.Matchers.isAlbum;
+import static net.pterodactylus.sone.Matchers.isImage;
+import static net.pterodactylus.sone.Matchers.isPost;
+import static net.pterodactylus.sone.Matchers.isPostReply;
import static org.hamcrest.CoreMatchers.is;
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.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.verify;
import static org.mockito.Mockito.when;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+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.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;
/**
* Tests for {@link MemoryDatabase}.
private static final String SONE_ID = "sone";
private static final String RECIPIENT_ID = "recipient";
- private final MemoryDatabase memoryDatabase = new MemoryDatabase(null, null);
+ 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<String, Value<String>> stringValues = new HashMap<String, Value<String>>();
+ private final Map<String, Value<Long>> longValues = new HashMap<String, Value<Long>>();
+
+ @Before
+ public void prepareConfigurationValues() {
+ when(configuration.getStringValue(anyString())).thenAnswer(new Answer<Value<String>>() {
+ @Override
+ public Value<String> answer(InvocationOnMock invocation) throws Throwable {
+ final String key = (String) invocation.getArguments()[0];
+ if (!stringValues.containsKey(key)) {
+ TestValue<String> value = Mockito.spy(new TestValue<String>(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<Value<Long>>() {
+ @Override
+ public Value<Long> answer(InvocationOnMock invocation) throws Throwable {
+ final String key = (String) invocation.getArguments()[0];
+ if (!longValues.containsKey(key)) {
+ TestValue<Long> value = Mockito.spy(new TestValue<Long>(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
+ public void storedSoneIsMadeAvailable() {
+ Post firstPost = new TestPostBuilder().withId("post1")
+ .from(SONE_ID)
+ .withTime(1000L)
+ .withText("post1")
+ .build();
+ Post secondPost = new TestPostBuilder().withId("post2")
+ .from(SONE_ID)
+ .withTime(2000L)
+ .withText("post2")
+ .to(RECIPIENT_ID)
+ .build();
+ List<Post> posts = asList(firstPost, secondPost);
+ when(sone.getPosts()).thenReturn(posts);
+ PostReply firstPostFirstReply =
+ new TestPostReplyBuilder().withId("reply1")
+ .from(SONE_ID)
+ .to(firstPost.getId())
+ .withTime(3000L)
+ .withText("reply1")
+ .build();
+ PostReply firstPostSecondReply =
+ new TestPostReplyBuilder().withId("reply3")
+ .from(RECIPIENT_ID)
+ .to(firstPost.getId())
+ .withTime(5000L)
+ .withText("reply3")
+ .build();
+ PostReply secondPostReply =
+ new TestPostReplyBuilder().withId("reply2")
+ .from(SONE_ID)
+ .to(secondPost.getId())
+ .withTime(4000L)
+ .withText("reply2")
+ .build();
+ Set<PostReply> postReplies = new HashSet<PostReply>(
+ asList(firstPostFirstReply, firstPostSecondReply,
+ secondPostReply));
+ when(sone.getReplies()).thenReturn(postReplies);
+ Album firstAlbum = new TestAlbumBuilder().withId("album1")
+ .by(sone)
+ .build()
+ .modify()
+ .setTitle("album1")
+ .setDescription("album-description1")
+ .update();
+ Album secondAlbum = new TestAlbumBuilder().withId("album2").by(
+ sone).build().modify().setTitle("album2").setDescription(
+ "album-description2").setAlbumImage("image1").update();
+ Album thirdAlbum = new TestAlbumBuilder().withId("album3").by(
+ sone).build().modify().setTitle("album3").setDescription(
+ "album-description3").update();
+ firstAlbum.addAlbum(thirdAlbum);
+ Album rootAlbum = mock(Album.class);
+ when(rootAlbum.getAlbums()).thenReturn(
+ asList(firstAlbum, secondAlbum));
+ when(sone.getRootAlbum()).thenReturn(rootAlbum);
+ Image firstImage = new TestImageBuilder().withId("image1")
+ .build()
+ .modify()
+ .setSone(sone)
+ .setCreationTime(1000L)
+ .setKey("KSK@image1")
+ .setTitle("image1")
+ .setDescription("image-description1")
+ .setWidth(16)
+ .setHeight(9)
+ .update();
+ Image secondImage = new TestImageBuilder().withId("image2")
+ .build()
+ .modify()
+ .setSone(sone)
+ .setCreationTime(2000L)
+ .setKey("KSK@image2")
+ .setTitle("image2")
+ .setDescription("image-description2")
+ .setWidth(32)
+ .setHeight(18)
+ .update();
+ Image thirdImage = new TestImageBuilder().withId("image3")
+ .build()
+ .modify()
+ .setSone(sone)
+ .setCreationTime(3000L)
+ .setKey("KSK@image3")
+ .setTitle("image3")
+ .setDescription("image-description3")
+ .setWidth(48)
+ .setHeight(27)
+ .update();
+ firstAlbum.addImage(firstImage);
+ firstAlbum.addImage(thirdImage);
+ secondAlbum.addImage(secondImage);
+ memoryDatabase.storeSone(sone);
+ assertThat(memoryDatabase.getPost("post1").get(),
+ isPost(firstPost.getId(), 1000L, "post1",
+ Optional.<String>absent()));
+ assertThat(memoryDatabase.getPost("post2").get(),
+ isPost(secondPost.getId(), 2000L, "post2", of(RECIPIENT_ID)));
+ assertThat(memoryDatabase.getPost("post3").isPresent(), is(false));
+ assertThat(memoryDatabase.getPostReply("reply1").get(),
+ isPostReply("reply1", "post1", 3000L, "reply1"));
+ assertThat(memoryDatabase.getPostReply("reply2").get(),
+ isPostReply("reply2", "post2", 4000L, "reply2"));
+ assertThat(memoryDatabase.getPostReply("reply3").get(),
+ isPostReply("reply3", "post1", 5000L, "reply3"));
+ assertThat(memoryDatabase.getPostReply("reply4").isPresent(),
+ is(false));
+ assertThat(memoryDatabase.getAlbum("album1").get(),
+ isAlbum("album1", null, "album1", "album-description1",
+ null));
+ assertThat(memoryDatabase.getAlbum("album2").get(),
+ isAlbum("album2", null, "album2", "album-description2",
+ "image1"));
+ assertThat(memoryDatabase.getAlbum("album3").get(),
+ isAlbum("album3", "album1", "album3", "album-description3",
+ null));
+ assertThat(memoryDatabase.getAlbum("album4").isPresent(), is(false));
+ assertThat(memoryDatabase.getImage("image1").get(),
+ isImage("image1", 1000L, "KSK@image1", "image1",
+ "image-description1", 16, 9));
+ assertThat(memoryDatabase.getImage("image2").get(),
+ isImage("image2", 2000L, "KSK@image2", "image2",
+ "image-description2", 32, 18));
+ assertThat(memoryDatabase.getImage("image3").get(),
+ isImage("image3", 3000L, "KSK@image3", "image3",
+ "image-description3", 48, 27));
+ assertThat(memoryDatabase.getImage("image4").isPresent(), is(false));
+ }
+
+ @Test
+ public void storedAndRemovedSoneIsNotAvailable() {
+ storedSoneIsMadeAvailable();
+ memoryDatabase.removeSone(sone);
+ assertThat(memoryDatabase.getSones(), empty());
}
@Test
Post postWithoutRecipient = createPost(Optional.<String>absent());
memoryDatabase.storePost(postWithoutRecipient);
assertThat(memoryDatabase.getDirectedPosts(RECIPIENT_ID),
- contains(postWithRecipient));
+ contains(IncompletePostMatcher.matches().recipient(RECIPIENT_ID)));
}
private Post createPost(Optional<String> recipient) {
assertThat(memoryDatabase.getAlbum(newAlbum.getId()), is(Optional.<Album>absent()));
}
+ 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));
+ }
+
}