github.com/googleapis/api-linter@v1.65.2/rules/aip0154/declarative_friendly_required_test.go (about) 1 // Copyright 2020 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 aip0154 16 17 import ( 18 "testing" 19 20 "github.com/googleapis/api-linter/rules/internal/testutils" 21 ) 22 23 func TestDeclarativeFriendlyRequired(t *testing.T) { 24 t.Run("Resource", func(t *testing.T) { 25 for _, test := range []struct { 26 name string 27 Style string 28 Etag string 29 problems testutils.Problems 30 }{ 31 {"ValidEtagNotDF", "", "string etag = 2;", nil}, 32 {"ValidNoEtagNotDF", "", "", nil}, 33 {"ValidEtagDF", "style: DECLARATIVE_FRIENDLY", "string etag = 2;", nil}, 34 {"InvalidNoEtagDF", "style: DECLARATIVE_FRIENDLY", "", testutils.Problems{{Message: "string etag"}}}, 35 } { 36 f := testutils.ParseProto3Tmpl(t, ` 37 import "google/api/resource.proto"; 38 message Book { 39 option (google.api.resource) = { 40 type: "library.googleapis.com/Book" 41 {{.Style}} 42 }; 43 44 string name = 1; 45 {{.Etag}} 46 } 47 `, test) 48 m := f.GetMessageTypes()[0] 49 if diff := test.problems.SetDescriptor(m).Diff(declarativeFriendlyRequired.Lint(f)); diff != "" { 50 t.Errorf(diff) 51 } 52 } 53 }) 54 55 t.Run("Requests", func(t *testing.T) { 56 for _, test := range []struct { 57 name string 58 Style string 59 Etag string 60 problems testutils.Problems 61 }{ 62 {"ValidEtagNotDF", "", "string etag = 2;", nil}, 63 {"ValidNoEtagNotDF", "", "", nil}, 64 {"ValidEtagDF", "style: DECLARATIVE_FRIENDLY", "string etag = 2;", nil}, 65 {"InvalidNoEtagDF", "style: DECLARATIVE_FRIENDLY", "", testutils.Problems{{Message: "string etag"}}}, 66 } { 67 t.Run(test.name, func(t *testing.T) { 68 f := testutils.ParseProto3Tmpl(t, ` 69 import "google/api/annotations.proto"; 70 import "google/api/resource.proto"; 71 service Library { 72 rpc GetBook(GetBookRequest) returns (Book) { 73 option (google.api.http) = { 74 get: "/v1/{name=publishers/*/books/*}" 75 }; 76 } 77 rpc CreateBook(CreateBookRequest) returns (Book) { 78 option (google.api.http) = { 79 post: "/v1/{parent=publishers/*}/books" 80 body: "*" 81 }; 82 } 83 rpc DeleteBook(DeleteBookRequest) returns (Book) { 84 option (google.api.http) = { 85 delete: "/v1/{name=publishers/*/books/*}" 86 }; 87 } 88 } 89 message Book { 90 option (google.api.resource) = { 91 type: "library.googleapis.com/Book" 92 {{.Style}} 93 }; 94 string name = 1; 95 string etag = 2; 96 } 97 message GetBookRequest { 98 string name = 1; 99 } 100 message CreateBookRequest { 101 string parent = 1; 102 Book book = 2; 103 string book_id = 3; 104 } 105 message DeleteBookRequest { 106 string name = 1; 107 {{.Etag}} 108 } 109 `, test) 110 m := f.FindMessage("DeleteBookRequest") 111 if diff := test.problems.SetDescriptor(m).Diff(declarativeFriendlyRequired.Lint(f)); diff != "" { 112 t.Errorf(diff) 113 } 114 }) 115 } 116 }) 117 }