github.com/googleapis/api-linter@v1.65.2/rules/aip0131/request_unknown_fields_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 aip0131
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/googleapis/api-linter/rules/internal/testutils"
    21  	"github.com/jhump/protoreflect/desc"
    22  	"github.com/jhump/protoreflect/desc/builder"
    23  	fpb "google.golang.org/genproto/protobuf/field_mask"
    24  )
    25  
    26  func TestUnknownFields(t *testing.T) {
    27  	// Get the correct message type for google.protobuf.FieldMask.
    28  	fieldMask, err := desc.LoadMessageDescriptorForMessage(&fpb.FieldMask{})
    29  	if err != nil {
    30  		t.Fatalf("Unable to load the field mask message.")
    31  	}
    32  
    33  	// Set up the testing permutations.
    34  	tests := []struct {
    35  		testName    string
    36  		messageName string
    37  		fieldName   string
    38  		fieldType   *builder.FieldType
    39  		problems    testutils.Problems
    40  	}{
    41  		{"RequestId", "GetBookRequest", "request_id", builder.FieldTypeImportedMessage(fieldMask), testutils.Problems{}},
    42  		{"ReadMask", "GetBookRequest", "read_mask", builder.FieldTypeImportedMessage(fieldMask), testutils.Problems{}},
    43  		{"View", "GetBookRequest", "view", builder.FieldTypeEnum(builder.NewEnum("View").AddValue(builder.NewEnumValue("BASIC"))), testutils.Problems{}},
    44  		{"Invalid", "GetBookRequest", "application_id", builder.FieldTypeString(), testutils.Problems{{
    45  			Message: "Unexpected field",
    46  		}}},
    47  		{"Irrelevant", "AcquireBookRequest", "application_id", builder.FieldTypeString(), testutils.Problems{}},
    48  	}
    49  
    50  	// Run each test individually.
    51  	for _, test := range tests {
    52  		t.Run(test.testName, func(t *testing.T) {
    53  			// Create an appropriate message descriptor.
    54  			message, err := builder.NewMessage(test.messageName).AddField(
    55  				builder.NewField("name", builder.FieldTypeString()),
    56  			).AddField(
    57  				builder.NewField(test.fieldName, test.fieldType),
    58  			).Build()
    59  			if err != nil {
    60  				t.Fatalf("Could not build GetBookRequest message: %s", err)
    61  			}
    62  
    63  			// Run the lint rule, and establish that it returns the correct problems.
    64  			wantProblems := test.problems.SetDescriptor(message.FindFieldByName(test.fieldName))
    65  			gotProblems := unknownFields.Lint(message.GetFile())
    66  			if diff := wantProblems.Diff(gotProblems); diff != "" {
    67  				t.Errorf(diff)
    68  			}
    69  		})
    70  	}
    71  }