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 net.pterodactylus.sone.fcp.AbstractSoneCommand.encodeSone;
23 import static net.pterodactylus.sone.fcp.AbstractSoneCommand.encodeString;
24 import static net.pterodactylus.sone.template.SoneAccessor.getNiceName;
25 import static org.hamcrest.CoreMatchers.is;
26 import static org.hamcrest.CoreMatchers.notNullValue;
27 import static org.hamcrest.CoreMatchers.nullValue;
28 import static org.hamcrest.MatcherAssert.assertThat;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.when;
32 import java.util.List;
34 import net.pterodactylus.sone.data.Profile;
35 import net.pterodactylus.sone.data.Sone;
37 import freenet.node.FSParseException;
38 import freenet.support.SimpleFieldSet;
40 import com.google.common.base.Optional;
41 import org.junit.Test;
42 import org.mockito.Matchers;
45 * Unit test for {@link AbstractSoneCommand}.
47 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
49 public class AbstractSoneCommandTest {
52 public void testStringEncoding() {
53 String testString = prepareStringToBeEncoded();
55 String encodedString = encodeString(testString);
56 assertThat(encodedString, notNullValue());
57 assertThat(encodedString.length(), is(testString.length() + 3));
60 private String prepareStringToBeEncoded() {
61 StringBuilder testString = new StringBuilder();
62 for (int i = 0; i < 4000; ++i) {
63 testString.append((char) i);
65 return testString.toString();
69 public void testEncodingASone() throws FSParseException {
70 Sone sone = prepareSoneToBeEncoded();
71 SimpleFieldSet soneFieldSet = encodeSone(sone, "Prefix.", Optional.<Sone>absent());
72 assertThat(soneFieldSet, notNullValue());
73 assertThat(soneFieldSet.get("Prefix.Name"), is("test"));
74 assertThat(soneFieldSet.get("Prefix.NiceName"), is("First M. Last"));
75 assertThat(soneFieldSet.getLong("Prefix.LastUpdated"), is(sone.getTime()));
76 assertThat(soneFieldSet.get("Prefix.Followed"), nullValue());
77 assertThat(soneFieldSet.getInt("Prefix.Field.Count"), is(1));
78 assertThat(soneFieldSet.get("Prefix.Field.0.Name"), is("Test1"));
79 assertThat(soneFieldSet.get("Prefix.Field.0.Value"), is("Value1"));
83 public void testEncodingAFollowedSone() throws FSParseException {
84 Sone sone = prepareSoneToBeEncoded();
85 Sone localSone = prepareLocalSoneThatFollowsEverybody();
86 SimpleFieldSet soneFieldSet = encodeSone(sone, "Prefix.", of(localSone));
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"), is("true"));
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 testEncodingANotFollowedSone() throws FSParseException {
99 Sone sone = prepareSoneToBeEncoded();
100 Sone localSone = prepareLocalSoneThatFollowsNobody();
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("false"));
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"));
112 private Sone prepareLocalSoneThatFollowsEverybody() {
113 Sone sone = mock(Sone.class);
114 when(sone.hasFriend(Matchers.<String>any())).thenReturn(true);
118 private Sone prepareLocalSoneThatFollowsNobody() {
119 Sone sone = mock(Sone.class);
120 when(sone.hasFriend(Matchers.<String>any())).thenReturn(false);
124 private Sone prepareSoneToBeEncoded() {
125 long time = (long) (Math.random() * Long.MAX_VALUE);
126 Sone sone = mock(Sone.class);
127 when(sone.getName()).thenReturn("test");
128 when(sone.getTime()).thenReturn(time);
129 when(sone.getProfile()).thenReturn(prepareProfile(sone, "First", "M.", "Last"));
134 public void testEncodingMultipleSones() throws FSParseException {
135 List<Sone> sones = prepareMultipleSones();
136 SimpleFieldSet sonesFieldSet = AbstractSoneCommand.encodeSones(sones, "Prefix.");
137 assertThat(sonesFieldSet, notNullValue());
138 assertThat(sonesFieldSet.getInt("Prefix.Count"), is(sones.size()));
139 assertThat(sonesFieldSet.get("Prefix.0.ID"), is(sones.get(0).getId()));
140 assertThat(sonesFieldSet.get("Prefix.0.Name"), is(sones.get(0).getName()));
141 assertThat(sonesFieldSet.get("Prefix.0.NiceName"), is(getNiceName(sones.get(0))));
142 assertThat(sonesFieldSet.getLong("Prefix.0.Time"), is(sones.get(0).getTime()));
143 assertThat(sonesFieldSet.get("Prefix.1.ID"), is(sones.get(1).getId()));
144 assertThat(sonesFieldSet.get("Prefix.1.Name"), is(sones.get(1).getName()));
145 assertThat(sonesFieldSet.get("Prefix.1.NiceName"), is(getNiceName(sones.get(1))));
146 assertThat(sonesFieldSet.getLong("Prefix.1.Time"), is(sones.get(1).getTime()));
147 assertThat(sonesFieldSet.get("Prefix.2.ID"), is(sones.get(2).getId()));
148 assertThat(sonesFieldSet.get("Prefix.2.Name"), is(sones.get(2).getName()));
149 assertThat(sonesFieldSet.get("Prefix.2.NiceName"), is(getNiceName(sones.get(2))));
150 assertThat(sonesFieldSet.getLong("Prefix.2.Time"), is(sones.get(2).getTime()));
153 private List<Sone> prepareMultipleSones() {
154 Sone sone1 = mock(Sone.class);
155 when(sone1.getId()).thenReturn("jXH8d-eFdm14R69WyaCgQoSjaY0jl-Ut6etlXjK0e6E");
156 when(sone1.getName()).thenReturn("Test1");
157 when(sone1.getProfile()).thenReturn(prepareProfile(sone1, "Alpha", "A.", "First"));
158 when(sone1.getTime()).thenReturn((long) (Math.random() * Long.MAX_VALUE));
159 Sone sone2 = mock(Sone.class);
160 when(sone2.getId()).thenReturn("KpoohJSbZGltHHG-YsxKV8ojjS5gwScRv50kl3AkLXg");
161 when(sone2.getName()).thenReturn("Test2");
162 when(sone2.getProfile()).thenReturn(prepareProfile(sone2, "Beta", "B.", "Second"));
163 when(sone2.getTime()).thenReturn((long) (Math.random() * Long.MAX_VALUE));
164 Sone sone3 = mock(Sone.class);
165 when(sone3.getId()).thenReturn("-1Q6LhHvx91C1mSjOS3zznRSNUC4OxoHUbhIgBAyW1U");
166 when(sone3.getName()).thenReturn("Test3");
167 when(sone3.getProfile()).thenReturn(prepareProfile(sone3, "Gamma", "C.", "Third"));
168 when(sone3.getTime()).thenReturn((long) (Math.random() * Long.MAX_VALUE));
169 return asList(sone1, sone2, sone3);
172 private Profile prepareProfile(Sone sone, String firstName, String middleName, String lastName) {
173 Profile profile = new Profile(sone).modify().setFirstName(firstName).setMiddleName(middleName).setLastName(lastName).update();
174 profile.setField(profile.addField("Test1"), "Value1");