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