@@ -19,18 +19,58 @@ import (
1919 "path/filepath"
2020 "testing"
2121
22+ "github.com/google/go-cmp/cmp"
2223 "github.com/googleapis/librarian/internal/sidekick/internal/config"
2324)
2425
2526var (
26- testdataDir , _ = filepath .Abs ("../../testdata" )
27+ testdataDir , _ = filepath .Abs ("../../testdata" )
28+ discoSourceFile = path .Join (testdataDir , "disco/compute.v1.json" )
29+ secretManagerYamlRelative = "google/cloud/secretmanager/v1/secretmanager_v1.yaml"
30+ secretManagerYamlFullPath = path .Join (testdataDir , "googleapis" , secretManagerYamlRelative )
2731)
2832
33+ func TestCreateModelDisco (t * testing.T ) {
34+ cfg := & config.Config {
35+ General : config.GeneralConfig {
36+ SpecificationFormat : "disco" ,
37+ ServiceConfig : secretManagerYamlFullPath ,
38+ SpecificationSource : discoSourceFile ,
39+ },
40+ }
41+ got , err := CreateModel (cfg )
42+ if err != nil {
43+ t .Fatal (err )
44+ }
45+ wantName := "secretmanager"
46+ wantTitle := "Secret Manager API"
47+ wantDescription := "Stores sensitive data such as API keys, passwords, and certificates.\n Provides convenience while improving security."
48+ wantPackageName := "google.cloud.secretmanager.v1"
49+ if got .Name != wantName {
50+ t .Errorf ("want = %q; got = %q" , wantName , got .Name )
51+ }
52+ if got .Title != wantTitle {
53+ t .Errorf ("want = %q; got = %q" , wantTitle , got .Title )
54+ }
55+ if diff := cmp .Diff (got .Description , wantDescription ); diff != "" {
56+ t .Errorf ("description mismatch (-want, +got):\n %s" , diff )
57+ }
58+ if got .PackageName != wantPackageName {
59+ t .Errorf ("want = %q; got = %q" , wantPackageName , got .PackageName )
60+ }
61+ // This is strange, but we want to verify the package name override from
62+ // the service config YAML applies to the message IDs too.
63+ wantMessage := ".google.cloud.secretmanager.v1.ZoneSetPolicyRequest"
64+ if _ , ok := got .State .MessageByID [wantMessage ]; ! ok {
65+ t .Errorf ("missing message %s in MessageByID index" , wantMessage )
66+ }
67+ }
68+
2969func TestCreateModelOpenAPI (t * testing.T ) {
3070 cfg := & config.Config {
3171 General : config.GeneralConfig {
3272 SpecificationFormat : "openapi" ,
33- ServiceConfig : path . Join ( testdataDir , "googleapis/google/cloud/secretmanager/v1/secretmanager_v1.yaml" ) ,
73+ ServiceConfig : secretManagerYamlFullPath ,
3474 SpecificationSource : path .Join (testdataDir , "openapi/secretmanager_openapi_v1.json" ),
3575 },
3676 }
@@ -50,7 +90,7 @@ func TestCreateModelProtobuf(t *testing.T) {
5090 cfg := & config.Config {
5191 General : config.GeneralConfig {
5292 SpecificationFormat : "protobuf" ,
53- ServiceConfig : "google/cloud/secretmanager/v1/secretmanager_v1.yaml" ,
93+ ServiceConfig : secretManagerYamlRelative ,
5494 SpecificationSource : "google/cloud/secretmanager/v1" ,
5595 },
5696 Source : map [string ]string {
@@ -73,7 +113,7 @@ func TestCreateModelOverrides(t *testing.T) {
73113 cfg := & config.Config {
74114 General : config.GeneralConfig {
75115 SpecificationFormat : "protobuf" ,
76- ServiceConfig : "google/cloud/secretmanager/v1/secretmanager_v1.yaml" ,
116+ ServiceConfig : secretManagerYamlRelative ,
77117 SpecificationSource : "google/cloud/secretmanager/v1" ,
78118 },
79119 Source : map [string ]string {
@@ -108,7 +148,7 @@ func TestCreateModelNone(t *testing.T) {
108148 cfg := & config.Config {
109149 General : config.GeneralConfig {
110150 SpecificationFormat : "none" ,
111- ServiceConfig : "google/cloud/secretmanager/v1/secretmanager_v1.yaml" ,
151+ ServiceConfig : secretManagerYamlRelative ,
112152 SpecificationSource : "none" ,
113153 },
114154 Source : map [string ]string {
@@ -126,3 +166,42 @@ func TestCreateModelNone(t *testing.T) {
126166 t .Errorf ("expected `nil` model with source format == none" )
127167 }
128168}
169+
170+ func TestCreateModelUnknown (t * testing.T ) {
171+ cfg := & config.Config {
172+ General : config.GeneralConfig {
173+ SpecificationFormat : "--unknown--" ,
174+ ServiceConfig : secretManagerYamlRelative ,
175+ SpecificationSource : "none" ,
176+ },
177+ Source : map [string ]string {
178+ "googleapis-root" : path .Join (testdataDir , "googleapis" ),
179+ "name-override" : "Name Override" ,
180+ "title-override" : "Title Override" ,
181+ "description-override" : "Description Override" ,
182+ },
183+ }
184+ if got , err := CreateModel (cfg ); err == nil {
185+ t .Errorf ("expected error with unknown specification format, got=%v" , got )
186+ }
187+ }
188+
189+ func TestCreateModelBadParse (t * testing.T ) {
190+ cfg := & config.Config {
191+ General : config.GeneralConfig {
192+ SpecificationFormat : "openapi" ,
193+ ServiceConfig : secretManagerYamlRelative ,
194+ // Note the mismatch between the format and the file contents.
195+ SpecificationSource : discoSourceFile ,
196+ },
197+ Source : map [string ]string {
198+ "googleapis-root" : path .Join (testdataDir , "googleapis" ),
199+ "name-override" : "Name Override" ,
200+ "title-override" : "Title Override" ,
201+ "description-override" : "Description Override" ,
202+ },
203+ }
204+ if got , err := CreateModel (cfg ); err == nil {
205+ t .Errorf ("expected error with bad specification, got=%v" , got )
206+ }
207+ }
0 commit comments