0753021de74d114e21d837fda3db8c9a3fca072a
[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.mockito.Mockito.mock;
31 import static org.mockito.Mockito.when;
32
33 import java.util.HashSet;
34 import java.util.List;
35 import java.util.Set;
36
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;
47
48 import com.google.common.base.Optional;
49 import org.junit.Before;
50 import org.junit.Test;
51
52 /**
53  * Tests for {@link MemoryDatabase}.
54  *
55  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
56  */
57 public class MemoryDatabaseTest {
58
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);
63
64         @Before
65         public void setupSone() {
66                 when(sone.getId()).thenReturn(SONE_ID);
67         }
68
69         @Test
70         public void storedSoneIsMadeAvailable() {
71                 Post firstPost = new TestPostBuilder().withId("post1")
72                                 .from(SONE_ID)
73                                 .withTime(1000L)
74                                 .withText("post1")
75                                 .build();
76                 Post secondPost = new TestPostBuilder().withId("post2")
77                                 .from(SONE_ID)
78                                 .withTime(2000L)
79                                 .withText("post2")
80                                 .to(RECIPIENT_ID)
81                                 .build();
82                 List<Post> posts = asList(firstPost, secondPost);
83                 when(sone.getPosts()).thenReturn(posts);
84                 PostReply firstPostFirstReply =
85                                 new TestPostReplyBuilder().withId("reply1")
86                                                 .from(SONE_ID)
87                                                 .to(firstPost.getId())
88                                                 .withTime(3000L)
89                                                 .withText("reply1")
90                                                 .build();
91                 PostReply firstPostSecondReply =
92                                 new TestPostReplyBuilder().withId("reply3")
93                                                 .from(RECIPIENT_ID)
94                                                 .to(firstPost.getId())
95                                                 .withTime(5000L)
96                                                 .withText("reply3")
97                                                 .build();
98                 PostReply secondPostReply =
99                                 new TestPostReplyBuilder().withId("reply2")
100                                                 .from(SONE_ID)
101                                                 .to(secondPost.getId())
102                                                 .withTime(4000L)
103                                                 .withText("reply2")
104                                                 .build();
105                 Set<PostReply> postReplies = new HashSet<PostReply>(
106                                 asList(firstPostFirstReply, firstPostSecondReply,
107                                                 secondPostReply));
108                 when(sone.getReplies()).thenReturn(postReplies);
109                 Album firstAlbum = new TestAlbumBuilder().withId("album1")
110                                 .by(sone)
111                                 .build()
112                                 .modify()
113                                 .setTitle("album1")
114                                 .setDescription("album-description1")
115                                 .update();
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")
128                                 .build()
129                                 .modify()
130                                 .setSone(sone)
131                                 .setCreationTime(1000L)
132                                 .setKey("KSK@image1")
133                                 .setTitle("image1")
134                                 .setDescription("image-description1")
135                                 .setWidth(16)
136                                 .setHeight(9)
137                                 .update();
138                 Image secondImage = new TestImageBuilder().withId("image2")
139                                 .build()
140                                 .modify()
141                                 .setSone(sone)
142                                 .setCreationTime(2000L)
143                                 .setKey("KSK@image2")
144                                 .setTitle("image2")
145                                 .setDescription("image-description2")
146                                 .setWidth(32)
147                                 .setHeight(18)
148                                 .update();
149                 Image thirdImage = new TestImageBuilder().withId("image3")
150                                 .build()
151                                 .modify()
152                                 .setSone(sone)
153                                 .setCreationTime(3000L)
154                                 .setKey("KSK@image3")
155                                 .setTitle("image3")
156                                 .setDescription("image-description3")
157                                 .setWidth(48)
158                                 .setHeight(27)
159                                 .update();
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(),
177                                 is(false));
178                 assertThat(memoryDatabase.getAlbum("album1").get(),
179                                 isAlbum("album1", null, "album1", "album-description1",
180                                                 null));
181                 assertThat(memoryDatabase.getAlbum("album2").get(),
182                                 isAlbum("album2", null, "album2", "album-description2",
183                                                 "image1"));
184                 assertThat(memoryDatabase.getAlbum("album3").get(),
185                                 isAlbum("album3", "album1", "album3", "album-description3",
186                                                 null));
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));
198         }
199
200         @Test
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));
208         }
209
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;
216         }
217
218         @Test
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));
234         }
235
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);
243                 return postReply;
244         }
245
246         @Test
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()));
254         }
255
256 }