Search in sources :

Example 1 with SimpleDate

use of com.google.api.server.spi.types.SimpleDate in project endpoints-java by cloudendpoints.

the class ServletResponseResultWriterTest method testTypeChangesInMapAsString.

@Test
public void testTypeChangesInMapAsString() throws Exception {
    Map<String, Object> value = new HashMap<>();
    value.put("nonPrimitive", 100L);
    value.put("primitive", 200L);
    value.put("date", new Date(DATE_VALUE));
    value.put("dateAndTime", DateAndTime.parseRfc3339String(DATE_AND_TIME_VALUE_STRING));
    value.put("simpleDate", new SimpleDate(2002, 10, 2));
    testTypeChangesAsString(value);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SimpleDate(com.google.api.server.spi.types.SimpleDate) Date(java.util.Date) SimpleDate(com.google.api.server.spi.types.SimpleDate) Test(org.junit.Test)

Example 2 with SimpleDate

use of com.google.api.server.spi.types.SimpleDate in project endpoints-java by cloudendpoints.

the class ServletResponseResultWriter method getWriteSimpleDateAsStringModule.

private static SimpleModule getWriteSimpleDateAsStringModule() {
    JsonSerializer<SimpleDate> simpleDateSerializer = new JsonSerializer<SimpleDate>() {

        @Override
        public void serialize(SimpleDate value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
            jgen.writeString(String.format("%04d-%02d-%02d", value.getYear(), value.getMonth(), value.getDay()));
        }
    };
    SimpleModule writeSimpleDateAsModule = new SimpleModule("writeSimpleDateAsModule", new Version(1, 0, 0, null, null, null));
    writeSimpleDateAsModule.addSerializer(SimpleDate.class, simpleDateSerializer);
    return writeSimpleDateAsModule;
}
Also used : Version(com.fasterxml.jackson.core.Version) SimpleDate(com.google.api.server.spi.types.SimpleDate) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) JsonSerializer(com.fasterxml.jackson.databind.JsonSerializer) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 3 with SimpleDate

use of com.google.api.server.spi.types.SimpleDate in project endpoints-java by cloudendpoints.

the class ServletResponseResultWriterTest method testTypeChangesInBeanAsString.

@SuppressWarnings("unused")
public void testTypeChangesInBeanAsString() throws Exception {
    Object value = new Object() {

        public long getPrimitive() {
            return 200L;
        }

        public Long getNonPrimitive() {
            return 100L;
        }

        public Long getLongNull() {
            return null;
        }

        public String getStringNull() {
            return null;
        }

        public String getStringEmpty() {
            return "";
        }

        public Date getDate() {
            return new Date(DATE_VALUE);
        }

        public Date getDateNull() {
            return null;
        }

        public DateAndTime getDateAndTime() {
            return DateAndTime.parseRfc3339String(DATE_AND_TIME_VALUE_STRING);
        }

        public DateAndTime getDateAndTimeNull() {
            return null;
        }

        public SimpleDate getSimpleDate() {
            return new SimpleDate(2002, 10, 2);
        }

        public SimpleDate getSimpleDateNull() {
            return null;
        }

        public Long[] getEmptyLongArray() {
            return new Long[0];
        }

        public List<Long> getEmptyLongList() {
            return new ArrayList<>();
        }
    };
    ObjectNode output = testTypeChangesAsString(value);
    assertTrue(output.path("longNull").isMissingNode());
    assertTrue(output.path("stringNull").isMissingNode());
    assertEquals("", output.path("stringEmpty").asText());
    assertTrue(output.path("dateNull").isMissingNode());
    assertTrue(output.path("dateAndTimeNull").isMissingNode());
    assertTrue(output.path("simpleDateNull").isMissingNode());
    assertTrue(output.path("emptyLongArray").isMissingNode());
    assertTrue(output.path("emptyLongList").isMissingNode());
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) SimpleDate(com.google.api.server.spi.types.SimpleDate) ArrayList(java.util.ArrayList) Date(java.util.Date) SimpleDate(com.google.api.server.spi.types.SimpleDate)

Example 4 with SimpleDate

use of com.google.api.server.spi.types.SimpleDate in project endpoints-java by cloudendpoints.

the class ServletRequestParamReaderTest method testReadSimpleDate_success.

@Test
public void testReadSimpleDate_success() throws Exception {
    Method method = TestEndpoint.class.getDeclaredMethod("getSimpleDate", SimpleDate.class);
    Object[] params = null;
    params = readParameters("{" + TestEndpoint.NAME_DATE_AND_TIME + ":\"2002-10-02\"}", method);
    assertThat(Arrays.asList(params)).containsExactly(new SimpleDate(2002, 10, 2)).inOrder();
}
Also used : SimpleDate(com.google.api.server.spi.types.SimpleDate) Method(java.lang.reflect.Method) EndpointMethod(com.google.api.server.spi.EndpointMethod) Test(org.junit.Test)

Aggregations

SimpleDate (com.google.api.server.spi.types.SimpleDate)4 Date (java.util.Date)2 Test (org.junit.Test)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 Version (com.fasterxml.jackson.core.Version)1 JsonSerializer (com.fasterxml.jackson.databind.JsonSerializer)1 SerializerProvider (com.fasterxml.jackson.databind.SerializerProvider)1 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 EndpointMethod (com.google.api.server.spi.EndpointMethod)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1