-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReplacementServicesUnitTests_FourStations.java
325 lines (257 loc) · 12.2 KB
/
ReplacementServicesUnitTests_FourStations.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package org.openmetromaps.maps;
import com.google.common.collect.Lists;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openmetromaps.maps.model.*;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
public class ReplacementServicesUnitTests_FourStations {
ModelData model;
Station stationA;
Station stationB;
Station stationC;
Station stationD;
Line line1;
/* CREATE MAP
A B C D
* ----- * ----- * ----- * 1
*/
@Before
public void createMap() {
List<Stop> stationAStops = new ArrayList<>();
stationA = new Station(0, "A", new Coordinate(47.4891, 19.0614), stationAStops);
List<Stop> stationBStops = new ArrayList<>();
stationB = new Station(1, "B", new Coordinate(47.4891, 19.0714), stationBStops);
List<Stop> stationCStops = new ArrayList<>();
stationC = new Station(2, "C", new Coordinate(47.4891, 19.0814), stationCStops);
List<Stop> stationDStops = new ArrayList<>();
stationD = new Station(3, "D", new Coordinate(47.4891, 19.0914), stationDStops);
List<Stop> line1Stops = new ArrayList<>();
line1 = new Line(4, "1", "#009EE3", false, line1Stops);
Stop line1AStop = new Stop(stationA, line1);
stationAStops.add(line1AStop);
line1Stops.add(line1AStop);
Stop line1BStop = new Stop(stationB, line1);
stationBStops.add(line1BStop);
line1Stops.add(line1BStop);
Stop line1CStop = new Stop(stationC, line1);
stationCStops.add(line1CStop);
line1Stops.add(line1CStop);
Stop line1DStop = new Stop(stationD, line1);
stationDStops.add(line1DStop);
line1Stops.add(line1DStop);
model = new ModelData(new ArrayList<>(List.of(line1)), new ArrayList<>(List.of(stationA, stationB, stationC, stationD)));
}
/* ASSERT
A B C D
* ----- * 1-1
* ----- * P1
* ----- * 1-2
*/
@Test
public void testReplacementServices_splitIntoThreeLinesBC() {
ReplacementServices.createReplacementService(model, List.of(stationB, stationC), List.of(line1));
assertModel(model)
.hasExactStations("A", "B", "C", "D")
.hasExactLines("1-1", "1-2", "P1")
.hasLineWithExactStations("1-1", "A", "B")
.hasLineWithExactStations("P1", "B", "C")
.hasLineWithExactStations("1-2", "C", "D");
}
/* ASSERT
A B C D
* ----- * P1
* ----- * ----- * 1
*/
@Test
public void testReplacementServices_splitIntoTwoLinesAB() {
ReplacementServices.createReplacementService(model, List.of(stationA, stationB), List.of(line1));
assertModel(model)
.hasExactStations("A", "B", "C", "D")
.hasExactLines("1", "P1")
.hasLineWithExactStations("1", "B", "C", "D")
.hasLineWithExactStations("P1", "A", "B");
}
/* ASSERT
A B C D
* ----- * ----- * 1
* ----- * P1
*/
@Test
public void testReplacementServices_splitIntoTwoLinesCD() {
ReplacementServices.createReplacementService(model, List.of(stationC, stationD), List.of(line1));
assertModel(model)
.hasExactStations("A", "B", "C", "D")
.hasExactLines("1", "P1")
.hasLineWithExactStations("1", "A", "B", "C")
.hasLineWithExactStations("P1", "C", "D");
}
private static ModelAsserter assertModel(ModelData data) {
return new ModelAsserter(data);
}
private static class ModelAsserter {
private final ModelData model;
private ModelAsserter(ModelData model) {
this.model = model;
}
public ReplacementServicesUnitTests_FourStations.ModelAsserter hasStations(int num) {
Assert.assertEquals("There are more or fewer stations lines than expected", num, model.stations.size());
return this;
}
public ReplacementServicesUnitTests_FourStations.ModelAsserter hasExactStations(String... stationNames) {
assertStationListEquals(getStationsFromNames(stationNames), model.stations);
return this;
}
public ReplacementServicesUnitTests_FourStations.ModelAsserter hasStations(String... stationNames) {
for(Station station : getStationsFromNames(stationNames)) {
Assert.assertTrue("The station was not present: " + station.getName() + ")", model.stations.contains(station));
}
return this;
}
public ReplacementServicesUnitTests_FourStations.ModelAsserter hasStation(String stationName) {
hasStations(stationName);
return this;
}
public ReplacementServicesUnitTests_FourStations.ModelAsserter hasStation(String stationName, Function<Station, Void> assertStation) {
hasStations(stationName);
assertStation.apply(getStationFromName(stationName));
return this;
}
public ReplacementServicesUnitTests_FourStations.ModelAsserter hasStationWithExactLines(String stationName, String... lineNames) {
hasStations(stationName);
Station station = getStationFromName(stationName);
List<Line> lines = getLinesFromNames(lineNames);
assertLineListEquals(lines, station.getStops().stream().map(Stop::getLine).toList());
return this;
}
public ReplacementServicesUnitTests_FourStations.ModelAsserter hasStationWithLines(String stationName, String... lineNames) {
hasStations(stationName);
Station station = getStationFromName(stationName);
List<Line> lines = getLinesFromNames(lineNames);
List<Line> stationLines = station.getStops().stream().map(Stop::getLine).toList();
for(Line line : lines) {
Assert.assertTrue(
"Line (" + line.getName() + ") does not stop at Station (" + station.getName() + ")",
stationLines.contains(line)
);
}
return this;
}
public ReplacementServicesUnitTests_FourStations.ModelAsserter hasLines(int num) {
Assert.assertEquals("There are more or fewer lines than expected", num, model.lines.size());
return this;
}
public ReplacementServicesUnitTests_FourStations.ModelAsserter hasExactLines(String... lineNames) {
assertLineListEquals(getLinesFromNames(lineNames), model.lines);
return this;
}
public ReplacementServicesUnitTests_FourStations.ModelAsserter hasLines(String... lineNames) {
for(Line line : getLinesFromNames(lineNames)) {
Assert.assertTrue("The line was not present (" + line + ")", model.lines.contains(line));
}
return this;
}
public ReplacementServicesUnitTests_FourStations.ModelAsserter hasLine(String lineName) {
hasLines(lineName);
return this;
}
public ReplacementServicesUnitTests_FourStations.ModelAsserter hasLine(String lineName, Function<Line, Void> assertLine) {
hasLines(lineName);
assertLine.apply(getLineFromName(lineName));
return this;
}
public ReplacementServicesUnitTests_FourStations.ModelAsserter hasLineWithExactStations(String lineName, String... stationNames) {
hasLines(lineName);
Line line = getLineFromName(lineName);
List<Station> stations = getStationsFromNames(stationNames);
List<Station> lineStations = line.getStops().stream().map(Stop::getStation).toList();
String expectedStationNames = stations.stream().map(Station::getName).collect(Collectors.joining(", "));
String actualStationNames = lineStations.stream().map(Station::getName).collect(Collectors.joining(", "));
Assert.assertTrue(
"Stations differ on line (" +
line.getName() +
"). Expected: " +
expectedStationNames +
". Actual: "
+ actualStationNames,
stations.equals(lineStations) || stations.equals(Lists.reverse(lineStations))
);
return this;
}
private List<Station> getStationsFromNames(String... stationNames) {
return getStationsFromNames(List.of(stationNames));
}
private List<Station> getStationsFromNames(List<String> stationNames) {
return stationNames.stream()
.map(this::getStationFromName)
.toList();
}
private Station getStationFromName(String stationName) {
return model.stations.stream()
.filter(s -> s.getName().equals(stationName))
.findFirst()
.orElseThrow(() -> {
Assert.fail("Station with name is not in the model: " + stationName);
return new RuntimeException();
});
}
private void assertStationListEquals(List<Station> expected, List<Station> actual) {
String expectedStations = expected.stream().map(Station::getName).collect(Collectors.joining(", "));
String actualStations = actual.stream().map(Station::getName).collect(Collectors.joining(", "));
Assert.assertEquals("There are more or fewer stations than expected", expected.size(), actual.size());
Assert.assertTrue(
"There are stations that were not expected but are present. Expected: " +
expectedStations +
". Actual: " +
actualStations,
expected.containsAll(actual)
);
Assert.assertTrue(
"There are stations that were expected but are not present. Expected: " +
expectedStations +
". Actual: " +
actualStations,
actual.containsAll(expected)
);
}
private List<Line> getLinesFromNames(String... lineNames) {
return getLinesFromNames(List.of(lineNames));
}
private List<Line> getLinesFromNames(List<String> lineNames) {
return lineNames.stream()
.map(this::getLineFromName)
.toList();
}
private Line getLineFromName(String lineName) {
return model.lines.stream()
.filter(l -> l.getName().equals(lineName))
.findFirst()
.orElseThrow(() -> {
Assert.fail("Line with name is not in the model: " + lineName);
return new RuntimeException();
});
}
private void assertLineListEquals(List<Line> expected, List<Line> actual) {
String expectedLines = expected.stream().map(Line::getName).collect(Collectors.joining(", "));
String actualLines = actual.stream().map(Line::getName).collect(Collectors.joining(", "));
Assert.assertEquals("There are more or fewer lines than expected", expected.size(), actual.size());
Assert.assertTrue(
"There are lines that were not expected but are present. Expected: " +
expectedLines +
". Actual: " +
actualLines,
expected.containsAll(actual)
);
Assert.assertTrue(
"There are lines that were expected but are not present. Expected: " +
expectedLines +
". Actual: " +
actualLines,
actual.containsAll(expected)
);
}
}
}