1
+ package org .heigit .ors .benchmark ;
2
+
3
+ import com .fasterxml .jackson .core .JsonProcessingException ;
4
+ import com .fasterxml .jackson .databind .JsonNode ;
5
+ import com .fasterxml .jackson .databind .ObjectMapper ;
6
+ import io .gatling .javaapi .core .Session ;
7
+ import org .heigit .ors .benchmark .BenchmarkEnums .MatrixModes ;
8
+ import org .heigit .ors .benchmark .exceptions .RequestBodyCreationException ;
9
+ import org .junit .jupiter .api .BeforeEach ;
10
+ import org .junit .jupiter .api .Test ;
11
+
12
+ import java .util .Arrays ;
13
+ import java .util .List ;
14
+ import java .util .Map ;
15
+
16
+ import static org .assertj .core .api .Assertions .assertThat ;
17
+ import static org .junit .jupiter .api .Assertions .*;
18
+ import static org .mockito .Mockito .mock ;
19
+ import static org .mockito .Mockito .when ;
20
+
21
+ class MatrixAlgorithmLoadTestTest {
22
+ private ObjectMapper objectMapper ;
23
+ private Session mockSession ;
24
+ private Config mockConfig ;
25
+ private MatrixModes mockMode ;
26
+
27
+ @ BeforeEach
28
+ void setUp () {
29
+ objectMapper = new ObjectMapper ();
30
+ mockSession = mock (Session .class );
31
+ mockConfig = mock (Config .class );
32
+ mockMode = mock (MatrixModes .class );
33
+
34
+ // Mock CSV data as it would appear in the session
35
+ when (mockSession .get ("coordinates" ))
36
+ .thenReturn ("[[8.695556, 49.392701], [8.684623, 49.398284], [8.705916, 49.406309]]" );
37
+ when (mockSession .get ("sources" )).thenReturn ("[0, 1]" );
38
+ when (mockSession .get ("destinations" )).thenReturn ("[2]" );
39
+ when (mockMode .getRequestParams ()).thenReturn (Map .of ("preference" , "recommended" ));
40
+ }
41
+
42
+ @ Test
43
+ void createRequestBody_ShouldCreateValidJson () throws JsonProcessingException {
44
+ // when
45
+ String result = MatrixAlgorithmLoadTest .createRequestBody (mockSession , mockConfig , mockMode );
46
+
47
+ // then
48
+ JsonNode json = objectMapper .readTree (result );
49
+ assertThat (json .get ("locations" )).isNotNull ();
50
+ assertThat (json .get ("sources" )).isNotNull ();
51
+ assertThat (json .get ("destinations" )).isNotNull ();
52
+ assertThat (json .get ("preference" ).asText ()).isEqualTo ("recommended" );
53
+ }
54
+
55
+ @ Test
56
+ void createRequestBody_ShouldIncludeCorrectLocations () throws JsonProcessingException {
57
+ // when
58
+ String result = MatrixAlgorithmLoadTest .createRequestBody (mockSession , mockConfig , mockMode );
59
+ JsonNode json = objectMapper .readTree (result );
60
+
61
+ // then
62
+ JsonNode locations = json .get ("locations" );
63
+ assertEquals (3 , locations .size ());
64
+ assertEquals (8.695556 , locations .get (0 ).get (0 ).asDouble (), 0.000001 );
65
+ assertEquals (49.392701 , locations .get (0 ).get (1 ).asDouble (), 0.000001 );
66
+ assertEquals (8.684623 , locations .get (1 ).get (0 ).asDouble (), 0.000001 );
67
+ assertEquals (49.398284 , locations .get (1 ).get (1 ).asDouble (), 0.000001 );
68
+ assertEquals (8.705916 , locations .get (2 ).get (0 ).asDouble (), 0.000001 );
69
+ assertEquals (49.406309 , locations .get (2 ).get (1 ).asDouble (), 0.000001 );
70
+ }
71
+
72
+ @ Test
73
+ void createRequestBody_ShouldIncludeCorrectSourcesAndDestinations () throws JsonProcessingException {
74
+ // when
75
+ String result = MatrixAlgorithmLoadTest .createRequestBody (mockSession , mockConfig , mockMode );
76
+ JsonNode json = objectMapper .readTree (result );
77
+
78
+ // then
79
+ JsonNode sources = json .get ("sources" );
80
+ JsonNode destinations = json .get ("destinations" );
81
+
82
+ assertEquals (2 , sources .size ());
83
+ assertEquals (0 , sources .get (0 ).asInt ());
84
+ assertEquals (1 , sources .get (1 ).asInt ());
85
+
86
+ assertEquals (1 , destinations .size ());
87
+ assertEquals (2 , destinations .get (0 ).asInt ());
88
+ }
89
+
90
+ @ Test
91
+ void parseCoordinatesFromString_ShouldParseValidCoordinates () {
92
+ // given
93
+ String coordinatesStr = "[[8.695556, 49.392701], [8.684623, 49.398284]]" ;
94
+
95
+ // when
96
+ List <List <Double >> result = MatrixAlgorithmLoadTest .parseCoordinatesFromString (coordinatesStr );
97
+
98
+ // then
99
+ assertEquals (2 , result .size ());
100
+ assertEquals (8.695556 , result .get (0 ).get (0 ), 0.000001 );
101
+ assertEquals (49.392701 , result .get (0 ).get (1 ), 0.000001 );
102
+ assertEquals (8.684623 , result .get (1 ).get (0 ), 0.000001 );
103
+ assertEquals (49.398284 , result .get (1 ).get (1 ), 0.000001 );
104
+ }
105
+
106
+ @ Test
107
+ void parseCoordinatesFromString_ShouldHandleQuotedStrings () {
108
+ // given
109
+ String coordinatesStr = "\" [[8.695556, 49.392701], [8.684623, 49.398284]]\" " ;
110
+
111
+ // when
112
+ List <List <Double >> result = MatrixAlgorithmLoadTest .parseCoordinatesFromString (coordinatesStr );
113
+
114
+ // then
115
+ assertEquals (2 , result .size ());
116
+ assertEquals (8.695556 , result .get (0 ).get (0 ), 0.000001 );
117
+ assertEquals (49.392701 , result .get (0 ).get (1 ), 0.000001 );
118
+ }
119
+
120
+ @ Test
121
+ void parseCoordinatesFromString_ShouldThrowExceptionForNullInput () {
122
+ // when & then
123
+ assertThrows (RequestBodyCreationException .class ,
124
+ () -> MatrixAlgorithmLoadTest .parseCoordinatesFromString (null ));
125
+ }
126
+
127
+ @ Test
128
+ void parseCoordinatesFromString_ShouldThrowExceptionForEmptyInput () {
129
+ // when & then
130
+ assertThrows (RequestBodyCreationException .class , () -> MatrixAlgorithmLoadTest .parseCoordinatesFromString ("" ));
131
+ }
132
+
133
+ @ Test
134
+ void parseCoordinatesFromString_ShouldThrowExceptionForInvalidCoordinatePair () {
135
+ // given
136
+ String invalidCoordinates = "[[8.695556], [8.684623, 49.398284]]" ;
137
+
138
+ // when & then
139
+ assertThrows (RequestBodyCreationException .class ,
140
+ () -> MatrixAlgorithmLoadTest .parseCoordinatesFromString (invalidCoordinates ));
141
+ }
142
+
143
+ @ Test
144
+ void parseIntegerArrayFromString_ShouldParseValidArray () {
145
+ // given
146
+ String arrayStr = "[0, 1, 2]" ;
147
+
148
+ // when
149
+ List <Integer > result = MatrixAlgorithmLoadTest .parseIntegerArrayFromString (arrayStr );
150
+
151
+ // then
152
+ assertEquals (3 , result .size ());
153
+ assertEquals (0 , result .get (0 ));
154
+ assertEquals (1 , result .get (1 ));
155
+ assertEquals (2 , result .get (2 ));
156
+ }
157
+
158
+ @ Test
159
+ void parseIntegerArrayFromString_ShouldHandleQuotedStrings () {
160
+ // given
161
+ String arrayStr = "\" [0, 1, 2]\" " ;
162
+
163
+ // when
164
+ List <Integer > result = MatrixAlgorithmLoadTest .parseIntegerArrayFromString (arrayStr );
165
+
166
+ // then
167
+ assertEquals (3 , result .size ());
168
+ assertEquals (0 , result .get (0 ));
169
+ assertEquals (1 , result .get (1 ));
170
+ assertEquals (2 , result .get (2 ));
171
+ }
172
+
173
+ @ Test
174
+ void parseIntegerArrayFromString_ShouldReturnEmptyListForEmptyArray () {
175
+ // given
176
+ String arrayStr = "[]" ;
177
+
178
+ // when
179
+ List <Integer > result = MatrixAlgorithmLoadTest .parseIntegerArrayFromString (arrayStr );
180
+
181
+ // then
182
+ assertTrue (result .isEmpty ());
183
+ }
184
+
185
+ @ Test
186
+ void parseIntegerArrayFromString_ShouldThrowExceptionForNullInput () {
187
+ // when & then
188
+ assertThrows (RequestBodyCreationException .class ,
189
+ () -> MatrixAlgorithmLoadTest .parseIntegerArrayFromString (null ));
190
+ }
191
+
192
+ @ Test
193
+ void parseIntegerArrayFromString_ShouldThrowExceptionForEmptyInput () {
194
+ // when & then
195
+ assertThrows (RequestBodyCreationException .class , () -> MatrixAlgorithmLoadTest .parseIntegerArrayFromString ("" ));
196
+ }
197
+
198
+ @ Test
199
+ void parseIntegerArrayFromString_ShouldThrowExceptionForInvalidInteger () {
200
+ // given
201
+ String invalidArray = "[0, invalid, 2]" ;
202
+
203
+ // when & then
204
+ assertThrows (RequestBodyCreationException .class ,
205
+ () -> MatrixAlgorithmLoadTest .parseIntegerArrayFromString (invalidArray ));
206
+ }
207
+
208
+ @ Test
209
+ void createRequestBody_WithDifferentMatrixMode () throws JsonProcessingException {
210
+ // given
211
+ when (mockMode .getRequestParams ()).thenReturn (Map .of (
212
+ "preference" , "fastest" ,
213
+ "options" , Map .of ("avoid_features" , Arrays .asList ("highways" ))));
214
+
215
+ // when
216
+ String result = MatrixAlgorithmLoadTest .createRequestBody (mockSession , mockConfig , mockMode );
217
+ JsonNode json = objectMapper .readTree (result );
218
+
219
+ // then
220
+ assertEquals ("fastest" , json .get ("preference" ).asText ());
221
+ assertThat (json .get ("options" )).isNotNull ();
222
+ }
223
+
224
+ @ Test
225
+ void createRequestBody_ShouldHandleComplexSourcesAndDestinations () throws JsonProcessingException {
226
+ // given
227
+ when (mockSession .get ("sources" )).thenReturn ("[0, 1, 2, 3]" );
228
+ when (mockSession .get ("destinations" )).thenReturn ("[4, 5, 6, 7, 8]" );
229
+
230
+ // when
231
+ String result = MatrixAlgorithmLoadTest .createRequestBody (mockSession , mockConfig , mockMode );
232
+ JsonNode json = objectMapper .readTree (result );
233
+
234
+ // then
235
+ JsonNode sources = json .get ("sources" );
236
+ JsonNode destinations = json .get ("destinations" );
237
+
238
+ assertEquals (4 , sources .size ());
239
+ assertEquals (5 , destinations .size ());
240
+ assertEquals (0 , sources .get (0 ).asInt ());
241
+ assertEquals (3 , sources .get (3 ).asInt ());
242
+ assertEquals (4 , destinations .get (0 ).asInt ());
243
+ assertEquals (8 , destinations .get (4 ).asInt ());
244
+ }
245
+
246
+ @ Test
247
+ void createRequestBody_ShouldHandleSingleCoordinate () throws JsonProcessingException {
248
+ // given
249
+ when (mockSession .get ("coordinates" )).thenReturn ("[[8.695556, 49.392701]]" );
250
+ when (mockSession .get ("sources" )).thenReturn ("[0]" );
251
+ when (mockSession .get ("destinations" )).thenReturn ("[0]" );
252
+
253
+ // when
254
+ String result = MatrixAlgorithmLoadTest .createRequestBody (mockSession , mockConfig , mockMode );
255
+ JsonNode json = objectMapper .readTree (result );
256
+
257
+ // then
258
+ JsonNode locations = json .get ("locations" );
259
+ assertEquals (1 , locations .size ());
260
+ assertEquals (8.695556 , locations .get (0 ).get (0 ).asDouble (), 0.000001 );
261
+ assertEquals (49.392701 , locations .get (0 ).get (1 ).asDouble (), 0.000001 );
262
+ }
263
+
264
+ @ Test
265
+ void parseCoordinatesFromString_ShouldHandleLargeCoordinateArray () {
266
+ // given
267
+ String coordinatesStr = "[[8.695556, 49.392701], [8.684623, 49.398284], [8.705916, 49.406309], [8.689981, 49.394522], [8.681502, 49.394791]]" ;
268
+
269
+ // when
270
+ List <List <Double >> result = MatrixAlgorithmLoadTest .parseCoordinatesFromString (coordinatesStr );
271
+
272
+ // then
273
+ assertEquals (5 , result .size ());
274
+ assertEquals (8.695556 , result .get (0 ).get (0 ), 0.000001 );
275
+ assertEquals (49.392701 , result .get (0 ).get (1 ), 0.000001 );
276
+ assertEquals (8.681502 , result .get (4 ).get (0 ), 0.000001 );
277
+ assertEquals (49.394791 , result .get (4 ).get (1 ), 0.000001 );
278
+ }
279
+ }
0 commit comments