|
37 | 37 | import java.util.Arrays;
|
38 | 38 | import java.util.List;
|
39 | 39 | import java.util.Map;
|
| 40 | +import java.util.Set; |
40 | 41 | import org.junit.Rule;
|
41 | 42 | import org.junit.Test;
|
42 | 43 | import org.junit.rules.ExpectedException;
|
@@ -377,6 +378,26 @@ public void complexResourceIdInParent() {
|
377 | 378 | Truth.assertThat(match.get("cell2")).isEqualTo("broomba");
|
378 | 379 | }
|
379 | 380 |
|
| 381 | + @Test |
| 382 | + public void complexResourcePathTemplateVariables() { |
| 383 | + String pattern = |
| 384 | + "projects/{foo}_{bar}/zones/{zone_a}-{zone_b}_{zone_c}/machines/{cell1}.{cell2}"; |
| 385 | + PathTemplate template = PathTemplate.create(pattern); |
| 386 | + Set<String> variables = template.vars(); |
| 387 | + Truth.assertThat(variables) |
| 388 | + .containsExactly("foo", "bar", "zone_a", "zone_b", "zone_c", "cell1", "cell2"); |
| 389 | + |
| 390 | + pattern = "projects/{foo}_{bar}/zones/*"; |
| 391 | + template = PathTemplate.create(pattern); |
| 392 | + Map<String, String> match = |
| 393 | + template.match("https://www.googleapis.com/compute/v1/projects/foo1_bar2/zones/azone"); |
| 394 | + Truth.assertThat(match).isNotNull(); |
| 395 | + Truth.assertThat(match.get("foo")).isEqualTo("foo1"); |
| 396 | + Truth.assertThat(match.get("bar")).isEqualTo("bar2"); |
| 397 | + variables = template.vars(); |
| 398 | + System.out.println("DEL: vars: " + variables); |
| 399 | + } |
| 400 | + |
380 | 401 | @Test
|
381 | 402 | public void complexResourceBasicInvalidIds() {
|
382 | 403 | thrown.expect(ValidationException.class);
|
@@ -575,6 +596,57 @@ public void instantiateWithUnusualCharactersNoEncoding() {
|
575 | 596 | Truth.assertThat(instance).isEqualTo("bar/asdf:;`~,.<>[]!@#$%^&*()");
|
576 | 597 | }
|
577 | 598 |
|
| 599 | + @Test |
| 600 | + public void instantiateWithComplexResourceId_basic() { |
| 601 | + PathTemplate template = PathTemplate.create("projects/{project}/zones/{zone_a}~{zone_b}"); |
| 602 | + String instance = |
| 603 | + template.instantiate("project", "a/b/c", "zone_a", "apple", "zone_b", "baseball"); |
| 604 | + Truth.assertThat(instance).isEqualTo("projects/a%2Fb%2Fc/zones/apple~baseball"); |
| 605 | + } |
| 606 | + |
| 607 | + @Test |
| 608 | + public void instantiateWithComplexResourceId_mixedSeparators() { |
| 609 | + PathTemplate template = |
| 610 | + PathTemplate.create( |
| 611 | + "projects/{project}/zones/{zone_a}~{zone_b}.{zone_c}-{zone_d}~{zone_e}"); |
| 612 | + String instance = |
| 613 | + template.instantiate( |
| 614 | + "project", |
| 615 | + "a/b/c", |
| 616 | + "zone_a", |
| 617 | + "apple", |
| 618 | + "zone_b", |
| 619 | + "baseball/basketball", |
| 620 | + "zone_c", |
| 621 | + "cat/kitty", |
| 622 | + "zone_d", |
| 623 | + "dog/hound", |
| 624 | + "zone_e", |
| 625 | + "12345"); |
| 626 | + Truth.assertThat(instance) |
| 627 | + .isEqualTo( |
| 628 | + "projects/a%2Fb%2Fc/zones/apple~baseball%2Fbasketball.cat%2Fkitty-dog%2Fhound~12345"); |
| 629 | + } |
| 630 | + |
| 631 | + @Test |
| 632 | + public void instantiateWithComplexResourceId_mixedSeparatorsInParent() { |
| 633 | + PathTemplate template = |
| 634 | + PathTemplate.create("projects/{project_a}~{project_b}.{project_c}/zones/{zone_a}~{zone_b}"); |
| 635 | + String instance = |
| 636 | + template.instantiate( |
| 637 | + "project_a", |
| 638 | + "a/b/c", |
| 639 | + "project_b", |
| 640 | + "foo", |
| 641 | + "project_c", |
| 642 | + "bar", |
| 643 | + "zone_a", |
| 644 | + "apple", |
| 645 | + "zone_b", |
| 646 | + "baseball"); |
| 647 | + Truth.assertThat(instance).isEqualTo("projects/a%2Fb%2Fc~foo.bar/zones/apple~baseball"); |
| 648 | + } |
| 649 | + |
578 | 650 | // Other
|
579 | 651 | // =====
|
580 | 652 |
|
|
0 commit comments