github.com/googleapis/api-linter@v1.65.2/rules/aip0151/lro_metadata_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 TestLROMetadataReachable(t *testing.T) {
    25  	t.Run("SameFile", func(t *testing.T) {
    26  		tests := []struct {
    27  			testName     string
    28  			Package      string
    29  			MetadataType string
    30  			problems     testutils.Problems
    31  		}{
    32  			{"Valid", "", "WriteBookMetadata", testutils.Problems{}},
    33  			{"ValidPkg", "package test;", "WriteBookMetadata", testutils.Problems{}},
    34  			{"InvalidTypo", "", "WriteBookMeta", testutils.Problems{{Message: "WriteBookMeta"}}},
    35  			{"InvalidTypoPkg", "package test;", "WriteBookMeta", testutils.Problems{{Message: "test.WriteBookMeta"}}},
    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: "WriteBookResponse"
    48  								metadata_type: "{{.MetadataType}}"
    49  							};
    50  						}
    51  					}
    52  					message WriteBookRequest {}
    53  					message WriteBookResponse {}
    54  					message WriteBookMetadata {}
    55  				`, test)
    56  				problems := lroMetadataReachable.Lint(f)
    57  				m := f.GetServices()[0].GetMethods()[0]
    58  				if diff := test.problems.SetDescriptor(m).Diff(problems); diff != "" {
    59  					t.Errorf(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 WriteBookMetadata {}", testutils.Problems{}},
    71  			{"Absent", "", testutils.Problems{{Message: "WriteBookMetadata"}}},
    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  						message WriteBookResponse {}
    79  						---
    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: "WriteBookMetadata"
    90  								};
    91  							}
    92  						}
    93  					`,
    94  				})
    95  				problems := lroMetadataReachable.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.Errorf(diff)
    99  				}
   100  			})
   101  		}
   102  	})
   103  }