Add unit test for CreateReplyCommand.
[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, "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                 mockPost(core, "PostId");
88                 CapturingPostReplyCreated capturingPostReplyCreated = new CapturingPostReplyCreated();
89                 when(core.postReplyCreated()).thenReturn(Optional.<PostReplyCreated>of(capturingPostReplyCreated));
90                 SimpleFieldSet createReplyFieldSet = new SimpleFieldSetBuilder()
91                                 .put("Message", "CreateReply")
92                                 .put("Post", "PostId")
93                                 .put("Text", "Text of the reply.")
94                                 .get();
95                 createReplyCommand.execute(createReplyFieldSet, null, DIRECT);
96         }
97
98         @Test(expected = FcpException.class)
99         public void verifyThatCreatingAReplyWithoutPostCausesAnError() throws FcpException {
100                 mockLocalSone(core, "SoneId");
101                 CapturingPostReplyCreated capturingPostReplyCreated = new CapturingPostReplyCreated();
102                 when(core.postReplyCreated()).thenReturn(Optional.<PostReplyCreated>of(capturingPostReplyCreated));
103                 SimpleFieldSet createReplyFieldSet = new SimpleFieldSetBuilder()
104                                 .put("Message", "CreateReply")
105                                 .put("Sone", "SoneId")
106                                 .put("Text", "Text of the reply.")
107                                 .get();
108                 createReplyCommand.execute(createReplyFieldSet, null, DIRECT);
109         }
110
111         @Test(expected = FcpException.class)
112         public void verifyThatCreatingAReplyWithoutTextCausesAnError() throws FcpException {
113                 mockLocalSone(core, "SoneId");
114                 mockPost(core, "PostId");
115                 CapturingPostReplyCreated capturingPostReplyCreated = new CapturingPostReplyCreated();
116                 when(core.postReplyCreated()).thenReturn(Optional.<PostReplyCreated>of(capturingPostReplyCreated));
117                 SimpleFieldSet createReplyFieldSet = new SimpleFieldSetBuilder()
118                                 .put("Message", "CreateReply")
119                                 .put("Sone", "SoneId")
120                                 .put("Post", "PostId")
121                                 .get();
122                 createReplyCommand.execute(createReplyFieldSet, null, DIRECT);
123         }
124
125         private class CapturingPostReplyCreated implements PostReplyCreated {
126
127                 public PostReply postReply;
128
129                 @Override
130                 public void postReplyCreated(PostReply postReply) {
131                         this.postReply = postReply;
132                 }
133
134         }
135 }