Require a Sone when mocking a post.
[Sone.git] / src / test / java / net / pterodactylus / sone / fcp / CreateReplyCommandTest.java
1 /*
2  * Sone - CreateReplyCommandTest.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 java.lang.System.currentTimeMillis;
21 import static net.pterodactylus.sone.data.Mocks.mockCore;
22 import static net.pterodactylus.sone.data.Mocks.mockDatabase;
23 import static net.pterodactylus.sone.data.Mocks.mockLocalSone;
24 import static net.pterodactylus.sone.data.Mocks.mockPost;
25 import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT;
26 import static org.hamcrest.MatcherAssert.assertThat;
27 import static org.hamcrest.Matchers.greaterThanOrEqualTo;
28 import static org.hamcrest.Matchers.is;
29 import static org.hamcrest.Matchers.lessThanOrEqualTo;
30 import static org.hamcrest.Matchers.notNullValue;
31 import static org.hamcrest.core.AllOf.allOf;
32 import static org.mockito.Mockito.when;
33
34 import net.pterodactylus.sone.core.Core;
35 import net.pterodactylus.sone.data.PostReply;
36 import net.pterodactylus.sone.data.Sone;
37 import net.pterodactylus.sone.database.Database;
38 import net.pterodactylus.sone.database.PostReplyBuilder.PostReplyCreated;
39 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
40 import net.pterodactylus.sone.freenet.fcp.Command.Response;
41 import net.pterodactylus.sone.freenet.fcp.FcpException;
42
43 import freenet.support.SimpleFieldSet;
44
45 import com.google.common.base.Optional;
46 import org.junit.Test;
47
48 /**
49  * Unit test for {@link CreateReplyCommand}.
50  *
51  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
52  */
53 public class CreateReplyCommandTest {
54
55         private final long now = currentTimeMillis();
56         private final Database database = mockDatabase();
57         private final Core core = mockCore(database);
58         private final CreateReplyCommand createReplyCommand = new CreateReplyCommand(core);
59
60         @Test
61         public void verifyThatCreatingAFullySpecifiedReplyWorks() throws FcpException {
62                 Sone sone = mockLocalSone(core, "SoneId");
63                 mockPost(core, sone, "PostId");
64                 CapturingPostReplyCreated capturingPostReplyCreated = new CapturingPostReplyCreated();
65                 when(core.postReplyCreated()).thenReturn(Optional.<PostReplyCreated>of(capturingPostReplyCreated));
66                 SimpleFieldSet createReplyFieldSet = new SimpleFieldSetBuilder()
67                                 .put("Message", "CreateReply")
68                                 .put("Sone", "SoneId")
69                                 .put("Post", "PostId")
70                                 .put("Text", "Text of the reply.")
71                                 .get();
72                 Response response = createReplyCommand.execute(createReplyFieldSet, null, DIRECT);
73                 assertThat(response, notNullValue());
74                 assertThat(capturingPostReplyCreated.postReply, notNullValue());
75                 assertThat(capturingPostReplyCreated.postReply.getId(), notNullValue());
76                 assertThat(capturingPostReplyCreated.postReply.getPostId(), is("PostId"));
77                 assertThat(capturingPostReplyCreated.postReply.getSone(), is(sone));
78                 assertThat(capturingPostReplyCreated.postReply.getTime(), allOf(greaterThanOrEqualTo(now), lessThanOrEqualTo(currentTimeMillis())));
79                 assertThat(capturingPostReplyCreated.postReply.getText(), is("Text of the reply."));
80                 assertThat(response.getReplyParameters(), notNullValue());
81                 assertThat(response.getReplyParameters().get("Message"), is("ReplyCreated"));
82                 assertThat(response.getReplyParameters().get("Reply"), is(capturingPostReplyCreated.postReply.getId()));
83         }
84
85         @Test(expected = FcpException.class)
86         public void verifyThatCreatingAReplyWithoutSoneCausesAnError() throws FcpException {
87                 Sone sone = mockLocalSone(core, "SoneId");
88                 mockPost(core, sone, "PostId");
89                 CapturingPostReplyCreated capturingPostReplyCreated = new CapturingPostReplyCreated();
90                 when(core.postReplyCreated()).thenReturn(Optional.<PostReplyCreated>of(capturingPostReplyCreated));
91                 SimpleFieldSet createReplyFieldSet = new SimpleFieldSetBuilder()
92                                 .put("Message", "CreateReply")
93                                 .put("Post", "PostId")
94                                 .put("Text", "Text of the reply.")
95                                 .get();
96                 createReplyCommand.execute(createReplyFieldSet, null, DIRECT);
97         }
98
99         @Test(expected = FcpException.class)
100         public void verifyThatCreatingAReplyWithoutPostCausesAnError() throws FcpException {
101                 mockLocalSone(core, "SoneId");
102                 CapturingPostReplyCreated capturingPostReplyCreated = new CapturingPostReplyCreated();
103                 when(core.postReplyCreated()).thenReturn(Optional.<PostReplyCreated>of(capturingPostReplyCreated));
104                 SimpleFieldSet createReplyFieldSet = new SimpleFieldSetBuilder()
105                                 .put("Message", "CreateReply")
106                                 .put("Sone", "SoneId")
107                                 .put("Text", "Text of the reply.")
108                                 .get();
109                 createReplyCommand.execute(createReplyFieldSet, null, DIRECT);
110         }
111
112         @Test(expected = FcpException.class)
113         public void verifyThatCreatingAReplyWithoutTextCausesAnError() throws FcpException {
114                 Sone sone = mockLocalSone(core, "SoneId");
115                 mockPost(core, sone, "PostId");
116                 CapturingPostReplyCreated capturingPostReplyCreated = new CapturingPostReplyCreated();
117                 when(core.postReplyCreated()).thenReturn(Optional.<PostReplyCreated>of(capturingPostReplyCreated));
118                 SimpleFieldSet createReplyFieldSet = new SimpleFieldSetBuilder()
119                                 .put("Message", "CreateReply")
120                                 .put("Sone", "SoneId")
121                                 .put("Post", "PostId")
122                                 .get();
123                 createReplyCommand.execute(createReplyFieldSet, null, DIRECT);
124         }
125
126         private class CapturingPostReplyCreated implements PostReplyCreated {
127
128                 public PostReply postReply;
129
130                 @Override
131                 public void postReplyCreated(PostReply postReply) {
132                         this.postReply = postReply;
133                 }
134
135         }
136 }