610ed6cab8fe4312044f226c932d1798d07a5bbf
[Sone.git] / src / test / java / net / pterodactylus / sone / fcp / LikeReplyCommandTest.java
1 /*
2  * Sone - LikeReplyCommandTest.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.fcp;
19
20 import static net.pterodactylus.sone.fcp.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;
27
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;
34
35 import freenet.node.FSParseException;
36 import freenet.support.SimpleFieldSet;
37
38 import org.junit.Test;
39
40 /**
41  * Unit test for {@link LikeReplyCommand}.
42  *
43  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44  */
45 public class LikeReplyCommandTest {
46
47         private final Mocks mocks = new Mocks();
48         private final LikeReplyCommand likeReplyCommand = new LikeReplyCommand(mocks.core);
49
50         @Test
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")
56                                 .put("Sone", "Sone")
57                                 .put("Reply", "Reply")
58                                 .get();
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));
63         }
64
65         @Test
66         public void likingALikedReplyDoesNotIncreaseTheLikeCount() throws FcpException, FSParseException {
67                 Sone sone = mocks.mockSone("Sone").local().create();
68                 PostReply postReply = mocks.mockPostReply(sone, "Reply").create();
69                 postReply.like(sone);
70                 SimpleFieldSet likeReplyFieldSet = new SimpleFieldSetBuilder()
71                                 .put("Message", "LikeReply")
72                                 .put("Sone", "Sone")
73                                 .put("Reply", "Reply")
74                                 .get();
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));
79         }
80
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")
87                                 .get();
88                 likeReplyCommand.execute(likeReplyFieldSet, null, DIRECT);
89         }
90
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")
96                                 .get();
97                 likeReplyCommand.execute(likeReplyFieldSet, null, DIRECT);
98         }
99
100         @Test(expected = FcpException.class)
101         public void nonExistingReplyCausesAnError() throws FcpException, FSParseException {
102                 SimpleFieldSet likeReplyFieldSet = new SimpleFieldSetBuilder()
103                                 .put("Message", "LikeReply")
104                                 .put("Sone", "Sone")
105                                 .put("Reply", "FalseReply")
106                                 .get();
107                 likeReplyCommand.execute(likeReplyFieldSet, null, DIRECT);
108         }
109
110         @Test(expected = FcpException.class)
111         public void missingReplyFieldCausesAnError() throws FcpException, FSParseException {
112                 SimpleFieldSet likeReplyFieldSet = new SimpleFieldSetBuilder()
113                                 .put("Message", "LikeReply")
114                                 .put("Sone", "Sone")
115                                 .get();
116                 likeReplyCommand.execute(likeReplyFieldSet, null, DIRECT);
117         }
118
119 }