Search in sources :

Example 1 with RestConfig

use of org.apache.metron.stellar.dsl.functions.RestConfig in project metron by apache.

the class RestFunctionsTest method restGetShouldThrowExceptionOnContentLengthMismatch.

@Test
public void restGetShouldThrowExceptionOnContentLengthMismatch() throws Exception {
    RestFunctions.RestGet restGet = new RestFunctions.RestGet();
    RestConfig restConfig = new RestConfig();
    HttpGet httpGet = mock(HttpGet.class);
    HttpEntity httpEntity = mock(HttpEntity.class);
    restConfig.put(VERIFY_CONTENT_LENGTH, true);
    when(httpGet.getURI()).thenReturn(new URI("uri"));
    when(httpEntity.getContent()).thenReturn(new ByteArrayInputStream("{\"get\":\"success\"}".getBytes(StandardCharsets.UTF_8)));
    when(httpEntity.getContentLength()).thenReturn(-1L);
    IOException e = assertThrows(IOException.class, () -> RestFunctions.parseResponse(restConfig, httpGet, httpEntity));
    assertEquals("Stellar REST request to uri returned incorrect or missing content length. Content length in the response was -1 but the actual body content length was 17.", e.getMessage());
}
Also used : RestConfig(org.apache.metron.stellar.dsl.functions.RestConfig) HttpEntity(org.apache.http.HttpEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpGet(org.apache.http.client.methods.HttpGet) IOException(java.io.IOException) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 2 with RestConfig

use of org.apache.metron.stellar.dsl.functions.RestConfig in project metron by apache.

the class RestFunctionsTest method restShouldBuildRestConfig.

/**
 * RestConfig should be built with settings in the correct order of precedence.
 */
@Test
public void restShouldBuildRestConfig() {
    Map<String, Object> config = new HashMap<String, Object>() {

        {
            put(BASIC_AUTH_USER, "user");
            put(PROXY_BASIC_AUTH_USER, "proxyUser");
        }
    };
    Map<String, Object> priorityConfig = new HashMap<String, Object>() {

        {
            put(BASIC_AUTH_USER, "priorityUser");
        }
    };
    RestConfig restConfig = RestFunctions.buildRestConfig(config, priorityConfig);
    assertEquals(6, restConfig.size());
    assertEquals(Collections.singletonList(200), restConfig.getResponseCodesAllowed());
    assertEquals("priorityUser", restConfig.getBasicAuthUser());
    assertEquals("proxyUser", restConfig.getProxyBasicAuthUser());
    assertTrue(restConfig.enforceJson());
    assertEquals(1000, restConfig.getTimeout().intValue());
    assertFalse(restConfig.verifyContentLength());
}
Also used : RestConfig(org.apache.metron.stellar.dsl.functions.RestConfig) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test)

Example 3 with RestConfig

use of org.apache.metron.stellar.dsl.functions.RestConfig in project metron by apache.

the class RestFunctionsTest method restGetShouldParseResponseOnNullContent.

@Test
public void restGetShouldParseResponseOnNullContent() throws Exception {
    RestConfig restConfig = new RestConfig();
    HttpGet httpGet = mock(HttpGet.class);
    HttpEntity httpEntity = mock(HttpEntity.class);
    // return empty on null content
    when(httpEntity.getContent()).thenReturn(null);
    assertEquals(Optional.empty(), RestFunctions.parseResponse(restConfig, httpGet, httpEntity));
}
Also used : RestConfig(org.apache.metron.stellar.dsl.functions.RestConfig) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) Test(org.junit.jupiter.api.Test)

Example 4 with RestConfig

use of org.apache.metron.stellar.dsl.functions.RestConfig in project metron by apache.

the class RestFunctionsTest method restGetShouldGetPoolingConnectionManager.

@Test
public void restGetShouldGetPoolingConnectionManager() {
    RestConfig restConfig = new RestConfig();
    restConfig.put(POOLING_MAX_TOTAL, 5);
    restConfig.put(POOLING_DEFAULT_MAX_PER_RUOTE, 2);
    PoolingHttpClientConnectionManager cm = RestFunctions.getConnectionManager(restConfig);
    assertEquals(5, cm.getMaxTotal());
    assertEquals(2, cm.getDefaultMaxPerRoute());
}
Also used : RestConfig(org.apache.metron.stellar.dsl.functions.RestConfig) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) Test(org.junit.jupiter.api.Test)

Example 5 with RestConfig

use of org.apache.metron.stellar.dsl.functions.RestConfig in project metron by apache.

the class RestFunctions method getHttpClient.

/**
 * Retrieves the ClosableHttpClient from a pooling connection manager.
 *
 * @param context The execution context.
 * @return A ClosableHttpClient.
 */
protected static CloseableHttpClient getHttpClient(Context context) {
    RestConfig restConfig = buildRestConfig(getGlobalConfig(context));
    PoolingHttpClientConnectionManager cm = getConnectionManager(restConfig);
    return HttpClients.custom().setConnectionManager(cm).build();
}
Also used : RestConfig(org.apache.metron.stellar.dsl.functions.RestConfig) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Aggregations

RestConfig (org.apache.metron.stellar.dsl.functions.RestConfig)9 Test (org.junit.jupiter.api.Test)8 HttpGet (org.apache.http.client.methods.HttpGet)5 HttpEntity (org.apache.http.HttpEntity)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)2 IOException (java.io.IOException)1 URI (java.net.URI)1 HashMap (java.util.HashMap)1 HttpHost (org.apache.http.HttpHost)1 AuthScope (org.apache.http.auth.AuthScope)1 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)1 CredentialsProvider (org.apache.http.client.CredentialsProvider)1 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)1 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)1