Add test for encoding a Sone.
[Sone.git] / src / test / java / net / pterodactylus / sone / fcp / AbstractSoneCommandTest.java
1 /*
2  * Sone - AbstractSoneCommandTest.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.fcp;
19
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.MatcherAssert.assertThat;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
27
28 import net.pterodactylus.sone.data.Profile;
29 import net.pterodactylus.sone.data.Sone;
30
31 import freenet.node.FSParseException;
32 import freenet.support.SimpleFieldSet;
33
34 import com.google.common.base.Optional;
35 import org.junit.Test;
36
37 /**
38  * Unit test for {@link AbstractSoneCommand}.
39  *
40  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41  */
42 public class AbstractSoneCommandTest {
43
44         @Test
45         public void testStringEncoding() {
46                 String testString = prepareStringToBeEncoded();
47
48                 String encodedString = encodeString(testString);
49                 assertThat(encodedString, notNullValue());
50                 assertThat(encodedString.length(), is(testString.length() + 3));
51         }
52
53         private String prepareStringToBeEncoded() {
54                 StringBuilder testString = new StringBuilder();
55                 for (int i = 0; i < 4000; ++i) {
56                         testString.append((char) i);
57                 }
58                 return testString.toString();
59         }
60
61         @Test
62         public void testEncodingASone() throws FSParseException {
63                 Sone sone = prepareSoneToBeEncoded();
64                 SimpleFieldSet soneFieldSet = encodeSone(sone, "Prefix.", Optional.<Sone>absent());
65                 assertThat(soneFieldSet, notNullValue());
66                 assertThat(soneFieldSet.get("Prefix.Name"), is("test"));
67                 assertThat(soneFieldSet.get("Prefix.NiceName"), is("First M. Last"));
68                 assertThat(soneFieldSet.getLong("Prefix.LastUpdated"), is(sone.getTime()));
69                 assertThat(soneFieldSet.getInt("Prefix.Field.Count"), is(1));
70                 assertThat(soneFieldSet.get("Prefix.Field.0.Name"), is("Test1"));
71                 assertThat(soneFieldSet.get("Prefix.Field.0.Value"), is("Value1"));
72         }
73
74         private Sone prepareSoneToBeEncoded() {
75                 long time = (long) (Math.random() * Long.MAX_VALUE);
76                 Sone sone = mock(Sone.class);
77                 Profile profile = new Profile(sone);
78                 profile.modify().setFirstName("First").setMiddleName("M.").setLastName("Last").update();
79                 profile.setField(profile.addField("Test1"), "Value1");
80                 when(sone.getName()).thenReturn("test");
81                 when(sone.getTime()).thenReturn(time);
82                 when(sone.getProfile()).thenReturn(profile);
83                 return sone;
84         }
85
86 }