Move verifiers to different package.
[Sone.git] / src / test / java / net / pterodactylus / sone / fcp / LikePostCommandTest.java
1 /*
2  * Sone - LikePostCommandTest.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.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.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;
34
35 import freenet.node.FSParseException;
36 import freenet.support.SimpleFieldSet;
37
38 import org.junit.Test;
39
40 /**
41  * Unit test for {@link LikePostCommand}.
42  *
43  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44  */
45 public class LikePostCommandTest {
46
47         private final Mocks mocks = new Mocks();
48         private final LikePostCommand likePostCommand = new LikePostCommand(mocks.core);
49
50         @Test
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")
56                                 .put("Sone", "LSone")
57                                 .put("Post", "Post")
58                                 .get();
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));
63         }
64
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")
71                                 .put("Sone", "Sone")
72                                 .put("Post", "Post")
73                                 .get();
74                 likePostCommand.execute(likePostFieldSet, null, DIRECT);
75         }
76
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")
83                                 .put("Post", "Post")
84                                 .get();
85                 likePostCommand.execute(likePostFieldSet, null, DIRECT);
86         }
87
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")
93                                 .put("Sone", "LSone")
94                                 .put("Post", "Post")
95                                 .get();
96                 likePostCommand.execute(likePostFieldSet, null, DIRECT);
97         }
98
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")
105                                 .get();
106                 likePostCommand.execute(likePostFieldSet, null, DIRECT);
107         }
108
109         @Test
110         public void multipleLikesDontCountMultipleTimes() throws FcpException, FSParseException {
111                 Sone sone = mocks.mockSone("LSone").local().create();
112                 Post post = mocks.mockPost(sone, "Post").create();
113                 post.like(sone);
114                 SimpleFieldSet likePostFieldSet = new SimpleFieldSetBuilder()
115                                 .put("Message", "LikePost")
116                                 .put("Sone", "LSone")
117                                 .put("Post", "Post")
118                                 .get();
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));
123         }
124
125 }