2 * Sone - CreatePostCommandTest.java - Copyright © 2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.fcp;
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;
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;
44 import freenet.support.SimpleFieldSet;
46 import com.google.common.base.Optional;
47 import org.junit.Test;
50 * Unit test for {@link CreatePostCommand}.
52 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
54 public class CreatePostCommandTest {
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);
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));
68 SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
70 .put("Text", "Text of the post.")
71 .put("Recipient", "OtherSone")
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())));
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));
90 SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
92 .put("Text", "Text of the post.")
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())));
106 public void verifyThatCreatingAPostDirectedToTheSendingSoneCausesAnError() throws FcpException {
107 mockLocalSone(core, "Sone");
108 CapturingPostCreated capturingPostCreated = new CapturingPostCreated();
109 when(core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
111 SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
113 .put("Recipient", "Sone")
114 .put("Text", "Text of the post.")
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());
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));
128 SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
131 createPostCommand.execute(createPostFieldSet, null, DIRECT);
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));
140 SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
141 .put("Text", "Text of the post.")
143 createPostCommand.execute(createPostFieldSet, null, DIRECT);
146 private static class CapturingPostCreated implements PostCreated {
151 public void postCreated(Post post) {