Skip to content

Commit

Permalink
Merge "b/146684996: fix processApis for multipleApis"
Browse files Browse the repository at this point in the history
  • Loading branch information
TAOXUY authored and Gerrit Code Review committed Jan 21, 2020
2 parents c59e8a1 + ac24f3d commit d119ade
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 12 deletions.
21 changes: 10 additions & 11 deletions src/go/configinfo/service_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,6 @@ func NewServiceInfoFromServiceConfig(serviceConfig *confpb.Service, id string, o
Options: opts,
}

for _, apiName := range serviceConfig.GetApis() {
serviceInfo.ApiNames = append(serviceInfo.ApiNames, apiName.Name)
}

// Order matters.
serviceInfo.processEndpoints()
serviceInfo.processApis()
Expand Down Expand Up @@ -163,13 +159,16 @@ func (s *ServiceInfo) processEmptyJwksUriByOpenID() error {

func (s *ServiceInfo) processApis() {
s.Methods = make(map[string]*methodInfo)
api := s.serviceConfig.GetApis()[0]
for _, method := range api.GetMethods() {
s.Methods[fmt.Sprintf("%s.%s", api.GetName(), method.GetName())] =
&methodInfo{
ShortName: method.GetName(),
ApiName: api.GetName(),
}
for _, api := range s.serviceConfig.GetApis() {
s.ApiNames = append(s.ApiNames, api.Name)

for _, method := range api.GetMethods() {
s.Methods[fmt.Sprintf("%s.%s", api.GetName(), method.GetName())] =
&methodInfo{
ShortName: method.GetName(),
ApiName: api.GetName(),
}
}
}
}

Expand Down
108 changes: 107 additions & 1 deletion src/go/configinfo/service_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ var (
testConfigID = "2019-03-02r0"
)


func TestProcessEndpoints(t *testing.T) {
testData := []struct {
desc string
Expand Down Expand Up @@ -1171,3 +1170,110 @@ func TestProcessEmptyJwksUriByOpenID(t *testing.T) {
}
}
}

func TestProcessApis(t *testing.T) {
testData := []struct {
desc string
fakeServiceConfig *confpb.Service
wantMethods map[string]*methodInfo
wantApiNames []string
}{
{
desc: "Succeed, process multiple apis",
fakeServiceConfig: &confpb.Service{
Apis: []*apipb.Api{
{
Name: "api-1",
Methods: []*apipb.Method{
{
Name: "foo",
},
{
Name: "bar",
},
},
},
{
Name: "api-2",
Methods: []*apipb.Method{
{
Name: "foo",
},
{
Name: "bar",
},
},
},
{
Name: "api-3",
Methods: []*apipb.Method{},
},
{
Name: "api-4",
Methods: []*apipb.Method{
{
Name: "bar",
},
{
Name: "baz",
},
},
},
},
},
wantMethods: map[string]*methodInfo{
"api-1.foo": &methodInfo{
ShortName: "foo",
ApiName: "api-1",
},
"api-1.bar": &methodInfo{
ShortName: "bar",
ApiName: "api-1",
},
"api-2.foo": &methodInfo{
ShortName: "foo",
ApiName: "api-2",
},
"api-2.bar": &methodInfo{
ShortName: "bar",
ApiName: "api-2",
},
"api-4.bar": &methodInfo{
ShortName: "bar",
ApiName: "api-4",
},
"api-4.baz": &methodInfo{
ShortName: "baz",
ApiName: "api-4",
},
},
wantApiNames: []string{
"api-1",
"api-2",
"api-3",
"api-4",
},
},
}

for i, tc := range testData {

serviceInfo := &ServiceInfo{
serviceConfig: tc.fakeServiceConfig,
}
serviceInfo.processApis()

for key, gotMethod := range serviceInfo.Methods {
wantMethod := tc.wantMethods[key]
if eq := cmp.Equal(gotMethod, wantMethod, cmp.Comparer(proto.Equal)); !eq {
t.Errorf("Test Desc(%d): %s,\ngot Method: %v,\nwant Method: %v", i, tc.desc, gotMethod, wantMethod)
}
}
for idx, gotApiName := range serviceInfo.ApiNames {
wantApiName := tc.wantApiNames[idx]
if gotApiName != wantApiName {
t.Errorf("Test Desc(%d): %s,\ngot ApiName: %v,\nwant Apiname: %v", i, tc.desc, gotApiName, wantApiName)
}
}
}
}

0 comments on commit d119ade

Please sign in to comment.