github.com/googleapis/api-linter@v1.65.2/rules/aip0133/http_uri_parent_test.go (about)

     1  // Copyright 2019 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package aip0133
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/googleapis/api-linter/rules/internal/testutils"
    21  )
    22  
    23  func TestHTTPURIParent(t *testing.T) {
    24  	tests := []struct {
    25  		testName   string
    26  		URI        string
    27  		MethodName string
    28  		Pattern    string
    29  		problems   testutils.Problems
    30  	}{
    31  		{"Valid", "/v1/{parent=publishers/*/books/*}", "CreateBook", "publishers/{publisher}/books/{book}", nil},
    32  		{"InvalidVarParent", "/v1/{book=publishers/*/books/*}", "CreateBook", "publishers/{publisher}/books/{book}", testutils.Problems{{Message: "`parent` variable"}}},
    33  		{"NoVarParent", "/v1/publishers/*/books/*", "CreateBook", "publishers/{publisher}/books/{book}", testutils.Problems{{Message: "`parent` variable"}}},
    34  		{"NoParent", "/v1/books/*", "CreateBook", "books/{book}", nil},
    35  		{"MultipleVars", "/v1/{parent=publishers/*}/{book=books/*}", "CreateBook", "publishers/{publisher}/books/{book}", testutils.Problems{{Message: "1 variable"}}},
    36  		{"Irrelevant", "/v1/{book=publishers/*/books/*}", "BuildBook", "publishers/{publisher}/books/{book}", nil},
    37  	}
    38  
    39  	for _, test := range tests {
    40  		t.Run(test.testName, func(t *testing.T) {
    41  			f := testutils.ParseProto3Tmpl(t, `
    42  				import "google/api/annotations.proto";
    43  				import "google/api/resource.proto";
    44  				service Library {
    45  					rpc {{.MethodName}}({{.MethodName}}Request) returns (Book) {
    46  						option (google.api.http) = {
    47  							post: "{{.URI}}"
    48  						};
    49  					}
    50  				}
    51  				message {{.MethodName}}Request {}
    52  				message Book {
    53  					option (google.api.resource) = {
    54  						pattern: "{{.Pattern}}"
    55  					};
    56  				}
    57  			`, test)
    58  			method := f.GetServices()[0].GetMethods()[0]
    59  			if diff := test.problems.SetDescriptor(method).Diff(httpURIParent.Lint(f)); diff != "" {
    60  				t.Error(diff)
    61  			}
    62  		})
    63  		t.Run(test.testName+"/Operation", func(t *testing.T) {
    64  			f := testutils.ParseProto3Tmpl(t, `
    65  				import "google/api/annotations.proto";
    66  				import "google/api/resource.proto";
    67  				import "google/longrunning/operations.proto";
    68  				service Library {
    69  					rpc {{.MethodName}}({{.MethodName}}Request) returns (google.longrunning.Operation) {
    70  						option (google.api.http) = {
    71  							post: "{{.URI}}"
    72  						};
    73  						option (google.longrunning.operation_info) = {
    74  							response_type: "Book"
    75  							metadata_type: "Book"
    76  						};
    77  					}
    78  				}
    79  				message {{.MethodName}}Request {}
    80  				message Book {
    81  					option (google.api.resource) = {
    82  						pattern: "{{.Pattern}}"
    83  					};
    84  				}
    85  			`, test)
    86  			method := f.GetServices()[0].GetMethods()[0]
    87  			if diff := test.problems.SetDescriptor(method).Diff(httpURIParent.Lint(f)); diff != "" {
    88  				t.Error(diff)
    89  			}
    90  		})
    91  	}
    92  
    93  	t.Run("AdditionalBinding", func(t *testing.T) {
    94  		f := testutils.ParseProto3String(t, `
    95  			import "google/api/annotations.proto";
    96  			import "google/api/resource.proto";
    97  			service Library {
    98  				rpc CreateBook(CreateBookRequest) returns (Book) {
    99  					option (google.api.http) = {
   100  						post: "/v1/{parent=publishers/*}/books"
   101  						body: "book"
   102  						additional_bindings: {
   103  							post: "/v1/{parent=publishers/*/locations/*}/books"
   104  							body: "book"
   105  						}
   106  					};
   107  				}
   108  			}
   109  			message CreateBookRequest {}
   110  			message Book {
   111  				option (google.api.resource) = {
   112  					pattern: "publishers/{publisher}/books/{book}"
   113  				};
   114  			}
   115  		`)
   116  		method := f.GetServices()[0].GetMethods()[0]
   117  		problems := testutils.Problems{}
   118  		if diff := problems.SetDescriptor(method).Diff(httpURIParent.Lint(f)); diff != "" {
   119  			t.Error(diff)
   120  		}
   121  	})
   122  }