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

     1  // Copyright 2022 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  	"strings"
    19  	"testing"
    20  
    21  	"github.com/googleapis/api-linter/rules/internal/testutils"
    22  	"github.com/jhump/protoreflect/desc"
    23  )
    24  
    25  func TestHTTPURIResource(t *testing.T) {
    26  	tests := []struct {
    27  		testName string
    28  		URI      string
    29  		Pattern  string
    30  		problems testutils.Problems
    31  	}{
    32  		{"Valid", "/v1/{parent=publishers/*}/books", "publishers/{publisher}/books/{book}", nil},
    33  		{"ValidCustomLookalike", "/v1/{parent=publishers/*}/books:createAndCheckout", "publishers/{publisher}/books/{book}", nil},
    34  		{"MethodMissingURIPath", "", "publishers/{publisher}/books/{book}", nil},
    35  		{"MethodMissingCollectionURISuffix", "/v1/", "publishers/{publisher}/books/{book}", testutils.Problems{{Message: "The URI path does not end in a collection identifier."}}},
    36  		{"ResourceMissingCollectionInPattern", "/v1/{parent=publishers/*}/books", "publishers/{publisher}", testutils.Problems{{Message: "Resource pattern should contain the collection identifier \"books/\"."}}},
    37  		{"ResourceMissingCollectionCustomLookalike", "/v1/{parent=publishers/*}/books:createAndCheckout", "publishers/{publisher}", testutils.Problems{{Message: "Resource pattern should contain the collection identifier \"books/\"."}}},
    38  	}
    39  
    40  	for _, test := range tests {
    41  		t.Run(test.testName, func(t *testing.T) {
    42  			f := testutils.ParseProto3Tmpl(t, `
    43  				import "google/api/annotations.proto";
    44  				import "google/api/resource.proto";
    45  				service Library {
    46  					rpc CreateBook(CreateBookRequest) returns (Book) {
    47  						option (google.api.http) = {
    48  							post: "{{.URI}}"
    49  						};
    50  					}
    51  				}
    52  				message CreateBookRequest {}
    53  				message Book {
    54  					option (google.api.resource) = {
    55  						pattern: "{{.Pattern}}"
    56  					};
    57  				}
    58  			`, test)
    59  
    60  			method := f.GetServices()[0].GetMethods()[0]
    61  			var d desc.Descriptor = method
    62  			if strings.HasPrefix(test.testName, "Resource") {
    63  				d = method.GetOutputType()
    64  			}
    65  			if diff := test.problems.SetDescriptor(d).Diff(httpURIResource.Lint(f)); diff != "" {
    66  				t.Error(diff)
    67  			}
    68  		})
    69  	}
    70  }