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.fcp.Verifiers.verifyAnswer;
23 import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT;
24 import static org.hamcrest.MatcherAssert.assertThat;
26 import java.util.Collection;
27 import java.util.List;
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;
34 import freenet.node.FSParseException;
35 import freenet.support.SimpleFieldSet;
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;
44 * Unit test for {@link GetLocalSonesCommand}.
46 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
48 public class GetLocalSonesCommandTest {
50 private final Mocks mocks = new Mocks();
51 private final GetLocalSonesCommand getLocalSonesCommand = new GetLocalSonesCommand(mocks.core);
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()
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));
69 private Matcher<Collection<ParsedSone>> matchesSones(final Collection<Sone> sones) {
70 return new TypeSafeMatcher<Collection<ParsedSone>>() {
72 protected boolean matchesSafely(Collection<ParsedSone> parsedSones) {
73 if (sones.size() != parsedSones.size()) {
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)) {
83 remainingSonesToMatch.remove(sone);
95 protected void describeMismatchSafely(Collection<ParsedSone> parsedSones, Description mismatchDescription) {
96 mismatchDescription.appendValue(parsedSones);
100 public void describeTo(Description description) {
101 description.appendValue(sones);
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));
119 private static class ParsedSone {
121 public final String id;
122 public final String name;
123 public final String niceName;
124 public final long time;
126 private ParsedSone(String id, String name, String niceName, long time) {
129 this.niceName = niceName;
134 public String toString() {
135 return format("Sone[%s,%s,%s,%d]", id, name, niceName, time);