2 * Sone - LikePostCommandTest.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.Post;
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 LikePostCommand}.
43 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45 public class LikePostCommandTest {
47 private final Mocks mocks = new Mocks();
48 private final LikePostCommand likePostCommand = new LikePostCommand(mocks.core);
51 public void thePostWasLiked() throws FcpException, FSParseException {
52 Sone sone = mocks.mockSone("LSone").local().create();
53 Post post = mocks.mockPost(sone, "Post").create();
54 SimpleFieldSet likePostFieldSet = new SimpleFieldSetBuilder()
55 .put("Message", "LikePost")
59 Response response = likePostCommand.execute(likePostFieldSet, null, DIRECT);
60 verifyAnswer(response, "PostLiked");
61 assertThat(response.getReplyParameters().getInt("LikeCount"), is(1));
62 verify(post).like(eq(sone));
65 @Test(expected = FcpException.class)
66 public void nonExistingLocalSoneCausesAnError() throws FcpException, FSParseException {
67 Sone sone = mocks.mockSone("LSone").local().create();
68 mocks.mockPost(sone, "Post").create();
69 SimpleFieldSet likePostFieldSet = new SimpleFieldSetBuilder()
70 .put("Message", "LikePost")
74 likePostCommand.execute(likePostFieldSet, null, DIRECT);
77 @Test(expected = FcpException.class)
78 public void missingLocalSoneFieldCausesAnError() throws FcpException, FSParseException {
79 Sone sone = mocks.mockSone("LSone").local().create();
80 mocks.mockPost(sone, "Post").create();
81 SimpleFieldSet likePostFieldSet = new SimpleFieldSetBuilder()
82 .put("Message", "LikePost")
85 likePostCommand.execute(likePostFieldSet, null, DIRECT);
88 @Test(expected = FcpException.class)
89 public void nonExistingPostCausesAnError() throws FcpException, FSParseException {
90 mocks.mockSone("LSone").local().create();
91 SimpleFieldSet likePostFieldSet = new SimpleFieldSetBuilder()
92 .put("Message", "LikePost")
96 likePostCommand.execute(likePostFieldSet, null, DIRECT);
99 @Test(expected = FcpException.class)
100 public void missingPostFieldCausesAnError() throws FcpException, FSParseException {
101 mocks.mockSone("LSone").local().create();
102 SimpleFieldSet likePostFieldSet = new SimpleFieldSetBuilder()
103 .put("Message", "LikePost")
104 .put("Sone", "LSone")
106 likePostCommand.execute(likePostFieldSet, null, DIRECT);
110 public void multipleLikesDontCountMultipleTimes() throws FcpException, FSParseException {
111 Sone sone = mocks.mockSone("LSone").local().create();
112 Post post = mocks.mockPost(sone, "Post").create();
114 SimpleFieldSet likePostFieldSet = new SimpleFieldSetBuilder()
115 .put("Message", "LikePost")
116 .put("Sone", "LSone")
119 Response response = likePostCommand.execute(likePostFieldSet, null, DIRECT);
120 verifyAnswer(response, "PostLiked");
121 assertThat(response.getReplyParameters().getInt("LikeCount"), is(1));
122 verify(post, times(2)).like(eq(sone));