Replace Sone mock methods with a mock builder.
[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(
57                                 mocks.mockSone("LSone1").local().create(),
58                                 mocks.mockSone("LSone2").local().create(),
59                                 mocks.mockSone("LSone3").local().create()
60                 );
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));
70         }
71
72         private Matcher<Collection<ParsedSone>> matchesSones(final Collection<Sone> sones) {
73                 return new TypeSafeMatcher<Collection<ParsedSone>>() {
74                         @Override
75                         protected boolean matchesSafely(Collection<ParsedSone> parsedSones) {
76                                 if (sones.size() != parsedSones.size()) {
77                                         return false;
78                                 }
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)) {
84                                                         continue;
85                                                 }
86                                                 remainingSonesToMatch.remove(sone);
87                                                 matchedSone = true;
88                                                 break;
89                                         }
90                                         if (!matchedSone) {
91                                                 return false;
92                                         }
93                                 }
94                                 return true;
95                         }
96
97                         @Override
98                         protected void describeMismatchSafely(Collection<ParsedSone> parsedSones, Description mismatchDescription) {
99                                 mismatchDescription.appendValue(parsedSones);
100                         }
101
102                         @Override
103                         public void describeTo(Description description) {
104                                 description.appendValue(sones);
105                         }
106                 };
107         }
108
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));
118                 }
119                 return parsedSones;
120         }
121
122         private static class ParsedSone {
123
124                 public final String id;
125                 public final String name;
126                 public final String niceName;
127                 public final long time;
128
129                 private ParsedSone(String id, String name, String niceName, long time) {
130                         this.id = id;
131                         this.name = name;
132                         this.niceName = niceName;
133                         this.time = time;
134                 }
135
136                 @Override
137                 public String toString() {
138                         return format("Sone[%s,%s,%s,%d]", id, name, niceName, time);
139                 }
140         }
141
142 }