Add test for encoding multiple Sones.
[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 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;
31
32 import java.util.List;
33
34 import net.pterodactylus.sone.data.Profile;
35 import net.pterodactylus.sone.data.Sone;
36
37 import freenet.node.FSParseException;
38 import freenet.support.SimpleFieldSet;
39
40 import com.google.common.base.Optional;
41 import org.junit.Test;
42 import org.mockito.Matchers;
43
44 /**
45  * Unit test for {@link AbstractSoneCommand}.
46  *
47  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
48  */
49 public class AbstractSoneCommandTest {
50
51         @Test
52         public void testStringEncoding() {
53                 String testString = prepareStringToBeEncoded();
54
55                 String encodedString = encodeString(testString);
56                 assertThat(encodedString, notNullValue());
57                 assertThat(encodedString.length(), is(testString.length() + 3));
58         }
59
60         private String prepareStringToBeEncoded() {
61                 StringBuilder testString = new StringBuilder();
62                 for (int i = 0; i < 4000; ++i) {
63                         testString.append((char) i);
64                 }
65                 return testString.toString();
66         }
67
68         @Test
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"));
80         }
81
82         @Test
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"));
95         }
96
97         @Test
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"));
110         }
111
112         private Sone prepareLocalSoneThatFollowsEverybody() {
113                 Sone sone = mock(Sone.class);
114                 when(sone.hasFriend(Matchers.<String>any())).thenReturn(true);
115                 return sone;
116         }
117
118         private Sone prepareLocalSoneThatFollowsNobody() {
119                 Sone sone = mock(Sone.class);
120                 when(sone.hasFriend(Matchers.<String>any())).thenReturn(false);
121                 return sone;
122         }
123
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"));
130                 return sone;
131         }
132
133         @Test
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()));
151         }
152
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);
170         }
171
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");
175                 return profile;
176         }
177
178 }