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