Search in sources :

Example 1 with RouteLookupConfigConverter

use of io.grpc.rls.RlsProtoConverters.RouteLookupConfigConverter in project grpc-java by grpc.

the class RlsProtoConvertersTest method convert_jsonRlsConfig_staleAgeGivenWithoutMaxAge.

@Test
public void convert_jsonRlsConfig_staleAgeGivenWithoutMaxAge() 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" + "  \"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("to specify staleAge, must have maxAge");
    }
}
Also used : RouteLookupConfigConverter(io.grpc.rls.RlsProtoConverters.RouteLookupConfigConverter) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) Test(org.junit.Test)

Example 2 with RouteLookupConfigConverter

use of io.grpc.rls.RlsProtoConverters.RouteLookupConfigConverter in project grpc-java by grpc.

the class RlsProtoConvertersTest method convert_jsonRlsConfig_keysNotUnique.

@Test
public void convert_jsonRlsConfig_keysNotUnique() throws IOException {
    String jsonStr = "{\n" + "  \"grpcKeyBuilders\": [\n" + "    {\n" + "      \"names\": [\n" + "        {\n" + "          \"service\": \"service1\"\n" + "        }\n" + "      ],\n" + "      \"headers\": [\n" + "        {\n" + // duplicate to extra_keys
    "          \"key\": \"service\"," + "          \"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("keys in KeyBuilder must be unique");
    }
}
Also used : RouteLookupConfigConverter(io.grpc.rls.RlsProtoConverters.RouteLookupConfigConverter) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) Test(org.junit.Test)

Example 3 with RouteLookupConfigConverter

use of io.grpc.rls.RlsProtoConverters.RouteLookupConfigConverter in project grpc-java by grpc.

the class RlsProtoConvertersTest method convert_jsonRlsConfig.

@Test
public void convert_jsonRlsConfig() 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\": \"*\"\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" + "}";
    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()), GrpcKeyBuilder.create(ImmutableList.of(Name.create("service1", "*")), ImmutableList.of(NameMatcher.create("user", ImmutableList.of("User", "Parent")), NameMatcher.create("password", ImmutableList.of("Password"))), ExtraKeys.DEFAULT, ImmutableMap.<String, String>of()), GrpcKeyBuilder.create(ImmutableList.of(Name.create("service3", "*")), ImmutableList.of(NameMatcher.create("user", ImmutableList.of("User", "Parent"))), ExtraKeys.create("host-key", "service-key", "method-key"), ImmutableMap.of("constKey1", "value1")))).lookupService("service1").lookupServiceTimeoutInNanos(TimeUnit.SECONDS.toNanos(2)).maxAgeInNanos(TimeUnit.SECONDS.toNanos(300)).staleAgeInNanos(TimeUnit.SECONDS.toNanos(240)).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);
}
Also used : RouteLookupConfig(io.grpc.rls.RlsProtoData.RouteLookupConfig) RouteLookupConfigConverter(io.grpc.rls.RlsProtoConverters.RouteLookupConfigConverter) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) Test(org.junit.Test)

Example 4 with RouteLookupConfigConverter

use of io.grpc.rls.RlsProtoConverters.RouteLookupConfigConverter in project grpc-java by grpc.

the class RlsProtoConvertersTest method convert_jsonRlsConfig_nameWithoutService.

@Test
public void convert_jsonRlsConfig_nameWithoutService() throws IOException {
    String jsonStr = "{\n" + "  \"grpcKeyBuilders\": [\n" + "    {\n" + "      \"names\": [\n" + "        {\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" + "  \"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("service must not be empty or null");
    }
}
Also used : RouteLookupConfigConverter(io.grpc.rls.RlsProtoConverters.RouteLookupConfigConverter) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) Test(org.junit.Test)

Example 5 with RouteLookupConfigConverter

use of io.grpc.rls.RlsProtoConverters.RouteLookupConfigConverter in project grpc-java by grpc.

the class RlsProtoConvertersTest method convert_jsonRlsConfig_defaultValues.

@Test
public void convert_jsonRlsConfig_defaultValues() throws IOException {
    String jsonStr = "{\n" + "  \"grpcKeyBuilders\": [\n" + "    {\n" + "      \"names\": [\n" + "        {\n" + "          \"service\": \"service1\"\n" + "        }\n" + "      ]\n" + "    }\n" + "  ],\n" + "  \"lookupService\": \"service1\",\n" + "  \"validTargets\": [\"a valid target\"]," + "  \"defaultTarget\": \"us_east_1.cloudbigtable.googleapis.com\"\n" + "}";
    RouteLookupConfig expectedConfig = RouteLookupConfig.builder().grpcKeyBuilders(ImmutableList.of(GrpcKeyBuilder.create(ImmutableList.of(Name.create("service1", null)), ImmutableList.<NameMatcher>of(), ExtraKeys.DEFAULT, ImmutableMap.<String, String>of()))).lookupService("service1").lookupServiceTimeoutInNanos(TimeUnit.SECONDS.toNanos(10)).maxAgeInNanos(TimeUnit.MINUTES.toNanos(5)).staleAgeInNanos(TimeUnit.MINUTES.toNanos(5)).cacheSizeBytes(5 * 1024 * 1024).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);
}
Also used : RouteLookupConfig(io.grpc.rls.RlsProtoData.RouteLookupConfig) NameMatcher(io.grpc.rls.RlsProtoData.NameMatcher) RouteLookupConfigConverter(io.grpc.rls.RlsProtoConverters.RouteLookupConfigConverter) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

RouteLookupConfigConverter (io.grpc.rls.RlsProtoConverters.RouteLookupConfigConverter)10 ImmutableMap (com.google.common.collect.ImmutableMap)9 Map (java.util.Map)9 Test (org.junit.Test)9 RouteLookupConfig (io.grpc.rls.RlsProtoData.RouteLookupConfig)4 ChildLoadBalancingPolicy (io.grpc.rls.LbPolicyConfiguration.ChildLoadBalancingPolicy)1 NameMatcher (io.grpc.rls.RlsProtoData.NameMatcher)1