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.database.PostBuilder.PostCreated;
23 import static net.pterodactylus.sone.Verifiers.verifyAnswer;
24 import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT;
25 import static org.hamcrest.MatcherAssert.assertThat;
26 import static org.hamcrest.Matchers.allOf;
27 import static org.hamcrest.Matchers.greaterThanOrEqualTo;
28 import static org.hamcrest.Matchers.is;
29 import static org.hamcrest.Matchers.lessThanOrEqualTo;
30 import static org.hamcrest.Matchers.nullValue;
31 import static org.mockito.Mockito.when;
33 import net.pterodactylus.sone.data.Mocks;
34 import net.pterodactylus.sone.data.Post;
35 import net.pterodactylus.sone.data.Sone;
36 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
37 import net.pterodactylus.sone.freenet.fcp.Command.Response;
38 import net.pterodactylus.sone.freenet.fcp.FcpException;
40 import freenet.support.SimpleFieldSet;
42 import com.google.common.base.Optional;
43 import org.junit.Test;
46 * Unit test for {@link CreatePostCommand}.
48 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
50 public class CreatePostCommandTest {
52 private final long now = currentTimeMillis();
53 private final Mocks mocks = new Mocks();
54 private final CreatePostCommand createPostCommand = new CreatePostCommand(mocks.core);
57 public void verifyThatCreatingAPostWorks() throws FcpException {
58 Sone sone = mocks.mockSone("Sone").local().create();
59 mocks.mockSone("OtherSone").local().create();
60 CapturingPostCreated capturingPostCreated = new CapturingPostCreated();
61 when(mocks.core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
63 SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
65 .put("Text", "Text of the post.")
66 .put("Recipient", "OtherSone")
68 Response response = createPostCommand.execute(createPostFieldSet, null, DIRECT);
69 verifyAnswer(response, "PostCreated");
70 assertThat(response.getReplyParameters().get("Post"), is(capturingPostCreated.post.getId()));
71 assertThat(capturingPostCreated.post.getSone(), is(sone));
72 assertThat(capturingPostCreated.post.getRecipientId(), is(of("OtherSone")));
73 assertThat(capturingPostCreated.post.getText(), is("Text of the post."));
74 assertThat(capturingPostCreated.post.getTime(), allOf(greaterThanOrEqualTo(now), lessThanOrEqualTo(currentTimeMillis())));
78 public void verifyThatCreatingAPostWithoutRecipientWorks() throws FcpException {
79 Sone sone = mocks.mockSone("Sone").local().create();
80 CapturingPostCreated capturingPostCreated = new CapturingPostCreated();
81 when(mocks.core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
83 SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
85 .put("Text", "Text of the post.")
87 Response response = createPostCommand.execute(createPostFieldSet, null, DIRECT);
88 verifyAnswer(response, "PostCreated");
89 assertThat(response.getReplyParameters().get("Post"), is(capturingPostCreated.post.getId()));
90 assertThat(capturingPostCreated.post.getSone(), is(sone));
91 assertThat(capturingPostCreated.post.getRecipientId(), is(Optional.<String>absent()));
92 assertThat(capturingPostCreated.post.getText(), is("Text of the post."));
93 assertThat(capturingPostCreated.post.getTime(), allOf(greaterThanOrEqualTo(now), lessThanOrEqualTo(currentTimeMillis())));
97 public void verifyThatCreatingAPostDirectedToTheSendingSoneCausesAnError() throws FcpException {
98 mocks.mockSone("Sone").local().create();
99 CapturingPostCreated capturingPostCreated = new CapturingPostCreated();
100 when(mocks.core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
102 SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
104 .put("Recipient", "Sone")
105 .put("Text", "Text of the post.")
107 Response response = createPostCommand.execute(createPostFieldSet, null, DIRECT);
108 verifyAnswer(response, "Error");
109 assertThat(capturingPostCreated.post, nullValue());
112 @Test(expected = FcpException.class)
113 public void verifyThatCreatingAPostWithoutTextCausesAnError() throws FcpException {
114 mocks.mockSone("Sone").create();
115 CapturingPostCreated capturingPostCreated = new CapturingPostCreated();
116 when(mocks.core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
118 SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
121 createPostCommand.execute(createPostFieldSet, null, DIRECT);
124 @Test(expected = FcpException.class)
125 public void verifyThatCreatingAPostWithoutSoneCausesAnError() throws FcpException {
126 mocks.mockSone("Sone").local().create();
127 CapturingPostCreated capturingPostCreated = new CapturingPostCreated();
128 when(mocks.core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
130 SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
131 .put("Text", "Text of the post.")
133 createPostCommand.execute(createPostFieldSet, null, DIRECT);
136 private static class CapturingPostCreated implements PostCreated {
141 public void postCreated(Post post) {