Search in sources :

Example 1 with Form

use of com.sun.jersey.api.representation.Form in project OpenAM by OpenRock.

the class RestTest method missingResourcesTest.

@Test
public void missingResourcesTest() throws Exception {
    Form params = new Form();
    params.add("subject", hashedUserTokenId);
    params.add("action", "GET");
    params.add("env", ATTR_NAME + "=" + ATTR_VAL);
    params.add("realm", REALM);
    try {
        decisionsClient.queryParams(params).header(RestServiceManager.SUBJECT_HEADER_NAME, userTokenIdHeader).cookie(cookie).accept("application/json").get(String.class);
        throw new Exception("RESTTest.missingResourceTest: no exception thrown.");
    } catch (UniformInterfaceException e) {
        int errorCode = e.getResponse().getStatus();
        if (errorCode != 400) {
            throw new Exception("RESTTest.missingResourceTest: incorrect error code");
        }
        String json = e.getResponse().getEntity(String.class);
        JSONObject jo = new JSONObject(json);
        if (jo.optInt("statusCode") != 424) {
            throw new Exception("RESTTest.missingResourcesTest() failed, status code not 424");
        }
        if (jo.optJSONObject("body") != null) {
            throw new Exception("RESTTest.missingResourcesTest() failed, body not empty");
        }
    }
}
Also used : UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) JSONObject(org.json.JSONObject) Form(com.sun.jersey.api.representation.Form) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) Test(org.testng.annotations.Test)

Example 2 with Form

use of com.sun.jersey.api.representation.Form in project OpenAM by OpenRock.

the class RestTest method getEntitlementsTest.

@Test
public void getEntitlementsTest() throws Exception {
    Form params = new Form();
    params.add("subject", hashedUserTokenId);
    params.add("resource", RESOURCE_NAME);
    params.add("env", ATTR_NAME + "=" + ATTR_VAL);
    params.add("realm", REALM);
    String json = entitlementsClient.queryParams(params).header(RestServiceManager.SUBJECT_HEADER_NAME, userTokenIdHeader).cookie(cookie).accept("application/json").get(String.class);
    JSONObject jo = new JSONObject(json);
    if (jo.optInt("statusCode") != 200) {
        throw new Exception("RESTTest.getEntitlementsTest: failed, status code not 200");
    }
    JSONObject jbody = jo.optJSONObject("body");
    if (jbody == null) {
        throw new Exception("RESTTest.getEntitlementsTest: failed, body element is null");
    }
    JSONArray results = jbody.optJSONArray("results");
    if (results == null) {
        throw new Exception("RESTTest.getEntitlementsTest: failed, results element is null");
    }
    if (results.length() < 1) {
        throw new Exception("RESTTest.getEntitlementsTest: failed, results array is empty");
    }
    // dude, there are two entitlements returned.
    // the first one is the root resource which is http://www.resttest.com
    // and the action value is empty.
    // we need to get the second one, which is http://www.resttest.com:80/*
    JSONEntitlement ent = new JSONEntitlement(results.getJSONObject(1));
    Object resultObj = ent.getActionValue("GET");
    if (resultObj != null) {
        if (!ent.getActionValue("GET")) {
            throw new Exception("RESTTest.getEntitlementsTest: failed, action value is false");
        }
    } else {
        throw new Exception("RESTTest.getEntitlementsTest: failed, action value is null");
    }
}
Also used : JSONEntitlement(com.sun.identity.entitlement.JSONEntitlement) JSONObject(org.json.JSONObject) Form(com.sun.jersey.api.representation.Form) JSONArray(org.json.JSONArray) JSONObject(org.json.JSONObject) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) Test(org.testng.annotations.Test)

Example 3 with Form

use of com.sun.jersey.api.representation.Form in project OpenAM by OpenRock.

the class RestTest method getEntitlementTest.

@Test
public void getEntitlementTest() throws Exception {
    Form params = new Form();
    params.add("subject", hashedUserTokenId);
    params.add("resource", RESOURCE_NAME + "/index.html");
    params.add("action", "GET");
    params.add("env", ATTR_NAME + "=" + ATTR_VAL);
    params.add("realm", REALM);
    String json = entitlementClient.queryParams(params).header(RestServiceManager.SUBJECT_HEADER_NAME, userTokenIdHeader).cookie(cookie).accept("application/json").get(String.class);
    JSONObject jo = new JSONObject(json);
    if (jo.optInt("statusCode") != 200) {
        throw new Exception("RESTTest.getEntitlementTest() failed, status code not 200");
    }
    JSONObject jbody = jo.optJSONObject("body");
    if (jbody == null) {
        throw new Exception("RESTTest.getEntitlementTest() failed, body element is null");
    }
    JSONEntitlement ent = new JSONEntitlement(jbody);
    boolean result = ent.getActionValue("GET");
    if (!result) {
        throw new Exception("RESTTest.getEntitlementTest() failed");
    }
}
Also used : JSONEntitlement(com.sun.identity.entitlement.JSONEntitlement) JSONObject(org.json.JSONObject) Form(com.sun.jersey.api.representation.Form) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) Test(org.testng.annotations.Test)

Example 4 with Form

use of com.sun.jersey.api.representation.Form in project OpenAM by OpenRock.

the class ListenerRestTest method testAddDifferentApp.

@Test(dependsOnMethods = { "testAddMoreResources" })
public void testAddDifferentApp() throws Exception {
    Form form = new Form();
    form.add("application", "sunBank");
    form.add("subject", hashedTokenId);
    form.add("url", NOTIFICATION_URL);
    String result = listenerClient.header(RestServiceManager.SUBJECT_HEADER_NAME, tokenIdHeader).cookie(cookie).post(String.class, form);
    JSONObject jo = new JSONObject(result);
    if (!jo.getString("statusCode").equals(IdRepoErrorCode.ILLEGAL_ARGUMENTS)) {
        throw new Exception("ListenerRESTTest.testAddDifferentApp failed to add");
    }
    Set<EntitlementListener> listeners = ListenerManager.getInstance().getListeners(adminSubject);
    if ((listeners == null) || listeners.isEmpty()) {
        throw new Exception("ListenerTestTest.testAddDifferentApp: no listeners");
    }
}
Also used : JSONObject(org.json.JSONObject) Form(com.sun.jersey.api.representation.Form) EntitlementListener(com.sun.identity.entitlement.EntitlementListener) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) EncodingException(org.owasp.esapi.errors.EncodingException) MalformedURLException(java.net.MalformedURLException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Test(org.testng.annotations.Test)

Example 5 with Form

use of com.sun.jersey.api.representation.Form in project OpenAM by OpenRock.

the class PrivilegeRestTest method noJSONStringInPut.

private void noJSONStringInPut() throws Exception {
    try {
        Form form = new Form();
        webClient.path(PRIVILEGE_NAME).queryParam("subject", hashedTokenId).header(RestServiceManager.SUBJECT_HEADER_NAME, tokenIdHeader).cookie(cookie).put(String.class, form);
    } catch (UniformInterfaceException e) {
        validateUniformInterfaceException(e, 9, "noJSONStringInPut");
    }
}
Also used : UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) Form(com.sun.jersey.api.representation.Form)

Aggregations

Form (com.sun.jersey.api.representation.Form)29 UniformInterfaceException (com.sun.jersey.api.client.UniformInterfaceException)16 Test (org.testng.annotations.Test)13 JSONObject (org.json.JSONObject)12 ClientResponse (com.sun.jersey.api.client.ClientResponse)8 JSONEntitlement (com.sun.identity.entitlement.JSONEntitlement)4 EntitlementListener (com.sun.identity.entitlement.EntitlementListener)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 MalformedURLException (java.net.MalformedURLException)3 EncodingException (org.owasp.esapi.errors.EncodingException)3 Privilege (com.sun.identity.entitlement.Privilege)2 WebResource (com.sun.jersey.api.client.WebResource)2 URL (java.net.URL)2 HashSet (java.util.HashSet)2 JSONArray (org.json.JSONArray)2 DelegationPrivilege (com.sun.identity.delegation.DelegationPrivilege)1 EntitlementException (com.sun.identity.entitlement.EntitlementException)1 NumericAttributeCondition (com.sun.identity.entitlement.NumericAttributeCondition)1 Client (com.sun.jersey.api.client.Client)1 ClientHandlerException (com.sun.jersey.api.client.ClientHandlerException)1