@@ -19,18 +19,58 @@ import (
19
19
"path/filepath"
20
20
"testing"
21
21
22
+ "github.com/google/go-cmp/cmp"
22
23
"github.com/googleapis/librarian/internal/sidekick/internal/config"
23
24
)
24
25
25
26
var (
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 )
27
31
)
28
32
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
+
29
69
func TestCreateModelOpenAPI (t * testing.T ) {
30
70
cfg := & config.Config {
31
71
General : config.GeneralConfig {
32
72
SpecificationFormat : "openapi" ,
33
- ServiceConfig : path . Join ( testdataDir , "googleapis/google/cloud/secretmanager/v1/secretmanager_v1.yaml" ) ,
73
+ ServiceConfig : secretManagerYamlFullPath ,
34
74
SpecificationSource : path .Join (testdataDir , "openapi/secretmanager_openapi_v1.json" ),
35
75
},
36
76
}
@@ -50,7 +90,7 @@ func TestCreateModelProtobuf(t *testing.T) {
50
90
cfg := & config.Config {
51
91
General : config.GeneralConfig {
52
92
SpecificationFormat : "protobuf" ,
53
- ServiceConfig : "google/cloud/secretmanager/v1/secretmanager_v1.yaml" ,
93
+ ServiceConfig : secretManagerYamlRelative ,
54
94
SpecificationSource : "google/cloud/secretmanager/v1" ,
55
95
},
56
96
Source : map [string ]string {
@@ -73,7 +113,7 @@ func TestCreateModelOverrides(t *testing.T) {
73
113
cfg := & config.Config {
74
114
General : config.GeneralConfig {
75
115
SpecificationFormat : "protobuf" ,
76
- ServiceConfig : "google/cloud/secretmanager/v1/secretmanager_v1.yaml" ,
116
+ ServiceConfig : secretManagerYamlRelative ,
77
117
SpecificationSource : "google/cloud/secretmanager/v1" ,
78
118
},
79
119
Source : map [string ]string {
@@ -108,7 +148,7 @@ func TestCreateModelNone(t *testing.T) {
108
148
cfg := & config.Config {
109
149
General : config.GeneralConfig {
110
150
SpecificationFormat : "none" ,
111
- ServiceConfig : "google/cloud/secretmanager/v1/secretmanager_v1.yaml" ,
151
+ ServiceConfig : secretManagerYamlRelative ,
112
152
SpecificationSource : "none" ,
113
153
},
114
154
Source : map [string ]string {
@@ -126,3 +166,42 @@ func TestCreateModelNone(t *testing.T) {
126
166
t .Errorf ("expected `nil` model with source format == none" )
127
167
}
128
168
}
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