Don’t store post replies sorted by post.
[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.UUID.randomUUID;
22 import static org.hamcrest.CoreMatchers.is;
23 import static org.hamcrest.MatcherAssert.assertThat;
24 import static org.hamcrest.Matchers.contains;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
27
28 import net.pterodactylus.sone.data.Album;
29 import net.pterodactylus.sone.data.AlbumImpl;
30 import net.pterodactylus.sone.data.Post;
31 import net.pterodactylus.sone.data.PostReply;
32 import net.pterodactylus.sone.data.Sone;
33
34 import com.google.common.base.Optional;
35 import org.junit.Before;
36 import org.junit.Test;
37
38 /**
39  * Tests for {@link MemoryDatabase}.
40  *
41  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42  */
43 public class MemoryDatabaseTest {
44
45         private static final String SONE_ID = "sone";
46         private static final String RECIPIENT_ID = "recipient";
47         private final MemoryDatabase memoryDatabase = new MemoryDatabase(null, null);
48         private final Sone sone = mock(Sone.class);
49
50         @Before
51         public void setupSone() {
52                 when(sone.getId()).thenReturn(SONE_ID);
53         }
54
55         @Test
56         public void postRecipientsAreDetectedCorrectly() {
57                 Post postWithRecipient = createPost(of(RECIPIENT_ID));
58                 memoryDatabase.storePost(postWithRecipient);
59                 Post postWithoutRecipient = createPost(Optional.<String>absent());
60                 memoryDatabase.storePost(postWithoutRecipient);
61                 assertThat(memoryDatabase.getDirectedPosts(RECIPIENT_ID),
62                                 contains(postWithRecipient));
63         }
64
65         private Post createPost(Optional<String> recipient) {
66                 Post postWithRecipient = mock(Post.class);
67                 when(postWithRecipient.getId()).thenReturn(randomUUID().toString());
68                 when(postWithRecipient.getSone()).thenReturn(sone);
69                 when(postWithRecipient.getRecipientId()).thenReturn(recipient);
70                 return postWithRecipient;
71         }
72
73         @Test
74         public void postRepliesAreManagedCorrectly() {
75                 Post firstPost = createPost(Optional.<String>absent());
76                 PostReply firstPostFirstReply = createPostReply(firstPost, 1000L);
77                 Post secondPost = createPost(Optional.<String>absent());
78                 PostReply secondPostFirstReply = createPostReply(secondPost, 1000L);
79                 PostReply secondPostSecondReply = createPostReply(secondPost, 2000L);
80                 memoryDatabase.storePost(firstPost);
81                 memoryDatabase.storePost(secondPost);
82                 memoryDatabase.storePostReply(firstPostFirstReply);
83                 memoryDatabase.storePostReply(secondPostFirstReply);
84                 memoryDatabase.storePostReply(secondPostSecondReply);
85                 assertThat(memoryDatabase.getReplies(firstPost.getId()),
86                                 contains(firstPostFirstReply));
87                 assertThat(memoryDatabase.getReplies(secondPost.getId()),
88                                 contains(secondPostFirstReply, secondPostSecondReply));
89         }
90
91         private PostReply createPostReply(Post post, long time) {
92                 PostReply postReply = mock(PostReply.class);
93                 when(postReply.getId()).thenReturn(randomUUID().toString());
94                 when(postReply.getTime()).thenReturn(time);
95                 when(postReply.getPost()).thenReturn(of(post));
96                 final String postId = post.getId();
97                 when(postReply.getPostId()).thenReturn(postId);
98                 return postReply;
99         }
100
101         @Test
102         public void testBasicAlbumFunctionality() {
103                 Album newAlbum = new AlbumImpl(mock(Sone.class));
104                 assertThat(memoryDatabase.getAlbum(newAlbum.getId()), is(Optional.<Album>absent()));
105                 memoryDatabase.storeAlbum(newAlbum);
106                 assertThat(memoryDatabase.getAlbum(newAlbum.getId()), is(of(newAlbum)));
107                 memoryDatabase.removeAlbum(newAlbum);
108                 assertThat(memoryDatabase.getAlbum(newAlbum.getId()), is(Optional.<Album>absent()));
109         }
110
111 }