github.com/googleapis/api-linter@v1.65.2/rules/aip0123/resource_annotation_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 aip0123
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/googleapis/api-linter/rules/internal/testutils"
    21  )
    22  
    23  func TestResourceAnnotation(t *testing.T) {
    24  	// The rule should pass if the option is present on a resource message.
    25  	t.Run("Present", func(t *testing.T) {
    26  		f := testutils.ParseProto3String(t, `
    27  			import "google/api/resource.proto";
    28  			message Book {
    29  				option (google.api.resource) = {
    30  					type: "library.googleapis.com/Book"
    31  					pattern: "publishers/{publisher}/books/{book}"
    32  				};
    33  				string name = 1;
    34  			}
    35  		`)
    36  		if diff := (testutils.Problems{}).Diff(resourceAnnotation.Lint(f)); diff != "" {
    37  			t.Errorf(diff)
    38  		}
    39  	})
    40  
    41  	t.Run("SkipNested", func(t *testing.T) {
    42  		f := testutils.ParseProto3String(t, `
    43  			message Foo {
    44  				message Bar {
    45  					string name = 1;
    46  				}
    47  				Bar bar = 1;
    48  			}
    49  		`)
    50  		if diff := (testutils.Problems{}).Diff(resourceAnnotation.Lint(f)); diff != "" {
    51  			t.Errorf(diff)
    52  		}
    53  	})
    54  
    55  	// The rule should fail if the option is absent on a resource message,
    56  	// but pass on messages that are not resource messages.
    57  	for _, test := range []struct {
    58  		name        string
    59  		MessageName string
    60  		FieldName   string
    61  		problems    testutils.Problems
    62  	}{
    63  		{"ValidNoNameField", "Book", "title", testutils.Problems{}},
    64  		{"ValidRequestMessage", "GetBookRequest", "name", testutils.Problems{}},
    65  		{"ValidResponseMessage", "GetBookResponse", "name", testutils.Problems{}},
    66  		{"Invalid", "Book", "name", testutils.Problems{{Message: "google.api.resource"}}},
    67  	} {
    68  		t.Run(test.name, func(t *testing.T) {
    69  			f := testutils.ParseProto3Tmpl(t, `
    70  				message {{.MessageName}} {
    71  					string {{.FieldName}} = 1;
    72  				}
    73  			`, test)
    74  			m := f.GetMessageTypes()[0]
    75  			if diff := test.problems.SetDescriptor(m).Diff(resourceAnnotation.Lint(f)); diff != "" {
    76  				t.Errorf(diff)
    77  			}
    78  		})
    79  	}
    80  }