|
13 | 13 |
|
14 | 14 | package com.ibm.cloud.sdk.core.test.service; |
15 | 15 |
|
| 16 | +import static org.junit.Assert.assertEquals; |
| 17 | +import static org.junit.Assert.assertNotNull; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.io.IOException; |
| 21 | +import java.io.InputStream; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | + |
| 28 | +import org.junit.Test; |
| 29 | + |
16 | 30 | import com.google.gson.JsonObject; |
17 | 31 | import com.ibm.cloud.sdk.core.http.HttpMediaType; |
18 | 32 | import com.ibm.cloud.sdk.core.http.RequestBuilder; |
|
27 | 41 | import okhttp3.Request; |
28 | 42 | import okhttp3.RequestBody; |
29 | 43 | import okio.Buffer; |
30 | | -import org.junit.Test; |
31 | | - |
32 | | -import java.io.File; |
33 | | -import java.io.IOException; |
34 | | -import java.io.InputStream; |
35 | | -import java.util.Arrays; |
36 | | -import java.util.ArrayList; |
37 | | -import java.util.List; |
38 | | - |
39 | | -import static org.junit.Assert.assertEquals; |
40 | | -import static org.junit.Assert.assertNotNull; |
41 | | -import static org.junit.Assert.assertNotEquals; |
42 | 44 |
|
43 | 45 | /** |
44 | 46 | * The Class RequestBuilderTest. |
45 | 47 | */ |
| 48 | +@SuppressWarnings("serial") |
46 | 49 | public class RequestBuilderTest { |
47 | 50 |
|
48 | 51 | private static final String X_TOKEN = "x-token"; |
@@ -283,7 +286,7 @@ public void testConstructHttpUrlGood() { |
283 | 286 | String[] pathParameters = { "param1", "param2" }; |
284 | 287 | HttpUrl url = RequestBuilder.constructHttpUrl("https://myserver.com/testservice/api", pathSegments, pathParameters); |
285 | 288 | assertNotNull(url); |
286 | | - assertNotEquals("https://myserver.com/testservice/api/v1/seg1/param1/seg2/param3/seg3", url); |
| 289 | + assertEquals("https://myserver.com/testservice/api/v1/seg1/param1/seg2/param2/seg3", url.toString()); |
287 | 290 | } |
288 | 291 |
|
289 | 292 | @Test |
@@ -343,6 +346,132 @@ public void testConstructHttpUrlNull() { |
343 | 346 | RequestBuilder.constructHttpUrl(null, pathSegments, pathParameters); |
344 | 347 | } |
345 | 348 |
|
| 349 | + @Test |
| 350 | + public void testResolveRequestUrlGood1() { |
| 351 | + Map<String, String> pathParameters = new HashMap<String, String>() {{ |
| 352 | + put("param_1", "param1"); |
| 353 | + put("param_2", "param2"); |
| 354 | + }}; |
| 355 | + |
| 356 | + HttpUrl url = RequestBuilder.resolveRequestUrl( |
| 357 | + "https://myserver.com", |
| 358 | + "/v1/seg1/{param_1}/seg2/{param_2}/seg3", |
| 359 | + pathParameters); |
| 360 | + assertNotNull(url); |
| 361 | + assertEquals("https://myserver.com/v1/seg1/param1/seg2/param2/seg3", url.toString()); |
| 362 | + } |
| 363 | + |
| 364 | + @Test |
| 365 | + public void testResolveRequestUrlGood2() { |
| 366 | + Map<String, String> pathParameters = new HashMap<String, String>() {{ |
| 367 | + put("param_1", "param1"); |
| 368 | + put("param_2", "param2"); |
| 369 | + }}; |
| 370 | + |
| 371 | + HttpUrl url = RequestBuilder.resolveRequestUrl( |
| 372 | + "https://myserver.com", |
| 373 | + "v1/seg1/{param_1}/seg2/{param_2}/seg3", |
| 374 | + pathParameters); |
| 375 | + assertNotNull(url); |
| 376 | + assertEquals("https://myserver.com/v1/seg1/param1/seg2/param2/seg3", url.toString()); |
| 377 | + } |
| 378 | + |
| 379 | + @Test |
| 380 | + public void testResolveRequestUrlGood3() { |
| 381 | + Map<String, String> pathParameters = new HashMap<String, String>() {{ |
| 382 | + put("param_1", "param1"); |
| 383 | + put("param_2", "param2"); |
| 384 | + }}; |
| 385 | + |
| 386 | + HttpUrl url = RequestBuilder.resolveRequestUrl( |
| 387 | + "https://myserver.com/testservice/api", |
| 388 | + "/v1/seg1/{param_1}/seg2/{param_2}/seg3", |
| 389 | + pathParameters); |
| 390 | + assertNotNull(url); |
| 391 | + assertEquals("https://myserver.com/testservice/api/v1/seg1/param1/seg2/param2/seg3", url.toString()); |
| 392 | + } |
| 393 | + |
| 394 | + @Test |
| 395 | + public void testResolveRequestUrlGood4() { |
| 396 | + Map<String, String> pathParameters = new HashMap<String, String>() {{ |
| 397 | + put("param_1", "param1"); |
| 398 | + put("param_2", "param2"); |
| 399 | + }}; |
| 400 | + |
| 401 | + HttpUrl url = RequestBuilder.resolveRequestUrl( |
| 402 | + "https://myserver.com/testservice/api", |
| 403 | + "v1/seg1/{param_1}/seg2/{param_2}/seg3", |
| 404 | + pathParameters); |
| 405 | + assertNotNull(url); |
| 406 | + assertEquals("https://myserver.com/testservice/api/v1/seg1/param1/seg2/param2/seg3", url.toString()); |
| 407 | + } |
| 408 | + |
| 409 | + @Test |
| 410 | + public void testResolveRequestUrlEmptyPath1() { |
| 411 | + HttpUrl url = RequestBuilder.resolveRequestUrl("https://myserver.com/testservice/api", ""); |
| 412 | + assertNotNull(url); |
| 413 | + assertEquals("https://myserver.com/testservice/api", url.toString()); |
| 414 | + } |
| 415 | + |
| 416 | + @Test |
| 417 | + public void testResolveRequestUrlEmptyPath2() { |
| 418 | + HttpUrl url = RequestBuilder.resolveRequestUrl("https://myserver.com/testservice/api", null); |
| 419 | + assertNotNull(url); |
| 420 | + assertEquals("https://myserver.com/testservice/api", url.toString()); |
| 421 | + } |
| 422 | + |
| 423 | + @Test |
| 424 | + public void testResolveRequestUrlPathSlash1() { |
| 425 | + HttpUrl url = RequestBuilder.resolveRequestUrl("https://myserver.com/testservice/api", "/"); |
| 426 | + assertNotNull(url); |
| 427 | + assertEquals("https://myserver.com/testservice/api/", url.toString()); |
| 428 | + } |
| 429 | + |
| 430 | + @Test |
| 431 | + public void testResolveRequestUrlPathSlash2() { |
| 432 | + HttpUrl url = RequestBuilder.resolveRequestUrl("https://myserver.com", "/"); |
| 433 | + assertNotNull(url); |
| 434 | + assertEquals("https://myserver.com/", url.toString()); |
| 435 | + } |
| 436 | + |
| 437 | + @Test |
| 438 | + public void testResolveRequestUrlEncodedPathParams() { |
| 439 | + Map<String, String> pathParameters = new HashMap<String, String>() {{ |
| 440 | + put("param_1", "param/1"); |
| 441 | + put("param_2", "param 2"); |
| 442 | + }}; |
| 443 | + |
| 444 | + HttpUrl url = RequestBuilder.resolveRequestUrl( |
| 445 | + "https://myserver.com", |
| 446 | + "/v1/seg1/{param_1}/seg2/{param_2}", |
| 447 | + pathParameters); |
| 448 | + assertNotNull(url); |
| 449 | + assertEquals("https://myserver.com/v1/seg1/param%2F1/seg2/param%202", url.toString()); |
| 450 | + } |
| 451 | + |
| 452 | + @Test(expected = IllegalArgumentException.class) |
| 453 | + public void testResolveRequestUrlEmptyPathParam() { |
| 454 | + Map<String, String> pathParameters = new HashMap<String, String>() {{ |
| 455 | + put("param_1", ""); |
| 456 | + put("param_2", "param2"); |
| 457 | + }}; |
| 458 | + |
| 459 | + HttpUrl url = RequestBuilder.resolveRequestUrl( |
| 460 | + "https://myserver.com/testservice/api", |
| 461 | + "v1/seg1/{param_1}/seg2/{param_2}/seg3", |
| 462 | + pathParameters); |
| 463 | + } |
| 464 | + |
| 465 | + @Test(expected = IllegalArgumentException.class) |
| 466 | + public void testResolveRequestUrlEmpty() { |
| 467 | + RequestBuilder.resolveRequestUrl("", "/"); |
| 468 | + } |
| 469 | + |
| 470 | + @Test(expected = IllegalArgumentException.class) |
| 471 | + public void testResolveRequestUrlNull() { |
| 472 | + RequestBuilder.resolveRequestUrl(null, "/"); |
| 473 | + } |
| 474 | + |
346 | 475 | /** |
347 | 476 | * Test bodyContent() with a model instance. |
348 | 477 | * @throws IOException |
|
0 commit comments