@@ -16,8 +16,159 @@ package api
1616
1717import (
1818 "testing"
19+
20+ "github.com/google/go-cmp/cmp"
1921)
2022
23+ // setupTestModel helper creates a minimal API model for testing resource identification.
24+ func setupTestModel (serviceID string , pathTemplate * PathTemplate , fields []* Field ) (* API , * PathBinding ) {
25+ binding := & PathBinding {PathTemplate : pathTemplate }
26+ method := & Method {
27+ Name : "TestMethod" ,
28+ InputType : & Message {
29+ Fields : fields ,
30+ },
31+ PathInfo : & PathInfo {
32+ Bindings : []* PathBinding {binding },
33+ },
34+ }
35+ service := & Service {
36+ ID : serviceID ,
37+ Methods : []* Method {method },
38+ }
39+ model := & API {
40+ Services : []* Service {service },
41+ }
42+ return model , binding
43+ }
44+
2145func TestIdentifyTargetResources (t * testing.T ) {
22- // TODO(#4099): Implement IdentifyTargetResources for consistent explicit resource identification in the sidekick parser.
46+ for _ , test := range []struct {
47+ name string
48+ serviceID string
49+ path * PathTemplate
50+ fields []* Field
51+ want * TargetResource
52+ }{
53+ {
54+ name : "explicit: standard resource reference" ,
55+ serviceID : "any.service" ,
56+ path : NewPathTemplate ().
57+ WithLiteral ("projects" ).WithVariableNamed ("project" ),
58+ fields : []* Field {
59+ {
60+ Name : "project" ,
61+ Typez : STRING_TYPE ,
62+ ResourceReference : & ResourceReference {Type : "cloudresourcemanager.googleapis.com/Project" },
63+ },
64+ },
65+ want : & TargetResource {
66+ FieldPaths : [][]string {{"project" }},
67+ },
68+ },
69+ {
70+ name : "explicit: multiple resource references" ,
71+ serviceID : "any.service" ,
72+ path : NewPathTemplate ().
73+ WithLiteral ("projects" ).WithVariableNamed ("project" ).
74+ WithLiteral ("locations" ).WithVariableNamed ("location" ),
75+ fields : []* Field {
76+ {
77+ Name : "project" ,
78+ Typez : STRING_TYPE ,
79+ ResourceReference : & ResourceReference {Type : "cloudresourcemanager.googleapis.com/Project" },
80+ },
81+ {
82+ Name : "location" ,
83+ Typez : STRING_TYPE , // Often locations are string IDs
84+ ResourceReference : & ResourceReference {Type : "locations.googleapis.com/Location" },
85+ },
86+ },
87+ want : & TargetResource {
88+ FieldPaths : [][]string {{"project" }, {"location" }},
89+ },
90+ },
91+ {
92+ name : "explicit: nested field reference" ,
93+ serviceID : "any.service" ,
94+ path : NewPathTemplate ().
95+ WithLiteral ("projects" ).WithVariableNamed ("parent" , "project" ),
96+ fields : []* Field {
97+ {
98+ Name : "parent" ,
99+ Typez : MESSAGE_TYPE ,
100+ MessageType : & Message {
101+ Fields : []* Field {
102+ {
103+ Name : "project" ,
104+ Typez : STRING_TYPE ,
105+ ResourceReference : & ResourceReference {Type : "cloudresourcemanager.googleapis.com/Project" },
106+ },
107+ },
108+ },
109+ },
110+ },
111+ want : & TargetResource {
112+ FieldPaths : [][]string {{"parent" , "project" }},
113+ },
114+ },
115+ } {
116+ t .Run (test .name , func (t * testing.T ) {
117+ model , binding := setupTestModel (test .serviceID , test .path , test .fields )
118+ IdentifyTargetResources (model )
119+
120+ got := binding .TargetResource
121+ if diff := cmp .Diff (test .want , got ); diff != "" {
122+ t .Errorf ("mismatch (-want +got):\n %s" , diff )
123+ }
124+ })
125+ }
126+ }
127+
128+ func TestIdentifyTargetResources_NoMatch (t * testing.T ) {
129+ for _ , test := range []struct {
130+ name string
131+ serviceID string
132+ path * PathTemplate
133+ fields []* Field
134+ }{
135+ {
136+ name : "Explicit: missing reference returns nil" ,
137+ serviceID : "any.service" ,
138+ path : NewPathTemplate ().
139+ WithLiteral ("projects" ).WithVariableNamed ("project" ),
140+ fields : []* Field {
141+ {Name : "project" , Typez : STRING_TYPE }, // No ResourceReference
142+ },
143+ },
144+ {
145+ name : "Explicit: partial reference returns nil" ,
146+ serviceID : "any.service" ,
147+ path : NewPathTemplate ().
148+ WithLiteral ("projects" ).WithVariableNamed ("project" ).
149+ WithLiteral ("glossaries" ).WithVariableNamed ("glossary" ),
150+ fields : []* Field {
151+ {
152+ Name : "project" ,
153+ Typez : STRING_TYPE ,
154+ ResourceReference : & ResourceReference {Type : "cloudresourcemanager.googleapis.com/Project" },
155+ },
156+ {
157+ Name : "glossary" ,
158+ Typez : STRING_TYPE ,
159+ // No ResourceReference on the second variable
160+ },
161+ },
162+ },
163+ } {
164+ t .Run (test .name , func (t * testing.T ) {
165+ model , binding := setupTestModel (test .serviceID , test .path , test .fields )
166+ IdentifyTargetResources (model )
167+
168+ got := binding .TargetResource
169+ if got != nil {
170+ t .Errorf ("IdentifyTargetResources() = %v, want nil" , got )
171+ }
172+ })
173+ }
23174}
0 commit comments