github.com/googleapis/api-linter@v1.65.2/rules/aip0151/lro_response_reachable_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 aip0151
    16  
    17  import (
    18  	"strings"
    19  	"testing"
    20  
    21  	"github.com/googleapis/api-linter/rules/internal/testutils"
    22  )
    23  
    24  func TestLROResponseReachable(t *testing.T) {
    25  	t.Run("SameFile", func(t *testing.T) {
    26  		tests := []struct {
    27  			testName     string
    28  			Package      string
    29  			ResponseType string
    30  			problems     testutils.Problems
    31  		}{
    32  			{"Valid", "", "WriteBookResponse", testutils.Problems{}},
    33  			{"ValidPkg", "package test;", "WriteBookResponse", testutils.Problems{}},
    34  			{"InvalidTypo", "", "WriteBookReponse", testutils.Problems{{Message: "WriteBookReponse"}}},
    35  			{"InvalidTypoPkg", "package test;", "WriteBookReponse", testutils.Problems{{Message: "test.WriteBookReponse"}}},
    36  			{"ValidExternal", "", "google.protobuf.Empty", testutils.Problems{}},
    37  			{"ValidExternalPkg", "package test;", "google.protobuf.Empty", testutils.Problems{}},
    38  		}
    39  		for _, test := range tests {
    40  			t.Run(test.testName, func(t *testing.T) {
    41  				f := testutils.ParseProto3Tmpl(t, `
    42  					{{.Package}}
    43  					import "google/longrunning/operations.proto";
    44  					service Library {
    45  						rpc WriteBook(WriteBookRequest) returns (google.longrunning.Operation) {
    46  							option (google.longrunning.operation_info) = {
    47  								response_type: "{{.ResponseType}}"
    48  								metadata_type: "OperationMetadata"
    49  							};
    50  						}
    51  					}
    52  					message WriteBookRequest {}
    53  					message WriteBookResponse {}
    54  					message OperationMetadata {}
    55  				`, test)
    56  				problems := lroResponseReachable.Lint(f)
    57  				m := f.GetServices()[0].GetMethods()[0]
    58  				if diff := test.problems.SetDescriptor(m).Diff(problems); diff != "" {
    59  					t.Error(diff)
    60  				}
    61  			})
    62  		}
    63  	})
    64  	t.Run("Imported", func(t *testing.T) {
    65  		for _, test := range []struct {
    66  			name     string
    67  			message  string
    68  			problems testutils.Problems
    69  		}{
    70  			{"Present", "message WriteBookResponse {}", testutils.Problems{}},
    71  			{"Absent", "", testutils.Problems{{Message: "WriteBookResponse"}}},
    72  		} {
    73  			t.Run(test.name, func(t *testing.T) {
    74  				files := testutils.ParseProtoStrings(t, map[string]string{
    75  					"imported.proto": strings.ReplaceAll(`
    76  						syntax = "proto3";
    77  						message WriteBookRequest {}
    78  						---
    79  						message OperationMetadata {}
    80  					`, "---", test.message),
    81  					"test.proto": `
    82  						syntax = "proto3";
    83  						import "google/longrunning/operations.proto";
    84  						import "imported.proto";
    85  						service Library {
    86  							rpc WriteBook(WriteBookRequest) returns (google.longrunning.Operation) {
    87  								option (google.longrunning.operation_info) = {
    88  									response_type: "WriteBookResponse"
    89  									metadata_type: "OperationMetadata"
    90  								};
    91  							}
    92  						}
    93  					`,
    94  				})
    95  				problems := lroResponseReachable.Lint(files["test.proto"])
    96  				method := files["test.proto"].GetServices()[0].GetMethods()[0]
    97  				if diff := test.problems.SetDescriptor(method).Diff(problems); diff != "" {
    98  					t.Error(diff)
    99  				}
   100  			})
   101  		}
   102  	})
   103  	t.Run("Transitive", func(t *testing.T) {
   104  		for _, test := range []struct {
   105  			name     string
   106  			message  string
   107  			problems testutils.Problems
   108  		}{
   109  			{"Present", "message WriteBookResponse {}", testutils.Problems{}},
   110  			{"Absent", "", testutils.Problems{{Message: "WriteBookResponse"}}},
   111  		} {
   112  			t.Run(test.name, func(t *testing.T) {
   113  				files := testutils.ParseProtoStrings(t, map[string]string{
   114  					"transitive.proto": strings.ReplaceAll(`
   115  						syntax = "proto3";
   116  						---
   117  						message OperationMetadata {}
   118  					`, "---", test.message),
   119  					"imported.proto": `
   120  						syntax = "proto3";
   121  						import "transitive.proto";
   122  						message WriteBookRequest {}
   123  					`,
   124  					"test.proto": `
   125  						syntax = "proto3";
   126  						import "google/longrunning/operations.proto";
   127  						import "imported.proto";
   128  						service Library {
   129  							rpc WriteBook(WriteBookRequest) returns (google.longrunning.Operation) {
   130  								option (google.longrunning.operation_info) = {
   131  									response_type: "WriteBookResponse"
   132  									metadata_type: "OperationMetadata"
   133  								};
   134  							}
   135  						}
   136  					`,
   137  				})
   138  				problems := lroResponseReachable.Lint(files["test.proto"])
   139  				method := files["test.proto"].GetServices()[0].GetMethods()[0]
   140  				if diff := test.problems.SetDescriptor(method).Diff(problems); diff != "" {
   141  					t.Error(diff)
   142  				}
   143  			})
   144  		}
   145  	})
   146  }