2 * Sone - LikeReplyCommandTest.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 net.pterodactylus.sone.Verifiers.verifyAnswer;
21 import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT;
22 import static org.hamcrest.MatcherAssert.assertThat;
23 import static org.hamcrest.Matchers.is;
24 import static org.mockito.Matchers.eq;
25 import static org.mockito.Mockito.times;
26 import static org.mockito.Mockito.verify;
28 import net.pterodactylus.sone.data.Mocks;
29 import net.pterodactylus.sone.data.PostReply;
30 import net.pterodactylus.sone.data.Sone;
31 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
32 import net.pterodactylus.sone.freenet.fcp.Command.Response;
33 import net.pterodactylus.sone.freenet.fcp.FcpException;
35 import freenet.node.FSParseException;
36 import freenet.support.SimpleFieldSet;
38 import org.junit.Test;
41 * Unit test for {@link LikeReplyCommand}.
43 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45 public class LikeReplyCommandTest {
47 private final Mocks mocks = new Mocks();
48 private final LikeReplyCommand likeReplyCommand = new LikeReplyCommand(mocks.core);
51 public void theReplyWasLiked() throws FcpException, FSParseException {
52 Sone sone = mocks.mockSone("Sone").local().create();
53 PostReply postReply = mocks.mockPostReply(sone, "Reply").create();
54 SimpleFieldSet likeReplyFieldSet = new SimpleFieldSetBuilder()
55 .put("Message", "LikeReply")
57 .put("Reply", "Reply")
59 Response response = likeReplyCommand.execute(likeReplyFieldSet, null, DIRECT);
60 verifyAnswer(response, "ReplyLiked");
61 verify(postReply).like(eq(sone));
62 assertThat(response.getReplyParameters().getInt("LikeCount"), is(1));
66 public void likingALikedReplyDoesNotIncreaseTheLikeCount() throws FcpException, FSParseException {
67 Sone sone = mocks.mockSone("Sone").local().create();
68 PostReply postReply = mocks.mockPostReply(sone, "Reply").create();
70 SimpleFieldSet likeReplyFieldSet = new SimpleFieldSetBuilder()
71 .put("Message", "LikeReply")
73 .put("Reply", "Reply")
75 Response response = likeReplyCommand.execute(likeReplyFieldSet, null, DIRECT);
76 verifyAnswer(response, "ReplyLiked");
77 verify(postReply, times(2)).like(eq(sone));
78 assertThat(response.getReplyParameters().getInt("LikeCount"), is(1));
81 @Test(expected = FcpException.class)
82 public void nonExistingSoneCausesAnError() throws FcpException, FSParseException {
83 SimpleFieldSet likeReplyFieldSet = new SimpleFieldSetBuilder()
84 .put("Message", "LikeReply")
85 .put("Sone", "FalseSone")
86 .put("Reply", "Reply")
88 likeReplyCommand.execute(likeReplyFieldSet, null, DIRECT);
91 @Test(expected = FcpException.class)
92 public void missingSoneFieldCausesAnError() throws FcpException, FSParseException {
93 SimpleFieldSet likeReplyFieldSet = new SimpleFieldSetBuilder()
94 .put("Message", "LikeReply")
95 .put("Reply", "Reply")
97 likeReplyCommand.execute(likeReplyFieldSet, null, DIRECT);
100 @Test(expected = FcpException.class)
101 public void nonExistingReplyCausesAnError() throws FcpException, FSParseException {
102 SimpleFieldSet likeReplyFieldSet = new SimpleFieldSetBuilder()
103 .put("Message", "LikeReply")
105 .put("Reply", "FalseReply")
107 likeReplyCommand.execute(likeReplyFieldSet, null, DIRECT);
110 @Test(expected = FcpException.class)
111 public void missingReplyFieldCausesAnError() throws FcpException, FSParseException {
112 SimpleFieldSet likeReplyFieldSet = new SimpleFieldSetBuilder()
113 .put("Message", "LikeReply")
116 likeReplyCommand.execute(likeReplyFieldSet, null, DIRECT);