Don’t store posts by recipient, generate them on the fly.
[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 org.hamcrest.CoreMatchers.is;
22 import static org.hamcrest.MatcherAssert.assertThat;
23 import static org.hamcrest.Matchers.contains;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
26
27 import net.pterodactylus.sone.data.Album;
28 import net.pterodactylus.sone.data.AlbumImpl;
29 import net.pterodactylus.sone.data.Post;
30 import net.pterodactylus.sone.data.Sone;
31
32 import com.google.common.base.Optional;
33 import org.junit.Before;
34 import org.junit.Test;
35
36 /**
37  * Tests for {@link MemoryDatabase}.
38  *
39  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40  */
41 public class MemoryDatabaseTest {
42
43         private static final String SONE_ID = "sone";
44         private static final String RECIPIENT_ID = "recipient";
45         private final MemoryDatabase memoryDatabase = new MemoryDatabase(null, null);
46         private final Sone sone = mock(Sone.class);
47
48         @Before
49         public void setupSone() {
50                 when(sone.getId()).thenReturn(SONE_ID);
51         }
52
53         @Test
54         public void postRecipientsAreDetectedCorrectly() {
55                 Post postWithRecipient = mock(Post.class);
56                 when(postWithRecipient.getSone()).thenReturn(sone);
57                 when(postWithRecipient.getRecipientId()).thenReturn(of(RECIPIENT_ID));
58                 memoryDatabase.storePost(postWithRecipient);
59                 Post postWithoutRecipient = mock(Post.class);
60                 when(postWithoutRecipient.getSone()).thenReturn(sone);
61                 when(postWithoutRecipient.getRecipientId()).thenReturn(
62                                 Optional.<String>absent());
63                 memoryDatabase.storePost(postWithoutRecipient);
64                 assertThat(memoryDatabase.getDirectedPosts(RECIPIENT_ID),
65                                 contains(postWithRecipient));
66         }
67
68         @Test
69         public void testBasicAlbumFunctionality() {
70                 Album newAlbum = new AlbumImpl(mock(Sone.class));
71                 assertThat(memoryDatabase.getAlbum(newAlbum.getId()), is(Optional.<Album>absent()));
72                 memoryDatabase.storeAlbum(newAlbum);
73                 assertThat(memoryDatabase.getAlbum(newAlbum.getId()), is(of(newAlbum)));
74                 memoryDatabase.removeAlbum(newAlbum);
75                 assertThat(memoryDatabase.getAlbum(newAlbum.getId()), is(Optional.<Album>absent()));
76         }
77
78 }