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

     1  // Copyright 2023 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 aip0203
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/googleapis/api-linter/rules/internal/testutils"
    21  )
    22  
    23  func TestResourceIdentifierOnly(t *testing.T) {
    24  	testCases := []struct {
    25  		name, message                                   string
    26  		ResourceField, NameField, NonResourceExtensions string
    27  		problems                                        testutils.Problems
    28  	}{
    29  		{
    30  			name:          "Valid",
    31  			message:       "Book",
    32  			ResourceField: "string name = 1 [(google.api.field_behavior) = IDENTIFIER];",
    33  			problems:      nil,
    34  		},
    35  		{
    36  			name:          "ValidNameField",
    37  			message:       "Book",
    38  			ResourceField: "string resource_name = 1 [(google.api.field_behavior) = IDENTIFIER];",
    39  			NameField:     "resource_name",
    40  			problems:      nil,
    41  		},
    42  		{
    43  			name:                  "InvalidNonResource",
    44  			message:               "NonResource",
    45  			NonResourceExtensions: "[(google.api.field_behavior) = IDENTIFIER]",
    46  			problems:              testutils.Problems{{Message: "resource's name field"}},
    47  		},
    48  		{
    49  			name:          "InvalidResourceNonIdentifier",
    50  			message:       "Book",
    51  			ResourceField: "string foo = 1 [(google.api.field_behavior) = IDENTIFIER];",
    52  			problems:      testutils.Problems{{Message: "resource's name field"}},
    53  		},
    54  	}
    55  
    56  	for _, test := range testCases {
    57  		t.Run(test.name, func(t *testing.T) {
    58  			file := testutils.ParseProto3Tmpl(t, `
    59  			import "google/api/field_behavior.proto";
    60  			import "google/api/resource.proto";
    61  			message Book {
    62  				option (google.api.resource) = {
    63  					type: "library.googleapis.com/Book"
    64  					pattern: "books/{book}"
    65  					name_field: "{{.NameField}}"
    66  				};
    67  				{{.ResourceField}}
    68  			}
    69  			message NonResource {
    70  				string foo = 1 {{.NonResourceExtensions}};
    71  			}
    72  			`, test)
    73  			f := file.FindMessage(test.message).GetFields()[0]
    74  			problems := resourceIdentifierOnly.Lint(file)
    75  			if diff := test.problems.SetDescriptor(f).Diff(problems); diff != "" {
    76  				t.Error(diff)
    77  			}
    78  		})
    79  	}
    80  }