Search in sources :

Example 16 with ForestConfiguration

use of com.dtflys.forest.config.ForestConfiguration in project forest by dromara.

the class ConverterBeanListener method onApplicationEvent.

@Override
public void onApplicationEvent(ApplicationContextEvent event) {
    ApplicationContext applicationContext = event.getApplicationContext();
    ForestConfiguration forestConfiguration = this.forestConfiguration;
    if (forestConfiguration == null) {
        try {
            forestConfiguration = applicationContext.getBean(ForestConfiguration.class);
        } catch (Exception ignored) {
            throw new ForestRuntimeException("property forestConfiguration undefined", ignored);
        }
    }
    Map<String, ForestConverter> forestConverterMap = applicationContext.getBeansOfType(ForestConverter.class);
    for (ForestConverter forestConverter : forestConverterMap.values()) {
        forestConfiguration.getConverterMap().put(forestConverter.getDataType(), forestConverter);
    }
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) ForestConverter(com.dtflys.forest.converter.ForestConverter) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException)

Example 17 with ForestConfiguration

use of com.dtflys.forest.config.ForestConfiguration 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)

Example 18 with ForestConfiguration

use of com.dtflys.forest.config.ForestConfiguration in project forest by dromara.

the class TestForestConfiguration method testDefault.

@Test
public void testDefault() {
    ForestConfiguration configuration = ForestConfiguration.createConfiguration();
    assertEquals("forestConfiguration" + configuration.hashCode(), configuration.getId());
    assertEquals(Integer.valueOf(3000), configuration.getTimeout());
    assertEquals(null, configuration.getConnectTimeout());
    assertEquals(null, configuration.getReadTimeout());
    assertEquals(Integer.valueOf(500), configuration.getMaxConnections());
    assertEquals(Integer.valueOf(500), configuration.getMaxRouteConnections());
    assertNotNull(configuration.getJsonConverter());
}
Also used : ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) Test(org.junit.Test)

Example 19 with ForestConfiguration

use of com.dtflys.forest.config.ForestConfiguration in project forest by dromara.

the class TestForestConfiguration method testDefaultParameters.

@Test
public void testDefaultParameters() {
    ForestConfiguration configuration = ForestConfiguration.createConfiguration();
    List<RequestNameValue> defaultParameters = new LinkedList<>();
    defaultParameters.add(new RequestNameValue("name", "Peter", TARGET_BODY));
    defaultParameters.add(new RequestNameValue("age", "15", TARGET_BODY));
    configuration.setDefaultParameters(defaultParameters);
    assertEquals(defaultParameters, configuration.getDefaultParameters());
}
Also used : ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) RequestNameValue(com.dtflys.forest.utils.RequestNameValue) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 20 with ForestConfiguration

use of com.dtflys.forest.config.ForestConfiguration in project forest by dromara.

the class TestForestConfiguration method testVars.

@Test
public void testVars() {
    ForestConfiguration configuration = ForestConfiguration.createConfiguration();
    configuration.setVariableValue("name", "Peter");
    configuration.setVariableValue("baseUrl", "http://abc.com");
    assertEquals("Peter", configuration.getVariableValue("name"));
    assertEquals("http://abc.com", configuration.getVariableValue("baseUrl"));
    Map<String, Object> varMap = new HashMap<>();
    varMap.put("name", "Linda");
    varMap.put("abc", 123);
    configuration.setVariables(varMap);
    assertThat(configuration.getVariableValue("name")).isEqualTo("Linda");
    assertThat(configuration.getVariableValue("abc")).isEqualTo(123);
    AtomicInteger value = new AtomicInteger(0);
    configuration.setVariableValue("foo", (method) -> value.getAndIncrement());
    assertThat(configuration.getVariableValue("foo")).isEqualTo(0);
    assertThat(configuration.getVariableValue("foo")).isEqualTo(1);
}
Also used : ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) HashMap(java.util.HashMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Aggregations

ForestConfiguration (com.dtflys.forest.config.ForestConfiguration)23 Test (org.junit.Test)12 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)7 HashMap (java.util.HashMap)4 ForestRequest (com.dtflys.forest.http.ForestRequest)3 ForestLogHandler (com.dtflys.forest.logging.ForestLogHandler)3 LogConfiguration (com.dtflys.forest.logging.LogConfiguration)3 SuccessWhen (com.dtflys.forest.callback.SuccessWhen)2 ForestConverter (com.dtflys.forest.converter.ForestConverter)2 MetaRequest (com.dtflys.forest.reflection.MetaRequest)2 RequestNameValue (com.dtflys.forest.utils.RequestNameValue)2 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)2 BindingVar (com.dtflys.forest.annotation.BindingVar)1 HttpBackend (com.dtflys.forest.backend.HttpBackend)1 HttpBackendSelector (com.dtflys.forest.backend.HttpBackendSelector)1 DefaultAutoConverter (com.dtflys.forest.converter.auto.DefaultAutoConverter)1 ForestFastjsonConverter (com.dtflys.forest.converter.json.ForestFastjsonConverter)1 ForestGsonConverter (com.dtflys.forest.converter.json.ForestGsonConverter)1