Add tests for verifying the “Followed” attribute if a local Sone is given.
[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 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;
29
30 import net.pterodactylus.sone.data.Profile;
31 import net.pterodactylus.sone.data.Sone;
32
33 import freenet.node.FSParseException;
34 import freenet.support.SimpleFieldSet;
35
36 import com.google.common.base.Optional;
37 import org.junit.Test;
38 import org.mockito.Matchers;
39
40 /**
41  * Unit test for {@link AbstractSoneCommand}.
42  *
43  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44  */
45 public class AbstractSoneCommandTest {
46
47         @Test
48         public void testStringEncoding() {
49                 String testString = prepareStringToBeEncoded();
50
51                 String encodedString = encodeString(testString);
52                 assertThat(encodedString, notNullValue());
53                 assertThat(encodedString.length(), is(testString.length() + 3));
54         }
55
56         private String prepareStringToBeEncoded() {
57                 StringBuilder testString = new StringBuilder();
58                 for (int i = 0; i < 4000; ++i) {
59                         testString.append((char) i);
60                 }
61                 return testString.toString();
62         }
63
64         @Test
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"));
76         }
77
78         @Test
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"));
91         }
92
93         @Test
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"));
106         }
107
108         private Sone prepareLocalSoneThatFollowsEverybody() {
109                 Sone sone = mock(Sone.class);
110                 when(sone.hasFriend(Matchers.<String>any())).thenReturn(true);
111                 return sone;
112         }
113
114         private Sone prepareLocalSoneThatFollowsNobody() {
115                 Sone sone = mock(Sone.class);
116                 when(sone.hasFriend(Matchers.<String>any())).thenReturn(false);
117                 return sone;
118         }
119
120         private Sone prepareSoneToBeEncoded() {
121                 long time = (long) (Math.random() * Long.MAX_VALUE);
122                 Sone sone = mock(Sone.class);
123                 Profile profile = new Profile(sone);
124                 profile.modify().setFirstName("First").setMiddleName("M.").setLastName("Last").update();
125                 profile.setField(profile.addField("Test1"), "Value1");
126                 when(sone.getName()).thenReturn("test");
127                 when(sone.getTime()).thenReturn(time);
128                 when(sone.getProfile()).thenReturn(profile);
129                 return sone;
130         }
131
132 }