github.com/googleapis/api-linter@v1.65.2/rules/aip0235/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 aip0235
    16  
    17  import (
    18  	"strings"
    19  	"testing"
    20  
    21  	"github.com/googleapis/api-linter/rules/internal/testutils"
    22  )
    23  
    24  type responseMessageNameTest struct {
    25  	testName         string
    26  	MethodName       string
    27  	MethodAnnotation string
    28  	Response         string
    29  	problems         testutils.Problems
    30  }
    31  
    32  func (t responseMessageNameTest) ResponseExternal() bool {
    33  	return strings.Contains(t.Response, ".")
    34  }
    35  
    36  func TestResponseMessageName(t *testing.T) {
    37  	// Set up the testing permutations.
    38  	tests := []responseMessageNameTest{
    39  		{
    40  			testName:   "Valid-BatchDeleteBooksResponse",
    41  			MethodName: "BatchDeleteBooks",
    42  			Response:   "BatchDeleteBooksResponse",
    43  			problems:   testutils.Problems{},
    44  		},
    45  		{
    46  			testName:   "Valid-Empty",
    47  			MethodName: "BatchDeleteBooks",
    48  			Response:   "google.protobuf.Empty",
    49  			problems:   testutils.Problems{},
    50  		},
    51  		{
    52  			testName:   "Valid-LRO-BatchDeleteBooksResponse",
    53  			MethodName: "BatchDeleteBooks",
    54  			Response:   "google.longrunning.Operation",
    55  			MethodAnnotation: `option (google.longrunning.operation_info) = {
    56  				response_type: "BatchDeleteBooksResponse"
    57  			};`,
    58  			problems: testutils.Problems{},
    59  		},
    60  		{
    61  			testName:   "Valid-LRO-Empty",
    62  			MethodName: "BatchDeleteBooks",
    63  			Response:   "google.longrunning.Operation",
    64  			MethodAnnotation: `option (google.longrunning.operation_info) = {
    65  				response_type: "google.protobuf.Empty"
    66  			};`,
    67  			problems: testutils.Problems{},
    68  		},
    69  		{
    70  			testName:   "Invalid",
    71  			MethodName: "BatchDeleteBooks",
    72  			Response:   "BatchDeleteBookResponse",
    73  			problems: testutils.Problems{{
    74  				Message:    "`BatchDeleteBooksResponse`",
    75  				Suggestion: "google.protobuf.Empty",
    76  			}},
    77  		},
    78  		{
    79  			testName:   "Invalid-LRO",
    80  			MethodName: "BatchDeleteBooks",
    81  			Response:   "google.longrunning.Operation",
    82  			MethodAnnotation: `option (google.longrunning.operation_info) = {
    83  				response_type: "BatchDeleteBookResponse"
    84  			};`,
    85  			problems: testutils.Problems{{
    86  				Message:    "`BatchDeleteBooksResponse`",
    87  				Suggestion: "google.protobuf.Empty",
    88  			}},
    89  		},
    90  		{
    91  			testName:   "Irrelevant",
    92  			MethodName: "DeleteBook",
    93  			Response:   "Book",
    94  			problems:   testutils.Problems{},
    95  		},
    96  	}
    97  
    98  	// Run each test individually.
    99  	for _, test := range tests {
   100  		t.Run(test.testName, func(t *testing.T) {
   101  			file := testutils.ParseProto3Tmpl(t, `
   102  				import "google/protobuf/empty.proto";
   103  				import "google/longrunning/operations.proto";
   104  
   105  				service BookService {
   106  					rpc {{.MethodName}}({{.MethodName}}Request) returns ({{.Response}}) {
   107  						{{.MethodAnnotation}}
   108  					}
   109  				}
   110  				message {{.MethodName}}Request {}
   111  				{{ if not .ResponseExternal }}message {{.Response}} {}{{ end }}
   112  				`, test)
   113  
   114  			m := file.GetServices()[0].GetMethods()[0]
   115  
   116  			problems := responseMessageName.Lint(file)
   117  			if diff := test.problems.SetDescriptor(m).Diff(problems); diff != "" {
   118  				t.Errorf(diff)
   119  			}
   120  		})
   121  	}
   122  }