@@ -1946,3 +1946,122 @@ func TestFindResourceIDField(t *testing.T) {
19461946 })
19471947 }
19481948}
1949+
1950+ func TestFindQuickstartMethod (t * testing.T ) {
1951+ fooMethod := & Method {Name : "FooMethod" }
1952+ listPolicies := & Method {Name : "ListAccessPolicies" , IsAIPStandardList : true , OutputType : & Message {Resource : & Resource {Singular : "accesspolicy" }}}
1953+ getPolicy := & Method {Name : "GetAccessPolicy" , IsAIPStandardGet : true }
1954+ createPolicy := & Method {Name : "CreateAccessPolicy" , IsAIPStandardCreate : true }
1955+ deletePolicy := & Method {Name : "DeleteAccessPolicy" , IsAIPStandardDelete : true }
1956+ updatePolicy := & Method {Name : "UpdateAccessPolicy" , IsAIPStandardUpdate : true }
1957+ listOther := & Method {Name : "ListOtherThings" , IsAIPStandardList : true , OutputType : & Message {Resource : & Resource {Singular : "otherthing" }}}
1958+
1959+ testCases := []struct {
1960+ name string
1961+ service * Service
1962+ want * Method
1963+ }{
1964+ {
1965+ name : "empty service" ,
1966+ service : & Service {Name : "AccessPolicyService" , Methods : []* Method {}},
1967+ want : nil ,
1968+ },
1969+ {
1970+ name : "fallback to simple method when no standard methods exist" ,
1971+ service : & Service {Name : "AccessPolicyService" , Methods : []* Method {fooMethod }},
1972+ want : fooMethod ,
1973+ },
1974+ {
1975+ name : "prefer non-deprecated simple method" ,
1976+ service : & Service {Name : "AccessPolicyService" , Methods : []* Method {{Name : "DeprecatedList" , Deprecated : true }, fooMethod }},
1977+ want : fooMethod ,
1978+ },
1979+ {
1980+ name : "only get method" ,
1981+ service : & Service {Name : "AccessPolicyService" , Methods : []* Method {getPolicy }},
1982+ want : getPolicy ,
1983+ },
1984+ {
1985+ name : "prioritizes list over get" ,
1986+ service : & Service {Name : "AccessPolicyService" , Methods : []* Method {getPolicy , listOther }},
1987+ want : listOther ,
1988+ },
1989+ {
1990+ name : "prioritizes create over delete" ,
1991+ service : & Service {Name : "AccessPolicyService" , Methods : []* Method {deletePolicy , createPolicy }},
1992+ want : createPolicy ,
1993+ },
1994+ {
1995+ name : "prioritizes delete over update" ,
1996+ service : & Service {Name : "AccessPolicyService" , Methods : []* Method {updatePolicy , deletePolicy }},
1997+ want : deletePolicy ,
1998+ },
1999+ {
2000+ name : "tie-breaking on name matching (ListAccessPolicies vs ListOtherThings for AccessPolicyService)" ,
2001+ service : & Service {Name : "AccessPolicyService" , Methods : []* Method {listOther , listPolicies }},
2002+ want : listPolicies ,
2003+ },
2004+ {
2005+ name : "tie-breaking fallback to resource singular/plural" ,
2006+ service : & Service {Name : "AccessPolicyService" , Methods : []* Method {listOther , {Name : "ListPolicies" , IsAIPStandardList : true , OutputType : & Message {Resource : & Resource {Singular : "accesspolicy" }}}}},
2007+ want : & Method {Name : "ListPolicies" , IsAIPStandardList : true , OutputType : & Message {Resource : & Resource {Singular : "accesspolicy" }}}, // matches singular 'accesspolicy'
2008+ },
2009+ }
2010+
2011+ for _ , tc := range testCases {
2012+ t .Run (tc .name , func (t * testing.T ) {
2013+ got := findQuickstartMethod (tc .service )
2014+ if diff := cmp .Diff (tc .want , got , cmpopts .IgnoreFields (Method {}, "Service" , "Model" )); diff != "" {
2015+ t .Errorf ("findQuickstartMethod() mismatch (-want +got):\n %s" , diff )
2016+ }
2017+ })
2018+ }
2019+ }
2020+
2021+ func TestFindQuickstartService (t * testing.T ) {
2022+ fooMethod := & Method {Name : "FooMethod" }
2023+ serviceA := & Service {Name : "ServiceA" , Methods : []* Method {fooMethod }}
2024+ serviceB := & Service {Name : "SecretManagerService" , Methods : []* Method {fooMethod }}
2025+ deprecatedService := & Service {Name : "SecretManagerService" , Deprecated : true , Methods : []* Method {fooMethod }}
2026+
2027+ testCases := []struct {
2028+ name string
2029+ api * API
2030+ want * Service
2031+ }{
2032+ {
2033+ name : "no services" ,
2034+ api : & API {Name : "secretmanager" , Services : nil },
2035+ want : nil ,
2036+ },
2037+ {
2038+ name : "one service" ,
2039+ api : & API {Name : "secretmanager" , Services : []* Service {serviceA }},
2040+ want : serviceA ,
2041+ },
2042+ {
2043+ name : "match service name to api name" ,
2044+ api : & API {Name : "secretmanager" , Services : []* Service {serviceA , serviceB }},
2045+ want : serviceB ,
2046+ },
2047+ {
2048+ name : "no match defaults to first" ,
2049+ api : & API {Name : "otherapi" , Services : []* Service {serviceA , serviceB }},
2050+ want : serviceA ,
2051+ },
2052+ {
2053+ name : "prefer non-deprecated service" ,
2054+ api : & API {Name : "secretmanager" , Services : []* Service {deprecatedService , serviceA }},
2055+ want : serviceA ,
2056+ },
2057+ }
2058+
2059+ for _ , tc := range testCases {
2060+ t .Run (tc .name , func (t * testing.T ) {
2061+ got := findQuickstartService (tc .api )
2062+ if diff := cmp .Diff (tc .want , got ); diff != "" {
2063+ t .Errorf ("findQuickstartService() mismatch (-want +got):\n %s" , diff )
2064+ }
2065+ })
2066+ }
2067+ }
0 commit comments