4b51388edf78e167cbf2c66b3db1493909252f3c
[Sone.git] / src / test / java / net / pterodactylus / sone / fcp / GetSoneCommandTest.java
1 /*
2  * Sone - GetSoneCommandTest.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 java.util.Arrays.asList;
21 import static net.pterodactylus.sone.fcp.Verifiers.verifyAnswer;
22 import static net.pterodactylus.sone.fcp.Verifiers.verifyFollowedSone;
23 import static net.pterodactylus.sone.fcp.Verifiers.verifyNotFollowedSone;
24 import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT;
25
26 import java.util.Arrays;
27
28 import net.pterodactylus.sone.data.Mocks;
29 import net.pterodactylus.sone.data.Sone;
30 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
31 import net.pterodactylus.sone.freenet.fcp.Command.Response;
32 import net.pterodactylus.sone.freenet.fcp.FcpException;
33
34 import freenet.node.FSParseException;
35 import freenet.support.SimpleFieldSet;
36
37 import org.junit.Test;
38
39 /**
40  * Unit test for {@link GetSoneCommand}.
41  *
42  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43  */
44 public class GetSoneCommandTest {
45
46         private final Mocks mocks = new Mocks();
47         private final GetSoneCommand getSoneCommand = new GetSoneCommand(mocks.core);
48
49         @Test
50         public void gettingAFollowedSone() throws FcpException, FSParseException {
51                 Sone followedSone = mocks.mockSone("FSone").withName("Followed Sone").withProfileName("F.", "Ollowed", "Sone").withTime(1000L).addProfileField("Field1", "Value1").create();
52                 mocks.mockSone("LSone").withFriends(asList(followedSone.getId())).local().create();
53                 SimpleFieldSet getSoneFieldSet = new SimpleFieldSetBuilder()
54                                 .put("Message", "GetSone")
55                                 .put("Sone", "FSone")
56                                 .put("LocalSone", "LSone")
57                                 .get();
58                 Response response = getSoneCommand.execute(getSoneFieldSet, null, DIRECT);
59                 verifyAnswer(response, "Sone");
60                 verifyFollowedSone(response.getReplyParameters(), "", followedSone);
61         }
62
63         @Test
64         public void gettingANotFollowedSone() throws FcpException, FSParseException {
65                 Sone unfollowedSone = mocks.mockSone("FSone").withName("Followed Sone").withProfileName("F.", "Ollowed", "Sone").withTime(1000L).addProfileField("Field1", "Value1").create();
66                 mocks.mockSone("LSone").withFriends(Arrays.<String>asList()).local().create();
67                 SimpleFieldSet getSoneFieldSet = new SimpleFieldSetBuilder()
68                                 .put("Message", "GetSone")
69                                 .put("Sone", "FSone")
70                                 .put("LocalSone", "LSone")
71                                 .get();
72                 Response response = getSoneCommand.execute(getSoneFieldSet, null, DIRECT);
73                 verifyAnswer(response, "Sone");
74                 verifyNotFollowedSone(response.getReplyParameters(), "", unfollowedSone);
75         }
76
77         @Test
78         public void gettingASoneWithoutALocalSone() throws FcpException, FSParseException {
79                 Sone sone = mocks.mockSone("Sone").withName("Some Sone").withProfileName("S.", "Ome", "Sone").withTime(1000L).addProfileField("Field1", "Value1").create();
80                 SimpleFieldSet getSoneFieldSet = new SimpleFieldSetBuilder()
81                                 .put("Message", "GetSone")
82                                 .put("Sone", "Sone")
83                                 .get();
84                 Response response = getSoneCommand.execute(getSoneFieldSet, null, DIRECT);
85                 verifyAnswer(response, "Sone");
86                 verifyNotFollowedSone(response.getReplyParameters(), "", sone);
87         }
88
89         @Test(expected = FcpException.class)
90         public void gettingANonExistingSoneCausesAnError() throws FcpException {
91                 SimpleFieldSet getSoneFieldSet = new SimpleFieldSetBuilder()
92                                 .put("Message", "GetSone")
93                                 .put("Sone", "Sone")
94                                 .get();
95                 getSoneCommand.execute(getSoneFieldSet, null, DIRECT);
96         }
97
98 }