Skip to content

Commit

Permalink
Merge branch 'master' into issue-659
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Sneeringer authored Oct 14, 2020
2 parents 05b692c + bfbbf28 commit c0a00c2
Show file tree
Hide file tree
Showing 26 changed files with 524 additions and 829 deletions.
61 changes: 24 additions & 37 deletions rules/aip0131/http_body_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,55 +15,42 @@
package aip0131

import (
"strings"
"testing"

dpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/jhump/protoreflect/desc/builder"
"google.golang.org/genproto/googleapis/api/annotations"
"google.golang.org/protobuf/proto"
"github.com/googleapis/api-linter/rules/internal/testutils"
)

func TestHttpBody(t *testing.T) {
tests := []struct {
testName string
body string
methodName string
msg string
Body string
MethodName string
problems testutils.Problems
}{
{"Valid", "", "GetBook", ""},
{"Invalid", "*", "GetBook", "HTTP body"},
{"Irrelevant", "*", "AcquireBook", ""},
{"Valid", "", "GetBook", nil},
{"Invalid", "*", "GetBook", testutils.Problems{{Message: "HTTP body"}}},
{"Irrelevant", "*", "AcquireBook", nil},
}

for _, test := range tests {
t.Run(test.testName, func(t *testing.T) {
// Create a MethodOptions with the annotation set.
opts := &dpb.MethodOptions{}
httpRule := &annotations.HttpRule{
Pattern: &annotations.HttpRule_Get{
Get: "/v1/{name=publishers/*/books/*}",
},
Body: test.body,
}
proto.SetExtension(opts, annotations.E_Http, httpRule)

// Create a minimal service with a AIP-131 Get method
// (or with a different method, in the "Irrelevant" case).
service, err := builder.NewService("Library").AddMethod(builder.NewMethod(test.methodName,
builder.RpcTypeMessage(builder.NewMessage("GetBookRequest"), false),
builder.RpcTypeMessage(builder.NewMessage("Book"), false),
).SetOptions(opts)).Build()
if err != nil {
t.Fatalf("Could not build %s method.", test.methodName)
}

// Run the method, ensure we get what we expect.
problems := httpBody.Lint(service.GetFile())
if test.msg == "" && len(problems) > 0 {
t.Errorf("Got %v, expected no problems.", problems)
} else if test.msg != "" && !strings.Contains(problems[0].Message, test.msg) {
t.Errorf("Got %q, expected message containing %q", problems[0].Message, test.msg)
file := testutils.ParseProto3Tmpl(t, `
import "google/api/annotations.proto";
service Library {
rpc {{.MethodName}}({{.MethodName}}Request) returns (Book) {
option (google.api.http) = {
get: "/v1/{name=publishers/*/book/*}"
body: "{{.Body}}"
};
}
}
message Book {}
message {{.MethodName}}Request {}
`, test)
method := file.GetServices()[0].GetMethods()[0]
problems := httpBody.Lint(file)
if diff := test.problems.SetDescriptor(method).Diff(problems); diff != "" {
t.Error(diff)
}
})
}
Expand Down
66 changes: 23 additions & 43 deletions rules/aip0131/http_method_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,63 +15,43 @@
package aip0131

import (
"strings"
"testing"

dpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/jhump/protoreflect/desc/builder"
"google.golang.org/genproto/googleapis/api/annotations"
"google.golang.org/protobuf/proto"
"github.com/googleapis/api-linter/rules/internal/testutils"
)

func TestHttpMethod(t *testing.T) {
// Set up GET and POST HTTP annotations.
httpGet := &annotations.HttpRule{
Pattern: &annotations.HttpRule_Get{
Get: "/v1/{name=publishers/*/books/*}",
},
}
httpPost := &annotations.HttpRule{
Pattern: &annotations.HttpRule_Post{
Post: "/v1/{name=publishers/*/books/*}",
},
}

// Set up testing permutations.
tests := []struct {
testName string
httpRule *annotations.HttpRule
methodName string
msg string
Method string
MethodName string
problems testutils.Problems
}{
{"Valid", httpGet, "GetBook", ""},
{"Invalid", httpPost, "GetBook", "HTTP GET"},
{"Irrelevant", httpPost, "AcquireBook", ""},
{"Valid", "get", "GetBook", nil},
{"Invalid", "post", "GetBook", testutils.Problems{{Message: "HTTP GET"}}},
{"Irrelevant", "post", "AcquireBook", nil},
}

// Run each test.
for _, test := range tests {
t.Run(test.testName, func(t *testing.T) {
// Create a MethodOptions with the annotation set.
opts := &dpb.MethodOptions{}
proto.SetExtension(opts, annotations.E_Http, test.httpRule)

// Create a minimal service with a AIP-131 Get method
// (or with a different method, in the "Irrelevant" case).
service, err := builder.NewService("Library").AddMethod(builder.NewMethod(test.methodName,
builder.RpcTypeMessage(builder.NewMessage("GetBookRequest"), false),
builder.RpcTypeMessage(builder.NewMessage("Book"), false),
).SetOptions(opts)).Build()
if err != nil {
t.Fatalf("Could not build %s method.", test.methodName)
}

// Run the method, ensure we get what we expect.
problems := httpMethod.Lint(service.GetFile())
if test.msg == "" && len(problems) > 0 {
t.Errorf("Got %v, expected no problems.", problems)
} else if test.msg != "" && !strings.Contains(problems[0].Message, test.msg) {
t.Errorf("Got %q, expected message containing %q", problems[0].Message, test.msg)
file := testutils.ParseProto3Tmpl(t, `
import "google/api/annotations.proto";
service Library {
rpc {{.MethodName}}({{.MethodName}}Request) returns (Book) {
option (google.api.http) = {
{{.Method}}: "/v1/{name=publishers/*/books/*}"
};
}
}
message Book {}
message {{.MethodName}}Request {}
`, test)
method := file.GetServices()[0].GetMethods()[0]
problems := httpMethod.Lint(file)
if diff := test.problems.SetDescriptor(method).Diff(problems); diff != "" {
t.Error(diff)
}
})
}
Expand Down
29 changes: 13 additions & 16 deletions rules/aip0131/request_message_name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ import (
"testing"

"github.com/googleapis/api-linter/rules/internal/testutils"
"github.com/jhump/protoreflect/desc/builder"
)

func TestRequestMessageName(t *testing.T) {
// Set up the testing permutations.
tests := []struct {
testName string
methodName string
reqMessageName string
MethodName string
ReqMessageName string
problems testutils.Problems
}{
{"Valid", "GetBook", "GetBookRequest", testutils.Problems{}},
Expand All @@ -38,19 +37,17 @@ func TestRequestMessageName(t *testing.T) {
// Run each test individually.
for _, test := range tests {
t.Run(test.testName, func(t *testing.T) {
// Create a minimal service with a AIP-131 Get method
// (or with a different method, in the "Irrelevant" case).
service, err := builder.NewService("Library").AddMethod(builder.NewMethod(test.methodName,
builder.RpcTypeMessage(builder.NewMessage(test.reqMessageName), false),
builder.RpcTypeMessage(builder.NewMessage("Book"), false),
)).Build()
if err != nil {
t.Fatalf("Could not build %s method.", test.methodName)
}

// Run the lint rule, and establish that it returns the expected problems.
problems := requestMessageName.Lint(service.GetFile())
if diff := test.problems.SetDescriptor(service.GetMethods()[0]).Diff(problems); diff != "" {
f := testutils.ParseProto3Tmpl(t, `
service Library {
rpc {{.MethodName}}({{.ReqMessageName}}) returns (Book) {}
}
message {{.ReqMessageName}} {}
{{if ne .ReqMessageName "Book"}}
message Book {}
{{end}}
`, test)
m := f.GetServices()[0].GetMethods()[0]
if diff := test.problems.SetDescriptor(m).Diff(requestMessageName.Lint(f)); diff != "" {
t.Errorf(diff)
}
})
Expand Down
31 changes: 13 additions & 18 deletions rules/aip0131/response_message_name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ import (
"testing"

"github.com/googleapis/api-linter/rules/internal/testutils"
"github.com/jhump/protoreflect/desc/builder"
)

func TestResponseMessageName(t *testing.T) {
// Set up the testing permutations.
tests := []struct {
testName string
methodName string
respMessageName string
MethodName string
RespMessageName string
problems testutils.Problems
}{
{"Valid", "GetBook", "Book", testutils.Problems{}},
Expand All @@ -37,21 +36,17 @@ func TestResponseMessageName(t *testing.T) {
// Run each test individually.
for _, test := range tests {
t.Run(test.testName, func(t *testing.T) {
// Create a minimal service with a AIP-131 Get method
// (or with a different method, in the "Irrelevant" case).
service, err := builder.NewService("Library").AddMethod(builder.NewMethod(test.methodName,
builder.RpcTypeMessage(builder.NewMessage("GetBookRequest"), false),
builder.RpcTypeMessage(builder.NewMessage(test.respMessageName), false),
)).Build()
if err != nil {
t.Fatalf("Could not build %s method.", test.methodName)
}

// Run the lint rule, and establish that it returns the correct
// number of problems.
problems := responseMessageName.Lint(service.GetFile())
if diff := test.problems.SetDescriptor(service.GetMethods()[0]).Diff(problems); diff != "" {
t.Errorf(diff)
file := testutils.ParseProto3Tmpl(t, `
service Library {
rpc {{.MethodName}}({{.MethodName}}Request) returns ({{.RespMessageName}}) {}
}
message {{.MethodName}}Request {}
message {{.RespMessageName}} {}
`, test)
method := file.GetServices()[0].GetMethods()[0]
problems := responseMessageName.Lint(file)
if diff := test.problems.SetDescriptor(method).Diff(problems); diff != "" {
t.Error(diff)
}
})
}
Expand Down
28 changes: 11 additions & 17 deletions rules/aip0132/request_message_name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ import (
"testing"

"github.com/googleapis/api-linter/rules/internal/testutils"
"github.com/jhump/protoreflect/desc/builder"
)

func TestRequestMessageName(t *testing.T) {
// Set up the testing permutations.
tests := []struct {
testName string
methodName string
reqMessageName string
MethodName string
ReqMessageName string
problems testutils.Problems
}{
{"Valid", "ListBooks", "ListBooksRequest", testutils.Problems{}},
Expand All @@ -37,20 +36,15 @@ func TestRequestMessageName(t *testing.T) {
// Run each test individually.
for _, test := range tests {
t.Run(test.testName, func(t *testing.T) {
// Create a minimal service with a AIP-131 Get method
// (or with a different method, in the "Irrelevant" case).
service, err := builder.NewService("Library").AddMethod(builder.NewMethod(test.methodName,
builder.RpcTypeMessage(builder.NewMessage(test.reqMessageName), false),
builder.RpcTypeMessage(builder.NewMessage("ListBooksResponse"), false),
)).Build()
if err != nil {
t.Fatalf("Could not build %s method.", test.methodName)
}

// Run the lint rule, and establish that it returns the correct
// number of problems.
problems := requestMessageName.Lint(service.GetFile())
if diff := test.problems.SetDescriptor(service.GetMethods()[0]).Diff(problems); diff != "" {
f := testutils.ParseProto3Tmpl(t, `
service Library {
rpc {{.MethodName}}({{.ReqMessageName}}) returns (ListBooksResponse) {}
}
message {{.ReqMessageName}} {}
message ListBooksResponse {}
`, test)
m := f.GetServices()[0].GetMethods()[0]
if diff := test.problems.SetDescriptor(m).Diff(requestMessageName.Lint(f)); diff != "" {
t.Errorf(diff)
}
})
Expand Down
31 changes: 13 additions & 18 deletions rules/aip0132/response_message_name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ import (
"testing"

"github.com/googleapis/api-linter/rules/internal/testutils"
"github.com/jhump/protoreflect/desc/builder"
)

func TestResponseMessageName(t *testing.T) {
// Set up the testing permutations.
tests := []struct {
testName string
methodName string
respMessageName string
MethodName string
RespMessageName string
problems testutils.Problems
}{
{"Valid", "ListBooks", "ListBooksResponse", testutils.Problems{}},
Expand All @@ -37,21 +36,17 @@ func TestResponseMessageName(t *testing.T) {
// Run each test individually.
for _, test := range tests {
t.Run(test.testName, func(t *testing.T) {
// Create a minimal service with a AIP-131 Get method
// (or with a different method, in the "Irrelevant" case).
service, err := builder.NewService("Library").AddMethod(builder.NewMethod(test.methodName,
builder.RpcTypeMessage(builder.NewMessage("ListBooksRequest"), false),
builder.RpcTypeMessage(builder.NewMessage(test.respMessageName), false),
)).Build()
if err != nil {
t.Fatalf("Could not build %s method.", test.methodName)
}

// Run the lint rule, and establish that it returns the correct
// number of problems.
problems := responseMessageName.Lint(service.GetFile())
if diff := test.problems.SetDescriptor(service.GetMethods()[0]).Diff(problems); diff != "" {
t.Errorf(diff)
file := testutils.ParseProto3Tmpl(t, `
service Library {
rpc {{.MethodName}}({{.MethodName}}Request) returns ({{.RespMessageName}}) {}
}
message {{.MethodName}}Request {}
message {{.RespMessageName}} {}
`, test)
method := file.GetServices()[0].GetMethods()[0]
problems := responseMessageName.Lint(file)
if diff := test.problems.SetDescriptor(method).Diff(problems); diff != "" {
t.Error(diff)
}
})
}
Expand Down
Loading

0 comments on commit c0a00c2

Please sign in to comment.