use of io.grpc.rls.RlsProtoConverters.RouteLookupConfigConverter in project grpc-java by grpc.
the class RlsProtoConvertersTest method convert_jsonRlsConfig_staleAgeCappedByMaxAge.
@Test
public void convert_jsonRlsConfig_staleAgeCappedByMaxAge() throws IOException {
String jsonStr = "{\n" + " \"grpcKeyBuilders\": [\n" + " {\n" + " \"names\": [\n" + " {\n" + " \"service\": \"service1\",\n" + " \"method\": \"create\"\n" + " }\n" + " ],\n" + " \"headers\": [\n" + " {\n" + " \"key\": \"user\"," + " \"names\": [\"User\", \"Parent\"],\n" + " \"optional\": true\n" + " },\n" + " {\n" + " \"key\": \"id\"," + " \"names\": [\"X-Google-Id\"],\n" + " \"optional\": true\n" + " }\n" + " ]\n" + " }\n" + " ],\n" + " \"lookupService\": \"service1\",\n" + " \"lookupServiceTimeout\": \"2s\",\n" + " \"maxAge\": \"300s\",\n" + " \"staleAge\": \"400s\",\n" + " \"validTargets\": [\"a valid target\"]," + " \"cacheSizeBytes\": \"1000\",\n" + " \"defaultTarget\": \"us_east_1.cloudbigtable.googleapis.com\"\n" + "}";
RouteLookupConfig expectedConfig = RouteLookupConfig.builder().grpcKeyBuilders(ImmutableList.of(GrpcKeyBuilder.create(ImmutableList.of(Name.create("service1", "create")), ImmutableList.of(NameMatcher.create("user", ImmutableList.of("User", "Parent")), NameMatcher.create("id", ImmutableList.of("X-Google-Id"))), ExtraKeys.DEFAULT, ImmutableMap.<String, String>of()))).lookupService("service1").lookupServiceTimeoutInNanos(TimeUnit.SECONDS.toNanos(2)).maxAgeInNanos(TimeUnit.SECONDS.toNanos(300)).staleAgeInNanos(TimeUnit.SECONDS.toNanos(300)).cacheSizeBytes(1000).defaultTarget("us_east_1.cloudbigtable.googleapis.com").build();
RouteLookupConfigConverter converter = new RouteLookupConfigConverter();
@SuppressWarnings("unchecked") Map<String, ?> parsedJson = (Map<String, ?>) JsonParser.parse(jsonStr);
RouteLookupConfig converted = converter.convert(parsedJson);
assertThat(converted).isEqualTo(expectedConfig);
}
use of io.grpc.rls.RlsProtoConverters.RouteLookupConfigConverter in project grpc-java by grpc.
the class RlsProtoConvertersTest method convert_jsonRlsConfig_emptyKeyBuilders.
@Test
public void convert_jsonRlsConfig_emptyKeyBuilders() throws IOException {
String jsonStr = "{\n" + " \"grpcKeyBuilders\": [],\n" + " \"lookupService\": \"service1\",\n" + " \"lookupServiceTimeout\": \"2s\",\n" + " \"maxAge\": \"300s\",\n" + " \"staleAge\": \"240s\",\n" + " \"validTargets\": [\"a valid target\"]," + " \"cacheSizeBytes\": \"1000\",\n" + " \"defaultTarget\": \"us_east_1.cloudbigtable.googleapis.com\"\n" + "}";
RouteLookupConfigConverter converter = new RouteLookupConfigConverter();
@SuppressWarnings("unchecked") Map<String, ?> parsedJson = (Map<String, ?>) JsonParser.parse(jsonStr);
try {
converter.convert(parsedJson);
fail("Exception expected");
} catch (IllegalArgumentException e) {
assertThat(e).hasMessageThat().contains("must have at least one GrpcKeyBuilder");
}
}
use of io.grpc.rls.RlsProtoConverters.RouteLookupConfigConverter in project grpc-java by grpc.
the class RlsProtoConvertersTest method convert_jsonRlsConfig_namesNotUnique.
@Test
public void convert_jsonRlsConfig_namesNotUnique() throws IOException {
String jsonStr = "{\n" + " \"grpcKeyBuilders\": [\n" + " {\n" + " \"names\": [\n" + " {\n" + " \"service\": \"service1\",\n" + " \"method\": \"create\"\n" + " }\n" + " ],\n" + " \"headers\": [\n" + " {\n" + " \"key\": \"user\"," + " \"names\": [\"User\", \"Parent\"],\n" + " \"optional\": true\n" + " },\n" + " {\n" + " \"key\": \"id\"," + " \"names\": [\"X-Google-Id\"],\n" + " \"optional\": true\n" + " }\n" + " ]\n" + " },\n" + " {\n" + " \"names\": [\n" + " {\n" + " \"service\": \"service1\",\n" + " \"method\": \"create\"\n" + " }\n" + " ],\n" + " \"headers\": [\n" + " {\n" + " \"key\": \"user\"," + " \"names\": [\"User\", \"Parent\"],\n" + " \"optional\": true\n" + " },\n" + " {\n" + " \"key\": \"password\"," + " \"names\": [\"Password\"],\n" + " \"optional\": true\n" + " }\n" + " ]\n" + " },\n" + " {\n" + " \"names\": [\n" + " {\n" + " \"service\": \"service3\",\n" + " \"method\": \"*\"\n" + " }\n" + " ],\n" + " \"headers\": [" + " {\n" + " \"key\": \"user\"," + " \"names\": [\"User\", \"Parent\"],\n" + " \"optional\": true\n" + " }\n" + " ],\n" + " \"extraKeys\": {\n" + " \"host\": \"host-key\",\n" + " \"service\": \"service-key\",\n" + " \"method\": \"method-key\"\n" + " }, \n" + " \"constantKeys\": {\n" + " \"constKey1\": \"value1\"\n" + " }\n" + " }\n" + " ],\n" + " \"lookupService\": \"service1\",\n" + " \"lookupServiceTimeout\": \"2s\",\n" + " \"maxAge\": \"300s\",\n" + " \"staleAge\": \"240s\",\n" + " \"validTargets\": [\"a valid target\"]," + " \"cacheSizeBytes\": \"1000\",\n" + " \"defaultTarget\": \"us_east_1.cloudbigtable.googleapis.com\"\n" + "}";
RouteLookupConfigConverter converter = new RouteLookupConfigConverter();
@SuppressWarnings("unchecked") Map<String, ?> parsedJson = (Map<String, ?>) JsonParser.parse(jsonStr);
try {
converter.convert(parsedJson);
fail("Exception expected");
} catch (IllegalArgumentException e) {
assertThat(e).hasMessageThat().contains("duplicate names in grpc_keybuilders: Name{service=service1, method=create}");
}
}
use of io.grpc.rls.RlsProtoConverters.RouteLookupConfigConverter in project grpc-java by grpc.
the class RlsProtoConvertersTest method convert_jsonRlsConfig_keyBuilderWithoutName.
@Test
public void convert_jsonRlsConfig_keyBuilderWithoutName() throws IOException {
String jsonStr = "{\n" + " \"grpcKeyBuilders\": [\n" + " {\n" + " \"headers\": [\n" + " {\n" + " \"key\": \"user\"," + " \"names\": [\"User\", \"Parent\"],\n" + " \"optional\": true\n" + " },\n" + " {\n" + " \"key\": \"id\"," + " \"names\": [\"X-Google-Id\"],\n" + " \"optional\": true\n" + " }\n" + " ]\n" + " }\n" + " ],\n" + " \"lookupService\": \"service1\",\n" + " \"lookupServiceTimeout\": \"2s\",\n" + " \"staleAge\": \"240s\",\n" + " \"validTargets\": [\"a valid target\"]," + " \"cacheSizeBytes\": \"1000\",\n" + " \"defaultTarget\": \"us_east_1.cloudbigtable.googleapis.com\"\n" + "}";
RouteLookupConfigConverter converter = new RouteLookupConfigConverter();
@SuppressWarnings("unchecked") Map<String, ?> parsedJson = (Map<String, ?>) JsonParser.parse(jsonStr);
try {
converter.convert(parsedJson);
fail("Exception expected");
} catch (IllegalArgumentException e) {
assertThat(e).hasMessageThat().contains("each keyBuilder must have at least one name");
}
}
use of io.grpc.rls.RlsProtoConverters.RouteLookupConfigConverter in project grpc-java by grpc.
the class RlsLoadBalancerProvider method parseLoadBalancingPolicyConfig.
@Override
public ConfigOrError parseLoadBalancingPolicyConfig(Map<String, ?> rawLoadBalancingConfigPolicy) {
try {
RouteLookupConfig routeLookupConfig = new RouteLookupConfigConverter().convert(JsonUtil.getObject(rawLoadBalancingConfigPolicy, "routeLookupConfig"));
Map<String, ?> routeLookupChannelServiceConfig = JsonUtil.getObject(rawLoadBalancingConfigPolicy, "routeLookupChannelServiceConfig");
ChildLoadBalancingPolicy lbPolicy = ChildLoadBalancingPolicy.create(JsonUtil.getString(rawLoadBalancingConfigPolicy, "childPolicyConfigTargetFieldName"), JsonUtil.checkObjectList(checkNotNull(JsonUtil.getList(rawLoadBalancingConfigPolicy, "childPolicy"))));
return ConfigOrError.fromConfig(new LbPolicyConfiguration(routeLookupConfig, routeLookupChannelServiceConfig, lbPolicy));
} catch (Exception e) {
return ConfigOrError.fromError(Status.INVALID_ARGUMENT.withDescription("can't parse config: " + e.getMessage()).withCause(e));
}
}
Aggregations