Search in sources :

Example 1 with ForestConfiguration

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

the class ForestBeanProcessor method processBean.

private void processBean(Object bean, Class beanClass) {
    Method[] methods = beanClass.getDeclaredMethods();
    for (Method method : methods) {
        BindingVar annotation = method.getAnnotation(BindingVar.class);
        if (annotation == null) {
            continue;
        }
        String confId = annotation.configuration();
        ForestConfiguration configuration = null;
        if (StringUtils.isNotBlank(confId)) {
            configuration = Forest.config(confId);
        } else {
            configuration = Forest.config();
        }
        String varName = annotation.value();
        SpringVariableValue variableValue = new SpringVariableValue(bean, method);
        configuration.setVariableValue(varName, variableValue);
    }
}
Also used : ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) Method(java.lang.reflect.Method) BindingVar(com.dtflys.forest.annotation.BindingVar)

Example 2 with ForestConfiguration

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

the class SSLTest method testSSL.

public void testSSL() {
    applicationContext = new ClassPathXmlApplicationContext(new String[] { "classpath:ssl-test.xml" });
    ForestConfiguration configuration = (ForestConfiguration) applicationContext.getBean("forestConfiguration");
    SSLKeyStore keyStore = configuration.getKeyStore("keystore1");
    assertNotNull(keyStore);
    assertNotNull(keyStore.getInputStream());
    assertEquals("keystore1", keyStore.getId());
    assertEquals("123456", keyStore.getKeystorePass());
    assertEquals("jks", keyStore.getKeystoreType());
    assertThat(keyStore.getSslSocketFactoryBuilder()).isNotNull().isInstanceOf(MySSLSocketFactoryBuilder.class);
    assertThat(keyStore.getHostnameVerifier()).isNotNull().isInstanceOf(MyHostnameVerifier.class);
    BeastshopClient beastshopClient = (BeastshopClient) applicationContext.getBean("beastshopClient");
    assertNotNull(beastshopClient);
    String result = beastshopClient.index();
    assertNotNull(result);
    Throwable th = null;
    try {
        beastshopClient.index2();
    } catch (ForestRuntimeException ex) {
        th = ex.getCause();
    }
    assertThat(th).isNotNull();
}
Also used : ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) BeastshopClient(com.dtflys.spring.test.client0.BeastshopClient) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) SSLKeyStore(com.dtflys.forest.ssl.SSLKeyStore)

Example 3 with ForestConfiguration

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

the class TestForestConfiguration method testBackend.

@Test
public void testBackend() {
    ForestConfiguration configuration = ForestConfiguration.createConfiguration();
    configuration.setBackendName("okhttp3");
    assertEquals("okhttp3", configuration.getBackend().getName());
    configuration.setBackend(null);
    configuration.setBackendName("httpclient");
    assertEquals("httpclient", configuration.getBackend().getName());
    HttpBackendSelector originSelector = new HttpBackendSelector();
    HttpBackendSelector selector = Mockito.spy(originSelector);
    configuration.setHttpBackendSelector(selector);
    Mockito.when(selector.findOkHttp3BackendInstance()).thenReturn(null);
    configuration.setBackendName(null);
    configuration.setBackend(null);
    Assert.assertEquals("httpclient", configuration.getBackend().getName());
    Mockito.when(selector.findHttpclientBackendInstance()).thenReturn(null);
    configuration.setBackendName(null);
    configuration.setBackend(null);
    boolean thrown = false;
    try {
        HttpBackend backend = configuration.getBackend();
        System.out.print(backend);
    } catch (ForestRuntimeException e) {
        thrown = true;
    }
    assertTrue(thrown);
}
Also used : HttpBackendSelector(com.dtflys.forest.backend.HttpBackendSelector) ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) HttpBackend(com.dtflys.forest.backend.HttpBackend) Test(org.junit.Test)

Example 4 with ForestConfiguration

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

the class TestForestConfiguration method testDefaultHeaders.

@Test
public void testDefaultHeaders() {
    ForestConfiguration configuration = ForestConfiguration.createConfiguration();
    List<RequestNameValue> defaultHeaders = new LinkedList<>();
    defaultHeaders.add(new RequestNameValue("Accept", "text/html", TARGET_HEADER));
    configuration.setDefaultHeaders(defaultHeaders);
    assertEquals(defaultHeaders, configuration.getDefaultHeaders());
}
Also used : ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) RequestNameValue(com.dtflys.forest.utils.RequestNameValue) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 5 with ForestConfiguration

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

the class TestAutoConverter method getConverter.

private DefaultAutoConverter getConverter() {
    ForestConfiguration configuration = ForestConfiguration.createConfiguration();
    DefaultAutoConverter autoConverter = (DefaultAutoConverter) configuration.getConverterMap().get(ForestDataType.AUTO);
    assertNotNull(autoConverter);
    return autoConverter;
}
Also used : ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) DefaultAutoConverter(com.dtflys.forest.converter.auto.DefaultAutoConverter)

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