Add unit test for CreatePostCommand.
[Sone.git] / src / test / java / net / pterodactylus / sone / fcp / CreatePostCommandTest.java
1 /*
2  * Sone - CreatePostCommandTest.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.fcp;
19
20 import static com.google.common.base.Optional.of;
21 import static java.lang.System.currentTimeMillis;
22 import static net.pterodactylus.sone.data.Mocks.mockCore;
23 import static net.pterodactylus.sone.data.Mocks.mockDatabase;
24 import static net.pterodactylus.sone.data.Mocks.mockLocalSone;
25 import static net.pterodactylus.sone.database.PostBuilder.PostCreated;
26 import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT;
27 import static org.hamcrest.MatcherAssert.assertThat;
28 import static org.hamcrest.Matchers.allOf;
29 import static org.hamcrest.Matchers.greaterThanOrEqualTo;
30 import static org.hamcrest.Matchers.is;
31 import static org.hamcrest.Matchers.lessThanOrEqualTo;
32 import static org.hamcrest.Matchers.notNullValue;
33 import static org.hamcrest.Matchers.nullValue;
34 import static org.mockito.Mockito.when;
35
36 import net.pterodactylus.sone.core.Core;
37 import net.pterodactylus.sone.data.Post;
38 import net.pterodactylus.sone.data.Sone;
39 import net.pterodactylus.sone.database.Database;
40 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
41 import net.pterodactylus.sone.freenet.fcp.Command.Response;
42 import net.pterodactylus.sone.freenet.fcp.FcpException;
43
44 import freenet.support.SimpleFieldSet;
45
46 import com.google.common.base.Optional;
47 import org.junit.Test;
48
49 /**
50  * Unit test for {@link CreatePostCommand}.
51  *
52  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
53  */
54 public class CreatePostCommandTest {
55
56         private final long now = currentTimeMillis();
57         private final Database database = mockDatabase();
58         private final Core core = mockCore(database);
59         private final CreatePostCommand createPostCommand = new CreatePostCommand(core);
60
61         @Test
62         public void verifyThatCreatingAPostWorks() throws FcpException {
63                 Sone sone = mockLocalSone(core, "Sone");
64                 mockLocalSone(core, "OtherSone");
65                 CapturingPostCreated capturingPostCreated = new CapturingPostCreated();
66                 when(core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
67
68                 SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
69                                 .put("Sone", "Sone")
70                                 .put("Text", "Text of the post.")
71                                 .put("Recipient", "OtherSone")
72                                 .get();
73                 Response response = createPostCommand.execute(createPostFieldSet, null, DIRECT);
74                 assertThat(response, notNullValue());
75                 assertThat(capturingPostCreated.post, notNullValue());
76                 assertThat(response.getReplyParameters().get("Message"), is("PostCreated"));
77                 assertThat(response.getReplyParameters().get("Post"), is(capturingPostCreated.post.getId()));
78                 assertThat(capturingPostCreated.post.getSone(), is(sone));
79                 assertThat(capturingPostCreated.post.getRecipientId(), is(of("OtherSone")));
80                 assertThat(capturingPostCreated.post.getText(), is("Text of the post."));
81                 assertThat(capturingPostCreated.post.getTime(), allOf(greaterThanOrEqualTo(now), lessThanOrEqualTo(currentTimeMillis())));
82         }
83
84         @Test
85         public void verifyThatCreatingAPostWithoutRecipientWorks() throws FcpException {
86                 Sone sone = mockLocalSone(core, "Sone");
87                 CapturingPostCreated capturingPostCreated = new CapturingPostCreated();
88                 when(core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
89
90                 SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
91                                 .put("Sone", "Sone")
92                                 .put("Text", "Text of the post.")
93                                 .get();
94                 Response response = createPostCommand.execute(createPostFieldSet, null, DIRECT);
95                 assertThat(response, notNullValue());
96                 assertThat(capturingPostCreated.post, notNullValue());
97                 assertThat(response.getReplyParameters().get("Message"), is("PostCreated"));
98                 assertThat(response.getReplyParameters().get("Post"), is(capturingPostCreated.post.getId()));
99                 assertThat(capturingPostCreated.post.getSone(), is(sone));
100                 assertThat(capturingPostCreated.post.getRecipientId(), is(Optional.<String>absent()));
101                 assertThat(capturingPostCreated.post.getText(), is("Text of the post."));
102                 assertThat(capturingPostCreated.post.getTime(), allOf(greaterThanOrEqualTo(now), lessThanOrEqualTo(currentTimeMillis())));
103         }
104
105         @Test
106         public void verifyThatCreatingAPostDirectedToTheSendingSoneCausesAnError() throws FcpException {
107                 mockLocalSone(core, "Sone");
108                 CapturingPostCreated capturingPostCreated = new CapturingPostCreated();
109                 when(core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
110
111                 SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
112                                 .put("Sone", "Sone")
113                                 .put("Recipient", "Sone")
114                                 .put("Text", "Text of the post.")
115                                 .get();
116                 Response response = createPostCommand.execute(createPostFieldSet, null, DIRECT);
117                 assertThat(response, notNullValue());
118                 assertThat(response.getReplyParameters().get("Message"), is("Error"));
119                 assertThat(capturingPostCreated.post, nullValue());
120         }
121
122         @Test(expected = FcpException.class)
123         public void verifyThatCreatingAPostWithoutTextCausesAnError() throws FcpException {
124                 mockLocalSone(core, "Sone");
125                 CapturingPostCreated capturingPostCreated = new CapturingPostCreated();
126                 when(core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
127
128                 SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
129                                 .put("Sone", "Sone")
130                                 .get();
131                 createPostCommand.execute(createPostFieldSet, null, DIRECT);
132         }
133
134         @Test(expected = FcpException.class)
135         public void verifyThatCreatingAPostWithoutSoneCausesAnError() throws FcpException {
136                 mockLocalSone(core, "Sone");
137                 CapturingPostCreated capturingPostCreated = new CapturingPostCreated();
138                 when(core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
139
140                 SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
141                                 .put("Text", "Text of the post.")
142                                 .get();
143                 createPostCommand.execute(createPostFieldSet, null, DIRECT);
144         }
145
146         private static class CapturingPostCreated implements PostCreated {
147
148                 public Post post;
149
150                 @Override
151                 public void postCreated(Post post) {
152                         this.post = post;
153                 }
154
155         }
156
157 }