2 * Sone - AbstractSoneCommandTest.java - Copyright © 2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.fcp;
20 import static com.google.common.base.Optional.of;
21 import static java.util.Arrays.asList;
22 import static java.util.UUID.randomUUID;
23 import static net.pterodactylus.sone.fcp.AbstractSoneCommand.encodeSone;
24 import static net.pterodactylus.sone.fcp.AbstractSoneCommand.encodeString;
25 import static net.pterodactylus.sone.template.SoneAccessor.getNiceName;
26 import static org.hamcrest.CoreMatchers.is;
27 import static org.hamcrest.CoreMatchers.notNullValue;
28 import static org.hamcrest.CoreMatchers.nullValue;
29 import static org.hamcrest.MatcherAssert.assertThat;
30 import static org.mockito.Matchers.eq;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.when;
34 import java.util.List;
36 import net.pterodactylus.sone.core.Core;
37 import net.pterodactylus.sone.data.PostReply;
38 import net.pterodactylus.sone.data.Profile;
39 import net.pterodactylus.sone.data.Sone;
40 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
41 import net.pterodactylus.sone.freenet.fcp.FcpException;
43 import freenet.node.FSParseException;
44 import freenet.support.SimpleFieldSet;
45 import freenet.support.api.Bucket;
47 import com.google.common.base.Optional;
48 import org.junit.Test;
49 import org.mockito.Matchers;
52 * Unit test for {@link AbstractSoneCommand}.
54 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
56 public class AbstractSoneCommandTest {
58 private final Core core = mock(Core.class);
59 private final AbstractSoneCommand abstractSoneCommand = new AbstractSoneCommand(core) {
61 public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException {
67 public void testStringEncoding() {
68 String testString = prepareStringToBeEncoded();
70 String encodedString = encodeString(testString);
71 assertThat(encodedString, notNullValue());
72 assertThat(encodedString.length(), is(testString.length() + 3));
75 private String prepareStringToBeEncoded() {
76 StringBuilder testString = new StringBuilder();
77 for (int i = 0; i < 4000; ++i) {
78 testString.append((char) i);
80 return testString.toString();
84 public void testEncodingASone() throws FSParseException {
85 Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
86 SimpleFieldSet soneFieldSet = encodeSone(sone, "Prefix.", Optional.<Sone>absent());
87 assertThat(soneFieldSet, notNullValue());
88 assertThat(soneFieldSet.get("Prefix.Name"), is("test"));
89 assertThat(soneFieldSet.get("Prefix.NiceName"), is("First M. Last"));
90 assertThat(soneFieldSet.getLong("Prefix.LastUpdated"), is(sone.getTime()));
91 assertThat(soneFieldSet.get("Prefix.Followed"), nullValue());
92 assertThat(soneFieldSet.getInt("Prefix.Field.Count"), is(1));
93 assertThat(soneFieldSet.get("Prefix.Field.0.Name"), is("Test1"));
94 assertThat(soneFieldSet.get("Prefix.Field.0.Value"), is("Value1"));
98 public void testEncodingAFollowedSone() throws FSParseException {
99 Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
100 Sone localSone = prepareLocalSoneThatFollowsEverybody();
101 SimpleFieldSet soneFieldSet = encodeSone(sone, "Prefix.", of(localSone));
102 assertThat(soneFieldSet, notNullValue());
103 assertThat(soneFieldSet.get("Prefix.Name"), is("test"));
104 assertThat(soneFieldSet.get("Prefix.NiceName"), is("First M. Last"));
105 assertThat(soneFieldSet.getLong("Prefix.LastUpdated"), is(sone.getTime()));
106 assertThat(soneFieldSet.get("Prefix.Followed"), is("true"));
107 assertThat(soneFieldSet.getInt("Prefix.Field.Count"), is(1));
108 assertThat(soneFieldSet.get("Prefix.Field.0.Name"), is("Test1"));
109 assertThat(soneFieldSet.get("Prefix.Field.0.Value"), is("Value1"));
113 public void testEncodingANotFollowedSone() throws FSParseException {
114 Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
115 Sone localSone = prepareLocalSoneThatFollowsNobody();
116 SimpleFieldSet soneFieldSet = encodeSone(sone, "Prefix.", of(localSone));
117 assertThat(soneFieldSet, notNullValue());
118 assertThat(soneFieldSet.get("Prefix.Name"), is("test"));
119 assertThat(soneFieldSet.get("Prefix.NiceName"), is("First M. Last"));
120 assertThat(soneFieldSet.getLong("Prefix.LastUpdated"), is(sone.getTime()));
121 assertThat(soneFieldSet.get("Prefix.Followed"), is("false"));
122 assertThat(soneFieldSet.getInt("Prefix.Field.Count"), is(1));
123 assertThat(soneFieldSet.get("Prefix.Field.0.Name"), is("Test1"));
124 assertThat(soneFieldSet.get("Prefix.Field.0.Value"), is("Value1"));
127 private Sone prepareLocalSoneThatFollowsEverybody() {
128 Sone sone = mock(Sone.class);
129 when(sone.hasFriend(Matchers.<String>any())).thenReturn(true);
133 private Sone prepareLocalSoneThatFollowsNobody() {
134 Sone sone = mock(Sone.class);
135 when(sone.hasFriend(Matchers.<String>any())).thenReturn(false);
140 public void testEncodingMultipleSones() throws FSParseException {
141 List<Sone> sones = prepareMultipleSones();
142 SimpleFieldSet sonesFieldSet = AbstractSoneCommand.encodeSones(sones, "Prefix.");
143 assertThat(sonesFieldSet, notNullValue());
144 assertThat(sonesFieldSet.getInt("Prefix.Count"), is(sones.size()));
145 assertThat(sonesFieldSet.get("Prefix.0.ID"), is(sones.get(0).getId()));
146 assertThat(sonesFieldSet.get("Prefix.0.Name"), is(sones.get(0).getName()));
147 assertThat(sonesFieldSet.get("Prefix.0.NiceName"), is(getNiceName(sones.get(0))));
148 assertThat(sonesFieldSet.getLong("Prefix.0.Time"), is(sones.get(0).getTime()));
149 assertThat(sonesFieldSet.get("Prefix.1.ID"), is(sones.get(1).getId()));
150 assertThat(sonesFieldSet.get("Prefix.1.Name"), is(sones.get(1).getName()));
151 assertThat(sonesFieldSet.get("Prefix.1.NiceName"), is(getNiceName(sones.get(1))));
152 assertThat(sonesFieldSet.getLong("Prefix.1.Time"), is(sones.get(1).getTime()));
153 assertThat(sonesFieldSet.get("Prefix.2.ID"), is(sones.get(2).getId()));
154 assertThat(sonesFieldSet.get("Prefix.2.Name"), is(sones.get(2).getName()));
155 assertThat(sonesFieldSet.get("Prefix.2.NiceName"), is(getNiceName(sones.get(2))));
156 assertThat(sonesFieldSet.getLong("Prefix.2.Time"), is(sones.get(2).getTime()));
159 private List<Sone> prepareMultipleSones() {
160 Sone sone1 = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test1", "Alpha", "A.", "First", (long) (Math.random() * Long.MAX_VALUE));
161 Sone sone2 = createSone("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg", "Test2", "Beta", "B.", "Second", (long) (Math.random() * Long.MAX_VALUE));
162 Sone sone3 = createSone("-1Q6LhHvx91C1mSjOS3zznRSNUC4OxoHUbhIgBAyW1U", "Test3", "Gamma", "C.", "Third", (long) (Math.random() * Long.MAX_VALUE));
163 return asList(sone1, sone2, sone3);
166 private Sone createSone(String id, String name, String firstName, String middleName, String lastName, long time) {
167 Sone sone = mock(Sone.class);
168 when(sone.getId()).thenReturn(id);
169 when(sone.getName()).thenReturn(name);
170 when(sone.getProfile()).thenReturn(prepareProfile(sone, firstName, middleName, lastName));
171 when(sone.getTime()).thenReturn(time);
175 private Sone createLocalSone(String id, String name, String firstName, String middleName, String lastName, long time) {
176 Sone sone = mock(Sone.class);
177 when(sone.getId()).thenReturn(id);
178 when(sone.getName()).thenReturn(name);
179 when(sone.getProfile()).thenReturn(prepareProfile(sone, firstName, middleName, lastName));
180 when(sone.getTime()).thenReturn(time);
181 when(sone.isLocal()).thenReturn(true);
185 private Profile prepareProfile(Sone sone, String firstName, String middleName, String lastName) {
186 Profile profile = new Profile(sone).modify().setFirstName(firstName).setMiddleName(middleName).setLastName(lastName).update();
187 profile.setField(profile.addField("Test1"), "Value1");
192 public void testEncodingReplies() throws FSParseException {
193 List<PostReply> postReplies = preparePostReplies();
194 SimpleFieldSet postRepliesFieldSet = AbstractSoneCommand.encodeReplies(postReplies, "Prefix.");
195 assertThat(postRepliesFieldSet, notNullValue());
196 assertThat(postRepliesFieldSet.getInt("Prefix.Replies.Count"), is(postReplies.size()));
197 assertThat(postRepliesFieldSet.get("Prefix.Replies.0.ID"), is(postReplies.get(0).getId()));
198 assertThat(postRepliesFieldSet.get("Prefix.Replies.0.Sone"), is(postReplies.get(0).getSone().getId()));
199 assertThat(postRepliesFieldSet.getLong("Prefix.Replies.0.Time"), is(postReplies.get(0).getTime()));
200 assertThat(postRepliesFieldSet.get("Prefix.Replies.0.Text"), is(postReplies.get(0).getText()));
201 assertThat(postRepliesFieldSet.get("Prefix.Replies.1.ID"), is(postReplies.get(1).getId()));
202 assertThat(postRepliesFieldSet.get("Prefix.Replies.1.Sone"), is(postReplies.get(1).getSone().getId()));
203 assertThat(postRepliesFieldSet.getLong("Prefix.Replies.1.Time"), is(postReplies.get(1).getTime()));
204 assertThat(postRepliesFieldSet.get("Prefix.Replies.1.Text"), is(postReplies.get(1).getText()));
205 assertThat(postRepliesFieldSet.get("Prefix.Replies.2.ID"), is(postReplies.get(2).getId()));
206 assertThat(postRepliesFieldSet.get("Prefix.Replies.2.Sone"), is(postReplies.get(2).getSone().getId()));
207 assertThat(postRepliesFieldSet.getLong("Prefix.Replies.2.Time"), is(postReplies.get(2).getTime()));
208 assertThat(postRepliesFieldSet.get("Prefix.Replies.2.Text"), is(postReplies.get(2).getText()));
211 private List<PostReply> preparePostReplies() {
212 Sone sone1 = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test1", "Alpha", "A.", "First", (long) (Math.random() * Long.MAX_VALUE));
213 Sone sone2 = createSone("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg", "Test2", "Beta", "B.", "Second", (long) (Math.random() * Long.MAX_VALUE));
214 Sone sone3 = createSone("-1Q6LhHvx91C1mSjOS3zznRSNUC4OxoHUbhIgBAyW1U", "Test3", "Gamma", "C.", "Third", (long) (Math.random() * Long.MAX_VALUE));
215 PostReply postReply1 = createPostReply(sone1, "Text 1");
216 PostReply postReply2 = createPostReply(sone2, "Text 2");
217 PostReply postReply3 = createPostReply(sone3, "Text 3");
218 return asList(postReply1, postReply2, postReply3);
221 private PostReply createPostReply(Sone sone, String text) {
222 PostReply postReply = mock(PostReply.class);
223 when(postReply.getId()).thenReturn(randomUUID().toString());
224 when(postReply.getSone()).thenReturn(sone);
225 when(postReply.getTime()).thenReturn((long) (Math.random() * Long.MAX_VALUE));
226 when(postReply.getText()).thenReturn(text);
231 public void testEncodingLikes() throws FSParseException {
232 List<Sone> likes = prepareMultipleSones();
233 SimpleFieldSet likesFieldSet = AbstractSoneCommand.encodeLikes(likes, "Prefix.");
234 assertThat(likesFieldSet, notNullValue());
235 assertThat(likesFieldSet.getInt("Prefix.Count"), is(likes.size()));
236 assertThat(likesFieldSet.get("Prefix.0.ID"), is(likes.get(0).getId()));
237 assertThat(likesFieldSet.get("Prefix.1.ID"), is(likes.get(1).getId()));
238 assertThat(likesFieldSet.get("Prefix.2.ID"), is(likes.get(2).getId()));
242 public void testParsingAMandatorySone() throws FcpException {
243 Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
244 when(core.getSone(eq("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E"))).thenReturn(of(sone));
245 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
246 Sone parsedSone = abstractSoneCommand.getMandatorySone(soneFieldSet, "Sone");
247 assertThat(parsedSone, notNullValue());
248 assertThat(parsedSone, is(sone));
251 @Test(expected = FcpException.class)
252 public void testParsingANonExistingMandatorySoneCausesAnError() throws FcpException {
253 when(core.getSone(Matchers.<String>any())).thenReturn(Optional.<Sone>absent());
254 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
255 abstractSoneCommand.getMandatorySone(soneFieldSet, "Sone");
258 @Test(expected = FcpException.class)
259 public void testParsingAMandatorySoneFromANonExistingFieldCausesAnError() throws FcpException {
260 when(core.getSone(Matchers.<String>any())).thenReturn(Optional.<Sone>absent());
261 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
262 abstractSoneCommand.getMandatorySone(soneFieldSet, "RealSone");
266 public void testParsingAMandatoryLocalSone() throws FcpException {
267 Sone sone = createLocalSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
268 when(core.getSone(eq("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E"))).thenReturn(of(sone));
269 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
270 Sone parsedSone = abstractSoneCommand.getMandatoryLocalSone(soneFieldSet, "Sone");
271 assertThat(parsedSone, notNullValue());
272 assertThat(parsedSone, is(sone));
273 assertThat(parsedSone.isLocal(), is(true));
276 @Test(expected = FcpException.class)
277 public void testParsingANonLocalSoneAsMandatoryLocalSoneCausesAnError() throws FcpException {
278 Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
279 when(core.getSone(eq("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E"))).thenReturn(of(sone));
280 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
281 abstractSoneCommand.getMandatoryLocalSone(soneFieldSet, "Sone");
284 @Test(expected = FcpException.class)
285 public void testParsingAMandatoryLocalSoneFromANonExistingFieldCausesAnError() throws FcpException {
286 when(core.getSone(Matchers.<String>any())).thenReturn(Optional.<Sone>absent());
287 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
288 abstractSoneCommand.getMandatoryLocalSone(soneFieldSet, "RealSone");
292 public void testParsingAnExistingOptionalSone() throws FcpException {
293 Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
294 when(core.getSone(eq("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E"))).thenReturn(of(sone));
295 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
296 Optional<Sone> parsedSone = abstractSoneCommand.getOptionalSone(soneFieldSet, "Sone");
297 assertThat(parsedSone, notNullValue());
298 assertThat(parsedSone.isPresent(), is(true));
299 assertThat(parsedSone.get(), is(sone));
303 public void testParsingANonExistingOptionalSone() throws FcpException {
304 when(core.getSone(Matchers.<String>any())).thenReturn(Optional.<Sone>absent());
305 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
306 Optional<Sone> parsedSone = abstractSoneCommand.getOptionalSone(soneFieldSet, "Sone");
307 assertThat(parsedSone, notNullValue());
308 assertThat(parsedSone.isPresent(), is(false));
311 @Test(expected = FcpException.class)
312 public void testParsingAnOptionalSoneFromANonExistingFieldCausesAnError() throws FcpException {
313 when(core.getSone(Matchers.<String>any())).thenReturn(Optional.<Sone>absent());
314 SimpleFieldSet soneFieldSet = new SimpleFieldSetBuilder().put("Sone", "jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E").get();
315 abstractSoneCommand.getOptionalSone(soneFieldSet, "RealSone");