Search in sources :

Example 1 with MockRequestMatcher

use of org.apache.knox.test.mock.MockRequestMatcher in project knox by apache.

the class GatewayBasicFuncTest method getYarnRmNodes.

private void getYarnRmNodes(ContentType contentType, Map<String, String> params) throws Exception {
    String username = "hdfs";
    String password = "hdfs-password";
    String path = "/v1/cluster/nodes/";
    String nodesResource = "/yarn/nodes";
    String nodeResource = "/yarn/node";
    String nodeId = "localhost:45454";
    String gatewayPath = driver.getUrl("RESOURCEMANAGER") + path;
    String gatewayPathQuery = driver.isUseGateway() ? "" : "?user.name=" + username;
    MockRequestMatcher mockRequestMatcher = driver.getMock("RESOURCEMANAGER").expect().method("GET").pathInfo(path).queryParam("user.name", username);
    if (params != null) {
        for (Entry<String, String> param : params.entrySet()) {
            mockRequestMatcher.queryParam(param.getKey(), param.getValue());
            if (gatewayPathQuery.isEmpty()) {
                gatewayPathQuery += "?";
            } else {
                gatewayPathQuery += "&";
            }
            gatewayPathQuery += param.getKey() + "=" + param.getValue();
        }
    }
    mockRequestMatcher.respond().status(HttpStatus.SC_OK).content(driver.getResourceBytes(nodesResource + (contentType == ContentType.JSON ? ".json" : ".xml"))).contentType(contentType.toString());
    String encryptedNodeId = given().auth().preemptive().basic(username, password).header("X-XSRF-Header", "jksdhfkhdsf").then().statusCode(HttpStatus.SC_OK).contentType(contentType).body("nodes.node[0].id", not(containsString(nodeId))).body("nodes.node[0].nodeHostName", isEmptyString()).body("nodes.node[0].nodeHTTPAddress", isEmptyString()).when().get(gatewayPath + gatewayPathQuery).getBody().path("nodes.node[0].id");
    driver.assertComplete();
    driver.getMock("RESOURCEMANAGER").expect().method("GET").pathInfo(path + nodeId).queryParam("user.name", username).respond().status(HttpStatus.SC_OK).content(driver.getResourceBytes(nodeResource + (contentType == ContentType.JSON ? ".json" : ".xml"))).contentType(contentType.toString());
    given().auth().preemptive().basic(username, password).header("X-XSRF-Header", "jksdhfkhdsf").then().statusCode(HttpStatus.SC_OK).contentType(contentType).body("node.id", not(containsString(nodeId))).body("node.nodeHostName", isEmptyString()).body("node.nodeHTTPAddress", isEmptyString()).when().get(gatewayPath + encryptedNodeId);
    driver.assertComplete();
}
Also used : MockRequestMatcher(org.apache.knox.test.mock.MockRequestMatcher) IsEmptyString.isEmptyString(org.hamcrest.text.IsEmptyString.isEmptyString) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 2 with MockRequestMatcher

use of org.apache.knox.test.mock.MockRequestMatcher in project knox by apache.

the class GatewayBasicFuncTest method getYarnRmApps.

private void getYarnRmApps(ContentType contentType, Map<String, String> params) throws Exception {
    String username = "hdfs";
    String password = "hdfs-password";
    String path = "/v1/cluster/apps/";
    String resource = "/yarn/apps";
    String gatewayPath = driver.getUrl("RESOURCEMANAGER") + path;
    String gatewayPathQuery = driver.isUseGateway() ? "" : "?user.name=" + username;
    InetSocketAddress gatewayAddress = driver.gateway.getAddresses()[0];
    String gatewayHostName = gatewayAddress.getHostName();
    String gatewayAddrName = InetAddress.getByName(gatewayHostName).getHostAddress();
    switch(contentType) {
        case JSON:
            resource += ".json";
            break;
        case XML:
            resource += ".xml";
            break;
        default:
            break;
    }
    MockRequestMatcher mockRequestMatcher = driver.getMock("RESOURCEMANAGER").expect().method("GET").pathInfo(path).queryParam("user.name", username);
    if (params != null) {
        for (Entry<String, String> param : params.entrySet()) {
            mockRequestMatcher.queryParam(param.getKey(), param.getValue());
            if (gatewayPathQuery.isEmpty()) {
                gatewayPathQuery += "?";
            } else {
                gatewayPathQuery += "&";
            }
            gatewayPathQuery += param.getKey() + "=" + param.getValue();
        }
    }
    mockRequestMatcher.respond().status(HttpStatus.SC_OK).content(driver.getResourceBytes(resource)).contentType(contentType.toString());
    given().auth().preemptive().basic(username, password).header("X-XSRF-Header", "jksdhfkhdsf").then().statusCode(HttpStatus.SC_OK).contentType(contentType).body("apps.app[0].trackingUrl", isEmptyString()).body("apps.app[1].trackingUrl", anyOf(startsWith("http://" + gatewayHostName + ":" + gatewayAddress.getPort() + "/"), startsWith("http://" + gatewayAddrName + ":" + gatewayAddress.getPort() + "/"))).body("apps.app[2].trackingUrl", isEmptyString()).body("apps.app[0].amContainerLogs", isEmptyString()).body("apps.app[1].amContainerLogs", isEmptyString()).body("apps.app[0].amHostHttpAddress", isEmptyString()).body("apps.app[1].amHostHttpAddress", isEmptyString()).body("apps.app[2].id", is("application_1399541193872_0009")).when().get(gatewayPath + gatewayPathQuery);
    driver.assertComplete();
}
Also used : MockRequestMatcher(org.apache.knox.test.mock.MockRequestMatcher) InetSocketAddress(java.net.InetSocketAddress) IsEmptyString.isEmptyString(org.hamcrest.text.IsEmptyString.isEmptyString) Matchers.containsString(org.hamcrest.Matchers.containsString)

Aggregations

MockRequestMatcher (org.apache.knox.test.mock.MockRequestMatcher)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 IsEmptyString.isEmptyString (org.hamcrest.text.IsEmptyString.isEmptyString)2 InetSocketAddress (java.net.InetSocketAddress)1