6650a1a6c688e003aa1e47f4e25d70290af691cf
[Sone.git] / src / test / java / net / pterodactylus / sone / fcp / GetSonesCommandTest.java
1 /*
2  * Sone - GetSonesCommandTest.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.verifySones;
23 import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT;
24
25 import java.util.Collections;
26 import java.util.List;
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
33 import freenet.node.FSParseException;
34 import freenet.support.SimpleFieldSet;
35
36 import org.junit.Test;
37
38 /**
39  * Unit test for {@link GetSonesCommand}.
40  *
41  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42  */
43 public class GetSonesCommandTest {
44
45         private final Mocks mocks = new Mocks();
46         private final GetSonesCommand getSonesCommand = new GetSonesCommand(mocks.core);
47         private final List<Sone> mockedSones = prepareSones();
48
49         @Test
50         public void gettingAllSones() throws FSParseException {
51                 SimpleFieldSet getSonesFieldSet = new SimpleFieldSetBuilder()
52                                 .put("Message", "GetSones")
53                                 .get();
54                 Response response = getSonesCommand.execute(getSonesFieldSet, null, DIRECT);
55                 verifyAnswer(response, "Sones");
56                 verifySones(response.getReplyParameters(), "", mockedSones);
57         }
58
59         @Test
60         public void skipMoreSonesThanThereAre() throws FSParseException {
61                 SimpleFieldSet getSonesFieldSet = new SimpleFieldSetBuilder()
62                                 .put("Message", "GetSones")
63                                 .put("StartSone", "5")
64                                 .get();
65                 Response response = getSonesCommand.execute(getSonesFieldSet, null, DIRECT);
66                 verifyAnswer(response, "Sones");
67                 verifySones(response.getReplyParameters(), "", Collections.<Sone>emptyList());
68         }
69
70         @Test
71         public void gettingOnlyTwoSones() throws FSParseException {
72                 SimpleFieldSet getSonesFieldSet = new SimpleFieldSetBuilder()
73                                 .put("Message", "GetSones")
74                                 .put("MaxSones", "2")
75                                 .get();
76                 Response response = getSonesCommand.execute(getSonesFieldSet, null, DIRECT);
77                 verifyAnswer(response, "Sones");
78                 verifySones(response.getReplyParameters(), "", mockedSones.subList(0, 2));
79         }
80
81         @Test
82         public void gettingAllSonesSkippingTheFirst() throws FSParseException {
83                 SimpleFieldSet getSonesFieldSet = new SimpleFieldSetBuilder()
84                                 .put("Message", "GetSones")
85                                 .put("StartSone", "1")
86                                 .get();
87                 Response response = getSonesCommand.execute(getSonesFieldSet, null, DIRECT);
88                 verifyAnswer(response, "Sones");
89                 verifySones(response.getReplyParameters(), "", mockedSones.subList(1, mockedSones.size()));
90         }
91
92         @Test
93         public void gettingOnlyOneSonesSkippingTheFirst() throws FSParseException {
94                 SimpleFieldSet getSonesFieldSet = new SimpleFieldSetBuilder()
95                                 .put("Message", "GetSones")
96                                 .put("MaxSones", "1")
97                                 .put("StartSone", "1")
98                                 .get();
99                 Response response = getSonesCommand.execute(getSonesFieldSet, null, DIRECT);
100                 verifyAnswer(response, "Sones");
101                 verifySones(response.getReplyParameters(), "", mockedSones.subList(1, 2));
102         }
103
104         private List<Sone> prepareSones() {
105                 Sone sone3 = mocks.mockSone("Sone3").withName("Sone3").withProfileName("S.", "O.", "Ne3").withTime(3000L).create();
106                 Sone sone1 = mocks.mockSone("Sone1").withName("Sone1").withProfileName("S.", "O.", "Ne1").withTime(1000L).create();
107                 Sone sone2 = mocks.mockSone("Sone2").withName("Sone2").withProfileName("S.", "O.", "Ne2").withTime(2000L).create();
108                 return asList(sone1, sone2, sone3);
109         }
110
111 }