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