use of org.apache.metron.stellar.dsl.functions.RestConfig in project metron by apache.
the class RestFunctionsTest method restGetShouldGetHttpClientContext.
/**
* The REST_GET function should set the proper credentials in the HttpClientContext.
* @throws Exception
*/
@Test
public void restGetShouldGetHttpClientContext() throws Exception {
HttpHost target = new HttpHost("localhost", 8080);
HttpHost proxy = new HttpHost("localhost", 3128);
{
RestConfig restConfig = new RestConfig();
HttpClientContext actual = RestFunctions.getHttpClientContext(restConfig, target, Optional.empty());
assertNull(actual.getCredentialsProvider());
}
{
RestConfig restConfig = new RestConfig();
restConfig.put(BASIC_AUTH_USER, "user");
restConfig.put(BASIC_AUTH_PASSWORD_PATH, basicAuthPasswordFile.getAbsolutePath());
HttpClientContext actual = RestFunctions.getHttpClientContext(restConfig, target, Optional.empty());
HttpClientContext expected = HttpClientContext.create();
CredentialsProvider expectedCredentialsProvider = new BasicCredentialsProvider();
expectedCredentialsProvider.setCredentials(new AuthScope(target), new UsernamePasswordCredentials(restConfig.getBasicAuthUser(), basicAuthPassword));
expected.setCredentialsProvider(expectedCredentialsProvider);
assertEquals(expected.getCredentialsProvider().getCredentials(new AuthScope(target)), actual.getCredentialsProvider().getCredentials(new AuthScope(target)));
assertEquals(expected.getCredentialsProvider().getCredentials(new AuthScope(proxy)), actual.getCredentialsProvider().getCredentials(new AuthScope(proxy)));
}
{
RestConfig restConfig = new RestConfig();
restConfig.put(PROXY_BASIC_AUTH_USER, "proxyUser");
restConfig.put(PROXY_BASIC_AUTH_PASSWORD_PATH, proxyBasicAuthPasswordFile.getAbsolutePath());
HttpClientContext actual = RestFunctions.getHttpClientContext(restConfig, target, Optional.of(proxy));
HttpClientContext expected = HttpClientContext.create();
CredentialsProvider expectedCredentialsProvider = new BasicCredentialsProvider();
expectedCredentialsProvider.setCredentials(new AuthScope(proxy), new UsernamePasswordCredentials(restConfig.getProxyBasicAuthUser(), proxyAuthPassword));
expected.setCredentialsProvider(expectedCredentialsProvider);
assertEquals(expected.getCredentialsProvider().getCredentials(new AuthScope(target)), actual.getCredentialsProvider().getCredentials(new AuthScope(target)));
assertEquals(expected.getCredentialsProvider().getCredentials(new AuthScope(proxy)), actual.getCredentialsProvider().getCredentials(new AuthScope(proxy)));
}
{
RestConfig restConfig = new RestConfig();
restConfig.put(BASIC_AUTH_USER, "user");
restConfig.put(BASIC_AUTH_PASSWORD_PATH, basicAuthPasswordFile.getAbsolutePath());
restConfig.put(PROXY_BASIC_AUTH_USER, "proxyUser");
restConfig.put(PROXY_BASIC_AUTH_PASSWORD_PATH, proxyBasicAuthPasswordFile.getAbsolutePath());
HttpClientContext actual = RestFunctions.getHttpClientContext(restConfig, target, Optional.of(proxy));
HttpClientContext expected = HttpClientContext.create();
CredentialsProvider expectedCredentialsProvider = new BasicCredentialsProvider();
expectedCredentialsProvider.setCredentials(new AuthScope(target), new UsernamePasswordCredentials(restConfig.getBasicAuthUser(), basicAuthPassword));
expectedCredentialsProvider.setCredentials(new AuthScope(proxy), new UsernamePasswordCredentials(restConfig.getProxyBasicAuthUser(), proxyAuthPassword));
expected.setCredentialsProvider(expectedCredentialsProvider);
assertEquals(expected.getCredentialsProvider().getCredentials(new AuthScope(target)), actual.getCredentialsProvider().getCredentials(new AuthScope(target)));
assertEquals(expected.getCredentialsProvider().getCredentials(new AuthScope(proxy)), actual.getCredentialsProvider().getCredentials(new AuthScope(proxy)));
}
}
use of org.apache.metron.stellar.dsl.functions.RestConfig in project metron by apache.
the class RestFunctionsTest method restGetShouldParseResponseOnNullHttpEntity.
@Test
public void restGetShouldParseResponseOnNullHttpEntity() throws Exception {
RestConfig restConfig = new RestConfig();
HttpGet httpGet = mock(HttpGet.class);
// return empty on null httpEntity
assertEquals(Optional.empty(), RestFunctions.parseResponse(restConfig, httpGet, null));
}
use of org.apache.metron.stellar.dsl.functions.RestConfig in project metron by apache.
the class RestFunctionsTest method restGetShouldParseResponseOnEmptyInputStream.
@Test
public void restGetShouldParseResponseOnEmptyInputStream() throws Exception {
RestConfig restConfig = new RestConfig();
HttpGet httpGet = mock(HttpGet.class);
HttpEntity httpEntity = mock(HttpEntity.class);
// return empty on empty input stream
when(httpEntity.getContent()).thenReturn(new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8)));
assertEquals(Optional.empty(), RestFunctions.parseResponse(restConfig, httpGet, httpEntity));
}
use of org.apache.metron.stellar.dsl.functions.RestConfig in project metron by apache.
the class RestFunctionsTest method restGetShouldParseResponse.
@Test
public void restGetShouldParseResponse() throws Exception {
RestConfig restConfig = new RestConfig();
HttpGet httpGet = mock(HttpGet.class);
HttpEntity httpEntity = mock(HttpEntity.class);
// return successfully parsed response
when(httpEntity.getContent()).thenReturn(new ByteArrayInputStream("{\"get\":\"success\"}".getBytes(StandardCharsets.UTF_8)));
Optional<Object> actual = RestFunctions.parseResponse(restConfig, httpGet, httpEntity);
assertTrue(actual.isPresent());
assertEquals("success", ((Map<String, Object>) actual.get()).get("get"));
}
Aggregations