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 Collection<Sone> remoteSones = asList(mocks.mockRemoteSone("RSone1"), mocks.mockRemoteSone("RSone2"));
58 SimpleFieldSet getLocalSonesFieldSet = new SimpleFieldSetBuilder().put("Message", "GetLocalSones").get();
59 Response response = getLocalSonesCommand.execute(getLocalSonesFieldSet, null, DIRECT);
60 assertThat(response, notNullValue());
61 assertThat(response.getReplyParameters(), notNullValue());
62 assertThat(response.getReplyParameters().get("Message"), is("ListLocalSones"));
63 Collection<ParsedSone> parsedSones = parseSones(response.getReplyParameters(), "LocalSones.");
64 assertThat(parsedSones, matchesSones(localSones));
67 private Matcher<Collection<ParsedSone>> matchesSones(final Collection<Sone> sones) {
68 return new TypeSafeMatcher<Collection<ParsedSone>>() {
70 protected boolean matchesSafely(Collection<ParsedSone> parsedSones) {
71 if (sones.size() != parsedSones.size()) {
74 List<Sone> remainingSonesToMatch = Lists.newArrayList(sones);
75 for (ParsedSone parsedSone : parsedSones) {
76 boolean matchedSone = false;
77 for (Sone sone : remainingSonesToMatch) {
78 if (!sone.getId().equals(parsedSone.id) || (sone.getTime() != parsedSone.time)) {
81 remainingSonesToMatch.remove(sone);
93 protected void describeMismatchSafely(Collection<ParsedSone> parsedSones, Description mismatchDescription) {
94 mismatchDescription.appendValue(parsedSones);
98 public void describeTo(Description description) {
99 description.appendValue(sones);
104 private Collection<ParsedSone> parseSones(SimpleFieldSet simpleFieldSet, String prefix) throws FSParseException {
105 List<ParsedSone> parsedSones = Lists.newArrayList();
106 int count = simpleFieldSet.getInt(prefix + "Count");
107 for (int index = 0; index < count; ++index) {
108 String id = simpleFieldSet.get(format("%s%d.ID", prefix, index));
109 String name = simpleFieldSet.get(format("%s%d.Name", prefix, index));
110 String niceName = simpleFieldSet.get(format("%s%d.NiceName", prefix, index));
111 long time = simpleFieldSet.getLong(format("%s%d.Time", prefix, index));
112 parsedSones.add(new ParsedSone(id, name, niceName, time));
117 private static class ParsedSone {
119 public final String id;
120 public final String name;
121 public final String niceName;
122 public final long time;
124 private ParsedSone(String id, String name, String niceName, long time) {
127 this.niceName = niceName;
132 public String toString() {
133 return format("Sone[%s,%s,%s,%d]", id, name, niceName, time);