use of org.apache.servicecomb.foundation.common.utils.RestObjectMapper in project incubator-servicecomb-java-chassis by apache.
the class TestRestObjectMapper method testFormateDate.
@Test
public void testFormateDate() throws Exception {
RestObjectMapper mapper = new RestObjectMapper();
// must read/write ISO 8061 dates
Date date = mapper.readValue("\"2017-07-21T17:32:28Z\"".getBytes(), Date.class);
String dateStr = mapper.writeValueAsString(date);
Assert.assertEquals(dateStr, "\"2017-07-21T17:32:28.000+00:00\"");
date = mapper.readValue("\"2017-07-21T17:32:28.320+0100\"".getBytes(), Date.class);
dateStr = mapper.writeValueAsString(date);
// one hour later
Assert.assertEquals(dateStr, "\"2017-07-21T16:32:28.320+00:00\"");
}
use of org.apache.servicecomb.foundation.common.utils.RestObjectMapper in project incubator-servicecomb-java-chassis by apache.
the class TestRestObjectMapper method testJsonObjectWork.
@Test
public void testJsonObjectWork() {
JsonObject obj = new JsonObject();
obj.put("name", "a");
obj.put("desc", "b");
PojoModel model = RestObjectMapperFactory.getRestObjectMapper().convertValue(obj, TypeFactory.defaultInstance().constructType(PojoModel.class));
Assert.assertEquals("a", model.getName());
Assert.assertEquals("b", model.getDesc());
RestObjectMapperFactory.setDefaultRestObjectMapper(new RestObjectMapper());
model = RestObjectMapperFactory.getRestObjectMapper().convertValue(obj, TypeFactory.defaultInstance().constructType(PojoModel.class));
Assert.assertEquals("a", model.getName());
Assert.assertEquals("b", model.getDesc());
InputStream inputStream = new ByteArrayInputStream(new byte[0]);
try {
RestObjectMapperFactory.getRestObjectMapper().readValue(inputStream, PojoModel.class);
Assert.fail();
} catch (MismatchedInputException e) {
// right place, nothing to do.
} catch (Exception e) {
Assert.fail();
}
}
use of org.apache.servicecomb.foundation.common.utils.RestObjectMapper in project java-chassis by ServiceComb.
the class TestRestObjectMapper method testFormateDate.
@Test
public void testFormateDate() throws Exception {
RestObjectMapper mapper = new RestObjectMapper();
// must read/write ISO 8061 dates
Date date = mapper.readValue("\"2017-07-21T17:32:28Z\"".getBytes(), Date.class);
String dateStr = mapper.writeValueAsString(date);
Assert.assertEquals(dateStr, "\"2017-07-21T17:32:28.000+00:00\"");
date = mapper.readValue("\"2017-07-21T17:32:28.320+0100\"".getBytes(), Date.class);
dateStr = mapper.writeValueAsString(date);
// one hour later
Assert.assertEquals(dateStr, "\"2017-07-21T16:32:28.320+00:00\"");
}
use of org.apache.servicecomb.foundation.common.utils.RestObjectMapper in project java-chassis by ServiceComb.
the class TestRestObjectMapper method testJsonObjectWork.
@Test
public void testJsonObjectWork() {
JsonObject obj = new JsonObject();
obj.put("name", "a");
obj.put("desc", "b");
PojoModel model = RestObjectMapperFactory.getRestObjectMapper().convertValue(obj, TypeFactory.defaultInstance().constructType(PojoModel.class));
Assert.assertEquals("a", model.getName());
Assert.assertEquals("b", model.getDesc());
RestObjectMapperFactory.setDefaultRestObjectMapper(new RestObjectMapper());
model = RestObjectMapperFactory.getRestObjectMapper().convertValue(obj, TypeFactory.defaultInstance().constructType(PojoModel.class));
Assert.assertEquals("a", model.getName());
Assert.assertEquals("b", model.getDesc());
InputStream inputStream = new ByteArrayInputStream(new byte[0]);
try {
RestObjectMapperFactory.getRestObjectMapper().readValue(inputStream, PojoModel.class);
Assert.fail();
} catch (MismatchedInputException e) {
// right place, nothing to do.
} catch (Exception e) {
Assert.fail();
}
}
Aggregations