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.freenet.fcp.Command.AccessType.DIRECT;
24 import static org.hamcrest.MatcherAssert.assertThat;
25 import static org.hamcrest.Matchers.allOf;
26 import static org.hamcrest.Matchers.greaterThanOrEqualTo;
27 import static org.hamcrest.Matchers.is;
28 import static org.hamcrest.Matchers.lessThanOrEqualTo;
29 import static org.hamcrest.Matchers.notNullValue;
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 assertThat(response, notNullValue());
70 assertThat(capturingPostCreated.post, notNullValue());
71 assertThat(response.getReplyParameters().get("Message"), is("PostCreated"));
72 assertThat(response.getReplyParameters().get("Post"), is(capturingPostCreated.post.getId()));
73 assertThat(capturingPostCreated.post.getSone(), is(sone));
74 assertThat(capturingPostCreated.post.getRecipientId(), is(of("OtherSone")));
75 assertThat(capturingPostCreated.post.getText(), is("Text of the post."));
76 assertThat(capturingPostCreated.post.getTime(), allOf(greaterThanOrEqualTo(now), lessThanOrEqualTo(currentTimeMillis())));
80 public void verifyThatCreatingAPostWithoutRecipientWorks() throws FcpException {
81 Sone sone = mocks.mockSone("Sone").local().create();
82 CapturingPostCreated capturingPostCreated = new CapturingPostCreated();
83 when(mocks.core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
85 SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
87 .put("Text", "Text of the post.")
89 Response response = createPostCommand.execute(createPostFieldSet, null, DIRECT);
90 assertThat(response, notNullValue());
91 assertThat(capturingPostCreated.post, notNullValue());
92 assertThat(response.getReplyParameters().get("Message"), is("PostCreated"));
93 assertThat(response.getReplyParameters().get("Post"), is(capturingPostCreated.post.getId()));
94 assertThat(capturingPostCreated.post.getSone(), is(sone));
95 assertThat(capturingPostCreated.post.getRecipientId(), is(Optional.<String>absent()));
96 assertThat(capturingPostCreated.post.getText(), is("Text of the post."));
97 assertThat(capturingPostCreated.post.getTime(), allOf(greaterThanOrEqualTo(now), lessThanOrEqualTo(currentTimeMillis())));
101 public void verifyThatCreatingAPostDirectedToTheSendingSoneCausesAnError() throws FcpException {
102 mocks.mockSone("Sone").local().create();
103 CapturingPostCreated capturingPostCreated = new CapturingPostCreated();
104 when(mocks.core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
106 SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
108 .put("Recipient", "Sone")
109 .put("Text", "Text of the post.")
111 Response response = createPostCommand.execute(createPostFieldSet, null, DIRECT);
112 assertThat(response, notNullValue());
113 assertThat(response.getReplyParameters().get("Message"), is("Error"));
114 assertThat(capturingPostCreated.post, nullValue());
117 @Test(expected = FcpException.class)
118 public void verifyThatCreatingAPostWithoutTextCausesAnError() throws FcpException {
119 mocks.mockSone("Sone").create();
120 CapturingPostCreated capturingPostCreated = new CapturingPostCreated();
121 when(mocks.core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
123 SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
126 createPostCommand.execute(createPostFieldSet, null, DIRECT);
129 @Test(expected = FcpException.class)
130 public void verifyThatCreatingAPostWithoutSoneCausesAnError() throws FcpException {
131 mocks.mockSone("Sone").local().create();
132 CapturingPostCreated capturingPostCreated = new CapturingPostCreated();
133 when(mocks.core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
135 SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
136 .put("Text", "Text of the post.")
138 createPostCommand.execute(createPostFieldSet, null, DIRECT);
141 private static class CapturingPostCreated implements PostCreated {
146 public void postCreated(Post post) {