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 net.pterodactylus.sone.fcp.AbstractSoneCommand.encodeSone;
22 import static net.pterodactylus.sone.fcp.AbstractSoneCommand.encodeString;
23 import static org.hamcrest.CoreMatchers.is;
24 import static org.hamcrest.CoreMatchers.notNullValue;
25 import static org.hamcrest.CoreMatchers.nullValue;
26 import static org.hamcrest.MatcherAssert.assertThat;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
30 import net.pterodactylus.sone.data.Profile;
31 import net.pterodactylus.sone.data.Sone;
33 import freenet.node.FSParseException;
34 import freenet.support.SimpleFieldSet;
36 import com.google.common.base.Optional;
37 import org.junit.Test;
38 import org.mockito.Matchers;
41 * Unit test for {@link AbstractSoneCommand}.
43 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45 public class AbstractSoneCommandTest {
48 public void testStringEncoding() {
49 String testString = prepareStringToBeEncoded();
51 String encodedString = encodeString(testString);
52 assertThat(encodedString, notNullValue());
53 assertThat(encodedString.length(), is(testString.length() + 3));
56 private String prepareStringToBeEncoded() {
57 StringBuilder testString = new StringBuilder();
58 for (int i = 0; i < 4000; ++i) {
59 testString.append((char) i);
61 return testString.toString();
65 public void testEncodingASone() throws FSParseException {
66 Sone sone = prepareSoneToBeEncoded();
67 SimpleFieldSet soneFieldSet = encodeSone(sone, "Prefix.", Optional.<Sone>absent());
68 assertThat(soneFieldSet, notNullValue());
69 assertThat(soneFieldSet.get("Prefix.Name"), is("test"));
70 assertThat(soneFieldSet.get("Prefix.NiceName"), is("First M. Last"));
71 assertThat(soneFieldSet.getLong("Prefix.LastUpdated"), is(sone.getTime()));
72 assertThat(soneFieldSet.get("Prefix.Followed"), nullValue());
73 assertThat(soneFieldSet.getInt("Prefix.Field.Count"), is(1));
74 assertThat(soneFieldSet.get("Prefix.Field.0.Name"), is("Test1"));
75 assertThat(soneFieldSet.get("Prefix.Field.0.Value"), is("Value1"));
79 public void testEncodingAFollowedSone() throws FSParseException {
80 Sone sone = prepareSoneToBeEncoded();
81 Sone localSone = prepareLocalSoneThatFollowsEverybody();
82 SimpleFieldSet soneFieldSet = encodeSone(sone, "Prefix.", of(localSone));
83 assertThat(soneFieldSet, notNullValue());
84 assertThat(soneFieldSet.get("Prefix.Name"), is("test"));
85 assertThat(soneFieldSet.get("Prefix.NiceName"), is("First M. Last"));
86 assertThat(soneFieldSet.getLong("Prefix.LastUpdated"), is(sone.getTime()));
87 assertThat(soneFieldSet.get("Prefix.Followed"), is("true"));
88 assertThat(soneFieldSet.getInt("Prefix.Field.Count"), is(1));
89 assertThat(soneFieldSet.get("Prefix.Field.0.Name"), is("Test1"));
90 assertThat(soneFieldSet.get("Prefix.Field.0.Value"), is("Value1"));
94 public void testEncodingANotFollowedSone() throws FSParseException {
95 Sone sone = prepareSoneToBeEncoded();
96 Sone localSone = prepareLocalSoneThatFollowsNobody();
97 SimpleFieldSet soneFieldSet = encodeSone(sone, "Prefix.", of(localSone));
98 assertThat(soneFieldSet, notNullValue());
99 assertThat(soneFieldSet.get("Prefix.Name"), is("test"));
100 assertThat(soneFieldSet.get("Prefix.NiceName"), is("First M. Last"));
101 assertThat(soneFieldSet.getLong("Prefix.LastUpdated"), is(sone.getTime()));
102 assertThat(soneFieldSet.get("Prefix.Followed"), is("false"));
103 assertThat(soneFieldSet.getInt("Prefix.Field.Count"), is(1));
104 assertThat(soneFieldSet.get("Prefix.Field.0.Name"), is("Test1"));
105 assertThat(soneFieldSet.get("Prefix.Field.0.Value"), is("Value1"));
108 private Sone prepareLocalSoneThatFollowsEverybody() {
109 Sone sone = mock(Sone.class);
110 when(sone.hasFriend(Matchers.<String>any())).thenReturn(true);
114 private Sone prepareLocalSoneThatFollowsNobody() {
115 Sone sone = mock(Sone.class);
116 when(sone.hasFriend(Matchers.<String>any())).thenReturn(false);
120 private Sone prepareSoneToBeEncoded() {
121 long time = (long) (Math.random() * Long.MAX_VALUE);
122 Sone sone = mock(Sone.class);
123 when(sone.getName()).thenReturn("test");
124 when(sone.getTime()).thenReturn(time);
125 when(sone.getProfile()).thenReturn(prepareProfile(sone, "First", "M.", "Last"));
129 private Profile prepareProfile(Sone sone, String firstName, String middleName, String lastName) {
130 Profile profile = new Profile(sone).modify().setFirstName(firstName).setMiddleName(middleName).setLastName(lastName).update();
131 profile.setField(profile.addField("Test1"), "Value1");