Search in sources :

Example 1 with ApiSerializationConfig

use of com.google.api.server.spi.config.model.ApiSerializationConfig in project endpoints-java by cloudendpoints.

the class SystemService method registerLoadedService.

private int registerLoadedService(Class<?> serviceClass, Object service, ApiConfig apiConfig) throws ApiConfigException {
    String fullName = serviceClass.getName();
    if (!servicesByName.containsKey(fullName)) {
        // TODO: The bit below uses two maps to store per-API serialization configurations.
        // The first map maps service names to an api-version key, and the second map maps the key to
        // a serialization config. This is currently required because the API information is not kept
        // outside of this method, but it would be nice to find a better way to clean this up.
        String api = apiConfig.getName() + "-" + apiConfig.getVersion();
        initialConfigsByApi.put(api, apiConfig);
        ApiSerializationConfig serializationConfig = serializationConfigs.get(api);
        if (serializationConfig == null) {
            serializationConfig = new ApiSerializationConfig();
        }
        for (SerializerConfig rule : apiConfig.getSerializationConfig().getSerializerConfigs()) {
            serializationConfig.addSerializationConfig(rule.getSerializer());
        }
        serializationConfigs.put(api, serializationConfig);
        registerServiceFromName(service, serviceClass.getSimpleName(), api);
        registerServiceFromName(service, fullName, api);
        updateEndpointConfig(service, apiConfig, null);
        return apiConfig.getApiClassConfig().getMethods().size();
    }
    return DUPLICATE_SERVICE_REGISTER_COUNT;
}
Also used : SerializerConfig(com.google.api.server.spi.config.model.ApiSerializationConfig.SerializerConfig) ApiSerializationConfig(com.google.api.server.spi.config.model.ApiSerializationConfig)

Example 2 with ApiSerializationConfig

use of com.google.api.server.spi.config.model.ApiSerializationConfig in project endpoints-java by cloudendpoints.

the class ApiConfigAnnotationReaderTest method validateEndpoint1.

private void validateEndpoint1(ApiConfig config, Class<? extends Endpoint1> clazz) throws Exception {
    assertEquals("api", config.getName());
    assertEquals("v1", config.getVersion());
    assertTrue(config.getIsDefaultVersion());
    assertFalse(config.getIsAbstract());
    assertTrue(config.getApiClassConfig().getUseDatastore());
    assertEquals(ImmutableList.of(PassAuthenticator.class), config.getAuthenticators());
    ApiAuthConfig auth = config.getAuthConfig();
    assertTrue(auth.getAllowCookieAuth());
    assertEquals(1, auth.getBlockedRegions().size());
    assertEquals("CU", auth.getBlockedRegions().get(0));
    ApiFrontendLimitsConfig frontendLimits = config.getFrontendLimitsConfig();
    assertEquals(1, frontendLimits.getUnregisteredUserQps());
    assertEquals(2, frontendLimits.getUnregisteredQps());
    assertEquals(3, frontendLimits.getUnregisteredDaily());
    // TODO: After fixing http://b/7459341, add check for number of rules.
    assertEquals("match0", frontendLimits.getRules().get(0).getMatch());
    assertEquals(1, frontendLimits.getRules().get(0).getQps());
    assertEquals(2, frontendLimits.getRules().get(0).getUserQps());
    assertEquals(3, frontendLimits.getRules().get(0).getDaily());
    assertEquals("analyticsId0", frontendLimits.getRules().get(0).getAnalyticsId());
    assertEquals("match10", frontendLimits.getRules().get(1).getMatch());
    assertEquals(11, frontendLimits.getRules().get(1).getQps());
    assertEquals(12, frontendLimits.getRules().get(1).getUserQps());
    assertEquals(13, frontendLimits.getRules().get(1).getDaily());
    assertEquals("analyticsId10", frontendLimits.getRules().get(1).getAnalyticsId());
    ApiCacheControlConfig cacheControl = config.getCacheControlConfig();
    assertEquals(ApiCacheControl.Type.PUBLIC, cacheControl.getType());
    assertEquals(1, cacheControl.getMaxAge());
    ApiSerializationConfig serialization = config.getSerializationConfig();
    assertEquals(1, serialization.getSerializerConfigs().size());
    ApiSerializationConfig.SerializerConfig serializerConfig = serialization.getSerializerConfig(TypeToken.of(SimpleBean.class));
    assertEquals(TypeToken.of(SimpleBean.class), serializerConfig.getSourceType());
    assertEquals(DumbSerializer1.class, serializerConfig.getSerializer());
    String[] defaultScopes = { "ss0", "ss1 ss2" };
    String[] defaultAudiences = { "aa0", "aa1" };
    String[] defaultClientIds = { "cc0", "cc1" };
    ApiMethodConfig listFoos = config.getApiClassConfig().getMethods().get(methodToEndpointMethod(Endpoint1.class.getMethod("listFoos")));
    String[] expectedScopes = { "s0", "s1 s2" };
    String[] expectedAudiences = { "a0", "a1" };
    String[] expectedClientIds = { "c0", "c1" };
    validateMethod(listFoos, "foos.list", "foos", ApiMethod.HttpMethod.GET, expectedScopes, expectedAudiences, expectedClientIds, ImmutableList.of(FailAuthenticator.class), ImmutableList.of(FailPeerAuthenticator.class));
    assertEquals(0, listFoos.getParameterConfigs().size());
    ApiMethodConfig getFoo = config.getApiClassConfig().getMethods().get(methodToEndpointMethod(Endpoint1.class.getMethod("getFoo", String.class)));
    validateMethod(getFoo, "foos.get", "foos/{id}", ApiMethod.HttpMethod.GET, defaultScopes, defaultAudiences, defaultClientIds, ImmutableList.of(PassAuthenticator.class), ImmutableList.of(PassPeerAuthenticator.class));
    assertEquals(1, getFoo.getParameterConfigs().size());
    validateParameter(getFoo.getParameterConfigs().get(0), "id", false, null, String.class);
    ApiMethodConfig insertFoo = config.getApiClassConfig().getMethods().get(methodToEndpointMethod(Endpoint1.class.getMethod("insertFoo", Foo.class)));
    validateMethod(insertFoo, "foos.insert", "foos", ApiMethod.HttpMethod.POST, defaultScopes, defaultAudiences, defaultClientIds, ImmutableList.of(PassAuthenticator.class), ImmutableList.of(PassPeerAuthenticator.class));
    assertEquals(1, insertFoo.getParameterConfigs().size());
    validateParameter(insertFoo.getParameterConfigs().get(0), null, false, null, Foo.class);
    ApiMethodConfig updateFoo = config.getApiClassConfig().getMethods().get(methodToEndpointMethod(Endpoint1.class.getMethod("updateFoo", String.class, Foo.class)));
    validateMethod(updateFoo, "foos.update", "foos/{id}", ApiMethod.HttpMethod.PUT, defaultScopes, defaultAudiences, defaultClientIds, ImmutableList.of(PassAuthenticator.class), ImmutableList.of(PassPeerAuthenticator.class));
    assertEquals(2, updateFoo.getParameterConfigs().size());
    validateParameter(updateFoo.getParameterConfigs().get(0), "id", false, null, String.class);
    validateParameter(updateFoo.getParameterConfigs().get(1), null, false, null, Foo.class);
    ApiMethodConfig removeFoo = config.getApiClassConfig().getMethods().get(methodToEndpointMethod(Endpoint1.class.getMethod("removeFoo", String.class)));
    validateMethod(removeFoo, "foos.remove", "foos/{id}", ApiMethod.HttpMethod.DELETE, defaultScopes, defaultAudiences, defaultClientIds, ImmutableList.of(PassAuthenticator.class), ImmutableList.of(PassPeerAuthenticator.class));
    assertEquals(1, removeFoo.getParameterConfigs().size());
    validateParameter(removeFoo.getParameterConfigs().get(0), "id", false, null, String.class);
    ApiMethodConfig execute0 = config.getApiClassConfig().getMethods().get(methodToEndpointMethod(Endpoint1.class.getMethod("execute0", String.class, int.class, Integer.class, long.class, Long.class, boolean.class, Boolean.class, float.class, Double.class)));
    validateMethod(execute0, "foos.execute0", "execute0", "POST", defaultScopes, defaultAudiences, defaultClientIds, ImmutableList.of(PassAuthenticator.class), ImmutableList.of(PassPeerAuthenticator.class));
    assertEquals(9, execute0.getParameterConfigs().size());
    validateParameter(execute0.getParameterConfigs().get(0), "id", false, null, String.class);
    validateParameter(execute0.getParameterConfigs().get(1), "i0", false, null, int.class);
    validateParameter(execute0.getParameterConfigs().get(2), "i1", true, null, Integer.class);
    validateParameter(execute0.getParameterConfigs().get(3), "long0", false, null, long.class);
    validateParameter(execute0.getParameterConfigs().get(4), "long1", true, null, Long.class);
    validateParameter(execute0.getParameterConfigs().get(5), "b0", false, null, boolean.class);
    validateParameter(execute0.getParameterConfigs().get(6), "b1", true, null, Boolean.class);
    validateParameter(execute0.getParameterConfigs().get(7), "f", false, null, float.class);
    validateParameter(execute0.getParameterConfigs().get(8), "d", true, null, Double.class);
    ApiMethodConfig execute1 = config.getApiClassConfig().getMethods().get(methodToEndpointMethod(Endpoint1.class.getMethod("execute1", Foo.class)));
    validateMethod(execute1, clazz.getSimpleName() + ".execute1", "execute1", ApiMethod.HttpMethod.POST, defaultScopes, defaultAudiences, defaultClientIds, ImmutableList.of(PassAuthenticator.class), ImmutableList.of(PassPeerAuthenticator.class));
    assertEquals(1, execute1.getParameterConfigs().size());
    validateParameter(execute1.getParameterConfigs().get(0), null, false, null, Foo.class);
    ApiMethodConfig execute2 = config.getApiClassConfig().getMethods().get(methodToEndpointMethod(Endpoint1.class.getMethod("execute2", SimpleBean.class)));
    validateMethod(execute2, "foos.execute2", "execute2/{serialized}", ApiMethod.HttpMethod.POST, defaultScopes, defaultAudiences, defaultClientIds, ImmutableList.of(PassAuthenticator.class), ImmutableList.of(PassPeerAuthenticator.class));
    assertEquals(1, execute2.getParameterConfigs().size());
    validateParameter(execute2.getParameterConfigs().get(0), "serialized", false, null, SimpleBean.class, DumbSerializer1.class, String.class);
}
Also used : FailAuthenticator(com.google.api.server.spi.testing.FailAuthenticator) FailPeerAuthenticator(com.google.api.server.spi.testing.FailPeerAuthenticator) ApiCacheControlConfig(com.google.api.server.spi.config.model.ApiCacheControlConfig) ApiSerializationConfig(com.google.api.server.spi.config.model.ApiSerializationConfig) ApiAuthConfig(com.google.api.server.spi.config.model.ApiAuthConfig) ApiMethodConfig(com.google.api.server.spi.config.model.ApiMethodConfig) ApiFrontendLimitsConfig(com.google.api.server.spi.config.model.ApiFrontendLimitsConfig) PassPeerAuthenticator(com.google.api.server.spi.testing.PassPeerAuthenticator) SimpleBean(com.google.api.server.spi.testing.SimpleBean) PassAuthenticator(com.google.api.server.spi.testing.PassAuthenticator)

Example 3 with ApiSerializationConfig

use of com.google.api.server.spi.config.model.ApiSerializationConfig in project endpoints-java by cloudendpoints.

the class ApiAnnotationIntrospectorTest method testApiDeserializationByConfig.

@Test
public void testApiDeserializationByConfig() throws Exception {
    ApiSerializationConfig config = new ApiSerializationConfig();
    config.addSerializationConfig(TestApiSerializationByConfigConverter.class);
    objectMapper.setAnnotationIntrospector(new ApiAnnotationIntrospector(config));
    TestApiSerializationByConfig value = objectMapper.readValue("\"test2serialized\"", TestApiSerializationByConfig.class);
    assertEquals("test2", value.getFoo());
}
Also used : ApiSerializationConfig(com.google.api.server.spi.config.model.ApiSerializationConfig) Test(org.junit.Test)

Example 4 with ApiSerializationConfig

use of com.google.api.server.spi.config.model.ApiSerializationConfig in project endpoints-java by cloudendpoints.

the class ApiAnnotationIntrospectorTest method testApiDeserializationByParameterizedConfig.

@Test
public void testApiDeserializationByParameterizedConfig() throws Exception {
    ApiSerializationConfig config = new ApiSerializationConfig();
    config.addSerializationConfig(TestApiSerializationByParameterizedConfigConverter.class);
    objectMapper.setAnnotationIntrospector(new ApiAnnotationIntrospector(config));
    TestApiSerializationByParameterizedConfig value = objectMapper.readValue("\"paramtest2serialized\"", TestApiSerializationByParameterizedConfig.class);
    assertEquals("paramtest2", value.getBar());
}
Also used : ApiSerializationConfig(com.google.api.server.spi.config.model.ApiSerializationConfig) Test(org.junit.Test)

Example 5 with ApiSerializationConfig

use of com.google.api.server.spi.config.model.ApiSerializationConfig in project endpoints-java by cloudendpoints.

the class ApiMethodAnnotationConfigTest method setUp.

@Before
public void setUp() throws Exception {
    serializationConfig = new ApiSerializationConfig();
    Mockito.when(apiClassConfig.getApiConfig()).thenReturn(apiConfig);
    Mockito.when(apiClassConfig.getAuthLevel()).thenReturn(AuthLevel.NONE);
    Mockito.when(apiClassConfig.getScopeExpression()).thenReturn(defaultScopeExpression);
    Mockito.when(apiClassConfig.getAudiences()).thenReturn(defaultAudiences);
    Mockito.when(apiClassConfig.getClientIds()).thenReturn(defaultClientIds);
    Mockito.<List<Class<? extends Authenticator>>>when(apiClassConfig.getAuthenticators()).thenReturn(defaultAuthenticators);
    Mockito.<List<Class<? extends PeerAuthenticator>>>when(apiClassConfig.getPeerAuthenticators()).thenReturn(defaultPeerAuthenticators);
    Mockito.when(apiClassConfig.getApiClassJavaSimpleName()).thenReturn(TestEndpoint.class.getSimpleName());
    Mockito.when(apiConfig.getSerializationConfig()).thenReturn(serializationConfig);
    EndpointMethod method = EndpointMethod.create(TestEndpoint.class, TestEndpoint.class.getMethod("overrideMethod1"));
    config = new ApiMethodConfig(method, new TypeLoader(), apiClassConfig);
    annotationConfig = new ApiMethodAnnotationConfig(config);
}
Also used : TestEndpoint(com.google.api.server.spi.testing.TestEndpoint) ApiMethodConfig(com.google.api.server.spi.config.model.ApiMethodConfig) EndpointsPeerAuthenticator(com.google.api.server.spi.auth.EndpointsPeerAuthenticator) PassPeerAuthenticator(com.google.api.server.spi.testing.PassPeerAuthenticator) PeerAuthenticator(com.google.api.server.spi.config.PeerAuthenticator) EndpointMethod(com.google.api.server.spi.EndpointMethod) TypeLoader(com.google.api.server.spi.TypeLoader) ApiSerializationConfig(com.google.api.server.spi.config.model.ApiSerializationConfig) EndpointsPeerAuthenticator(com.google.api.server.spi.auth.EndpointsPeerAuthenticator) PassAuthenticator(com.google.api.server.spi.testing.PassAuthenticator) PassPeerAuthenticator(com.google.api.server.spi.testing.PassPeerAuthenticator) Authenticator(com.google.api.server.spi.config.Authenticator) EndpointsAuthenticator(com.google.api.server.spi.auth.EndpointsAuthenticator) PeerAuthenticator(com.google.api.server.spi.config.PeerAuthenticator) Before(org.junit.Before)

Aggregations

ApiSerializationConfig (com.google.api.server.spi.config.model.ApiSerializationConfig)9 Test (org.junit.Test)4 Before (org.junit.Before)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 TypeLoader (com.google.api.server.spi.TypeLoader)2 ApiMethodConfig (com.google.api.server.spi.config.model.ApiMethodConfig)2 PassAuthenticator (com.google.api.server.spi.testing.PassAuthenticator)2 PassPeerAuthenticator (com.google.api.server.spi.testing.PassPeerAuthenticator)2 EndpointMethod (com.google.api.server.spi.EndpointMethod)1 ServiceContext (com.google.api.server.spi.ServiceContext)1 EndpointsAuthenticator (com.google.api.server.spi.auth.EndpointsAuthenticator)1 EndpointsPeerAuthenticator (com.google.api.server.spi.auth.EndpointsPeerAuthenticator)1 Authenticator (com.google.api.server.spi.config.Authenticator)1 PeerAuthenticator (com.google.api.server.spi.config.PeerAuthenticator)1 ApiConfigAnnotationReader (com.google.api.server.spi.config.annotationreader.ApiConfigAnnotationReader)1 ApiAuthConfig (com.google.api.server.spi.config.model.ApiAuthConfig)1 ApiCacheControlConfig (com.google.api.server.spi.config.model.ApiCacheControlConfig)1 ApiFrontendLimitsConfig (com.google.api.server.spi.config.model.ApiFrontendLimitsConfig)1 SerializerConfig (com.google.api.server.spi.config.model.ApiSerializationConfig.SerializerConfig)1 FailAuthenticator (com.google.api.server.spi.testing.FailAuthenticator)1