a424079976ae7b9532b8aa7dfc217472424b6899
[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.fcp.Verifiers.verifyAnswer;
24 import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT;
25 import static org.hamcrest.MatcherAssert.assertThat;
26 import static org.hamcrest.Matchers.allOf;
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.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                 verifyAnswer(response, "PostCreated");
70                 assertThat(response.getReplyParameters().get("Post"), is(capturingPostCreated.post.getId()));
71                 assertThat(capturingPostCreated.post.getSone(), is(sone));
72                 assertThat(capturingPostCreated.post.getRecipientId(), is(of("OtherSone")));
73                 assertThat(capturingPostCreated.post.getText(), is("Text of the post."));
74                 assertThat(capturingPostCreated.post.getTime(), allOf(greaterThanOrEqualTo(now), lessThanOrEqualTo(currentTimeMillis())));
75         }
76
77         @Test
78         public void verifyThatCreatingAPostWithoutRecipientWorks() throws FcpException {
79                 Sone sone = mocks.mockSone("Sone").local().create();
80                 CapturingPostCreated capturingPostCreated = new CapturingPostCreated();
81                 when(mocks.core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
82
83                 SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
84                                 .put("Sone", "Sone")
85                                 .put("Text", "Text of the post.")
86                                 .get();
87                 Response response = createPostCommand.execute(createPostFieldSet, null, DIRECT);
88                 verifyAnswer(response, "PostCreated");
89                 assertThat(response.getReplyParameters().get("Post"), is(capturingPostCreated.post.getId()));
90                 assertThat(capturingPostCreated.post.getSone(), is(sone));
91                 assertThat(capturingPostCreated.post.getRecipientId(), is(Optional.<String>absent()));
92                 assertThat(capturingPostCreated.post.getText(), is("Text of the post."));
93                 assertThat(capturingPostCreated.post.getTime(), allOf(greaterThanOrEqualTo(now), lessThanOrEqualTo(currentTimeMillis())));
94         }
95
96         @Test
97         public void verifyThatCreatingAPostDirectedToTheSendingSoneCausesAnError() throws FcpException {
98                 mocks.mockSone("Sone").local().create();
99                 CapturingPostCreated capturingPostCreated = new CapturingPostCreated();
100                 when(mocks.core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
101
102                 SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
103                                 .put("Sone", "Sone")
104                                 .put("Recipient", "Sone")
105                                 .put("Text", "Text of the post.")
106                                 .get();
107                 Response response = createPostCommand.execute(createPostFieldSet, null, DIRECT);
108                 verifyAnswer(response, "Error");
109                 assertThat(capturingPostCreated.post, nullValue());
110         }
111
112         @Test(expected = FcpException.class)
113         public void verifyThatCreatingAPostWithoutTextCausesAnError() throws FcpException {
114                 mocks.mockSone("Sone").create();
115                 CapturingPostCreated capturingPostCreated = new CapturingPostCreated();
116                 when(mocks.core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
117
118                 SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
119                                 .put("Sone", "Sone")
120                                 .get();
121                 createPostCommand.execute(createPostFieldSet, null, DIRECT);
122         }
123
124         @Test(expected = FcpException.class)
125         public void verifyThatCreatingAPostWithoutSoneCausesAnError() throws FcpException {
126                 mocks.mockSone("Sone").local().create();
127                 CapturingPostCreated capturingPostCreated = new CapturingPostCreated();
128                 when(mocks.core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
129
130                 SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
131                                 .put("Text", "Text of the post.")
132                                 .get();
133                 createPostCommand.execute(createPostFieldSet, null, DIRECT);
134         }
135
136         private static class CapturingPostCreated implements PostCreated {
137
138                 public Post post;
139
140                 @Override
141                 public void postCreated(Post post) {
142                         this.post = post;
143                 }
144
145         }
146
147 }