Move verifiers to different package.
[Sone.git] / src / test / java / net / pterodactylus / sone / fcp / GetLocalSonesCommandTest.java
1 /*
2  * Sone - GetLocalSonesCommandTest.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.lang.String.format;
21 import static java.util.Arrays.asList;
22 import static net.pterodactylus.sone.Verifiers.verifyAnswer;
23 import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT;
24 import static org.hamcrest.MatcherAssert.assertThat;
25
26 import java.util.Collection;
27 import java.util.List;
28
29 import net.pterodactylus.sone.data.Mocks;
30 import net.pterodactylus.sone.data.Sone;
31 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
32 import net.pterodactylus.sone.freenet.fcp.Command.Response;
33
34 import freenet.node.FSParseException;
35 import freenet.support.SimpleFieldSet;
36
37 import com.google.common.collect.Lists;
38 import org.hamcrest.Description;
39 import org.hamcrest.Matcher;
40 import org.hamcrest.TypeSafeMatcher;
41 import org.junit.Test;
42
43 /**
44  * Unit test for {@link GetLocalSonesCommand}.
45  *
46  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
47  */
48 public class GetLocalSonesCommandTest {
49
50         private final Mocks mocks = new Mocks();
51         private final GetLocalSonesCommand getLocalSonesCommand = new GetLocalSonesCommand(mocks.core);
52
53         @Test
54         public void verifyThatOnlyLocalSonesAreReturned() throws FSParseException {
55                 Collection<Sone> localSones = asList(
56                                 mocks.mockSone("LSone1").local().create(),
57                                 mocks.mockSone("LSone2").local().create(),
58                                 mocks.mockSone("LSone3").local().create()
59                 );
60                 mocks.mockSone("RSone1").create();
61                 mocks.mockSone("RSone2").create();
62                 SimpleFieldSet getLocalSonesFieldSet = new SimpleFieldSetBuilder().put("Message", "GetLocalSones").get();
63                 Response response = getLocalSonesCommand.execute(getLocalSonesFieldSet, null, DIRECT);
64                 verifyAnswer(response, "ListLocalSones");
65                 Collection<ParsedSone> parsedSones = parseSones(response.getReplyParameters(), "LocalSones.");
66                 assertThat(parsedSones, matchesSones(localSones));
67         }
68
69         private Matcher<Collection<ParsedSone>> matchesSones(final Collection<Sone> sones) {
70                 return new TypeSafeMatcher<Collection<ParsedSone>>() {
71                         @Override
72                         protected boolean matchesSafely(Collection<ParsedSone> parsedSones) {
73                                 if (sones.size() != parsedSones.size()) {
74                                         return false;
75                                 }
76                                 List<Sone> remainingSonesToMatch = Lists.newArrayList(sones);
77                                 for (ParsedSone parsedSone : parsedSones) {
78                                         boolean matchedSone = false;
79                                         for (Sone sone : remainingSonesToMatch) {
80                                                 if (!sone.getId().equals(parsedSone.id) || (sone.getTime() != parsedSone.time)) {
81                                                         continue;
82                                                 }
83                                                 remainingSonesToMatch.remove(sone);
84                                                 matchedSone = true;
85                                                 break;
86                                         }
87                                         if (!matchedSone) {
88                                                 return false;
89                                         }
90                                 }
91                                 return true;
92                         }
93
94                         @Override
95                         protected void describeMismatchSafely(Collection<ParsedSone> parsedSones, Description mismatchDescription) {
96                                 mismatchDescription.appendValue(parsedSones);
97                         }
98
99                         @Override
100                         public void describeTo(Description description) {
101                                 description.appendValue(sones);
102                         }
103                 };
104         }
105
106         private Collection<ParsedSone> parseSones(SimpleFieldSet simpleFieldSet, String prefix) throws FSParseException {
107                 List<ParsedSone> parsedSones = Lists.newArrayList();
108                 int count = simpleFieldSet.getInt(prefix + "Count");
109                 for (int index = 0; index < count; ++index) {
110                         String id = simpleFieldSet.get(format("%s%d.ID", prefix, index));
111                         String name = simpleFieldSet.get(format("%s%d.Name", prefix, index));
112                         String niceName = simpleFieldSet.get(format("%s%d.NiceName", prefix, index));
113                         long time = simpleFieldSet.getLong(format("%s%d.Time", prefix, index));
114                         parsedSones.add(new ParsedSone(id, name, niceName, time));
115                 }
116                 return parsedSones;
117         }
118
119         private static class ParsedSone {
120
121                 public final String id;
122                 public final String name;
123                 public final String niceName;
124                 public final long time;
125
126                 private ParsedSone(String id, String name, String niceName, long time) {
127                         this.id = id;
128                         this.name = name;
129                         this.niceName = niceName;
130                         this.time = time;
131                 }
132
133                 @Override
134                 public String toString() {
135                         return format("Sone[%s,%s,%s,%d]", id, name, niceName, time);
136                 }
137         }
138
139 }