OLD | NEW |
| (Empty) |
1 library googleapis.cloudlatencytest.v2.test; | |
2 | |
3 import "dart:core" as core; | |
4 import "dart:collection" as collection; | |
5 import "dart:async" as async; | |
6 import "dart:convert" as convert; | |
7 | |
8 import 'package:http/http.dart' as http; | |
9 import 'package:http/testing.dart' as http_testing; | |
10 import 'package:unittest/unittest.dart' as unittest; | |
11 | |
12 import 'package:googleapis/cloudlatencytest/v2.dart' as api; | |
13 | |
14 class HttpServerMock extends http.BaseClient { | |
15 core.Function _callback; | |
16 core.bool _expectJson; | |
17 | |
18 void register(core.Function callback, core.bool expectJson) { | |
19 _callback = callback; | |
20 _expectJson = expectJson; | |
21 } | |
22 | |
23 async.Future<http.StreamedResponse> send(http.BaseRequest request) { | |
24 if (_expectJson) { | |
25 return request.finalize() | |
26 .transform(convert.UTF8.decoder) | |
27 .join('') | |
28 .then((core.String jsonString) { | |
29 if (jsonString.isEmpty) { | |
30 return _callback(request, null); | |
31 } else { | |
32 return _callback(request, convert.JSON.decode(jsonString)); | |
33 } | |
34 }); | |
35 } else { | |
36 var stream = request.finalize(); | |
37 if (stream == null) { | |
38 return _callback(request, []); | |
39 } else { | |
40 return stream.toBytes().then((data) { | |
41 return _callback(request, data); | |
42 }); | |
43 } | |
44 } | |
45 } | |
46 } | |
47 | |
48 http.StreamedResponse stringResponse( | |
49 core.int status, core.Map headers, core.String body) { | |
50 var stream = new async.Stream.fromIterable([convert.UTF8.encode(body)]); | |
51 return new http.StreamedResponse(stream, status, headers: headers); | |
52 } | |
53 | |
54 buildUnnamed657() { | |
55 var o = new core.List<api.Stats>(); | |
56 o.add(buildStats()); | |
57 o.add(buildStats()); | |
58 return o; | |
59 } | |
60 | |
61 checkUnnamed657(core.List<api.Stats> o) { | |
62 unittest.expect(o, unittest.hasLength(2)); | |
63 checkStats(o[0]); | |
64 checkStats(o[1]); | |
65 } | |
66 | |
67 core.int buildCounterAggregatedStats = 0; | |
68 buildAggregatedStats() { | |
69 var o = new api.AggregatedStats(); | |
70 buildCounterAggregatedStats++; | |
71 if (buildCounterAggregatedStats < 3) { | |
72 o.stats = buildUnnamed657(); | |
73 } | |
74 buildCounterAggregatedStats--; | |
75 return o; | |
76 } | |
77 | |
78 checkAggregatedStats(api.AggregatedStats o) { | |
79 buildCounterAggregatedStats++; | |
80 if (buildCounterAggregatedStats < 3) { | |
81 checkUnnamed657(o.stats); | |
82 } | |
83 buildCounterAggregatedStats--; | |
84 } | |
85 | |
86 core.int buildCounterAggregatedStatsReply = 0; | |
87 buildAggregatedStatsReply() { | |
88 var o = new api.AggregatedStatsReply(); | |
89 buildCounterAggregatedStatsReply++; | |
90 if (buildCounterAggregatedStatsReply < 3) { | |
91 o.testValue = "foo"; | |
92 } | |
93 buildCounterAggregatedStatsReply--; | |
94 return o; | |
95 } | |
96 | |
97 checkAggregatedStatsReply(api.AggregatedStatsReply o) { | |
98 buildCounterAggregatedStatsReply++; | |
99 if (buildCounterAggregatedStatsReply < 3) { | |
100 unittest.expect(o.testValue, unittest.equals('foo')); | |
101 } | |
102 buildCounterAggregatedStatsReply--; | |
103 } | |
104 | |
105 core.int buildCounterDoubleValue = 0; | |
106 buildDoubleValue() { | |
107 var o = new api.DoubleValue(); | |
108 buildCounterDoubleValue++; | |
109 if (buildCounterDoubleValue < 3) { | |
110 o.label = "foo"; | |
111 o.value = 42.0; | |
112 } | |
113 buildCounterDoubleValue--; | |
114 return o; | |
115 } | |
116 | |
117 checkDoubleValue(api.DoubleValue o) { | |
118 buildCounterDoubleValue++; | |
119 if (buildCounterDoubleValue < 3) { | |
120 unittest.expect(o.label, unittest.equals('foo')); | |
121 unittest.expect(o.value, unittest.equals(42.0)); | |
122 } | |
123 buildCounterDoubleValue--; | |
124 } | |
125 | |
126 core.int buildCounterIntValue = 0; | |
127 buildIntValue() { | |
128 var o = new api.IntValue(); | |
129 buildCounterIntValue++; | |
130 if (buildCounterIntValue < 3) { | |
131 o.label = "foo"; | |
132 o.value = "foo"; | |
133 } | |
134 buildCounterIntValue--; | |
135 return o; | |
136 } | |
137 | |
138 checkIntValue(api.IntValue o) { | |
139 buildCounterIntValue++; | |
140 if (buildCounterIntValue < 3) { | |
141 unittest.expect(o.label, unittest.equals('foo')); | |
142 unittest.expect(o.value, unittest.equals('foo')); | |
143 } | |
144 buildCounterIntValue--; | |
145 } | |
146 | |
147 buildUnnamed658() { | |
148 var o = new core.List<api.DoubleValue>(); | |
149 o.add(buildDoubleValue()); | |
150 o.add(buildDoubleValue()); | |
151 return o; | |
152 } | |
153 | |
154 checkUnnamed658(core.List<api.DoubleValue> o) { | |
155 unittest.expect(o, unittest.hasLength(2)); | |
156 checkDoubleValue(o[0]); | |
157 checkDoubleValue(o[1]); | |
158 } | |
159 | |
160 buildUnnamed659() { | |
161 var o = new core.List<api.IntValue>(); | |
162 o.add(buildIntValue()); | |
163 o.add(buildIntValue()); | |
164 return o; | |
165 } | |
166 | |
167 checkUnnamed659(core.List<api.IntValue> o) { | |
168 unittest.expect(o, unittest.hasLength(2)); | |
169 checkIntValue(o[0]); | |
170 checkIntValue(o[1]); | |
171 } | |
172 | |
173 buildUnnamed660() { | |
174 var o = new core.List<api.StringValue>(); | |
175 o.add(buildStringValue()); | |
176 o.add(buildStringValue()); | |
177 return o; | |
178 } | |
179 | |
180 checkUnnamed660(core.List<api.StringValue> o) { | |
181 unittest.expect(o, unittest.hasLength(2)); | |
182 checkStringValue(o[0]); | |
183 checkStringValue(o[1]); | |
184 } | |
185 | |
186 core.int buildCounterStats = 0; | |
187 buildStats() { | |
188 var o = new api.Stats(); | |
189 buildCounterStats++; | |
190 if (buildCounterStats < 3) { | |
191 o.doubleValues = buildUnnamed658(); | |
192 o.intValues = buildUnnamed659(); | |
193 o.stringValues = buildUnnamed660(); | |
194 o.time = 42.0; | |
195 } | |
196 buildCounterStats--; | |
197 return o; | |
198 } | |
199 | |
200 checkStats(api.Stats o) { | |
201 buildCounterStats++; | |
202 if (buildCounterStats < 3) { | |
203 checkUnnamed658(o.doubleValues); | |
204 checkUnnamed659(o.intValues); | |
205 checkUnnamed660(o.stringValues); | |
206 unittest.expect(o.time, unittest.equals(42.0)); | |
207 } | |
208 buildCounterStats--; | |
209 } | |
210 | |
211 core.int buildCounterStatsReply = 0; | |
212 buildStatsReply() { | |
213 var o = new api.StatsReply(); | |
214 buildCounterStatsReply++; | |
215 if (buildCounterStatsReply < 3) { | |
216 o.testValue = "foo"; | |
217 } | |
218 buildCounterStatsReply--; | |
219 return o; | |
220 } | |
221 | |
222 checkStatsReply(api.StatsReply o) { | |
223 buildCounterStatsReply++; | |
224 if (buildCounterStatsReply < 3) { | |
225 unittest.expect(o.testValue, unittest.equals('foo')); | |
226 } | |
227 buildCounterStatsReply--; | |
228 } | |
229 | |
230 core.int buildCounterStringValue = 0; | |
231 buildStringValue() { | |
232 var o = new api.StringValue(); | |
233 buildCounterStringValue++; | |
234 if (buildCounterStringValue < 3) { | |
235 o.label = "foo"; | |
236 o.value = "foo"; | |
237 } | |
238 buildCounterStringValue--; | |
239 return o; | |
240 } | |
241 | |
242 checkStringValue(api.StringValue o) { | |
243 buildCounterStringValue++; | |
244 if (buildCounterStringValue < 3) { | |
245 unittest.expect(o.label, unittest.equals('foo')); | |
246 unittest.expect(o.value, unittest.equals('foo')); | |
247 } | |
248 buildCounterStringValue--; | |
249 } | |
250 | |
251 | |
252 main() { | |
253 unittest.group("obj-schema-AggregatedStats", () { | |
254 unittest.test("to-json--from-json", () { | |
255 var o = buildAggregatedStats(); | |
256 var od = new api.AggregatedStats.fromJson(o.toJson()); | |
257 checkAggregatedStats(od); | |
258 }); | |
259 }); | |
260 | |
261 | |
262 unittest.group("obj-schema-AggregatedStatsReply", () { | |
263 unittest.test("to-json--from-json", () { | |
264 var o = buildAggregatedStatsReply(); | |
265 var od = new api.AggregatedStatsReply.fromJson(o.toJson()); | |
266 checkAggregatedStatsReply(od); | |
267 }); | |
268 }); | |
269 | |
270 | |
271 unittest.group("obj-schema-DoubleValue", () { | |
272 unittest.test("to-json--from-json", () { | |
273 var o = buildDoubleValue(); | |
274 var od = new api.DoubleValue.fromJson(o.toJson()); | |
275 checkDoubleValue(od); | |
276 }); | |
277 }); | |
278 | |
279 | |
280 unittest.group("obj-schema-IntValue", () { | |
281 unittest.test("to-json--from-json", () { | |
282 var o = buildIntValue(); | |
283 var od = new api.IntValue.fromJson(o.toJson()); | |
284 checkIntValue(od); | |
285 }); | |
286 }); | |
287 | |
288 | |
289 unittest.group("obj-schema-Stats", () { | |
290 unittest.test("to-json--from-json", () { | |
291 var o = buildStats(); | |
292 var od = new api.Stats.fromJson(o.toJson()); | |
293 checkStats(od); | |
294 }); | |
295 }); | |
296 | |
297 | |
298 unittest.group("obj-schema-StatsReply", () { | |
299 unittest.test("to-json--from-json", () { | |
300 var o = buildStatsReply(); | |
301 var od = new api.StatsReply.fromJson(o.toJson()); | |
302 checkStatsReply(od); | |
303 }); | |
304 }); | |
305 | |
306 | |
307 unittest.group("obj-schema-StringValue", () { | |
308 unittest.test("to-json--from-json", () { | |
309 var o = buildStringValue(); | |
310 var od = new api.StringValue.fromJson(o.toJson()); | |
311 checkStringValue(od); | |
312 }); | |
313 }); | |
314 | |
315 | |
316 unittest.group("resource-StatscollectionResourceApi", () { | |
317 unittest.test("method--updateaggregatedstats", () { | |
318 | |
319 var mock = new HttpServerMock(); | |
320 api.StatscollectionResourceApi res = new api.CloudlatencytestApi(mock).sta
tscollection; | |
321 var arg_request = buildAggregatedStats(); | |
322 mock.register(unittest.expectAsync((http.BaseRequest req, json) { | |
323 var obj = new api.AggregatedStats.fromJson(json); | |
324 checkAggregatedStats(obj); | |
325 | |
326 var path = (req.url).path; | |
327 var pathOffset = 0; | |
328 var index; | |
329 var subPart; | |
330 unittest.expect(path.substring(pathOffset, pathOffset + 1), unittest.equ
als("/")); | |
331 pathOffset += 1; | |
332 unittest.expect(path.substring(pathOffset, pathOffset + 19), unittest.eq
uals("v2/statscollection/")); | |
333 pathOffset += 19; | |
334 unittest.expect(path.substring(pathOffset, pathOffset + 21), unittest.eq
uals("updateaggregatedstats")); | |
335 pathOffset += 21; | |
336 | |
337 var query = (req.url).query; | |
338 var queryOffset = 0; | |
339 var queryMap = {}; | |
340 addQueryParam(n, v) => queryMap.putIfAbsent(n, () => []).add(v); | |
341 parseBool(n) { | |
342 if (n == "true") return true; | |
343 if (n == "false") return false; | |
344 if (n == null) return null; | |
345 throw new core.ArgumentError("Invalid boolean: $n"); | |
346 } | |
347 if (query.length > 0) { | |
348 for (var part in query.split("&")) { | |
349 var keyvalue = part.split("="); | |
350 addQueryParam(core.Uri.decodeQueryComponent(keyvalue[0]), core.Uri.d
ecodeQueryComponent(keyvalue[1])); | |
351 } | |
352 } | |
353 | |
354 | |
355 var h = { | |
356 "content-type" : "application/json; charset=utf-8", | |
357 }; | |
358 var resp = convert.JSON.encode(buildAggregatedStatsReply()); | |
359 return new async.Future.value(stringResponse(200, h, resp)); | |
360 }), true); | |
361 res.updateaggregatedstats(arg_request).then(unittest.expectAsync(((api.Agg
regatedStatsReply response) { | |
362 checkAggregatedStatsReply(response); | |
363 }))); | |
364 }); | |
365 | |
366 unittest.test("method--updatestats", () { | |
367 | |
368 var mock = new HttpServerMock(); | |
369 api.StatscollectionResourceApi res = new api.CloudlatencytestApi(mock).sta
tscollection; | |
370 var arg_request = buildStats(); | |
371 mock.register(unittest.expectAsync((http.BaseRequest req, json) { | |
372 var obj = new api.Stats.fromJson(json); | |
373 checkStats(obj); | |
374 | |
375 var path = (req.url).path; | |
376 var pathOffset = 0; | |
377 var index; | |
378 var subPart; | |
379 unittest.expect(path.substring(pathOffset, pathOffset + 1), unittest.equ
als("/")); | |
380 pathOffset += 1; | |
381 unittest.expect(path.substring(pathOffset, pathOffset + 19), unittest.eq
uals("v2/statscollection/")); | |
382 pathOffset += 19; | |
383 unittest.expect(path.substring(pathOffset, pathOffset + 11), unittest.eq
uals("updatestats")); | |
384 pathOffset += 11; | |
385 | |
386 var query = (req.url).query; | |
387 var queryOffset = 0; | |
388 var queryMap = {}; | |
389 addQueryParam(n, v) => queryMap.putIfAbsent(n, () => []).add(v); | |
390 parseBool(n) { | |
391 if (n == "true") return true; | |
392 if (n == "false") return false; | |
393 if (n == null) return null; | |
394 throw new core.ArgumentError("Invalid boolean: $n"); | |
395 } | |
396 if (query.length > 0) { | |
397 for (var part in query.split("&")) { | |
398 var keyvalue = part.split("="); | |
399 addQueryParam(core.Uri.decodeQueryComponent(keyvalue[0]), core.Uri.d
ecodeQueryComponent(keyvalue[1])); | |
400 } | |
401 } | |
402 | |
403 | |
404 var h = { | |
405 "content-type" : "application/json; charset=utf-8", | |
406 }; | |
407 var resp = convert.JSON.encode(buildStatsReply()); | |
408 return new async.Future.value(stringResponse(200, h, resp)); | |
409 }), true); | |
410 res.updatestats(arg_request).then(unittest.expectAsync(((api.StatsReply re
sponse) { | |
411 checkStatsReply(response); | |
412 }))); | |
413 }); | |
414 | |
415 }); | |
416 | |
417 | |
418 } | |
419 | |
OLD | NEW |