2 * Sone - GetLocalSonesCommandTest.java - Copyright © 2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.fcp;
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;
27 import java.util.Collection;
28 import java.util.List;
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;
35 import freenet.node.FSParseException;
36 import freenet.support.SimpleFieldSet;
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;
45 * Unit test for {@link GetLocalSonesCommand}.
47 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
49 public class GetLocalSonesCommandTest {
51 private final Mocks mocks = new Mocks();
52 private final GetLocalSonesCommand getLocalSonesCommand = new GetLocalSonesCommand(mocks.core);
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));
68 private Matcher<Collection<ParsedSone>> matchesSones(final Collection<Sone> sones) {
69 return new TypeSafeMatcher<Collection<ParsedSone>>() {
71 protected boolean matchesSafely(Collection<ParsedSone> parsedSones) {
72 if (sones.size() != parsedSones.size()) {
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)) {
82 remainingSonesToMatch.remove(sone);
94 protected void describeMismatchSafely(Collection<ParsedSone> parsedSones, Description mismatchDescription) {
95 mismatchDescription.appendValue(parsedSones);
99 public void describeTo(Description description) {
100 description.appendValue(sones);
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));
118 private static class ParsedSone {
120 public final String id;
121 public final String name;
122 public final String niceName;
123 public final long time;
125 private ParsedSone(String id, String name, String niceName, long time) {
128 this.niceName = niceName;
133 public String toString() {
134 return format("Sone[%s,%s,%s,%d]", id, name, niceName, time);