2 * Sone - MemoryDatabaseTest.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.database.memory;
20 import static com.google.common.base.Optional.of;
21 import static java.util.Arrays.asList;
22 import static java.util.UUID.randomUUID;
23 import static net.pterodactylus.sone.Matchers.isAlbum;
24 import static net.pterodactylus.sone.Matchers.isImage;
25 import static net.pterodactylus.sone.Matchers.isPost;
26 import static net.pterodactylus.sone.Matchers.isPostReply;
27 import static org.hamcrest.CoreMatchers.is;
28 import static org.hamcrest.MatcherAssert.assertThat;
29 import static org.hamcrest.Matchers.contains;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.when;
33 import java.util.HashSet;
34 import java.util.List;
37 import net.pterodactylus.sone.TestAlbumBuilder;
38 import net.pterodactylus.sone.TestImageBuilder;
39 import net.pterodactylus.sone.TestPostBuilder;
40 import net.pterodactylus.sone.TestPostReplyBuilder;
41 import net.pterodactylus.sone.data.Album;
42 import net.pterodactylus.sone.data.AlbumImpl;
43 import net.pterodactylus.sone.data.Image;
44 import net.pterodactylus.sone.data.Post;
45 import net.pterodactylus.sone.data.PostReply;
46 import net.pterodactylus.sone.data.Sone;
48 import com.google.common.base.Optional;
49 import org.junit.Before;
50 import org.junit.Test;
53 * Tests for {@link MemoryDatabase}.
55 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
57 public class MemoryDatabaseTest {
59 private static final String SONE_ID = "sone";
60 private static final String RECIPIENT_ID = "recipient";
61 private final MemoryDatabase memoryDatabase = new MemoryDatabase(null, null);
62 private final Sone sone = mock(Sone.class);
65 public void setupSone() {
66 when(sone.getId()).thenReturn(SONE_ID);
70 public void storedSoneIsMadeAvailable() {
71 Post firstPost = new TestPostBuilder().withId("post1")
76 Post secondPost = new TestPostBuilder().withId("post2")
82 List<Post> posts = asList(firstPost, secondPost);
83 when(sone.getPosts()).thenReturn(posts);
84 PostReply firstPostFirstReply =
85 new TestPostReplyBuilder().withId("reply1")
87 .to(firstPost.getId())
91 PostReply firstPostSecondReply =
92 new TestPostReplyBuilder().withId("reply3")
94 .to(firstPost.getId())
98 PostReply secondPostReply =
99 new TestPostReplyBuilder().withId("reply2")
101 .to(secondPost.getId())
105 Set<PostReply> postReplies = new HashSet<PostReply>(
106 asList(firstPostFirstReply, firstPostSecondReply,
108 when(sone.getReplies()).thenReturn(postReplies);
109 Album firstAlbum = new TestAlbumBuilder().withId("album1")
114 .setDescription("album-description1")
116 Album secondAlbum = new TestAlbumBuilder().withId("album2").by(
117 sone).build().modify().setTitle("album2").setDescription(
118 "album-description2").setAlbumImage("image1").update();
119 Album thirdAlbum = new TestAlbumBuilder().withId("album3").by(
120 sone).build().modify().setTitle("album3").setDescription(
121 "album-description3").update();
122 firstAlbum.addAlbum(thirdAlbum);
123 Album rootAlbum = mock(Album.class);
124 when(rootAlbum.getAlbums()).thenReturn(
125 asList(firstAlbum, secondAlbum));
126 when(sone.getRootAlbum()).thenReturn(rootAlbum);
127 Image firstImage = new TestImageBuilder().withId("image1")
131 .setCreationTime(1000L)
132 .setKey("KSK@image1")
134 .setDescription("image-description1")
138 Image secondImage = new TestImageBuilder().withId("image2")
142 .setCreationTime(2000L)
143 .setKey("KSK@image2")
145 .setDescription("image-description2")
149 Image thirdImage = new TestImageBuilder().withId("image3")
153 .setCreationTime(3000L)
154 .setKey("KSK@image3")
156 .setDescription("image-description3")
160 firstAlbum.addImage(firstImage);
161 firstAlbum.addImage(thirdImage);
162 secondAlbum.addImage(secondImage);
163 memoryDatabase.storeSone(sone);
164 assertThat(memoryDatabase.getPost("post1").get(),
165 isPost(firstPost.getId(), 1000L, "post1",
166 Optional.<String>absent()));
167 assertThat(memoryDatabase.getPost("post2").get(),
168 isPost(secondPost.getId(), 2000L, "post2", of(RECIPIENT_ID)));
169 assertThat(memoryDatabase.getPost("post3").isPresent(), is(false));
170 assertThat(memoryDatabase.getPostReply("reply1").get(),
171 isPostReply("reply1", "post1", 3000L, "reply1"));
172 assertThat(memoryDatabase.getPostReply("reply2").get(),
173 isPostReply("reply2", "post2", 4000L, "reply2"));
174 assertThat(memoryDatabase.getPostReply("reply3").get(),
175 isPostReply("reply3", "post1", 5000L, "reply3"));
176 assertThat(memoryDatabase.getPostReply("reply4").isPresent(),
178 assertThat(memoryDatabase.getAlbum("album1").get(),
179 isAlbum("album1", null, "album1", "album-description1",
181 assertThat(memoryDatabase.getAlbum("album2").get(),
182 isAlbum("album2", null, "album2", "album-description2",
184 assertThat(memoryDatabase.getAlbum("album3").get(),
185 isAlbum("album3", "album1", "album3", "album-description3",
187 assertThat(memoryDatabase.getAlbum("album4").isPresent(), is(false));
188 assertThat(memoryDatabase.getImage("image1").get(),
189 isImage("image1", 1000L, "KSK@image1", "image1",
190 "image-description1", 16, 9));
191 assertThat(memoryDatabase.getImage("image2").get(),
192 isImage("image2", 2000L, "KSK@image2", "image2",
193 "image-description2", 32, 18));
194 assertThat(memoryDatabase.getImage("image3").get(),
195 isImage("image3", 3000L, "KSK@image3", "image3",
196 "image-description3", 48, 27));
197 assertThat(memoryDatabase.getImage("image4").isPresent(), is(false));
201 public void postRecipientsAreDetectedCorrectly() {
202 Post postWithRecipient = createPost(of(RECIPIENT_ID));
203 memoryDatabase.storePost(postWithRecipient);
204 Post postWithoutRecipient = createPost(Optional.<String>absent());
205 memoryDatabase.storePost(postWithoutRecipient);
206 assertThat(memoryDatabase.getDirectedPosts(RECIPIENT_ID),
207 contains(postWithRecipient));
210 private Post createPost(Optional<String> recipient) {
211 Post postWithRecipient = mock(Post.class);
212 when(postWithRecipient.getId()).thenReturn(randomUUID().toString());
213 when(postWithRecipient.getSone()).thenReturn(sone);
214 when(postWithRecipient.getRecipientId()).thenReturn(recipient);
215 return postWithRecipient;
219 public void postRepliesAreManagedCorrectly() {
220 Post firstPost = createPost(Optional.<String>absent());
221 PostReply firstPostFirstReply = createPostReply(firstPost, 1000L);
222 Post secondPost = createPost(Optional.<String>absent());
223 PostReply secondPostFirstReply = createPostReply(secondPost, 1000L);
224 PostReply secondPostSecondReply = createPostReply(secondPost, 2000L);
225 memoryDatabase.storePost(firstPost);
226 memoryDatabase.storePost(secondPost);
227 memoryDatabase.storePostReply(firstPostFirstReply);
228 memoryDatabase.storePostReply(secondPostFirstReply);
229 memoryDatabase.storePostReply(secondPostSecondReply);
230 assertThat(memoryDatabase.getReplies(firstPost.getId()),
231 contains(firstPostFirstReply));
232 assertThat(memoryDatabase.getReplies(secondPost.getId()),
233 contains(secondPostFirstReply, secondPostSecondReply));
236 private PostReply createPostReply(Post post, long time) {
237 PostReply postReply = mock(PostReply.class);
238 when(postReply.getId()).thenReturn(randomUUID().toString());
239 when(postReply.getTime()).thenReturn(time);
240 when(postReply.getPost()).thenReturn(of(post));
241 final String postId = post.getId();
242 when(postReply.getPostId()).thenReturn(postId);
247 public void testBasicAlbumFunctionality() {
248 Album newAlbum = new AlbumImpl(mock(Sone.class));
249 assertThat(memoryDatabase.getAlbum(newAlbum.getId()), is(Optional.<Album>absent()));
250 memoryDatabase.storeAlbum(newAlbum);
251 assertThat(memoryDatabase.getAlbum(newAlbum.getId()), is(of(newAlbum)));
252 memoryDatabase.removeAlbum(newAlbum);
253 assertThat(memoryDatabase.getAlbum(newAlbum.getId()), is(Optional.<Album>absent()));