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(
57 mocks.mockSone("LSone1").local().create(),
58 mocks.mockSone("LSone2").local().create(),
59 mocks.mockSone("LSone3").local().create()
61 mocks.mockSone("RSone1").create();
62 mocks.mockSone("RSone2").create();
63 SimpleFieldSet getLocalSonesFieldSet = new SimpleFieldSetBuilder().put("Message", "GetLocalSones").get();
64 Response response = getLocalSonesCommand.execute(getLocalSonesFieldSet, null, DIRECT);
65 assertThat(response, notNullValue());
66 assertThat(response.getReplyParameters(), notNullValue());
67 assertThat(response.getReplyParameters().get("Message"), is("ListLocalSones"));
68 Collection<ParsedSone> parsedSones = parseSones(response.getReplyParameters(), "LocalSones.");
69 assertThat(parsedSones, matchesSones(localSones));
72 private Matcher<Collection<ParsedSone>> matchesSones(final Collection<Sone> sones) {
73 return new TypeSafeMatcher<Collection<ParsedSone>>() {
75 protected boolean matchesSafely(Collection<ParsedSone> parsedSones) {
76 if (sones.size() != parsedSones.size()) {
79 List<Sone> remainingSonesToMatch = Lists.newArrayList(sones);
80 for (ParsedSone parsedSone : parsedSones) {
81 boolean matchedSone = false;
82 for (Sone sone : remainingSonesToMatch) {
83 if (!sone.getId().equals(parsedSone.id) || (sone.getTime() != parsedSone.time)) {
86 remainingSonesToMatch.remove(sone);
98 protected void describeMismatchSafely(Collection<ParsedSone> parsedSones, Description mismatchDescription) {
99 mismatchDescription.appendValue(parsedSones);
103 public void describeTo(Description description) {
104 description.appendValue(sones);
109 private Collection<ParsedSone> parseSones(SimpleFieldSet simpleFieldSet, String prefix) throws FSParseException {
110 List<ParsedSone> parsedSones = Lists.newArrayList();
111 int count = simpleFieldSet.getInt(prefix + "Count");
112 for (int index = 0; index < count; ++index) {
113 String id = simpleFieldSet.get(format("%s%d.ID", prefix, index));
114 String name = simpleFieldSet.get(format("%s%d.Name", prefix, index));
115 String niceName = simpleFieldSet.get(format("%s%d.NiceName", prefix, index));
116 long time = simpleFieldSet.getLong(format("%s%d.Time", prefix, index));
117 parsedSones.add(new ParsedSone(id, name, niceName, time));
122 private static class ParsedSone {
124 public final String id;
125 public final String name;
126 public final String niceName;
127 public final long time;
129 private ParsedSone(String id, String name, String niceName, long time) {
132 this.niceName = niceName;
137 public String toString() {
138 return format("Sone[%s,%s,%s,%d]", id, name, niceName, time);