Search in sources :

Example 16 with KnoxSession

use of org.apache.knox.gateway.shell.KnoxSession in project knox by apache.

the class AliasTest method createMockKnoxSession.

private KnoxSession createMockKnoxSession() throws Exception {
    KnoxSession knoxSession = createMock(KnoxSession.class);
    expect(knoxSession.base()).andReturn("http://localhost/base").atLeastOnce();
    expect(knoxSession.getHeaders()).andReturn(Collections.emptyMap()).atLeastOnce();
    expect(knoxSession.executeNow(isA(HttpRequest.class))).andReturn(null).atLeastOnce();
    replay(knoxSession);
    return knoxSession;
}
Also used : KnoxSession(org.apache.knox.gateway.shell.KnoxSession)

Example 17 with KnoxSession

use of org.apache.knox.gateway.shell.KnoxSession in project knox by apache.

the class AliasTest method testRemoveGatewayAlias.

@Test
public void testRemoveGatewayAlias() {
    KnoxSession session = null;
    try {
        session = createMockKnoxSession();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    final String alias = "aliasPut1";
    final String expectedEndpointPath = session.base() + AbstractAliasRequest.SERVICE_PATH + "/" + AbstractAliasRequest.GATEWAY_CLUSTER_NAME + "/" + alias;
    AbstractAliasRequest request = Alias.remove(session, alias);
    assertTrue(request instanceof DeleteRequest);
    assertEquals("Endpoint mismatch", expectedEndpointPath, request.getRequestURI().toString());
    Callable callable = request.callable();
    try {
        callable.call();
    } catch (Exception e) {
    // expected
    }
    assertEquals("Unexpected HTTP method.", HttpDelete.METHOD_NAME, request.getRequest().getMethod());
}
Also used : KnoxSession(org.apache.knox.gateway.shell.KnoxSession) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 18 with KnoxSession

use of org.apache.knox.gateway.shell.KnoxSession in project knox by apache.

the class AliasTest method testAddGatewayAlias.

@Test
public void testAddGatewayAlias() {
    KnoxSession session = null;
    try {
        session = createMockKnoxSession();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    final String alias = "aliasPut1";
    final String pwd = "aliasPut1_pwd";
    final String expectedEndpointPath = session.base() + AbstractAliasRequest.SERVICE_PATH + "/" + AbstractAliasRequest.GATEWAY_CLUSTER_NAME + "/" + alias;
    AbstractAliasRequest request = Alias.add(session, alias, pwd);
    assertTrue(request instanceof PostRequest);
    assertEquals("Endpoint mismatch", expectedEndpointPath, request.getRequestURI().toString());
    Callable callable = request.callable();
    try {
        callable.call();
    } catch (Exception e) {
    // expected
    }
    assertEquals("Unexpected HTTP method.", HttpPost.METHOD_NAME, request.getRequest().getMethod());
    HttpRequestBase httpRequest = request.getRequest();
    assertTrue(httpRequest instanceof HttpPost);
    HttpEntity entity = ((HttpPost) httpRequest).getEntity();
    assertNotNull("Missing expected form data.", entity);
    assertTrue(entity instanceof UrlEncodedFormEntity);
    String formData = null;
    try {
        formData = EntityUtils.toString(entity);
    } catch (IOException e) {
        fail("Failed to consume request entity: " + e.getMessage());
    }
    assertNotNull(formData);
    assertEquals("Form data mismatch", PostRequest.FORM_PARAM_VALUE + "=" + pwd, formData);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpEntity(org.apache.http.HttpEntity) KnoxSession(org.apache.knox.gateway.shell.KnoxSession) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 19 with KnoxSession

use of org.apache.knox.gateway.shell.KnoxSession in project knox by apache.

the class AliasTest method testRemoveClusterAlias.

@Test
public void testRemoveClusterAlias() {
    KnoxSession session = null;
    try {
        session = createMockKnoxSession();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    final String clusterName = "myCluster";
    final String alias = "aliasPut1";
    final String expectedEndpointPath = session.base() + AbstractAliasRequest.SERVICE_PATH + "/" + clusterName + "/" + alias;
    AbstractAliasRequest request = Alias.remove(session, clusterName, alias);
    assertTrue(request instanceof DeleteRequest);
    assertEquals("Endpoint mismatch", expectedEndpointPath, request.getRequestURI().toString());
    Callable callable = request.callable();
    try {
        callable.call();
    } catch (Exception e) {
    // expected
    }
    assertEquals("Unexpected HTTP method.", HttpDelete.METHOD_NAME, request.getRequest().getMethod());
}
Also used : KnoxSession(org.apache.knox.gateway.shell.KnoxSession) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 20 with KnoxSession

use of org.apache.knox.gateway.shell.KnoxSession in project knox by apache.

the class TokenTest method createMockKnoxSession.

private KnoxSession createMockKnoxSession() throws Exception {
    KnoxSession knoxSession = createMock(KnoxSession.class);
    expect(knoxSession.base()).andReturn("http://localhost/base").atLeastOnce();
    expect(knoxSession.getHeaders()).andReturn(Collections.emptyMap()).atLeastOnce();
    expect(knoxSession.executeNow(isA(HttpRequest.class))).andReturn(null).atLeastOnce();
    replay(knoxSession);
    return knoxSession;
}
Also used : KnoxSession(org.apache.knox.gateway.shell.KnoxSession)

Aggregations

KnoxSession (org.apache.knox.gateway.shell.KnoxSession)22 IOException (java.io.IOException)12 Callable (java.util.concurrent.Callable)6 Test (org.junit.Test)6 KnoxShellException (org.apache.knox.gateway.shell.KnoxShellException)5 URISyntaxException (java.net.URISyntaxException)2 HttpEntity (org.apache.http.HttpEntity)2 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)2 HttpPost (org.apache.http.client.methods.HttpPost)2 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)2 CredentialCollectionException (org.apache.knox.gateway.shell.CredentialCollectionException)2 HashMap (java.util.HashMap)1 CredentialCollector (org.apache.knox.gateway.shell.CredentialCollector)1 KnoxShellTable (org.apache.knox.gateway.shell.table.KnoxShellTable)1