Search in sources :

Example 1 with ForestGsonConverter

use of com.dtflys.forest.converter.json.ForestGsonConverter in project forest by dromara.

the class TestGsonConverter method testJavaObjectToMap.

@Test
public void testJavaObjectToMap() {
    Coordinate coordinate = new Coordinate("11.11111", "22.22222");
    ForestGsonConverter gsonConverter = new ForestGsonConverter();
    Map map = gsonConverter.convertObjectToMap(coordinate);
    assertNotNull(map);
    assertEquals("11.11111", map.get("longitude"));
    assertEquals("22.22222", map.get("latitude"));
}
Also used : SubCoordinate(com.dtflys.test.model.SubCoordinate) Coordinate(com.dtflys.test.model.Coordinate) ForestGsonConverter(com.dtflys.forest.converter.json.ForestGsonConverter) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Example 2 with ForestGsonConverter

use of com.dtflys.forest.converter.json.ForestGsonConverter in project forest by dromara.

the class TestGsonConverter method testDate.

@Test
public void testDate() throws ParseException {
    ForestGsonConverter gsonConverter = new ForestGsonConverter();
    String json = "{\"name\":\"foo\",\"date\":\"2020-10-10 10:12:00\"}";
    gsonConverter.setDateFormat("yyyy-MM-dd hh:mm:ss");
    TestJsonObj testJsonObj = gsonConverter.convertToJavaObject(json, TestJsonObj.class);
    assertNotNull(testJsonObj);
    assertEquals("foo", testJsonObj.getName());
    assertDateEquals("2020-10-10 10:12:00", testJsonObj.getDate(), "yyyy-MM-dd hh:mm:ss");
    json = "{\"name\":\"foo\",\"date\":\"2020/10/10 10:12:00\"}";
    gsonConverter.setDateFormat("yyyy/MM/dd hh:mm:ss");
    testJsonObj = gsonConverter.convertToJavaObject(json, TestJsonObj.class);
    assertNotNull(testJsonObj);
    assertEquals("foo", testJsonObj.getName());
    assertDateEquals("2020-10-10 10:12:00", testJsonObj.getDate(), "yyyy-MM-dd hh:mm:ss");
}
Also used : ForestGsonConverter(com.dtflys.forest.converter.json.ForestGsonConverter) Test(org.junit.Test)

Example 3 with ForestGsonConverter

use of com.dtflys.forest.converter.json.ForestGsonConverter in project forest by dromara.

the class TestGsonConverter method testConvertToJson.

@Test
public void testConvertToJson() {
    ForestGsonConverter gsonConverter = new ForestGsonConverter();
    String text = gsonConverter.encodeToString(new Integer[] { 100, 10 });
    Assert.assertEquals("[100,10]", text);
}
Also used : ForestGsonConverter(com.dtflys.forest.converter.json.ForestGsonConverter) Test(org.junit.Test)

Example 4 with ForestGsonConverter

use of com.dtflys.forest.converter.json.ForestGsonConverter in project forest by dromara.

the class TestGsonConverter method testConfig.

@Test
public void testConfig() {
    ForestJsonConverter jsonConverter = forestConfiguration.getJsonConverter();
    assertThat(jsonConverter).isNotNull().isInstanceOf(ForestGsonConverter.class);
    ForestGsonConverter forestGsonConverter = (ForestGsonConverter) jsonConverter;
    assertThat(forestGsonConverter.getDateFormat()).isEqualTo("yyyy/MM/dd hh:mm:ss");
}
Also used : ForestJsonConverter(com.dtflys.forest.converter.json.ForestJsonConverter) ForestGsonConverter(com.dtflys.forest.converter.json.ForestGsonConverter) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 5 with ForestGsonConverter

use of com.dtflys.forest.converter.json.ForestGsonConverter in project forest by dromara.

the class ConfigurationTest method testConfiguration.

public void testConfiguration() {
    applicationContext = new ClassPathXmlApplicationContext(new String[] { "classpath:configuration-test.xml" });
    ForestConfiguration forestConfiguration = (ForestConfiguration) applicationContext.getBean("forestConfiguration2");
    assertNotNull(forestConfiguration);
    assertNotNull(forestConfiguration.getBackend());
    assertEquals(forestConfiguration.getTimeout(), new Integer(30000));
    assertEquals(forestConfiguration.getConnectTimeout(), new Integer(10000));
    assertEquals(forestConfiguration.getMaxConnections(), new Integer(500));
    assertEquals(forestConfiguration.getMaxRouteConnections(), new Integer(500));
    assertEquals(forestConfiguration.getSslProtocol(), "SSLv3");
    assertEquals(forestConfiguration.getVariableValue("baseUrl"), "http://www.thebeastshop.com");
    assertEquals(forestConfiguration.getVariableValue("x"), "0");
    assertEquals(forestConfiguration.getVariableValue("y"), "1");
    assertEquals(forestConfiguration.getBackend().getName(), "httpclient");
    assertEquals("GBK", forestConfiguration.getCharset());
    assertEquals(Boolean.TRUE, Boolean.valueOf(forestConfiguration.isLogEnabled()));
    assertEquals(Boolean.FALSE, Boolean.valueOf(forestConfiguration.isLogResponseStatus()));
    assertEquals(Boolean.TRUE, Boolean.valueOf(forestConfiguration.isLogResponseContent()));
    assertEquals(NoneRetryer.class, forestConfiguration.getRetryer());
    assertTrue(forestConfiguration.getLogHandler() instanceof TestLogHandler);
    ForestJsonConverter jsonConverter = forestConfiguration.getJsonConverter();
    assertNotNull(jsonConverter);
    assertTrue(jsonConverter instanceof ForestGsonConverter);
    assertEquals("yyyy/MM/dd hh:mm:ss", jsonConverter.getDateFormat());
}
Also used : ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) ForestJsonConverter(com.dtflys.forest.converter.json.ForestJsonConverter) TestLogHandler(com.dtflys.spring.test.logging.TestLogHandler) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ForestGsonConverter(com.dtflys.forest.converter.json.ForestGsonConverter)

Aggregations

ForestGsonConverter (com.dtflys.forest.converter.json.ForestGsonConverter)10 Test (org.junit.Test)9 LinkedHashMap (java.util.LinkedHashMap)4 ForestJsonConverter (com.dtflys.forest.converter.json.ForestJsonConverter)3 Map (java.util.Map)3 SubCoordinate (com.dtflys.test.model.SubCoordinate)2 ForestConfiguration (com.dtflys.forest.config.ForestConfiguration)1 ForestJacksonConverter (com.dtflys.forest.converter.json.ForestJacksonConverter)1 JSONConverterSelector (com.dtflys.forest.converter.json.JSONConverterSelector)1 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)1 TestLogHandler (com.dtflys.spring.test.logging.TestLogHandler)1 Coordinate (com.dtflys.test.model.Coordinate)1 TypeToken (com.google.gson.reflect.TypeToken)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 List (java.util.List)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1