|
31 | 31 | package com.google.api.pathtemplate; |
32 | 32 |
|
33 | 33 | import com.google.common.collect.ImmutableMap; |
| 34 | +import com.google.common.collect.ImmutableSet; |
34 | 35 | import com.google.common.truth.Truth; |
35 | 36 | import java.util.Map; |
36 | 37 | import java.util.Set; |
@@ -894,6 +895,188 @@ void testTemplateWithMultipleSimpleBindings() { |
894 | 895 | Truth.assertThat(url).isEqualTo("v1/shelves/s1/books/b1"); |
895 | 896 | } |
896 | 897 |
|
| 898 | + @Test |
| 899 | + void name() { |
| 900 | + PathTemplate pathTemplate = |
| 901 | + PathTemplate.create("projects/{project}/zones/{zone}/{parent_name}"); |
| 902 | + System.out.println( |
| 903 | + pathTemplate.instantiate("project", "project1", "zone", "zone1", "parent_name", "name1")); |
| 904 | + } |
| 905 | + |
| 906 | + @Test |
| 907 | + void testGetResourceLiterals_simplePath() { |
| 908 | + PathTemplate template = |
| 909 | + PathTemplate.create("/compute/v1/projects/{project}/locations/{location}/widgets/{widget}"); |
| 910 | + Truth.assertThat(template.getResourceLiterals()) |
| 911 | + .containsExactly("projects", "locations", "widgets"); |
| 912 | + } |
| 913 | + |
| 914 | + @Test |
| 915 | + void testGetResourceLiterals_regexPath() { |
| 916 | + PathTemplate template = |
| 917 | + PathTemplate.create("v1/projects/{project=projects/*}/instances/{instance_id=instances/*}"); |
| 918 | + Truth.assertThat(template.getResourceLiterals()).containsExactly("projects", "instances"); |
| 919 | + } |
| 920 | + |
| 921 | + @Test |
| 922 | + void testGetResourceSegments_onlyNonResourceLiterals() { |
| 923 | + PathTemplate template = PathTemplate.create("compute/v1/projects"); |
| 924 | + Truth.assertThat(template.getResourceLiterals()).isEmpty(); |
| 925 | + } |
| 926 | + |
| 927 | + @Test |
| 928 | + void testGetResourceLiterals_nameBinding() { |
| 929 | + PathTemplate template = PathTemplate.create("v1/{name=projects/*/instances/*}"); |
| 930 | + Truth.assertThat(template.getResourceLiterals()).containsExactly("projects", "instances"); |
| 931 | + } |
| 932 | + |
| 933 | + @Test |
| 934 | + void testGetResourceSegments_complexResourceId() { |
| 935 | + PathTemplate template = PathTemplate.create("projects/{project}/zones/{zone_a}~{zone_b}"); |
| 936 | + Truth.assertThat(template.getResourceLiterals()).containsExactly("projects", "zones"); |
| 937 | + } |
| 938 | + |
| 939 | + @Test |
| 940 | + void testGetResourceLiterals_customVerb() { |
| 941 | + PathTemplate template = PathTemplate.create("projects/{project}/instances/{instance}:execute"); |
| 942 | + Truth.assertThat(template.getResourceLiterals()).containsExactly("projects", "instances"); |
| 943 | + } |
| 944 | + |
| 945 | + @Test |
| 946 | + void testGetResourceLiterals_multipleVersions() { |
| 947 | + PathTemplate template = |
| 948 | + PathTemplate.create( |
| 949 | + "v1/compute/v2/projects/{project}/locations/{location}/widgets/{widget}"); |
| 950 | + Truth.assertThat(template.getResourceLiterals()) |
| 951 | + .containsExactly("projects", "locations", "widgets"); |
| 952 | + } |
| 953 | + |
| 954 | + @Test |
| 955 | + void testGetResourceLiterals_namedBindings() { |
| 956 | + PathTemplate template = |
| 957 | + PathTemplate.create( |
| 958 | + "/compute/v1/projects/{project}/zones/{zone}/{parent_name=reservations/*/reservationBlocks/*/reservationSubBlocks/*}"); |
| 959 | + Truth.assertThat(template.getResourceLiterals()) |
| 960 | + .containsExactly( |
| 961 | + "projects", "zones", "reservations", "reservationBlocks", "reservationSubBlocks"); |
| 962 | + } |
| 963 | + |
| 964 | + @Test |
| 965 | + void testGetCanonicalResourceName_namedBindings() { |
| 966 | + PathTemplate template = |
| 967 | + PathTemplate.create( |
| 968 | + "/v1/projects/{project}/locations/{location}/{parent_name=reservations/*/reservationBlocks/*/reservationSubBlocks/*}/heuristics/{heuristic}"); |
| 969 | + |
| 970 | + Set<String> resourceLiterals = ImmutableSet.of("projects", "locations", "heuristics"); |
| 971 | + Truth.assertThat(template.getCanonicalResourceName(resourceLiterals)) |
| 972 | + .isEqualTo( |
| 973 | + "projects/{project}/locations/{location}/{parent_name=reservations/*/reservationBlocks/*/reservationSubBlocks/*}/heuristics/{heuristic}"); |
| 974 | + } |
| 975 | + |
| 976 | + @Test |
| 977 | + void testGetCanonicalResourceName_namedBindingsSimple() { |
| 978 | + Set<String> moreKnownResources = ImmutableSet.of("projects", "locations", "bars"); |
| 979 | + PathTemplate template = PathTemplate.create("/v1/{bar=projects/*/locations/*/bars/*}"); |
| 980 | + Truth.assertThat(template.getCanonicalResourceName(moreKnownResources)) |
| 981 | + .isEqualTo("{bar=projects/*/locations/*/bars/*}"); |
| 982 | + } |
| 983 | + |
| 984 | + @Test |
| 985 | + void testGetCanonicalResourceName_simplePath() { |
| 986 | + Set<String> knownResources = ImmutableSet.of("projects", "locations", "instances", "widgets"); |
| 987 | + PathTemplate template = |
| 988 | + PathTemplate.create("/compute/v1/projects/{project}/locations/{location}/widgets/{widget}"); |
| 989 | + Truth.assertThat(template.getCanonicalResourceName(knownResources)) |
| 990 | + .isEqualTo("projects/{project}/locations/{location}/widgets/{widget}"); |
| 991 | + } |
| 992 | + |
| 993 | + @Test |
| 994 | + void testGetCanonicalResourceName_regexVariables() { |
| 995 | + Set<String> knownResources = ImmutableSet.of("projects", "locations", "instances", "widgets"); |
| 996 | + PathTemplate template = |
| 997 | + PathTemplate.create("v1/projects/{project=projects/*}/instances/{instance_id=instances/*}"); |
| 998 | + Truth.assertThat(template.getCanonicalResourceName(knownResources)) |
| 999 | + .isEqualTo("projects/{project}/instances/{instance_id}"); |
| 1000 | + } |
| 1001 | + |
| 1002 | + @Test |
| 1003 | + void testGetCanonicalResourceName_noVariables() { |
| 1004 | + Set<String> knownResources = ImmutableSet.of("projects", "locations", "instances", "widgets"); |
| 1005 | + PathTemplate template = PathTemplate.create("v1/projects/locations"); |
| 1006 | + Truth.assertThat(template.getCanonicalResourceName(knownResources)).isEmpty(); |
| 1007 | + } |
| 1008 | + |
| 1009 | + @Test |
| 1010 | + void testGetCanonicalResourceName_unknownResource() { |
| 1011 | + Set<String> knownResources = ImmutableSet.of("projects", "locations", "instances", "widgets"); |
| 1012 | + PathTemplate template = |
| 1013 | + PathTemplate.create("v1/projects/{project}/unknownResource/{unknownResource}"); |
| 1014 | + Truth.assertThat(template.getCanonicalResourceName(knownResources)) |
| 1015 | + .isEqualTo("projects/{project}"); |
| 1016 | + } |
| 1017 | + |
| 1018 | + @Test |
| 1019 | + void testGetCanonicalResourceName_ignoreVersions() { |
| 1020 | + Set<String> knownResources = ImmutableSet.of("projects", "locations", "instances", "widgets"); |
| 1021 | + PathTemplate template = |
| 1022 | + PathTemplate.create( |
| 1023 | + "v1/compute/v2/projects/{project}/locations/{location}/widgets/{widget}"); |
| 1024 | + Truth.assertThat(template.getCanonicalResourceName(knownResources)) |
| 1025 | + .isEqualTo("projects/{project}/locations/{location}/widgets/{widget}"); |
| 1026 | + } |
| 1027 | + |
| 1028 | + @Test |
| 1029 | + void testGetCanonicalResourceName_customVerb() { |
| 1030 | + Set<String> knownResources = ImmutableSet.of("projects", "locations", "instances", "widgets"); |
| 1031 | + PathTemplate template = PathTemplate.create("projects/{project}/instances/{instance}:execute"); |
| 1032 | + Truth.assertThat(template.getCanonicalResourceName(knownResources)) |
| 1033 | + .isEqualTo("projects/{project}/instances/{instance}"); |
| 1034 | + } |
| 1035 | + |
| 1036 | + @Test |
| 1037 | + void testGetCanonicalResourceName_nameBinding() { |
| 1038 | + Set<String> knownResources = ImmutableSet.of("projects", "locations", "instances", "widgets"); |
| 1039 | + PathTemplate template = PathTemplate.create("v1/{field=projects/*/instances/*}"); |
| 1040 | + Truth.assertThat(template.getCanonicalResourceName(knownResources)) |
| 1041 | + .isEqualTo("{field=projects/*/instances/*}"); |
| 1042 | + } |
| 1043 | + |
| 1044 | + @Test |
| 1045 | + void testGetCanonicalResourceName_nameBindingMixedWithSimpleBinding() { |
| 1046 | + Set<String> knownResources = ImmutableSet.of("projects", "locations", "instances", "widgets"); |
| 1047 | + PathTemplate template = |
| 1048 | + PathTemplate.create("v1/{field=projects/*/instances/*}/actions/{action}"); |
| 1049 | + Truth.assertThat(template.getCanonicalResourceName(knownResources)) |
| 1050 | + .isEqualTo("{field=projects/*/instances/*}/actions/{action}"); |
| 1051 | + } |
| 1052 | + |
| 1053 | + @Test |
| 1054 | + void testGetCanonicalResourceName_nameBindingWithUnknownLiterals() { |
| 1055 | + PathTemplate template = |
| 1056 | + PathTemplate.create( |
| 1057 | + "/compute/v1/projects/{project}/zones/{zone}/{parent_name=reservations/*/reservationBlocks/*/reservationSubBlocks/*}/reservationSlots/{reservation_slot}"); |
| 1058 | + String canonical = template.getCanonicalResourceName(template.getResourceLiterals()); |
| 1059 | + Truth.assertThat(canonical) |
| 1060 | + .isEqualTo( |
| 1061 | + "projects/{project}/zones/{zone}/{parent_name=reservations/*/reservationBlocks/*/reservationSubBlocks/*}/reservationSlots/{reservation_slot}"); |
| 1062 | + } |
| 1063 | + |
| 1064 | + @Test |
| 1065 | + void testGetCanonicalResourceName_nameBindingMixedWithSimpleBinding_moreKnownResources() { |
| 1066 | + Set<String> moreKnownResources = ImmutableSet.of("projects", "instances", "actions"); |
| 1067 | + PathTemplate template = |
| 1068 | + PathTemplate.create("v1/{name=projects/*/instances/*}/actions/{action}"); |
| 1069 | + Truth.assertThat(template.getCanonicalResourceName(moreKnownResources)) |
| 1070 | + .isEqualTo("projects/*/instances/*/actions/{action}"); |
| 1071 | + } |
| 1072 | + |
| 1073 | + @Test |
| 1074 | + void testGetCanonicalResourceName_nullKnownResources() { |
| 1075 | + PathTemplate template = |
| 1076 | + PathTemplate.create("v1/projects/{project}/locations/{location}/widgets/{widget}"); |
| 1077 | + Truth.assertThat(template.getCanonicalResourceName(null)).isEmpty(); |
| 1078 | + } |
| 1079 | + |
897 | 1080 | private static void assertPositionalMatch(Map<String, String> match, String... expected) { |
898 | 1081 | Truth.assertThat(match).isNotNull(); |
899 | 1082 | int i = 0; |
|
0 commit comments