From f375d04571513e99fab77a3507f34507e098a2c7 Mon Sep 17 00:00:00 2001 From: Blake Li Date: Tue, 4 Jan 2022 15:40:15 -0700 Subject: [PATCH] #869. Add/refactor unit tests for RoutingRuleParserTest and MessageTest. --- .../generator/gapic/model/MessageTest.java | 4 +- .../protoparser/RoutingRuleParserTest.java | 62 +++++++++++++------ .../routing_rule_parser_testing.proto | 14 +++++ 3 files changed, 59 insertions(+), 21 deletions(-) diff --git a/src/test/java/com/google/api/generator/gapic/model/MessageTest.java b/src/test/java/com/google/api/generator/gapic/model/MessageTest.java index fe5c54085a..cb733c7edc 100644 --- a/src/test/java/com/google/api/generator/gapic/model/MessageTest.java +++ b/src/test/java/com/google/api/generator/gapic/model/MessageTest.java @@ -73,10 +73,10 @@ public void shouldThrowExceptionIfMessageDoesNotExist() { Message message = TEST_MESSAGE_BUILDER.setFieldMap(ImmutableMap.of(subFieldName, subField)).build(); String fieldName = subFieldName + "." + "size"; - NullPointerException illegalStateException = + NullPointerException nullPointerException = assertThrows( NullPointerException.class, () -> message.validateField(fieldName, ImmutableMap.of())); - assertThat(illegalStateException.getMessage()) + assertThat(nullPointerException.getMessage()) .isEqualTo(String.format(MESSAGE_NOT_FOUND_ERROR_MESSAGE, subFieldName, fieldTypeName)); } diff --git a/src/test/java/com/google/api/generator/gapic/protoparser/RoutingRuleParserTest.java b/src/test/java/com/google/api/generator/gapic/protoparser/RoutingRuleParserTest.java index c3465d921b..3363270813 100644 --- a/src/test/java/com/google/api/generator/gapic/protoparser/RoutingRuleParserTest.java +++ b/src/test/java/com/google/api/generator/gapic/protoparser/RoutingRuleParserTest.java @@ -40,30 +40,22 @@ public class RoutingRuleParserTest { @Test public void shouldReturnEmptyRoutingHeadersIfMethodHasNoRoutingRules() { - MethodDescriptor rpcMethod = TESTING_SERVICE.getMethods().get(0); - Message inputMessage = MESSAGES.get("com." + rpcMethod.getInputType().getFullName()); - RoutingHeaders routingHeaders = RoutingRuleParser.parse(rpcMethod, inputMessage, MESSAGES); - assertThat(routingHeaders.routingHeadersList()).isEmpty(); + RoutingHeaders actual = getRoutingHeaders(0); + assertThat(actual.routingHeadersList()).isEmpty(); } @Test public void shouldSetPathTemplateToWildcardIfNotDefined() { - MethodDescriptor rpcMethod = TESTING_SERVICE.getMethods().get(1); - Message inputMessage = MESSAGES.get("com." + rpcMethod.getInputType().getFullName()); - RoutingHeaders routingHeaders = RoutingRuleParser.parse(rpcMethod, inputMessage, MESSAGES); - RoutingHeader routingHeader = + RoutingHeaders actual = getRoutingHeaders(1); + RoutingHeader expected = RoutingHeader.create("name", "name", String.format(WILDCARD_PATTERN, "name")); - assertThat(routingHeaders.routingHeadersList()).containsExactly(routingHeader); + assertThat(actual.routingHeadersList()).containsExactly(expected); } @Test public void shouldThrowExceptionIfPathTemplateHasZeroNamedSegment() { - MethodDescriptor rpcMethod = TESTING_SERVICE.getMethods().get(2); - Message inputMessage = MESSAGES.get("com." + rpcMethod.getInputType().getFullName()); IllegalArgumentException illegalArgumentException = - assertThrows( - IllegalArgumentException.class, - () -> RoutingRuleParser.parse(rpcMethod, inputMessage, MESSAGES)); + assertThrows(IllegalArgumentException.class, () -> getRoutingHeaders(2)); assertThat(illegalArgumentException.getMessage()) .isEqualTo( String.format( @@ -72,16 +64,48 @@ public void shouldThrowExceptionIfPathTemplateHasZeroNamedSegment() { @Test public void shouldThrowExceptionIfPathTemplateHasMoreThanOneNamedSegment() { - MethodDescriptor rpcMethod = TESTING_SERVICE.getMethods().get(3); - Message inputMessage = MESSAGES.get("com." + rpcMethod.getInputType().getFullName()); IllegalArgumentException illegalArgumentException = - assertThrows( - IllegalArgumentException.class, - () -> RoutingRuleParser.parse(rpcMethod, inputMessage, MESSAGES)); + assertThrows(IllegalArgumentException.class, () -> getRoutingHeaders(3)); assertThat(illegalArgumentException.getMessage()) .isEqualTo( String.format( PATH_TEMPLATE_WRONG_NUMBER_OF_NAMED_SEGMENT_ERROR_MESSAGE, "/v1beta1/{name=tests/*}/{second_name=*}")); } + + @Test + public void shouldParseRoutingRulesWithOneParameter() { + RoutingHeaders actual = getRoutingHeaders(4); + RoutingHeader expected = RoutingHeader.create("name", "rename", "/v1beta1/{rename=tests/*}"); + assertThat(actual.routingHeadersList()).containsExactly(expected); + } + + @Test + public void shouldParseRoutingRulesWithMultipleParameter() { + RoutingHeaders actual = getRoutingHeaders(5); + RoutingHeader expectedHeader1 = + RoutingHeader.create("name", "rename", "/v1beta1/{rename=tests/*}"); + RoutingHeader expectedHeader2 = + RoutingHeader.create("routing_id", "id", "/v1beta1/{id=projects/*}/tables/*"); + assertThat(actual.routingHeadersList()).containsExactly(expectedHeader1, expectedHeader2); + } + + @Test + public void shouldParseRoutingRulesWithNestedFields() { + RoutingHeaders actual = getRoutingHeaders(6); + RoutingHeader expectedHeader1 = + RoutingHeader.create("account.name", "rename", "/v1beta1/{rename=tests/*}"); + assertThat(actual.routingHeadersList()).containsExactly(expectedHeader1); + } + + @Test + public void shouldThrowExceptionIfFieldValidationFailed() { + assertThrows(IllegalStateException.class, () -> getRoutingHeaders(7)); + } + + private RoutingHeaders getRoutingHeaders(int testingIndex) { + MethodDescriptor rpcMethod = TESTING_SERVICE.getMethods().get(testingIndex); + Message inputMessage = MESSAGES.get("com." + rpcMethod.getInputType().getFullName()); + return RoutingRuleParser.parse(rpcMethod, inputMessage, MESSAGES); + } } diff --git a/src/test/java/com/google/api/generator/gapic/testdata/routing_rule_parser_testing.proto b/src/test/java/com/google/api/generator/gapic/testdata/routing_rule_parser_testing.proto index 031ea45641..8cb3f17511 100644 --- a/src/test/java/com/google/api/generator/gapic/testdata/routing_rule_parser_testing.proto +++ b/src/test/java/com/google/api/generator/gapic/testdata/routing_rule_parser_testing.proto @@ -94,6 +94,16 @@ service RoutingRuleParserTesting { }; } + // Test case for field validation. We already have extensive unit tests for field validation, so only testing one simple case. + rpc FieldDoesNotExistTest(FieldDoesNotExistTestRequest) returns (google.protobuf.Empty) { + option (google.api.routing) = { + routing_parameters { + field: "does_not_exist" + path_template: "/v1beta1/{rename=tests/*}" + } + }; + } + } message NoRoutingRuleTestRequest { @@ -124,6 +134,10 @@ message NestedFieldsHappyPathTestRequest { Account account = 1; } +message FieldDoesNotExistTestRequest { + string name = 1; +} + message Account { string name = 1; }