Search in sources :

Example 1 with JSONEntitlement

use of com.sun.identity.entitlement.JSONEntitlement 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 2 with JSONEntitlement

use of com.sun.identity.entitlement.JSONEntitlement 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 3 with JSONEntitlement

use of com.sun.identity.entitlement.JSONEntitlement in project OpenAM by OpenRock.

the class MultipleResourceRestTest method testEntitlements.

@Test
public void testEntitlements() throws Exception {
    MultivaluedMap params = new MultivaluedMapImpl();
    params.add("resource", RESOURCE_NAME);
    params.add("realm", REALM);
    params.add("subject", hashedTokenId);
    String json = entitlementsClient.queryParams(params).accept("application/json").header(RestServiceManager.SUBJECT_HEADER_NAME, tokenIdHeader).cookie(cookie).get(String.class);
    List<JSONEntitlement> entitlements = JSONEntitlement.getEntitlements(new JSONObject(json));
    for (JSONEntitlement e : entitlements) {
        String res = e.getResourceName();
        Map<String, Boolean> actionValues = e.getActionValues();
        if (res.equals(RESOURCE_NAME)) {
            if (!actionValues.isEmpty()) {
                throw new Exception("MultipleResourceRestTest.testEntitlements: incorrect result for root");
            }
        } else if (res.equals(RESOURCE_NAME + ":80/index.html")) {
            if (actionValues.get("GET")) {
                throw new Exception("MultipleResourceRestTest.testEntitlements: incorrect result for /index.html");
            }
        } else if (res.equals(RESOURCE_NAME + ":80/*")) {
            if (!actionValues.get("GET")) {
                throw new Exception("MultipleResourceRestTest.testEntitlements: incorrect result for /*");
            }
        }
    }
}
Also used : JSONEntitlement(com.sun.identity.entitlement.JSONEntitlement) JSONObject(org.json.JSONObject) MultivaluedMapImpl(com.sun.jersey.core.util.MultivaluedMapImpl) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Test(org.testng.annotations.Test)

Example 4 with JSONEntitlement

use of com.sun.identity.entitlement.JSONEntitlement in project OpenAM by OpenRock.

the class MultipleResourceRestTest method testDecisions.

@Test
public void testDecisions() throws Exception {
    MultivaluedMap params = new MultivaluedMapImpl();
    params.add("resources", RESOURCE_NAME + "/index.html");
    params.add("resources", RESOURCE_NAME + "/a");
    params.add("action", "GET");
    params.add("realm", REALM);
    params.add("subject", hashedTokenId);
    String json = decisionsClient.queryParams(params).accept("application/json").header(RestServiceManager.SUBJECT_HEADER_NAME, tokenIdHeader).cookie(cookie).get(String.class);
    List<JSONEntitlement> entitlements = JSONEntitlement.getEntitlements(new JSONObject(json));
    for (JSONEntitlement e : entitlements) {
        String res = e.getResourceName();
        Map<String, Boolean> actionValues = e.getActionValues();
        if (res.equals(RESOURCE_NAME + "/index.html")) {
            if (actionValues.get("GET")) {
                throw new Exception("MultipleResourceRestTest.testDecisions: incorrect result for /index.html");
            }
        } else if (res.equals(RESOURCE_NAME + "/a")) {
            if (!actionValues.get("GET")) {
                throw new Exception("MultipleResourceRestTest.testDecisions: incorrect result for /a");
            }
        }
    }
}
Also used : JSONEntitlement(com.sun.identity.entitlement.JSONEntitlement) JSONObject(org.json.JSONObject) MultivaluedMapImpl(com.sun.jersey.core.util.MultivaluedMapImpl) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Test(org.testng.annotations.Test)

Example 5 with JSONEntitlement

use of com.sun.identity.entitlement.JSONEntitlement in project OpenAM by OpenRock.

the class RestTest method getDecisionsTest.

@Test
public void getDecisionsTest() throws Exception {
    Form params = new Form();
    params.add("subject", hashedUserTokenId);
    params.add("resources", RESOURCE_NAME + "/index.html");
    params.add("action", "GET");
    params.add("env", ATTR_NAME + "=" + ATTR_VAL);
    params.add("realm", REALM);
    String json = decisionsClient.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.getDecisionsTest() failed, status code not 200");
    }
    JSONObject jbody = jo.optJSONObject("body");
    if (jbody == null) {
        throw new Exception("RESTTest.getDecisionsTest() failed, body element is null");
    }
    JSONArray results = jbody.optJSONArray("results");
    if (results == null) {
        throw new Exception("RESTTest.getDecisionsTest() failed, results array is null");
    }
    if (results.length() < 1) {
        throw new Exception("RESTTest.getDecisionsTest() failed, results array is empty");
    }
    JSONEntitlement ent = new JSONEntitlement(results.getJSONObject(0));
    boolean result = ent.getActionValue("GET");
    if (!result) {
        throw new Exception("RESTTest.getDecisionsTest() failed");
    }
}
Also used : JSONEntitlement(com.sun.identity.entitlement.JSONEntitlement) JSONObject(org.json.JSONObject) Form(com.sun.jersey.api.representation.Form) JSONArray(org.json.JSONArray) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) Test(org.testng.annotations.Test)

Aggregations

JSONEntitlement (com.sun.identity.entitlement.JSONEntitlement)6 JSONObject (org.json.JSONObject)6 Test (org.testng.annotations.Test)6 UniformInterfaceException (com.sun.jersey.api.client.UniformInterfaceException)4 Form (com.sun.jersey.api.representation.Form)4 MultivaluedMapImpl (com.sun.jersey.core.util.MultivaluedMapImpl)2 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)2 JSONArray (org.json.JSONArray)2 NumericAttributeCondition (com.sun.identity.entitlement.NumericAttributeCondition)1 Set (java.util.Set)1