f9cb1abeca7bd16210530d6bbf8fa6e7b04f598d
[Sone.git] / src / test / java / net / pterodactylus / sone / fcp / CreatePostCommandTest.java
1 /*
2  * Sone - CreatePostCommandTest.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 com.google.common.base.Optional.of;
21 import static java.lang.System.currentTimeMillis;
22 import static net.pterodactylus.sone.database.PostBuilder.PostCreated;
23 import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT;
24 import static org.hamcrest.MatcherAssert.assertThat;
25 import static org.hamcrest.Matchers.allOf;
26 import static org.hamcrest.Matchers.greaterThanOrEqualTo;
27 import static org.hamcrest.Matchers.is;
28 import static org.hamcrest.Matchers.lessThanOrEqualTo;
29 import static org.hamcrest.Matchers.notNullValue;
30 import static org.hamcrest.Matchers.nullValue;
31 import static org.mockito.Mockito.when;
32
33 import net.pterodactylus.sone.data.Mocks;
34 import net.pterodactylus.sone.data.Post;
35 import net.pterodactylus.sone.data.Sone;
36 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
37 import net.pterodactylus.sone.freenet.fcp.Command.Response;
38 import net.pterodactylus.sone.freenet.fcp.FcpException;
39
40 import freenet.support.SimpleFieldSet;
41
42 import com.google.common.base.Optional;
43 import org.junit.Test;
44
45 /**
46  * Unit test for {@link CreatePostCommand}.
47  *
48  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
49  */
50 public class CreatePostCommandTest {
51
52         private final long now = currentTimeMillis();
53         private final Mocks mocks = new Mocks();
54         private final CreatePostCommand createPostCommand = new CreatePostCommand(mocks.core);
55
56         @Test
57         public void verifyThatCreatingAPostWorks() throws FcpException {
58                 Sone sone = mocks.mockSone("Sone").local().create();
59                 mocks.mockSone("OtherSone").local().create();
60                 CapturingPostCreated capturingPostCreated = new CapturingPostCreated();
61                 when(mocks.core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
62
63                 SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
64                                 .put("Sone", "Sone")
65                                 .put("Text", "Text of the post.")
66                                 .put("Recipient", "OtherSone")
67                                 .get();
68                 Response response = createPostCommand.execute(createPostFieldSet, null, DIRECT);
69                 assertThat(response, notNullValue());
70                 assertThat(capturingPostCreated.post, notNullValue());
71                 assertThat(response.getReplyParameters().get("Message"), is("PostCreated"));
72                 assertThat(response.getReplyParameters().get("Post"), is(capturingPostCreated.post.getId()));
73                 assertThat(capturingPostCreated.post.getSone(), is(sone));
74                 assertThat(capturingPostCreated.post.getRecipientId(), is(of("OtherSone")));
75                 assertThat(capturingPostCreated.post.getText(), is("Text of the post."));
76                 assertThat(capturingPostCreated.post.getTime(), allOf(greaterThanOrEqualTo(now), lessThanOrEqualTo(currentTimeMillis())));
77         }
78
79         @Test
80         public void verifyThatCreatingAPostWithoutRecipientWorks() throws FcpException {
81                 Sone sone = mocks.mockSone("Sone").local().create();
82                 CapturingPostCreated capturingPostCreated = new CapturingPostCreated();
83                 when(mocks.core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
84
85                 SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
86                                 .put("Sone", "Sone")
87                                 .put("Text", "Text of the post.")
88                                 .get();
89                 Response response = createPostCommand.execute(createPostFieldSet, null, DIRECT);
90                 assertThat(response, notNullValue());
91                 assertThat(capturingPostCreated.post, notNullValue());
92                 assertThat(response.getReplyParameters().get("Message"), is("PostCreated"));
93                 assertThat(response.getReplyParameters().get("Post"), is(capturingPostCreated.post.getId()));
94                 assertThat(capturingPostCreated.post.getSone(), is(sone));
95                 assertThat(capturingPostCreated.post.getRecipientId(), is(Optional.<String>absent()));
96                 assertThat(capturingPostCreated.post.getText(), is("Text of the post."));
97                 assertThat(capturingPostCreated.post.getTime(), allOf(greaterThanOrEqualTo(now), lessThanOrEqualTo(currentTimeMillis())));
98         }
99
100         @Test
101         public void verifyThatCreatingAPostDirectedToTheSendingSoneCausesAnError() throws FcpException {
102                 mocks.mockSone("Sone").local().create();
103                 CapturingPostCreated capturingPostCreated = new CapturingPostCreated();
104                 when(mocks.core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
105
106                 SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
107                                 .put("Sone", "Sone")
108                                 .put("Recipient", "Sone")
109                                 .put("Text", "Text of the post.")
110                                 .get();
111                 Response response = createPostCommand.execute(createPostFieldSet, null, DIRECT);
112                 assertThat(response, notNullValue());
113                 assertThat(response.getReplyParameters().get("Message"), is("Error"));
114                 assertThat(capturingPostCreated.post, nullValue());
115         }
116
117         @Test(expected = FcpException.class)
118         public void verifyThatCreatingAPostWithoutTextCausesAnError() throws FcpException {
119                 mocks.mockSone("Sone").create();
120                 CapturingPostCreated capturingPostCreated = new CapturingPostCreated();
121                 when(mocks.core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
122
123                 SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
124                                 .put("Sone", "Sone")
125                                 .get();
126                 createPostCommand.execute(createPostFieldSet, null, DIRECT);
127         }
128
129         @Test(expected = FcpException.class)
130         public void verifyThatCreatingAPostWithoutSoneCausesAnError() throws FcpException {
131                 mocks.mockSone("Sone").local().create();
132                 CapturingPostCreated capturingPostCreated = new CapturingPostCreated();
133                 when(mocks.core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
134
135                 SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
136                                 .put("Text", "Text of the post.")
137                                 .get();
138                 createPostCommand.execute(createPostFieldSet, null, DIRECT);
139         }
140
141         private static class CapturingPostCreated implements PostCreated {
142
143                 public Post post;
144
145                 @Override
146                 public void postCreated(Post post) {
147                         this.post = post;
148                 }
149
150         }
151
152 }