github.com/googleapis/api-linter@v1.65.2/rules/aip0164/response_message_name_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 aip0164
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/googleapis/api-linter/rules/internal/testutils"
    21  )
    22  
    23  func TestResponseMessageName(t *testing.T) {
    24  	tmpl := map[string]string{
    25  		"sync": `
    26  			package test;
    27  			import "google/protobuf/empty.proto";
    28  			service Library {
    29  				rpc {{.MethodName}}({{.MethodName}}Request) returns ({{.RespTypeName}});
    30  			}
    31  			message {{.MethodName}}Request {}
    32  			message {{.RespTypeName}} {}
    33  		`,
    34  		"lro": `
    35  			package test;
    36  			import "google/longrunning/operations.proto";
    37  			service Library {
    38  				rpc {{.MethodName}}({{.MethodName}}Request)
    39  				    returns (google.longrunning.Operation) {
    40  					option (google.longrunning.operation_info) = {
    41  						response_type: "{{.RespTypeName}}"
    42  						metadata_type: "{{.MethodName}}Metadata"
    43  					};
    44  				}
    45  			}
    46  			message {{.MethodName}}Request {}
    47  		`,
    48  	}
    49  
    50  	// Set up the testing permutations.
    51  	tests := []struct {
    52  		testName     string
    53  		tmpl         string
    54  		MethodName   string
    55  		RespTypeName string
    56  		problems     testutils.Problems
    57  	}{
    58  		{"ValidResource", tmpl["sync"], "UndeleteBook", "Book", testutils.Problems{}},
    59  		{"ValidLROResource", tmpl["lro"], "UndeleteBook", "Book", testutils.Problems{}},
    60  		{"Invalid", tmpl["sync"], "UndeleteBook", "UndeleteBookResponse", testutils.Problems{{Suggestion: "Book"}}},
    61  		{"InvalidLRO", tmpl["lro"], "UndeleteBook", "UndeleteBookResponse", testutils.Problems{{Suggestion: "Book"}}},
    62  		{"Irrelevant", tmpl["sync"], "DestroyBook", "DestroyBookResponse", testutils.Problems{}},
    63  		{"IrrelevantLRO", tmpl["lro"], "DestroyBook", "DestroyBookResponse", testutils.Problems{}},
    64  	}
    65  
    66  	// Run each test individually.
    67  	for _, test := range tests {
    68  		t.Run(test.testName, func(t *testing.T) {
    69  			// Create a minimal service with a AIP-164 Undelete method
    70  			file := testutils.ParseProto3Tmpl(t, test.tmpl, test)
    71  
    72  			// Run the lint rule, and establish that it returns the expected problems.
    73  			method := file.GetServices()[0].GetMethods()[0]
    74  			problems := responseMessageName.Lint(file)
    75  			if diff := test.problems.SetDescriptor(method).Diff(problems); diff != "" {
    76  				t.Errorf(diff)
    77  			}
    78  		})
    79  	}
    80  }