Search in sources :

Example 1 with Nullable

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

the class ServletRequestParamReaderTest method testJavaxNamed.

@Test
public void testJavaxNamed() throws Exception {
    class Test {

        @SuppressWarnings("unused")
        public void foo(@javax.inject.Named("str") String str, @Nullable @javax.inject.Named("i") Integer i) {
        }
    }
    String requestString = "{\"str\":\"hello\"}";
    Method method = Test.class.getDeclaredMethod("foo", String.class, Integer.class);
    Object[] params = readParameters(requestString, method);
    assertEquals(2, params.length);
    assertEquals("hello", params[0]);
    assertNull(params[1]);
}
Also used : Method(java.lang.reflect.Method) EndpointMethod(com.google.api.server.spi.EndpointMethod) Nullable(com.google.api.server.spi.config.Nullable) Test(org.junit.Test)

Example 2 with Nullable

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

the class ServletRequestParamReaderTest method testReadNullArray.

@Test
public void testReadNullArray() throws Exception {
    class Test {

        @SuppressWarnings("unused")
        public void collection(@Nullable @Named("integer") Integer[] integers) {
        }
    }
    Method method = Test.class.getDeclaredMethod("collection", Integer[].class);
    Object[] params = readParameters("{}", method);
    assertEquals(1, params.length);
    @SuppressWarnings("unchecked") Integer[] integers = (Integer[]) params[0];
    assertNull(integers);
}
Also used : Named(com.google.api.server.spi.config.Named) Method(java.lang.reflect.Method) EndpointMethod(com.google.api.server.spi.EndpointMethod) Nullable(com.google.api.server.spi.config.Nullable) Test(org.junit.Test)

Example 3 with Nullable

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

the class ServletRequestParamReaderTest method testReadNullList.

@Test
public void testReadNullList() throws Exception {
    class Test {

        @SuppressWarnings("unused")
        public void collection(@Nullable @Named("list") List<Integer> integers) {
        }
    }
    Method method = Test.class.getDeclaredMethod("collection", List.class);
    Object[] params = readParameters("{}", method);
    assertEquals(1, params.length);
    @SuppressWarnings("unchecked") List<Integer> integers = (List<Integer>) params[0];
    assertNull(integers);
}
Also used : Named(com.google.api.server.spi.config.Named) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Method(java.lang.reflect.Method) EndpointMethod(com.google.api.server.spi.EndpointMethod) Nullable(com.google.api.server.spi.config.Nullable) Test(org.junit.Test)

Example 4 with Nullable

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

the class ServletRequestParamReaderTest method testCachedNamesAreUsed.

@Test
public void testCachedNamesAreUsed() throws Exception {
    class Test {

        @SuppressWarnings("unused")
        public void foo(@Named("foo1") String f1, @Nullable @Named("foo2") String f2, @Named("foo3") String f3) {
        }
    }
    Method method = Test.class.getDeclaredMethod("foo", String.class, String.class, String.class);
    EndpointMethod endpointMethod = EndpointMethod.create(method.getDeclaringClass(), method);
    endpointMethod.setParameterNames(ImmutableList.of("name1", "name2", "name3"));
    String requestString = "{\"name1\":\"v1\", \"foo2\":\"v2\", \"name3\":\"v3\"}";
    Object[] params = readParameters(requestString, endpointMethod);
    assertEquals(3, params.length);
    assertEquals("v1", params[0]);
    assertNull(params[1]);
    assertEquals("v3", params[2]);
}
Also used : Named(com.google.api.server.spi.config.Named) EndpointMethod(com.google.api.server.spi.EndpointMethod) Method(java.lang.reflect.Method) EndpointMethod(com.google.api.server.spi.EndpointMethod) Nullable(com.google.api.server.spi.config.Nullable) Test(org.junit.Test)

Example 5 with Nullable

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

the class ServletRequestParamReaderTest method testNamesAreCached.

@Test
public void testNamesAreCached() throws Exception {
    class Test {

        @SuppressWarnings("unused")
        public void foo(@Nullable @Named("foo1") String f1, @Nullable @Named("foo2") String f2, @Nullable @Named("foo3") String f3) {
        }
    }
    Method method = Test.class.getDeclaredMethod("foo", String.class, String.class, String.class);
    EndpointMethod endpointMethod = EndpointMethod.create(method.getDeclaringClass(), method);
    readParameters("{}", endpointMethod);
    List<String> parameterNames = endpointMethod.getParameterNames();
    assertEquals(3, parameterNames.size());
    assertEquals("foo1", parameterNames.get(0));
    assertEquals("foo2", parameterNames.get(1));
    assertEquals("foo3", parameterNames.get(2));
}
Also used : Named(com.google.api.server.spi.config.Named) EndpointMethod(com.google.api.server.spi.EndpointMethod) Method(java.lang.reflect.Method) EndpointMethod(com.google.api.server.spi.EndpointMethod) Nullable(com.google.api.server.spi.config.Nullable) Test(org.junit.Test)

Aggregations

Nullable (com.google.api.server.spi.config.Nullable)6 Test (org.junit.Test)6 EndpointMethod (com.google.api.server.spi.EndpointMethod)5 Named (com.google.api.server.spi.config.Named)5 Method (java.lang.reflect.Method)5 Api (com.google.api.server.spi.config.Api)1 DefaultValue (com.google.api.server.spi.config.DefaultValue)1 ApiConfig (com.google.api.server.spi.config.model.ApiConfig)1 ApiMethodConfig (com.google.api.server.spi.config.model.ApiMethodConfig)1 ApiParameterConfig (com.google.api.server.spi.config.model.ApiParameterConfig)1 SimpleLevelOverridingApi (com.google.api.server.spi.testing.SimpleLevelOverridingApi)1 SimpleLevelOverridingInheritedApi (com.google.api.server.spi.testing.SimpleLevelOverridingInheritedApi)1 ImmutableList (com.google.common.collect.ImmutableList)1 List (java.util.List)1