github.com/googleapis/api-linter@v1.65.2/rules/internal/utils/resource_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 utils
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/googleapis/api-linter/rules/internal/testutils"
    21  	apb "google.golang.org/genproto/googleapis/api/annotations"
    22  )
    23  
    24  func TestGetResourceSingular(t *testing.T) {
    25  	for _, test := range []struct {
    26  		name     string
    27  		resource *apb.ResourceDescriptor
    28  		want     string
    29  	}{
    30  		{
    31  			name: "SingularSpecified",
    32  			resource: &apb.ResourceDescriptor{
    33  				Singular: "bookShelf",
    34  			},
    35  			want: "bookShelf",
    36  		},
    37  		{
    38  			name: "SingularAndTypeSpecified",
    39  			resource: &apb.ResourceDescriptor{
    40  				Singular: "bookShelf",
    41  				// NOTE: this is not a correct resource annotation.
    42  				// it must match singular.
    43  				Type: "library.googleapis.com/book",
    44  			},
    45  			want: "bookShelf",
    46  		},
    47  		{
    48  			name: "TypeSpecified",
    49  			resource: &apb.ResourceDescriptor{
    50  				Type: "library.googleapis.com/bookShelf",
    51  			},
    52  			want: "bookShelf",
    53  		},
    54  		{
    55  			name:     "NothingSpecified",
    56  			resource: &apb.ResourceDescriptor{},
    57  			want:     "",
    58  		},
    59  		{
    60  			name:     "Nil",
    61  			resource: nil,
    62  			want:     "",
    63  		},
    64  	} {
    65  		t.Run(test.name, func(t *testing.T) {
    66  			got := GetResourceSingular(test.resource)
    67  			if got != test.want {
    68  				t.Errorf("GetResourceSingular: expected %v, got %v", test.want, got)
    69  			}
    70  		})
    71  	}
    72  }
    73  
    74  func TestGetResourcePlural(t *testing.T) {
    75  	for _, test := range []struct {
    76  		name     string
    77  		resource *apb.ResourceDescriptor
    78  		want     string
    79  	}{
    80  		{
    81  			name: "PluralSpecified",
    82  			resource: &apb.ResourceDescriptor{
    83  				Plural: "bookShelves",
    84  			},
    85  			want: "bookShelves",
    86  		},
    87  		{
    88  			name:     "NothingSpecified",
    89  			resource: &apb.ResourceDescriptor{},
    90  			want:     "",
    91  		},
    92  		{
    93  			name:     "Nil",
    94  			resource: nil,
    95  			want:     "",
    96  		},
    97  	} {
    98  		t.Run(test.name, func(t *testing.T) {
    99  			got := GetResourcePlural(test.resource)
   100  			if got != test.want {
   101  				t.Errorf("GetResourcePlural: expected %v, got %v", test.want, got)
   102  			}
   103  		})
   104  	}
   105  }
   106  
   107  func TestIsResourceRevision(t *testing.T) {
   108  	for _, test := range []struct {
   109  		name, Message, Resource string
   110  		want                    bool
   111  	}{
   112  		{
   113  			name:     "valid_revision",
   114  			Message:  "BookRevision",
   115  			Resource: `option (google.api.resource) = {type: "library.googleapis.com/BookRevision"};`,
   116  			want:     true,
   117  		},
   118  		{
   119  			name:    "not_revision_no_resource",
   120  			Message: "BookRevision",
   121  			want:    false,
   122  		},
   123  		{
   124  			name:     "not_revision_bad_name",
   125  			Message:  "Book",
   126  			Resource: `option (google.api.resource) = {type: "library.googleapis.com/Book"};`,
   127  			want:     false,
   128  		},
   129  	} {
   130  		f := testutils.ParseProto3Tmpl(t, `
   131  			import "google/api/resource.proto";
   132  			message {{.Message}} {
   133  				{{.Resource}}
   134  				string name = 1;
   135  			}
   136  		`, test)
   137  		m := f.FindMessage(test.Message)
   138  		if got := IsResourceRevision(m); got != test.want {
   139  			t.Errorf("IsResourceRevision(%+v): got %v, want %v", m, got, test.want)
   140  		}
   141  	}
   142  }
   143  
   144  func TestIsRevisionRelationship(t *testing.T) {
   145  	for _, test := range []struct {
   146  		name         string
   147  		typeA, typeB string
   148  		want         bool
   149  	}{
   150  		{
   151  			name:  "revision_relationship",
   152  			typeA: "library.googleapis.com/Book",
   153  			typeB: "library.googleapis.com/BookRevision",
   154  			want:  true,
   155  		},
   156  		{
   157  			name:  "non_revision_relationship",
   158  			typeA: "library.googleapis.com/Book",
   159  			typeB: "library.googleapis.com/Library",
   160  			want:  false,
   161  		},
   162  		{
   163  			name:  "invalid_type_a",
   164  			typeA: "library.googleapis.com",
   165  			typeB: "library.googleapis.com/Library",
   166  			want:  false,
   167  		},
   168  		{
   169  			name:  "invalid_type_b",
   170  			typeA: "library.googleapis.com/Book",
   171  			typeB: "library.googleapis.com",
   172  			want:  false,
   173  		},
   174  	} {
   175  		t.Run(test.name, func(t *testing.T) {
   176  			a := &apb.ResourceDescriptor{Type: test.typeA}
   177  			b := &apb.ResourceDescriptor{Type: test.typeB}
   178  			if got := IsRevisionRelationship(a, b); got != test.want {
   179  				t.Errorf("IsRevisionRelationship(%s, %s): got %v, want %v", test.typeA, test.typeB, got, test.want)
   180  			}
   181  		})
   182  	}
   183  }