Search in sources :

Example 6 with JSONEntitlement

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

the class RestTest method negativeTest.

@Test
public void negativeTest() throws Exception {
    Form params = new Form();
    params.add("subject", hashedUserTokenId);
    params.add("resource", RESOURCE_NAME + "/index.html");
    params.add("action", "GET");
    params.add("realm", REALM);
    String decision = decisionClient.queryParams(params).header(RestServiceManager.SUBJECT_HEADER_NAME, userTokenIdHeader).cookie(cookie).accept("text/plain").get(String.class);
    if ((decision != null) && decision.equals("allow")) {
        throw new Exception("RESTTest.negativeTest (/decision) failed");
    }
    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.negativeTest() failed, status code not 200");
    }
    JSONObject jbody = jo.optJSONObject("body");
    if (jbody == null) {
        throw new Exception("RESTTest.negativeTest() failed, body element is null");
    }
    JSONEntitlement ent = new JSONEntitlement(jbody);
    boolean result = false;
    Object resultObj = ent.getActionValue("GET");
    if (resultObj != null) {
        result = ent.getActionValue("GET");
    }
    if (result) {
        throw new Exception("RESTTest.getnegativeTest() failed");
    }
    Map<String, Set<String>> advices = ent.getAdvices();
    Set<String> setNumericCondAdvice = advices.get(NumericAttributeCondition.class.getName());
    if ((setNumericCondAdvice == null) || setNumericCondAdvice.isEmpty()) {
        throw new Exception("RESTTest.negativeTest: no advice");
    }
    String advice = setNumericCondAdvice.iterator().next();
    if (!advice.equals(ATTR_NAME + "=" + ATTR_VAL)) {
        throw new Exception("RESTTest.negativeTest: incorrect advice");
    }
}
Also used : JSONEntitlement(com.sun.identity.entitlement.JSONEntitlement) Set(java.util.Set) JSONObject(org.json.JSONObject) NumericAttributeCondition(com.sun.identity.entitlement.NumericAttributeCondition) Form(com.sun.jersey.api.representation.Form) JSONObject(org.json.JSONObject) 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