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

     1  // Copyright 2020 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 aip0163
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/googleapis/api-linter/rules/internal/testutils"
    21  )
    22  
    23  func TestDeclarativeFriendlyRequired(t *testing.T) {
    24  	problems := testutils.Problems{{Message: "`bool validate_only`"}}
    25  	for _, test := range []struct {
    26  		name         string
    27  		Style        string
    28  		ValidateOnly string
    29  		problems     testutils.Problems
    30  	}{
    31  		{"ValidNotDF", "", "", nil},
    32  		{"ValidDF", "style: DECLARATIVE_FRIENDLY", "bool validate_only = 2;", nil},
    33  		{"InvalidDFWrongType", "style: DECLARATIVE_FRIENDLY", "int32 validate_only = 2;", problems},
    34  		{"InvalidDFRepeated", "style: DECLARATIVE_FRIENDLY", "repeated bool validate_only = 2;", problems},
    35  		{"InvalidDFMissing", "style: DECLARATIVE_FRIENDLY", "", problems},
    36  	} {
    37  		t.Run(test.name, func(t *testing.T) {
    38  			f := testutils.ParseProto3Tmpl(t, `
    39  				import "google/api/annotations.proto";
    40  				import "google/api/resource.proto";
    41  
    42  				service Library {
    43  					rpc GetBook(GetBookRequest) returns (Book) {
    44  						option (google.api.http) = {
    45  							get: "/v1/{name=publishers/*/books/*}"
    46  						};
    47  					}
    48  
    49  					rpc DeleteBook(DeleteBookRequest) returns (Book) {
    50  						option (google.api.http) = {
    51  							delete: "/v1/{name=publishers/*/books/*}"
    52  						};
    53  					}
    54  				}
    55  
    56  				message Book {
    57  					option (google.api.resource) = {
    58  						type: "library.googleapis.com/Book"
    59  						pattern: "publishers/{publisher}/books/{book}"
    60  						{{.Style}}
    61  					};
    62  					string name = 1;
    63  				}
    64  
    65  				message GetBookRequest {
    66  					string name = 1;
    67  				}
    68  
    69  				message DeleteBookRequest {
    70  					string name = 1;
    71  					{{.ValidateOnly}}
    72  				}
    73  			`, test)
    74  			dbr := f.FindMessage("DeleteBookRequest")
    75  			if diff := test.problems.SetDescriptor(dbr).Diff(declarativeFriendlyRequired.Lint(f)); diff != "" {
    76  				t.Errorf(diff)
    77  			}
    78  		})
    79  	}
    80  }