Refactor post mocking into its own method.
[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.Sone;
32
33 import com.google.common.base.Optional;
34 import org.junit.Before;
35 import org.junit.Test;
36
37 /**
38  * Tests for {@link MemoryDatabase}.
39  *
40  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41  */
42 public class MemoryDatabaseTest {
43
44         private static final String SONE_ID = "sone";
45         private static final String RECIPIENT_ID = "recipient";
46         private final MemoryDatabase memoryDatabase = new MemoryDatabase(null, null);
47         private final Sone sone = mock(Sone.class);
48
49         @Before
50         public void setupSone() {
51                 when(sone.getId()).thenReturn(SONE_ID);
52         }
53
54         @Test
55         public void postRecipientsAreDetectedCorrectly() {
56                 Post postWithRecipient = createPost(of(RECIPIENT_ID));
57                 memoryDatabase.storePost(postWithRecipient);
58                 Post postWithoutRecipient = createPost(Optional.<String>absent());
59                 memoryDatabase.storePost(postWithoutRecipient);
60                 assertThat(memoryDatabase.getDirectedPosts(RECIPIENT_ID),
61                                 contains(postWithRecipient));
62         }
63
64         private Post createPost(Optional<String> recipient) {
65                 Post postWithRecipient = mock(Post.class);
66                 when(postWithRecipient.getId()).thenReturn(randomUUID().toString());
67                 when(postWithRecipient.getSone()).thenReturn(sone);
68                 when(postWithRecipient.getRecipientId()).thenReturn(recipient);
69                 return postWithRecipient;
70         }
71
72         @Test
73         public void testBasicAlbumFunctionality() {
74                 Album newAlbum = new AlbumImpl(mock(Sone.class));
75                 assertThat(memoryDatabase.getAlbum(newAlbum.getId()), is(Optional.<Album>absent()));
76                 memoryDatabase.storeAlbum(newAlbum);
77                 assertThat(memoryDatabase.getAlbum(newAlbum.getId()), is(of(newAlbum)));
78                 memoryDatabase.removeAlbum(newAlbum);
79                 assertThat(memoryDatabase.getAlbum(newAlbum.getId()), is(Optional.<Album>absent()));
80         }
81
82 }