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 net.pterodactylus.sone.fcp.AbstractSoneCommand.encodeSone;
21 import static net.pterodactylus.sone.fcp.AbstractSoneCommand.encodeString;
22 import static org.hamcrest.CoreMatchers.is;
23 import static org.hamcrest.CoreMatchers.notNullValue;
24 import static org.hamcrest.CoreMatchers.nullValue;
25 import static org.hamcrest.MatcherAssert.assertThat;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
29 import net.pterodactylus.sone.data.Profile;
30 import net.pterodactylus.sone.data.Sone;
32 import freenet.node.FSParseException;
33 import freenet.support.SimpleFieldSet;
35 import com.google.common.base.Optional;
36 import org.junit.Test;
39 * Unit test for {@link AbstractSoneCommand}.
41 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43 public class AbstractSoneCommandTest {
46 public void testStringEncoding() {
47 String testString = prepareStringToBeEncoded();
49 String encodedString = encodeString(testString);
50 assertThat(encodedString, notNullValue());
51 assertThat(encodedString.length(), is(testString.length() + 3));
54 private String prepareStringToBeEncoded() {
55 StringBuilder testString = new StringBuilder();
56 for (int i = 0; i < 4000; ++i) {
57 testString.append((char) i);
59 return testString.toString();
63 public void testEncodingASone() throws FSParseException {
64 Sone sone = prepareSoneToBeEncoded();
65 SimpleFieldSet soneFieldSet = encodeSone(sone, "Prefix.", Optional.<Sone>absent());
66 assertThat(soneFieldSet, notNullValue());
67 assertThat(soneFieldSet.get("Prefix.Name"), is("test"));
68 assertThat(soneFieldSet.get("Prefix.NiceName"), is("First M. Last"));
69 assertThat(soneFieldSet.getLong("Prefix.LastUpdated"), is(sone.getTime()));
70 assertThat(soneFieldSet.get("Prefix.Followed"), nullValue());
71 assertThat(soneFieldSet.getInt("Prefix.Field.Count"), is(1));
72 assertThat(soneFieldSet.get("Prefix.Field.0.Name"), is("Test1"));
73 assertThat(soneFieldSet.get("Prefix.Field.0.Value"), is("Value1"));
76 private Sone prepareSoneToBeEncoded() {
77 long time = (long) (Math.random() * Long.MAX_VALUE);
78 Sone sone = mock(Sone.class);
79 Profile profile = new Profile(sone);
80 profile.modify().setFirstName("First").setMiddleName("M.").setLastName("Last").update();
81 profile.setField(profile.addField("Test1"), "Value1");
82 when(sone.getName()).thenReturn("test");
83 when(sone.getTime()).thenReturn(time);
84 when(sone.getProfile()).thenReturn(profile);