c5cd9f3cd858865da395d85178468ceb0841bad5
[Sone.git] / src / test / java / net / pterodactylus / sone / database / memory / MemoryDatabaseTest.java
1 /*
2  * Sone - MemoryDatabaseTest.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.database.memory;
19
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;
33
34 import java.util.HashSet;
35 import java.util.List;
36 import java.util.Set;
37
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.impl.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;
48
49 import com.google.common.base.Optional;
50 import org.junit.Before;
51 import org.junit.Test;
52
53 /**
54  * Tests for {@link MemoryDatabase}.
55  *
56  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
57  */
58 public class MemoryDatabaseTest {
59
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);
64
65         @Before
66         public void setupSone() {
67                 when(sone.getId()).thenReturn(SONE_ID);
68         }
69
70         @Test
71         public void storedSoneIsMadeAvailable() {
72                 Post firstPost = new TestPostBuilder().withId("post1")
73                                 .from(SONE_ID)
74                                 .withTime(1000L)
75                                 .withText("post1")
76                                 .build();
77                 Post secondPost = new TestPostBuilder().withId("post2")
78                                 .from(SONE_ID)
79                                 .withTime(2000L)
80                                 .withText("post2")
81                                 .to(RECIPIENT_ID)
82                                 .build();
83                 List<Post> posts = asList(firstPost, secondPost);
84                 when(sone.getPosts()).thenReturn(posts);
85                 PostReply firstPostFirstReply =
86                                 new TestPostReplyBuilder().withId("reply1")
87                                                 .from(SONE_ID)
88                                                 .to(firstPost.getId())
89                                                 .withTime(3000L)
90                                                 .withText("reply1")
91                                                 .build();
92                 PostReply firstPostSecondReply =
93                                 new TestPostReplyBuilder().withId("reply3")
94                                                 .from(RECIPIENT_ID)
95                                                 .to(firstPost.getId())
96                                                 .withTime(5000L)
97                                                 .withText("reply3")
98                                                 .build();
99                 PostReply secondPostReply =
100                                 new TestPostReplyBuilder().withId("reply2")
101                                                 .from(SONE_ID)
102                                                 .to(secondPost.getId())
103                                                 .withTime(4000L)
104                                                 .withText("reply2")
105                                                 .build();
106                 Set<PostReply> postReplies = new HashSet<PostReply>(
107                                 asList(firstPostFirstReply, firstPostSecondReply,
108                                                 secondPostReply));
109                 when(sone.getReplies()).thenReturn(postReplies);
110                 Album firstAlbum = new TestAlbumBuilder().withId("album1")
111                                 .by(sone)
112                                 .build()
113                                 .modify()
114                                 .setTitle("album1")
115                                 .setDescription("album-description1")
116                                 .update();
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")
129                                 .build()
130                                 .modify()
131                                 .setSone(sone)
132                                 .setCreationTime(1000L)
133                                 .setKey("KSK@image1")
134                                 .setTitle("image1")
135                                 .setDescription("image-description1")
136                                 .setWidth(16)
137                                 .setHeight(9)
138                                 .update();
139                 Image secondImage = new TestImageBuilder().withId("image2")
140                                 .build()
141                                 .modify()
142                                 .setSone(sone)
143                                 .setCreationTime(2000L)
144                                 .setKey("KSK@image2")
145                                 .setTitle("image2")
146                                 .setDescription("image-description2")
147                                 .setWidth(32)
148                                 .setHeight(18)
149                                 .update();
150                 Image thirdImage = new TestImageBuilder().withId("image3")
151                                 .build()
152                                 .modify()
153                                 .setSone(sone)
154                                 .setCreationTime(3000L)
155                                 .setKey("KSK@image3")
156                                 .setTitle("image3")
157                                 .setDescription("image-description3")
158                                 .setWidth(48)
159                                 .setHeight(27)
160                                 .update();
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(),
178                                 is(false));
179                 assertThat(memoryDatabase.getAlbum("album1").get(),
180                                 isAlbum("album1", null, "album1", "album-description1",
181                                                 null));
182                 assertThat(memoryDatabase.getAlbum("album2").get(),
183                                 isAlbum("album2", null, "album2", "album-description2",
184                                                 "image1"));
185                 assertThat(memoryDatabase.getAlbum("album3").get(),
186                                 isAlbum("album3", "album1", "album3", "album-description3",
187                                                 null));
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));
199         }
200
201         @Test
202         public void storedAndRemovedSoneIsNotAvailable() {
203             storedSoneIsMadeAvailable();
204                 memoryDatabase.removeSone(sone);
205                 assertThat(memoryDatabase.getSones(), empty());
206         }
207
208         @Test
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));
216         }
217
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;
224         }
225
226         @Test
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));
242         }
243
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);
251                 return postReply;
252         }
253
254         @Test
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()));
262         }
263
264 }