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.Mockito.mock;
31 import static org.mockito.Mockito.when;
33 import java.util.List;
35 import net.pterodactylus.sone.data.PostReply;
36 import net.pterodactylus.sone.data.Profile;
37 import net.pterodactylus.sone.data.Sone;
39 import freenet.node.FSParseException;
40 import freenet.support.SimpleFieldSet;
42 import com.google.common.base.Optional;
43 import org.junit.Test;
44 import org.mockito.Matchers;
47 * Unit test for {@link AbstractSoneCommand}.
49 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
51 public class AbstractSoneCommandTest {
54 public void testStringEncoding() {
55 String testString = prepareStringToBeEncoded();
57 String encodedString = encodeString(testString);
58 assertThat(encodedString, notNullValue());
59 assertThat(encodedString.length(), is(testString.length() + 3));
62 private String prepareStringToBeEncoded() {
63 StringBuilder testString = new StringBuilder();
64 for (int i = 0; i < 4000; ++i) {
65 testString.append((char) i);
67 return testString.toString();
71 public void testEncodingASone() throws FSParseException {
72 Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
73 SimpleFieldSet soneFieldSet = encodeSone(sone, "Prefix.", Optional.<Sone>absent());
74 assertThat(soneFieldSet, notNullValue());
75 assertThat(soneFieldSet.get("Prefix.Name"), is("test"));
76 assertThat(soneFieldSet.get("Prefix.NiceName"), is("First M. Last"));
77 assertThat(soneFieldSet.getLong("Prefix.LastUpdated"), is(sone.getTime()));
78 assertThat(soneFieldSet.get("Prefix.Followed"), nullValue());
79 assertThat(soneFieldSet.getInt("Prefix.Field.Count"), is(1));
80 assertThat(soneFieldSet.get("Prefix.Field.0.Name"), is("Test1"));
81 assertThat(soneFieldSet.get("Prefix.Field.0.Value"), is("Value1"));
85 public void testEncodingAFollowedSone() throws FSParseException {
86 Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
87 Sone localSone = prepareLocalSoneThatFollowsEverybody();
88 SimpleFieldSet soneFieldSet = encodeSone(sone, "Prefix.", of(localSone));
89 assertThat(soneFieldSet, notNullValue());
90 assertThat(soneFieldSet.get("Prefix.Name"), is("test"));
91 assertThat(soneFieldSet.get("Prefix.NiceName"), is("First M. Last"));
92 assertThat(soneFieldSet.getLong("Prefix.LastUpdated"), is(sone.getTime()));
93 assertThat(soneFieldSet.get("Prefix.Followed"), is("true"));
94 assertThat(soneFieldSet.getInt("Prefix.Field.Count"), is(1));
95 assertThat(soneFieldSet.get("Prefix.Field.0.Name"), is("Test1"));
96 assertThat(soneFieldSet.get("Prefix.Field.0.Value"), is("Value1"));
100 public void testEncodingANotFollowedSone() throws FSParseException {
101 Sone sone = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "test", "First", "M.", "Last", (long) (Math.random() * Long.MAX_VALUE));
102 Sone localSone = prepareLocalSoneThatFollowsNobody();
103 SimpleFieldSet soneFieldSet = encodeSone(sone, "Prefix.", of(localSone));
104 assertThat(soneFieldSet, notNullValue());
105 assertThat(soneFieldSet.get("Prefix.Name"), is("test"));
106 assertThat(soneFieldSet.get("Prefix.NiceName"), is("First M. Last"));
107 assertThat(soneFieldSet.getLong("Prefix.LastUpdated"), is(sone.getTime()));
108 assertThat(soneFieldSet.get("Prefix.Followed"), is("false"));
109 assertThat(soneFieldSet.getInt("Prefix.Field.Count"), is(1));
110 assertThat(soneFieldSet.get("Prefix.Field.0.Name"), is("Test1"));
111 assertThat(soneFieldSet.get("Prefix.Field.0.Value"), is("Value1"));
114 private Sone prepareLocalSoneThatFollowsEverybody() {
115 Sone sone = mock(Sone.class);
116 when(sone.hasFriend(Matchers.<String>any())).thenReturn(true);
120 private Sone prepareLocalSoneThatFollowsNobody() {
121 Sone sone = mock(Sone.class);
122 when(sone.hasFriend(Matchers.<String>any())).thenReturn(false);
127 public void testEncodingMultipleSones() throws FSParseException {
128 List<Sone> sones = prepareMultipleSones();
129 SimpleFieldSet sonesFieldSet = AbstractSoneCommand.encodeSones(sones, "Prefix.");
130 assertThat(sonesFieldSet, notNullValue());
131 assertThat(sonesFieldSet.getInt("Prefix.Count"), is(sones.size()));
132 assertThat(sonesFieldSet.get("Prefix.0.ID"), is(sones.get(0).getId()));
133 assertThat(sonesFieldSet.get("Prefix.0.Name"), is(sones.get(0).getName()));
134 assertThat(sonesFieldSet.get("Prefix.0.NiceName"), is(getNiceName(sones.get(0))));
135 assertThat(sonesFieldSet.getLong("Prefix.0.Time"), is(sones.get(0).getTime()));
136 assertThat(sonesFieldSet.get("Prefix.1.ID"), is(sones.get(1).getId()));
137 assertThat(sonesFieldSet.get("Prefix.1.Name"), is(sones.get(1).getName()));
138 assertThat(sonesFieldSet.get("Prefix.1.NiceName"), is(getNiceName(sones.get(1))));
139 assertThat(sonesFieldSet.getLong("Prefix.1.Time"), is(sones.get(1).getTime()));
140 assertThat(sonesFieldSet.get("Prefix.2.ID"), is(sones.get(2).getId()));
141 assertThat(sonesFieldSet.get("Prefix.2.Name"), is(sones.get(2).getName()));
142 assertThat(sonesFieldSet.get("Prefix.2.NiceName"), is(getNiceName(sones.get(2))));
143 assertThat(sonesFieldSet.getLong("Prefix.2.Time"), is(sones.get(2).getTime()));
146 private List<Sone> prepareMultipleSones() {
147 Sone sone1 = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test1", "Alpha", "A.", "First", (long) (Math.random() * Long.MAX_VALUE));
148 Sone sone2 = createSone("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg", "Test2", "Beta", "B.", "Second", (long) (Math.random() * Long.MAX_VALUE));
149 Sone sone3 = createSone("-1Q6LhHvx91C1mSjOS3zznRSNUC4OxoHUbhIgBAyW1U", "Test3", "Gamma", "C.", "Third", (long) (Math.random() * Long.MAX_VALUE));
150 return asList(sone1, sone2, sone3);
153 private Sone createSone(String id, String name, String firstName, String middleName, String lastName, long time) {
154 Sone sone = mock(Sone.class);
155 when(sone.getId()).thenReturn(id);
156 when(sone.getName()).thenReturn(name);
157 when(sone.getProfile()).thenReturn(prepareProfile(sone, firstName, middleName, lastName));
158 when(sone.getTime()).thenReturn(time);
162 private Profile prepareProfile(Sone sone, String firstName, String middleName, String lastName) {
163 Profile profile = new Profile(sone).modify().setFirstName(firstName).setMiddleName(middleName).setLastName(lastName).update();
164 profile.setField(profile.addField("Test1"), "Value1");
169 public void testEncodingReplies() throws FSParseException {
170 List<PostReply> postReplies = preparePostReplies();
171 SimpleFieldSet postRepliesFieldSet = AbstractSoneCommand.encodeReplies(postReplies, "Prefix.");
172 assertThat(postRepliesFieldSet, notNullValue());
173 assertThat(postRepliesFieldSet.getInt("Prefix.Replies.Count"), is(postReplies.size()));
174 assertThat(postRepliesFieldSet.get("Prefix.Replies.0.ID"), is(postReplies.get(0).getId()));
175 assertThat(postRepliesFieldSet.get("Prefix.Replies.0.Sone"), is(postReplies.get(0).getSone().getId()));
176 assertThat(postRepliesFieldSet.getLong("Prefix.Replies.0.Time"), is(postReplies.get(0).getTime()));
177 assertThat(postRepliesFieldSet.get("Prefix.Replies.0.Text"), is(postReplies.get(0).getText()));
178 assertThat(postRepliesFieldSet.get("Prefix.Replies.1.ID"), is(postReplies.get(1).getId()));
179 assertThat(postRepliesFieldSet.get("Prefix.Replies.1.Sone"), is(postReplies.get(1).getSone().getId()));
180 assertThat(postRepliesFieldSet.getLong("Prefix.Replies.1.Time"), is(postReplies.get(1).getTime()));
181 assertThat(postRepliesFieldSet.get("Prefix.Replies.1.Text"), is(postReplies.get(1).getText()));
182 assertThat(postRepliesFieldSet.get("Prefix.Replies.2.ID"), is(postReplies.get(2).getId()));
183 assertThat(postRepliesFieldSet.get("Prefix.Replies.2.Sone"), is(postReplies.get(2).getSone().getId()));
184 assertThat(postRepliesFieldSet.getLong("Prefix.Replies.2.Time"), is(postReplies.get(2).getTime()));
185 assertThat(postRepliesFieldSet.get("Prefix.Replies.2.Text"), is(postReplies.get(2).getText()));
188 private List<PostReply> preparePostReplies() {
189 Sone sone1 = createSone("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E", "Test1", "Alpha", "A.", "First", (long) (Math.random() * Long.MAX_VALUE));
190 Sone sone2 = createSone("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg", "Test2", "Beta", "B.", "Second", (long) (Math.random() * Long.MAX_VALUE));
191 Sone sone3 = createSone("-1Q6LhHvx91C1mSjOS3zznRSNUC4OxoHUbhIgBAyW1U", "Test3", "Gamma", "C.", "Third", (long) (Math.random() * Long.MAX_VALUE));
192 PostReply postReply1 = createPostReply(sone1, "Text 1");
193 PostReply postReply2 = createPostReply(sone2, "Text 2");
194 PostReply postReply3 = createPostReply(sone3, "Text 3");
195 return asList(postReply1, postReply2, postReply3);
198 private PostReply createPostReply(Sone sone, String text) {
199 PostReply postReply = mock(PostReply.class);
200 when(postReply.getId()).thenReturn(randomUUID().toString());
201 when(postReply.getSone()).thenReturn(sone);
202 when(postReply.getTime()).thenReturn((long) (Math.random() * Long.MAX_VALUE));
203 when(postReply.getText()).thenReturn(text);
208 public void testEncodingLikes() throws FSParseException {
209 List<Sone> likes = prepareMultipleSones();
210 SimpleFieldSet likesFieldSet = AbstractSoneCommand.encodeLikes(likes, "Prefix.");
211 assertThat(likesFieldSet, notNullValue());
212 assertThat(likesFieldSet.getInt("Prefix.Count"), is(likes.size()));
213 assertThat(likesFieldSet.get("Prefix.0.ID"), is(likes.get(0).getId()));
214 assertThat(likesFieldSet.get("Prefix.1.ID"), is(likes.get(1).getId()));
215 assertThat(likesFieldSet.get("Prefix.2.ID"), is(likes.get(2).getId()));