869a36e2649ae064c98063cf1e55198488d78d12Allan Foster/**
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster *
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * Copyright (c) 2009 Sun Microsystems Inc. All Rights Reserved
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster *
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * The contents of this file are subject to the terms
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * of the Common Development and Distribution License
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * (the License). You may not use this file except in
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * compliance with the License.
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster *
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * You can obtain a copy of the License at
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * opensso/legal/CDDLv1.0.txt
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * See the License for the specific language governing
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * permission and limitations under the License.
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster *
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * When distributing Covered Code, include this CDDL
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * Header Notice in each file and include the License file
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * at opensso/legal/CDDLv1.0.txt.
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * If applicable, add the following below the CDDL Header,
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * with the fields enclosed by brackets [] replaced by
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * your own identifying information:
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster *
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * $Id: RestTest.java,v 1.3 2009/11/24 23:08:35 veiming Exp $
d1b57e78cf72f41c7c6a52bd7c2a6d6aa4da6ba4Craig McDonnell *
d1b57e78cf72f41c7c6a52bd7c2a6d6aa4da6ba4Craig McDonnell * Portions Copyrighted 2014 ForgeRock AS
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster */
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
6909255a1970175507277a0f2f105979625f76b2Jaco Jooste/**
6909255a1970175507277a0f2f105979625f76b2Jaco Jooste * Portions copyright 2014 ForgeRock AS.
6909255a1970175507277a0f2f105979625f76b2Jaco Jooste */
6909255a1970175507277a0f2f105979625f76b2Jaco Jooste
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterpackage com.sun.identity.rest;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.identity.entitlement.util.AuthUtils;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.iplanet.am.util.SystemProperties;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.iplanet.sso.SSOToken;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.identity.entitlement.Entitlement;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.identity.entitlement.EntitlementSubject;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.identity.entitlement.JSONEntitlement;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.identity.entitlement.NumericAttributeCondition;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.identity.entitlement.Privilege;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.identity.entitlement.PrivilegeManager;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.identity.entitlement.opensso.SubjectUtils;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.identity.entitlement.util.IdRepoUtils;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.identity.idm.AMIdentity;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.identity.unittest.UnittestLog;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.jersey.api.client.Client;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.jersey.api.client.WebResource;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.jersey.api.representation.Form;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.identity.shared.Constants;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.identity.shared.encode.Hash;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.jersey.api.client.UniformInterfaceException;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport java.net.URLEncoder;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport java.util.HashMap;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport java.util.Map;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport java.util.Set;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport javax.security.auth.Subject;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport javax.ws.rs.core.Cookie;
6909255a1970175507277a0f2f105979625f76b2Jaco Jooste
6909255a1970175507277a0f2f105979625f76b2Jaco Joosteimport org.forgerock.openam.entitlement.conditions.subject.AuthenticatedUsers;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport org.json.JSONArray;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport org.json.JSONObject;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport org.testng.annotations.AfterClass;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport org.testng.annotations.BeforeClass;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport org.testng.annotations.Test;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster/**
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * To Test REST interface
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * @author Ravi Hingarajiya <ravi.hingarajiya@sun.com>
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster */
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterpublic class RestTest {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private static final String REALM = "/";
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private static final String AGENT_NAME = "RestTestAgent";
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private static final String PRIVILEGE_NAME = "RestTestPrivilege";
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private Subject adminSubject = SubjectUtils.createSuperAdminSubject();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private static final String RESOURCE_NAME = "http://www.resttest.com";
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private static final String ATTR_NAME = "bankAcc";
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private static final float ATTR_VAL = 1234f;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private AMIdentity user;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private String userTokenIdHeader;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private Cookie cookie;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private String hashedUserTokenId;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private WebResource entitlementClient;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private WebResource entitlementsClient;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private WebResource decisionClient;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private WebResource decisionsClient;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster @BeforeClass
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster public void setup() throws Exception {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster try {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster PrivilegeManager pm = PrivilegeManager.getInstance(REALM,
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster adminSubject);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Privilege privilege = Privilege.getNewInstance();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster privilege.setName(PRIVILEGE_NAME);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Map<String, Boolean> actions = new HashMap<String, Boolean>();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster actions.put("GET", true);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Entitlement entitlement = new Entitlement(RESOURCE_NAME + "/*",
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster actions);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster privilege.setEntitlement(entitlement);
6909255a1970175507277a0f2f105979625f76b2Jaco Jooste EntitlementSubject sbj = new AuthenticatedUsers();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster privilege.setSubject(sbj);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster NumericAttributeCondition cond = new NumericAttributeCondition();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster cond.setAttributeName(ATTR_NAME);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster cond.setOperator(NumericAttributeCondition.Operator.EQUAL);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster cond.setValue(ATTR_VAL);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster privilege.setCondition(cond);
d1b57e78cf72f41c7c6a52bd7c2a6d6aa4da6ba4Craig McDonnell pm.add(privilege);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster user = IdRepoUtils.createAgent(REALM, AGENT_NAME);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster SSOToken ssoToken = AuthUtils.authenticate(REALM, AGENT_NAME,
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster AGENT_NAME);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster String userTokenId = ssoToken.getTokenID().toString();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster hashedUserTokenId = Hash.hash(userTokenId);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster userTokenIdHeader = RestServiceManager.SSOTOKEN_SUBJECT_PREFIX +
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster RestServiceManager.SUBJECT_DELIMITER + userTokenId;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster String cookieValue = userTokenId;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (Boolean.parseBoolean(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster SystemProperties.get(Constants.AM_COOKIE_ENCODE, "false"))) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster cookieValue = URLEncoder.encode(userTokenId, "UTF-8");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster cookie = new Cookie(SystemProperties.get(Constants.AM_COOKIE_NAME),
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster cookieValue);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster String serverURL = SystemProperties.getServerInstanceName();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster decisionClient = Client.create().resource(serverURL +
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "/ws/1/entitlement/decision");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster decisionsClient = Client.create().resource(serverURL +
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "/ws/1/entitlement/decisions");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster entitlementClient = Client.create().resource(serverURL +
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "/ws/1/entitlement/entitlement");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster entitlementsClient = Client.create().resource(serverURL +
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "/ws/1/entitlement/entitlements");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster } catch (Exception e) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster UnittestLog.logError("RestTest.setup() failed:", e);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw e;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster @AfterClass
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster public void cleanup() throws Exception {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster PrivilegeManager pm = PrivilegeManager.getInstance(REALM,
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster adminSubject);
d1b57e78cf72f41c7c6a52bd7c2a6d6aa4da6ba4Craig McDonnell pm.remove(PRIVILEGE_NAME);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster IdRepoUtils.deleteIdentity(REALM, user);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster @Test
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster public void getDecisionTest() throws Exception {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Form params = new Form();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("subject", hashedUserTokenId);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("resource", RESOURCE_NAME + "/index.html");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("action", "GET");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("env", ATTR_NAME + "=" + ATTR_VAL);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("realm", REALM);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster String decision = decisionClient
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .queryParams(params)
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .header(RestServiceManager.SUBJECT_HEADER_NAME, userTokenIdHeader)
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .cookie(cookie)
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .accept("text/plain")
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .get(String.class);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if ((decision == null) || !decision.equals("allow")) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception("RESTTest.getDecisionTest() failed");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster @Test
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster public void getDecisionsTest() throws Exception {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Form params = new Form();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("subject", hashedUserTokenId);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("resources", RESOURCE_NAME + "/index.html");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("action", "GET");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("env", ATTR_NAME + "=" + ATTR_VAL);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("realm", REALM);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster String json = decisionsClient
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .queryParams(params)
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .header(RestServiceManager.SUBJECT_HEADER_NAME, userTokenIdHeader)
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .cookie(cookie)
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .accept("application/json")
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .get(String.class);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster JSONObject jo = new JSONObject(json);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (jo.optInt("statusCode") != 200) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "RESTTest.getDecisionsTest() failed, status code not 200");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster JSONObject jbody = jo.optJSONObject("body");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (jbody == null) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "RESTTest.getDecisionsTest() failed, body element is null");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster JSONArray results = jbody.optJSONArray("results");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (results == null) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "RESTTest.getDecisionsTest() failed, results array is null");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (results.length() < 1) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "RESTTest.getDecisionsTest() failed, results array is empty");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster JSONEntitlement ent = new JSONEntitlement(results.getJSONObject(0));
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster boolean result = ent.getActionValue("GET");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (!result) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception("RESTTest.getDecisionsTest() failed");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster @Test
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster public void getEntitlementTest() throws Exception {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Form params = new Form();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("subject", hashedUserTokenId);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("resource", RESOURCE_NAME + "/index.html");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("action", "GET");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("env", ATTR_NAME + "=" + ATTR_VAL);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("realm", REALM);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster String json = entitlementClient
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .queryParams(params)
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .header(RestServiceManager.SUBJECT_HEADER_NAME, userTokenIdHeader)
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .cookie(cookie)
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .accept("application/json")
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .get(String.class);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster JSONObject jo = new JSONObject(json);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (jo.optInt("statusCode") != 200) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "RESTTest.getEntitlementTest() failed, status code not 200");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster JSONObject jbody = jo.optJSONObject("body");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (jbody == null) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "RESTTest.getEntitlementTest() failed, body element is null");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster JSONEntitlement ent = new JSONEntitlement(jbody);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster boolean result = ent.getActionValue("GET");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (!result) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception("RESTTest.getEntitlementTest() failed");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster @Test
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster public void getEntitlementsTest() throws Exception {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Form params = new Form();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("subject", hashedUserTokenId);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("resource", RESOURCE_NAME);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("env", ATTR_NAME + "=" + ATTR_VAL);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("realm", REALM);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster String json = entitlementsClient
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .queryParams(params)
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .header(RestServiceManager.SUBJECT_HEADER_NAME, userTokenIdHeader)
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .cookie(cookie)
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .accept("application/json")
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .get(String.class);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster JSONObject jo = new JSONObject(json);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (jo.optInt("statusCode") != 200) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "RESTTest.getEntitlementsTest: failed, status code not 200");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster JSONObject jbody = jo.optJSONObject("body");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (jbody == null) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "RESTTest.getEntitlementsTest: failed, body element is null");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster JSONArray results = jbody.optJSONArray("results");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (results == null) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "RESTTest.getEntitlementsTest: failed, results element is null");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (results.length() < 1) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "RESTTest.getEntitlementsTest: failed, results array is empty");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster // dude, there are two entitlements returned.
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster // the first one is the root resource which is http://www.resttest.com
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster // and the action value is empty.
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster // we need to get the second one, which is http://www.resttest.com:80/*
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster JSONEntitlement ent = new JSONEntitlement(results.getJSONObject(1));
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Object resultObj = ent.getActionValue("GET");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (resultObj != null) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (!ent.getActionValue("GET")) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "RESTTest.getEntitlementsTest: failed, action value is false");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster } else {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "RESTTest.getEntitlementsTest: failed, action value is null");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster @Test
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster public void negativeTest() throws Exception {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Form params = new Form();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("subject", hashedUserTokenId);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("resource", RESOURCE_NAME + "/index.html");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("action", "GET");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("realm", REALM);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster String decision = decisionClient
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .queryParams(params)
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .header(RestServiceManager.SUBJECT_HEADER_NAME, userTokenIdHeader)
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .cookie(cookie)
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .accept("text/plain")
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .get(String.class);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if ((decision != null) && decision.equals("allow")) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception("RESTTest.negativeTest (/decision) failed");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster String json = entitlementClient
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .queryParams(params)
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .header(RestServiceManager.SUBJECT_HEADER_NAME, userTokenIdHeader)
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .cookie(cookie)
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .accept("application/json")
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .get(String.class);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster JSONObject jo = new JSONObject(json);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (jo.optInt("statusCode") != 200) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception("RESTTest.negativeTest() failed, status code not 200");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster JSONObject jbody = jo.optJSONObject("body");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (jbody == null) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception("RESTTest.negativeTest() failed, body element is null");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster JSONEntitlement ent = new JSONEntitlement(jbody);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster boolean result = false;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Object resultObj = ent.getActionValue("GET");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (resultObj != null) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster result = ent.getActionValue("GET");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (result) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception("RESTTest.getnegativeTest() failed");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Map<String, Set<String>> advices = ent.getAdvices();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Set<String> setNumericCondAdvice = advices.get(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster NumericAttributeCondition.class.getName());
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if ((setNumericCondAdvice == null) || setNumericCondAdvice.isEmpty()) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception("RESTTest.negativeTest: no advice");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster String advice = setNumericCondAdvice.iterator().next();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (!advice.equals(ATTR_NAME + "=" + ATTR_VAL)) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception("RESTTest.negativeTest: incorrect advice");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster @Test
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster public void missingResourceTest() throws Exception {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Form params = new Form();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("subject", hashedUserTokenId);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("action", "GET");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("env", ATTR_NAME + "=" + ATTR_VAL);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("realm", REALM);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster try {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster entitlementClient
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .queryParams(params)
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .header(RestServiceManager.SUBJECT_HEADER_NAME, userTokenIdHeader)
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .cookie(cookie)
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .accept("application/json")
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .get(String.class);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "RESTTest.missingResourceTest: no exception thrown.");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster } catch (UniformInterfaceException e) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster int errorCode = e.getResponse().getStatus();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (errorCode != 400) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "RESTTest.missingResourceTest: incorrect error code");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster String json = e.getResponse().getEntity(String.class);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster JSONObject jo = new JSONObject(json);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (jo.optInt("statusCode") != 420) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "RESTTest.missingResourceTest() failed, status code not 420");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (jo.optJSONObject("body") != null) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "RESTTest.missingResourceTest() failed, body not empty");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster @Test
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster public void missingResourcesTest() throws Exception {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Form params = new Form();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("subject", hashedUserTokenId);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("action", "GET");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("env", ATTR_NAME + "=" + ATTR_VAL);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("realm", REALM);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster try {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster decisionsClient
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .queryParams(params)
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .header(RestServiceManager.SUBJECT_HEADER_NAME, userTokenIdHeader)
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .cookie(cookie)
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .accept("application/json")
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .get(String.class);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "RESTTest.missingResourceTest: no exception thrown.");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster } catch (UniformInterfaceException e) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster int errorCode = e.getResponse().getStatus();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (errorCode != 400) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "RESTTest.missingResourceTest: incorrect error code");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster String json = e.getResponse().getEntity(String.class);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster JSONObject jo = new JSONObject(json);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (jo.optInt("statusCode") != 424) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "RESTTest.missingResourcesTest() failed, status code not 424");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (jo.optJSONObject("body") != null) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "RESTTest.missingResourcesTest() failed, body not empty");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster @Test
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster public void missingActionTest() throws Exception {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster Form params = new Form();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("subject", hashedUserTokenId);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("resource", RESOURCE_NAME + "/index.html");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("env", ATTR_NAME + "=" + ATTR_VAL);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster params.add("realm", REALM);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster try {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster decisionClient
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .queryParams(params)
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .header(RestServiceManager.SUBJECT_HEADER_NAME, userTokenIdHeader)
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .cookie(cookie)
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .accept("text/plain")
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster .get(String.class);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "RESTTest.missingActionTest: no exception thrown.");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster } catch (UniformInterfaceException e) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster int errorCode = e.getResponse().getStatus();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (errorCode != 400) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster throw new Exception(
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster "RESTTest.missingActionTest: incorrect error code");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster}