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.hamcrest.Matchers.empty;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.when;
34 import java.util.HashSet;
35 import java.util.List;
38 import net.pterodactylus.sone.TestAlbumBuilder;
39 import net.pterodactylus.sone.TestImageBuilder;
40 import net.pterodactylus.sone.TestPostBuilder;
41 import net.pterodactylus.sone.TestPostReplyBuilder;
42 import net.pterodactylus.sone.data.Album;
43 import net.pterodactylus.sone.data.AlbumImpl;
44 import net.pterodactylus.sone.data.Image;
45 import net.pterodactylus.sone.data.Post;
46 import net.pterodactylus.sone.data.PostReply;
47 import net.pterodactylus.sone.data.Sone;
49 import com.google.common.base.Optional;
50 import org.junit.Before;
51 import org.junit.Test;
54 * Tests for {@link MemoryDatabase}.
56 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
58 public class MemoryDatabaseTest {
60 private static final String SONE_ID = "sone";
61 private static final String RECIPIENT_ID = "recipient";
62 private final MemoryDatabase memoryDatabase = new MemoryDatabase(null, null);
63 private final Sone sone = mock(Sone.class);
66 public void setupSone() {
67 when(sone.getId()).thenReturn(SONE_ID);
71 public void storedSoneIsMadeAvailable() {
72 Post firstPost = new TestPostBuilder().withId("post1")
77 Post secondPost = new TestPostBuilder().withId("post2")
83 List<Post> posts = asList(firstPost, secondPost);
84 when(sone.getPosts()).thenReturn(posts);
85 PostReply firstPostFirstReply =
86 new TestPostReplyBuilder().withId("reply1")
88 .to(firstPost.getId())
92 PostReply firstPostSecondReply =
93 new TestPostReplyBuilder().withId("reply3")
95 .to(firstPost.getId())
99 PostReply secondPostReply =
100 new TestPostReplyBuilder().withId("reply2")
102 .to(secondPost.getId())
106 Set<PostReply> postReplies = new HashSet<PostReply>(
107 asList(firstPostFirstReply, firstPostSecondReply,
109 when(sone.getReplies()).thenReturn(postReplies);
110 Album firstAlbum = new TestAlbumBuilder().withId("album1")
115 .setDescription("album-description1")
117 Album secondAlbum = new TestAlbumBuilder().withId("album2").by(
118 sone).build().modify().setTitle("album2").setDescription(
119 "album-description2").setAlbumImage("image1").update();
120 Album thirdAlbum = new TestAlbumBuilder().withId("album3").by(
121 sone).build().modify().setTitle("album3").setDescription(
122 "album-description3").update();
123 firstAlbum.addAlbum(thirdAlbum);
124 Album rootAlbum = mock(Album.class);
125 when(rootAlbum.getAlbums()).thenReturn(
126 asList(firstAlbum, secondAlbum));
127 when(sone.getRootAlbum()).thenReturn(rootAlbum);
128 Image firstImage = new TestImageBuilder().withId("image1")
132 .setCreationTime(1000L)
133 .setKey("KSK@image1")
135 .setDescription("image-description1")
139 Image secondImage = new TestImageBuilder().withId("image2")
143 .setCreationTime(2000L)
144 .setKey("KSK@image2")
146 .setDescription("image-description2")
150 Image thirdImage = new TestImageBuilder().withId("image3")
154 .setCreationTime(3000L)
155 .setKey("KSK@image3")
157 .setDescription("image-description3")
161 firstAlbum.addImage(firstImage);
162 firstAlbum.addImage(thirdImage);
163 secondAlbum.addImage(secondImage);
164 memoryDatabase.storeSone(sone);
165 assertThat(memoryDatabase.getPost("post1").get(),
166 isPost(firstPost.getId(), 1000L, "post1",
167 Optional.<String>absent()));
168 assertThat(memoryDatabase.getPost("post2").get(),
169 isPost(secondPost.getId(), 2000L, "post2", of(RECIPIENT_ID)));
170 assertThat(memoryDatabase.getPost("post3").isPresent(), is(false));
171 assertThat(memoryDatabase.getPostReply("reply1").get(),
172 isPostReply("reply1", "post1", 3000L, "reply1"));
173 assertThat(memoryDatabase.getPostReply("reply2").get(),
174 isPostReply("reply2", "post2", 4000L, "reply2"));
175 assertThat(memoryDatabase.getPostReply("reply3").get(),
176 isPostReply("reply3", "post1", 5000L, "reply3"));
177 assertThat(memoryDatabase.getPostReply("reply4").isPresent(),
179 assertThat(memoryDatabase.getAlbum("album1").get(),
180 isAlbum("album1", null, "album1", "album-description1",
182 assertThat(memoryDatabase.getAlbum("album2").get(),
183 isAlbum("album2", null, "album2", "album-description2",
185 assertThat(memoryDatabase.getAlbum("album3").get(),
186 isAlbum("album3", "album1", "album3", "album-description3",
188 assertThat(memoryDatabase.getAlbum("album4").isPresent(), is(false));
189 assertThat(memoryDatabase.getImage("image1").get(),
190 isImage("image1", 1000L, "KSK@image1", "image1",
191 "image-description1", 16, 9));
192 assertThat(memoryDatabase.getImage("image2").get(),
193 isImage("image2", 2000L, "KSK@image2", "image2",
194 "image-description2", 32, 18));
195 assertThat(memoryDatabase.getImage("image3").get(),
196 isImage("image3", 3000L, "KSK@image3", "image3",
197 "image-description3", 48, 27));
198 assertThat(memoryDatabase.getImage("image4").isPresent(), is(false));
202 public void storedAndRemovedSoneIsNotAvailable() {
203 storedSoneIsMadeAvailable();
204 memoryDatabase.removeSone(sone);
205 assertThat(memoryDatabase.getSones(), empty());
209 public void postRecipientsAreDetectedCorrectly() {
210 Post postWithRecipient = createPost(of(RECIPIENT_ID));
211 memoryDatabase.storePost(postWithRecipient);
212 Post postWithoutRecipient = createPost(Optional.<String>absent());
213 memoryDatabase.storePost(postWithoutRecipient);
214 assertThat(memoryDatabase.getDirectedPosts(RECIPIENT_ID),
215 contains(postWithRecipient));
218 private Post createPost(Optional<String> recipient) {
219 Post postWithRecipient = mock(Post.class);
220 when(postWithRecipient.getId()).thenReturn(randomUUID().toString());
221 when(postWithRecipient.getSone()).thenReturn(sone);
222 when(postWithRecipient.getRecipientId()).thenReturn(recipient);
223 return postWithRecipient;
227 public void postRepliesAreManagedCorrectly() {
228 Post firstPost = createPost(Optional.<String>absent());
229 PostReply firstPostFirstReply = createPostReply(firstPost, 1000L);
230 Post secondPost = createPost(Optional.<String>absent());
231 PostReply secondPostFirstReply = createPostReply(secondPost, 1000L);
232 PostReply secondPostSecondReply = createPostReply(secondPost, 2000L);
233 memoryDatabase.storePost(firstPost);
234 memoryDatabase.storePost(secondPost);
235 memoryDatabase.storePostReply(firstPostFirstReply);
236 memoryDatabase.storePostReply(secondPostFirstReply);
237 memoryDatabase.storePostReply(secondPostSecondReply);
238 assertThat(memoryDatabase.getReplies(firstPost.getId()),
239 contains(firstPostFirstReply));
240 assertThat(memoryDatabase.getReplies(secondPost.getId()),
241 contains(secondPostFirstReply, secondPostSecondReply));
244 private PostReply createPostReply(Post post, long time) {
245 PostReply postReply = mock(PostReply.class);
246 when(postReply.getId()).thenReturn(randomUUID().toString());
247 when(postReply.getTime()).thenReturn(time);
248 when(postReply.getPost()).thenReturn(of(post));
249 final String postId = post.getId();
250 when(postReply.getPostId()).thenReturn(postId);
255 public void testBasicAlbumFunctionality() {
256 Album newAlbum = new AlbumImpl(mock(Sone.class));
257 assertThat(memoryDatabase.getAlbum(newAlbum.getId()), is(Optional.<Album>absent()));
258 memoryDatabase.storeAlbum(newAlbum);
259 assertThat(memoryDatabase.getAlbum(newAlbum.getId()), is(of(newAlbum)));
260 memoryDatabase.removeAlbum(newAlbum);
261 assertThat(memoryDatabase.getAlbum(newAlbum.getId()), is(Optional.<Album>absent()));