Search in sources :

Example 16 with Form

use of com.sun.jersey.api.representation.Form 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)

Example 17 with Form

use of com.sun.jersey.api.representation.Form 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)

Example 18 with Form

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

the class RestTest method getDecisionTest.

@Test
public void getDecisionTest() 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 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.getDecisionTest() failed");
    }
}
Also used : Form(com.sun.jersey.api.representation.Form) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) Test(org.testng.annotations.Test)

Example 19 with Form

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

the class RestTest method missingActionTest.

@Test
public void missingActionTest() throws Exception {
    Form params = new Form();
    params.add("subject", hashedUserTokenId);
    params.add("resource", RESOURCE_NAME + "/index.html");
    params.add("env", ATTR_NAME + "=" + ATTR_VAL);
    params.add("realm", REALM);
    try {
        decisionClient.queryParams(params).header(RestServiceManager.SUBJECT_HEADER_NAME, userTokenIdHeader).cookie(cookie).accept("text/plain").get(String.class);
        throw new Exception("RESTTest.missingActionTest: no exception thrown.");
    } catch (UniformInterfaceException e) {
        int errorCode = e.getResponse().getStatus();
        if (errorCode != 400) {
            throw new Exception("RESTTest.missingActionTest: incorrect error code");
        }
    }
}
Also used : UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) Form(com.sun.jersey.api.representation.Form) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) Test(org.testng.annotations.Test)

Aggregations

Form (com.sun.jersey.api.representation.Form)19 UniformInterfaceException (com.sun.jersey.api.client.UniformInterfaceException)16 Test (org.testng.annotations.Test)13 JSONObject (org.json.JSONObject)12 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 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 ClientHandlerException (com.sun.jersey.api.client.ClientHandlerException)1 ClientResponse (com.sun.jersey.api.client.ClientResponse)1 WebResource (com.sun.jersey.api.client.WebResource)1 Set (java.util.Set)1