2 * Sone - CreateReplyCommandTest.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 java.lang.System.currentTimeMillis;
21 import static net.pterodactylus.sone.fcp.Verifiers.verifyAnswer;
22 import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT;
23 import static org.hamcrest.MatcherAssert.assertThat;
24 import static org.hamcrest.Matchers.greaterThanOrEqualTo;
25 import static org.hamcrest.Matchers.is;
26 import static org.hamcrest.Matchers.lessThanOrEqualTo;
27 import static org.hamcrest.Matchers.notNullValue;
28 import static org.hamcrest.core.AllOf.allOf;
29 import static org.mockito.Mockito.when;
31 import net.pterodactylus.sone.data.Mocks;
32 import net.pterodactylus.sone.data.PostReply;
33 import net.pterodactylus.sone.data.Sone;
34 import net.pterodactylus.sone.database.PostReplyBuilder.PostReplyCreated;
35 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
36 import net.pterodactylus.sone.freenet.fcp.Command.Response;
37 import net.pterodactylus.sone.freenet.fcp.FcpException;
39 import freenet.support.SimpleFieldSet;
41 import com.google.common.base.Optional;
42 import org.junit.Test;
45 * Unit test for {@link CreateReplyCommand}.
47 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
49 public class CreateReplyCommandTest {
51 private final long now = currentTimeMillis();
52 private final Mocks mocks = new Mocks();
53 private final CreateReplyCommand createReplyCommand = new CreateReplyCommand(mocks.core);
56 public void verifyThatCreatingAFullySpecifiedReplyWorks() throws FcpException {
57 Sone sone = mocks.mockSone("SoneId").local().create();
58 mocks.mockPost(sone, "PostId").create();
59 CapturingPostReplyCreated capturingPostReplyCreated = new CapturingPostReplyCreated();
60 when(mocks.core.postReplyCreated()).thenReturn(Optional.<PostReplyCreated>of(capturingPostReplyCreated));
61 SimpleFieldSet createReplyFieldSet = new SimpleFieldSetBuilder()
62 .put("Message", "CreateReply")
63 .put("Sone", "SoneId")
64 .put("Post", "PostId")
65 .put("Text", "Text of the reply.")
67 Response response = createReplyCommand.execute(createReplyFieldSet, null, DIRECT);
68 verifyAnswer(response, "ReplyCreated");
69 assertThat(capturingPostReplyCreated.postReply, notNullValue());
70 assertThat(capturingPostReplyCreated.postReply.getId(), notNullValue());
71 assertThat(capturingPostReplyCreated.postReply.getPostId(), is("PostId"));
72 assertThat(capturingPostReplyCreated.postReply.getSone(), is(sone));
73 assertThat(capturingPostReplyCreated.postReply.getTime(), allOf(greaterThanOrEqualTo(now), lessThanOrEqualTo(currentTimeMillis())));
74 assertThat(capturingPostReplyCreated.postReply.getText(), is("Text of the reply."));
75 assertThat(response.getReplyParameters().get("Reply"), is(capturingPostReplyCreated.postReply.getId()));
78 @Test(expected = FcpException.class)
79 public void verifyThatCreatingAReplyWithoutSoneCausesAnError() throws FcpException {
80 Sone sone = mocks.mockSone("SoneId").local().create();
81 mocks.mockPost(sone, "PostId").create();
82 CapturingPostReplyCreated capturingPostReplyCreated = new CapturingPostReplyCreated();
83 when(mocks.core.postReplyCreated()).thenReturn(Optional.<PostReplyCreated>of(capturingPostReplyCreated));
84 SimpleFieldSet createReplyFieldSet = new SimpleFieldSetBuilder()
85 .put("Message", "CreateReply")
86 .put("Post", "PostId")
87 .put("Text", "Text of the reply.")
89 createReplyCommand.execute(createReplyFieldSet, null, DIRECT);
92 @Test(expected = FcpException.class)
93 public void verifyThatCreatingAReplyWithoutPostCausesAnError() throws FcpException {
94 mocks.mockSone("SoneId").local().create();
95 CapturingPostReplyCreated capturingPostReplyCreated = new CapturingPostReplyCreated();
96 when(mocks.core.postReplyCreated()).thenReturn(Optional.<PostReplyCreated>of(capturingPostReplyCreated));
97 SimpleFieldSet createReplyFieldSet = new SimpleFieldSetBuilder()
98 .put("Message", "CreateReply")
99 .put("Sone", "SoneId")
100 .put("Text", "Text of the reply.")
102 createReplyCommand.execute(createReplyFieldSet, null, DIRECT);
105 @Test(expected = FcpException.class)
106 public void verifyThatCreatingAReplyWithoutTextCausesAnError() throws FcpException {
107 Sone sone = mocks.mockSone("SoneId").local().create();
108 mocks.mockPost(sone, "PostId").create();
109 CapturingPostReplyCreated capturingPostReplyCreated = new CapturingPostReplyCreated();
110 when(mocks.core.postReplyCreated()).thenReturn(Optional.<PostReplyCreated>of(capturingPostReplyCreated));
111 SimpleFieldSet createReplyFieldSet = new SimpleFieldSetBuilder()
112 .put("Message", "CreateReply")
113 .put("Sone", "SoneId")
114 .put("Post", "PostId")
116 createReplyCommand.execute(createReplyFieldSet, null, DIRECT);
119 private class CapturingPostReplyCreated implements PostReplyCreated {
121 public PostReply postReply;
124 public void postReplyCreated(PostReply postReply) {
125 this.postReply = postReply;