github.com/googleapis/api-linter@v1.65.2/rules/aip0134/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  package aip0134
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/googleapis/api-linter/rules/internal/testutils"
    20  	"github.com/jhump/protoreflect/desc"
    21  	"github.com/jhump/protoreflect/desc/builder"
    22  	fpb "google.golang.org/genproto/protobuf/field_mask"
    23  )
    24  
    25  func TestUnknownFields(t *testing.T) {
    26  	// Get the correct message type for google.protobuf.FieldMask.
    27  	fieldMask, err := desc.LoadMessageDescriptorForMessage(&fpb.FieldMask{})
    28  	if err != nil {
    29  		t.Fatalf("Unable to load the field mask message.")
    30  	}
    31  
    32  	// Set up the testing permutations.
    33  	tests := []struct {
    34  		testName    string
    35  		messageName string
    36  		fieldName   string
    37  		fieldType   *builder.FieldType
    38  		problems    testutils.Problems
    39  	}{
    40  		// Use BigBook instead of Book to test correct casing logic
    41  		{
    42  			"UpdateMask", "UpdateBigBookRequest", "update_mask",
    43  			builder.FieldTypeImportedMessage(fieldMask),
    44  			testutils.Problems{},
    45  		},
    46  		{
    47  			"ValidateOnly", "UpdateBigBookRequest", "validate_only",
    48  			builder.FieldTypeBool(),
    49  			testutils.Problems{},
    50  		},
    51  		{
    52  			"Invalid", "UpdateBigBookRequest", "application_id",
    53  			builder.FieldTypeString(),
    54  			testutils.Problems{{Message: "Unexpected field"}},
    55  		},
    56  		{
    57  			"InvalidCasing", "UpdateBigBookRequest", "bigbook",
    58  			builder.FieldTypeString(),
    59  			testutils.Problems{{Message: "Unexpected field"}},
    60  		},
    61  		{
    62  			"Irrelevant", "AcquireBigBookRequest", "application_id",
    63  			builder.FieldTypeString(),
    64  			testutils.Problems{},
    65  		},
    66  	}
    67  
    68  	// Run each test individually.
    69  	for _, test := range tests {
    70  		t.Run(test.testName, func(t *testing.T) {
    71  			// Create an appropriate message descriptor.
    72  			message, err := builder.NewMessage(test.messageName).AddField(
    73  				builder.NewField("big_book", builder.FieldTypeMessage(builder.NewMessage("BigBook"))),
    74  			).AddField(
    75  				builder.NewField(test.fieldName, test.fieldType),
    76  			).Build()
    77  			if err != nil {
    78  				t.Fatalf("Could not build UpdateBookRequest message.")
    79  			}
    80  
    81  			// Run the lint rule, and establish that it returns the correct problems.
    82  			wantProblems := test.problems.SetDescriptor(message.FindFieldByName(test.fieldName))
    83  			gotProblems := unknownFields.Lint(message.GetFile())
    84  			if diff := wantProblems.Diff(gotProblems); diff != "" {
    85  				t.Errorf(diff)
    86  			}
    87  		})
    88  	}
    89  }