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

     1  // Copyright 2019 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 aip0135
    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/api/resource.proto";
    28  			import "google/protobuf/empty.proto";
    29  			service Library {
    30  				rpc {{.MethodName}}({{.MethodName}}Request) returns ({{.RespTypeName}});
    31  			}
    32  			message {{.MethodName}}Request {}
    33  			message Book {
    34  				option (google.api.resource) = {
    35  					type: "library.googleapis.com/Book"
    36  					pattern: "publishers/{publisher}/books/{book}"
    37  					{{.Style}}
    38  				};
    39  			}
    40  			{{ if (ne .RespTypeName "google.protobuf.Empty") }}{{ if (ne .RespTypeName "Book") }}
    41  			message {{.RespTypeName}} {}
    42  			{{ end }}{{ end }}
    43  		`,
    44  		"lro": `
    45  			package test;
    46  			import "google/api/resource.proto";
    47  			import "google/longrunning/operations.proto";
    48  			service Library {
    49  				rpc {{.MethodName}}({{.MethodName}}Request)
    50  				    returns (google.longrunning.Operation) {
    51  					option (google.longrunning.operation_info) = {
    52  						response_type: "{{.RespTypeName}}"
    53  						metadata_type: "{{.MethodName}}Metadata"
    54  					};
    55  				}
    56  			}
    57  			message {{.MethodName}}Request {}
    58  			message Book {
    59  				option (google.api.resource) = {
    60  					type: "library.googleapis.com/Book"
    61  					pattern: "publishers/{publisher}/books/{book}"
    62  					{{.Style}}
    63  				};
    64  			}
    65  		`,
    66  	}
    67  
    68  	// Prepare problems.
    69  	problems := map[string]map[string]testutils.Problems{
    70  		"book": {
    71  			"sync": {{Suggestion: "Book", Message: "of the resource"}},
    72  			"lro":  {{Message: "of the resource"}},
    73  		},
    74  		"empty": {
    75  			"sync": {{Suggestion: "google.protobuf.Empty", Message: "of Empty or the resource"}},
    76  			"lro":  {{Message: "of Empty or the resource"}},
    77  		},
    78  		"none": {"sync": nil, "lro": nil},
    79  	}
    80  
    81  	// Set up the testing permutations.
    82  	tests := []struct {
    83  		name         string
    84  		MethodName   string
    85  		RespTypeName string
    86  		Style        string
    87  		problems     map[string]testutils.Problems
    88  	}{
    89  		{"ValidEmpty", "DeleteBook", "google.protobuf.Empty", "", problems["none"]},
    90  		// the declarative friendly style is no longer deviated for delete.
    91  		{"ValidEmptyDF", "DeleteBook", "google.protobuf.Empty", "style: DECLARATIVE_FRIENDLY", problems["none"]},
    92  		{"ValidResource", "DeleteBook", "Book", "", problems["none"]},
    93  		{"ValidResourceOperation", "DeleteUnitOperation", "UnitOperation", "", problems["none"]},
    94  		{"Invalid", "DeleteBook", "DeleteBookResponse", "", problems["empty"]},
    95  		{"Irrelevant", "DestroyBook", "DestroyBookResponse", "", problems["none"]},
    96  	}
    97  
    98  	// Run each test individually.
    99  	for _, test := range tests {
   100  		t.Run(test.name, func(t *testing.T) {
   101  			for _, tmplName := range []string{"sync", "lro"} {
   102  				t.Run(tmplName, func(t *testing.T) {
   103  					// Create a minimal service with a AIP-135 Delete method
   104  					file := testutils.ParseProto3Tmpl(t, tmpl[tmplName], test)
   105  
   106  					// Run the lint rule, and establish that it returns the expected problems.
   107  					method := file.GetServices()[0].GetMethods()[0]
   108  					problems := responseMessageName.Lint(file)
   109  					if diff := test.problems[tmplName].SetDescriptor(method).Diff(problems); diff != "" {
   110  						t.Errorf(diff)
   111  					}
   112  				})
   113  			}
   114  		})
   115  	}
   116  }