Skip to content

Commit

Permalink
#869. Add/refactor unit tests for RoutingRuleParserTest and MessageTest.
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeli0 committed Jan 4, 2022
1 parent 83d1abd commit f375d04
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -124,6 +134,10 @@ message NestedFieldsHappyPathTestRequest {
Account account = 1;
}

message FieldDoesNotExistTestRequest {
string name = 1;
}

message Account {
string name = 1;
}
Expand Down

0 comments on commit f375d04

Please sign in to comment.