From: Sanjay Dalal Date: Wed, 12 May 2010 18:05:35 +0000 (+0000) Subject: CSPACE-1364 allow user test in ROLE_ADMINISTRATOR to access (CRUDL) dimension service X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=d3399900634fd3dcc2a4089250d30326e2376d38;p=tmp%2Fjakarta-migration.git CSPACE-1364 allow user test in ROLE_ADMINISTRATOR to access (CRUDL) dimension service CSPACE-1365 deny user bigbird2010 in ROLE_MMI_CM access (C) to the dimension service moved authorization generation and seeding to authorization-mgt/client from authorization service enabled access control for the dimension service only in the security interceptor added setAuth convenience method into client test framework to change authn properites, httpclient and proxy test: all service tests, specifically security/client and authorization-mgt/client tests !!REQUIRES the following sequence of actions at the services level - mvn clean install -DskipTests - ant undeploy deploy - mvn test (note that authorization-mgt/client tests have to run before running dimension/client and security/client tests) A services/authorization-mgt/service/src/test/javadoc A services/authorization-mgt/service/src/test/javadoc/org A services/authorization-mgt/service/src/test/javadoc/org/collectionspace A services/authorization-mgt/service/src/test/javadoc/org/collectionspace/services A services/authorization-mgt/service/src/test/javadoc/org/collectionspace/services/authorization A services/authorization-mgt/service/src/test/javadoc/org/collectionspace/services/authorization/test A + services/authorization-mgt/service/src/test/javadoc/org/collectionspace/services/authorization/test/package.html M services/authorization-mgt/service/src/main/java/org/collectionspace/services/authorization/storage/AuthorizationDelegate.java A services/authorization-mgt/client/src/test/java/org/collectionspace/services/authorization/test A + services/authorization-mgt/client/src/test/java/org/collectionspace/services/authorization/test/AuthorizationSeedTest.java A + services/authorization-mgt/client/src/test/java/org/collectionspace/services/authorization/test/AbstractAuthorizationTestImpl.java A + services/authorization-mgt/client/src/test/java/org/collectionspace/services/authorization/test/AuthorizationGen.java A + services/authorization-mgt/client/src/test/resources/applicationContext-authorization-test.xml A services/authorization-mgt/client/src/test/resources/test-data A + services/authorization-mgt/client/src/test/resources/test-data/test-permissions.xml A + services/authorization-mgt/client/src/test/resources/test-data/test-permissions-roles.xml M services/authorization-mgt/client/pom.xml M services/dimension/client/src/test/java/org/collectionspace/services/client/test/DimensionServiceTest.java A services/dimension/client/src/main/java/org/collectionspace/services/client/DimensionFactory.java M services/dimension/client/pom.xml D services/authorization/service/src/test/java/org/collectionspace/services/authorization/test/AuthorizationSeedTest.java D services/authorization/service/src/test/java/org/collectionspace/services/authorization/test/AbstractAuthorizationTestImpl.java D services/authorization/service/src/test/java/org/collectionspace/services/authorization/test/AuthorizationGen.java D services/authorization/service/src/test/javadoc/org/collectionspace/services/authorization/test/package.html D services/authorization/service/src/test/resources/applicationContext-authorization-test.xml D services/authorization/service/src/test/resources/test-data/test-permissions.xml D services/authorization/service/src/test/resources/test-data/test-permissions-roles.xml M services/authorization/service/pom.xml M services/authorization/pstore/src/main/resources/db/mysql/test_authorization.sql M services/common/src/main/java/org/collectionspace/services/common/security/SecurityInterceptor.java M services/security/client/src/test/java/org/collectionspace/services/security/client/test/AuthenticationServiceTest.java A services/security/client/src/test/java/org/collectionspace/services/security/client/test/AuthorizationServiceTest.java M services/security/client/pom.xml M services/client/src/main/java/org/collectionspace/services/client/test/BaseServiceTest.java M services/client/src/main/java/org/collectionspace/services/client/TestServiceClient.java M services/client/src/main/java/org/collectionspace/services/client/AbstractServiceClientImpl.java M services/client/src/main/java/org/collectionspace/services/client/CollectionSpaceClient.java --- diff --git a/services/authorization-mgt/client/pom.xml b/services/authorization-mgt/client/pom.xml index e0554bef5..0e694320a 100644 --- a/services/authorization-mgt/client/pom.xml +++ b/services/authorization-mgt/client/pom.xml @@ -12,7 +12,10 @@ org.collectionspace.services org.collectionspace.services.authorization-mgt.client services.authorization-mgt.client - + + 3.0.0.RELEASE + 3.0.2.RELEASE + @@ -23,6 +26,18 @@ org.slf4j slf4j-log4j12 + + org.collectionspace.services + org.collectionspace.services.authentication.service + ${project.version} + provided + + + org.collectionspace.services + org.collectionspace.services.authorization.service + ${project.version} + provided + org.collectionspace.services org.collectionspace.services.authorization.jaxb @@ -37,11 +52,6 @@ - - mysql - mysql-connector-java - test - + + net.sf.ehcache + ehcache + 1.6.2 + true + + + mysql + mysql-connector-java + test + cspace-services-authorization-mgt-client - + + + org.apache.maven.plugins + maven-surefire-plugin + + + + maven.basedir + ${basedir} + + + log4j.configuration + file:${project.build.directory}/test-classes/log4j.properties + + + spring-beans-config + applicationContext-authorization-test.xml + + + + diff --git a/services/authorization/service/src/test/java/org/collectionspace/services/authorization/test/AbstractAuthorizationTestImpl.java b/services/authorization-mgt/client/src/test/java/org/collectionspace/services/authorization/test/AbstractAuthorizationTestImpl.java similarity index 100% rename from services/authorization/service/src/test/java/org/collectionspace/services/authorization/test/AbstractAuthorizationTestImpl.java rename to services/authorization-mgt/client/src/test/java/org/collectionspace/services/authorization/test/AbstractAuthorizationTestImpl.java diff --git a/services/authorization/service/src/test/java/org/collectionspace/services/authorization/test/AuthorizationGen.java b/services/authorization-mgt/client/src/test/java/org/collectionspace/services/authorization/test/AuthorizationGen.java similarity index 97% rename from services/authorization/service/src/test/java/org/collectionspace/services/authorization/test/AuthorizationGen.java rename to services/authorization-mgt/client/src/test/java/org/collectionspace/services/authorization/test/AuthorizationGen.java index aa03956af..6cf2bd0fc 100644 --- a/services/authorization/service/src/test/java/org/collectionspace/services/authorization/test/AuthorizationGen.java +++ b/services/authorization-mgt/client/src/test/java/org/collectionspace/services/authorization/test/AuthorizationGen.java @@ -75,8 +75,8 @@ public class AuthorizationGen { Permission accPerm = buildCommonPermission("1", "accounts"); apcList.add(accPerm); - Permission coPerm = buildCommonPermission("2", "collectionobjects"); - apcList.add(coPerm); + Permission dimPerm = buildCommonPermission("2", "dimensions"); + apcList.add(dimPerm); AbstractAuthorizationTestImpl.toFile(pcList, PermissionsList.class, AbstractAuthorizationTestImpl.testDataDir + "test-permissions.xml"); logger.info("generated permissions to " @@ -112,7 +112,7 @@ public class AuthorizationGen { PermissionsRolesList psrsl = new PermissionsRolesList(); ArrayList prl = new ArrayList(); prl.add(buildCommonPermissionRoles("1", "accounts")); - prl.add(buildCommonPermissionRoles("2", "collectionobjects")); + prl.add(buildCommonPermissionRoles("2", "dimensions")); psrsl.setPermissionRoles(prl); AbstractAuthorizationTestImpl.toFile(psrsl, PermissionsRolesList.class, AbstractAuthorizationTestImpl.testDataDir + "test-permissions-roles.xml"); diff --git a/services/authorization/service/src/test/java/org/collectionspace/services/authorization/test/AuthorizationSeedTest.java b/services/authorization-mgt/client/src/test/java/org/collectionspace/services/authorization/test/AuthorizationSeedTest.java similarity index 55% rename from services/authorization/service/src/test/java/org/collectionspace/services/authorization/test/AuthorizationSeedTest.java rename to services/authorization-mgt/client/src/test/java/org/collectionspace/services/authorization/test/AuthorizationSeedTest.java index d45fffe97..ea650f014 100644 --- a/services/authorization/service/src/test/java/org/collectionspace/services/authorization/test/AuthorizationSeedTest.java +++ b/services/authorization-mgt/client/src/test/java/org/collectionspace/services/authorization/test/AuthorizationSeedTest.java @@ -25,14 +25,22 @@ package org.collectionspace.services.authorization.test; //import java.util.ArrayList; //import java.util.List; +import java.util.ArrayList; +import java.util.List; +import org.collectionspace.services.authorization.ActionType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; //import org.collectionspace.services.client.test.BaseServiceTest; import org.collectionspace.services.authorization.AuthZ; +import org.collectionspace.services.authorization.CSpaceAction; import org.collectionspace.services.authorization.Permission; +import org.collectionspace.services.authorization.PermissionAction; +import org.collectionspace.services.authorization.PermissionException; import org.collectionspace.services.authorization.PermissionRole; import org.collectionspace.services.authorization.PermissionsList; import org.collectionspace.services.authorization.PermissionsRolesList; +import org.collectionspace.services.authorization.RoleValue; +import org.collectionspace.services.authorization.URIResourceImpl; import org.springframework.transaction.TransactionStatus; import org.testng.annotations.BeforeClass; @@ -84,9 +92,64 @@ public class AuthorizationSeedTest extends AbstractAuthorizationTestImpl { } for (PermissionRole pr : pcrList.getPermissionRoles()) { if (pr.getPermissions().get(0).getPermissionId().equals(p.getCsid())) { -// authZ.addPermissionsForUri(p, pr); + addPermissionsForUri(p, pr); } } } } + + /** + * addPermissionsForUri add permissions from given permission configuration + * with assumption that resource is of type URI + * @param permission configuration + */ + //FIXME this method should be in the restful web service resource of authz + private void addPermissionsForUri(Permission perm, + PermissionRole permRole) throws PermissionException { + List principals = new ArrayList(); + if (!perm.getCsid().equals(permRole.getPermissions().get(0).getPermissionId())) { + throw new IllegalArgumentException("permission ids do not" + + " match for role=" + permRole.getRoles().get(0).getRoleName() + + " with permissionId=" + permRole.getPermissions().get(0).getPermissionId() + + " for permission with csid=" + perm.getCsid()); + } + for (RoleValue roleValue : permRole.getRoles()) { + principals.add(roleValue.getRoleName()); + } + List permActions = perm.getActions(); + for (PermissionAction permAction : permActions) { + CSpaceAction action = getAction(permAction.getName()); + URIResourceImpl uriRes = new URIResourceImpl(perm.getTenantId(), + perm.getResourceName(), action); + AuthZ.get().addPermissions(uriRes, principals.toArray(new String[0])); + } + } + + + /** + * getAction is a convenience method to get corresponding action for + * given ActionType + * @param action + * @return + */ + private CSpaceAction getAction(ActionType action) { + if (ActionType.CREATE.equals(action)) { + return CSpaceAction.CREATE; + } else if (ActionType.READ.equals(action)) { + return CSpaceAction.READ; + } else if (ActionType.UPDATE.equals(action)) { + return CSpaceAction.UPDATE; + } else if (ActionType.DELETE.equals(action)) { + return CSpaceAction.DELETE; + } else if (ActionType.SEARCH.equals(action)) { + return CSpaceAction.SEARCH; + } else if (ActionType.ADMIN.equals(action)) { + return CSpaceAction.ADMIN; + } else if (ActionType.START.equals(action)) { + return CSpaceAction.START; + } else if (ActionType.STOP.equals(action)) { + return CSpaceAction.STOP; + } + throw new IllegalArgumentException("action = " + action.toString()); + } } diff --git a/services/authorization/service/src/test/resources/applicationContext-authorization-test.xml b/services/authorization-mgt/client/src/test/resources/applicationContext-authorization-test.xml similarity index 98% rename from services/authorization/service/src/test/resources/applicationContext-authorization-test.xml rename to services/authorization-mgt/client/src/test/resources/applicationContext-authorization-test.xml index d9f03ee47..856c38d94 100644 --- a/services/authorization/service/src/test/resources/applicationContext-authorization-test.xml +++ b/services/authorization-mgt/client/src/test/resources/applicationContext-authorization-test.xml @@ -88,5 +88,6 @@ + diff --git a/services/authorization/service/src/test/resources/test-data/test-permissions-roles.xml b/services/authorization-mgt/client/src/test/resources/test-data/test-permissions-roles.xml similarity index 94% rename from services/authorization/service/src/test/resources/test-data/test-permissions-roles.xml rename to services/authorization-mgt/client/src/test/resources/test-data/test-permissions-roles.xml index 5a0ff5715..ea59f1115 100644 --- a/services/authorization/service/src/test/resources/test-data/test-permissions-roles.xml +++ b/services/authorization-mgt/client/src/test/resources/test-data/test-permissions-roles.xml @@ -19,7 +19,7 @@ ROLE 2 - collectionobjects + dimensions 1 diff --git a/services/authorization/service/src/test/resources/test-data/test-permissions.xml b/services/authorization-mgt/client/src/test/resources/test-data/test-permissions.xml similarity index 94% rename from services/authorization/service/src/test/resources/test-data/test-permissions.xml rename to services/authorization-mgt/client/src/test/resources/test-data/test-permissions.xml index 512f2127c..3287d54ff 100644 --- a/services/authorization/service/src/test/resources/test-data/test-permissions.xml +++ b/services/authorization-mgt/client/src/test/resources/test-data/test-permissions.xml @@ -18,7 +18,7 @@ 1 - collectionobjects + dimensions CREATE diff --git a/services/authorization-mgt/service/src/main/java/org/collectionspace/services/authorization/storage/AuthorizationDelegate.java b/services/authorization-mgt/service/src/main/java/org/collectionspace/services/authorization/storage/AuthorizationDelegate.java index 79a22b7be..a4d7deabb 100644 --- a/services/authorization-mgt/service/src/main/java/org/collectionspace/services/authorization/storage/AuthorizationDelegate.java +++ b/services/authorization-mgt/service/src/main/java/org/collectionspace/services/authorization/storage/AuthorizationDelegate.java @@ -125,32 +125,7 @@ public class AuthorizationDelegate { } } - /** - * addPermissionsForUri add permissions from given permission configuration - * with assumption that resource is of type URI - * @param permission configuration - */ - //FIXME this method should be in the restful web service resource of authz - public void addPermissionsForUri(Permission perm, - PermissionRole permRole) throws PermissionException { - List principals = new ArrayList(); - if (!perm.getCsid().equals(permRole.getPermissions().get(0).getPermissionId())) { - throw new IllegalArgumentException("permission ids do not" - + " match for role=" + permRole.getRoles().get(0).getRoleName() - + " with permissionId=" + permRole.getPermissions().get(0).getPermissionId() - + " for permission with csid=" + perm.getCsid()); - } - for (RoleValue roleValue : permRole.getRoles()) { - principals.add(roleValue.getRoleName()); - } - List permActions = perm.getActions(); - for (PermissionAction permAction : permActions) { - CSpaceAction action = getAction(permAction.getName()); - URIResourceImpl uriRes = new URIResourceImpl(perm.getTenantId(), - perm.getResourceName(), action); - AuthZ.get().addPermissions(uriRes, principals.toArray(new String[0])); - } - } + /** * getRoles get roles (string) array from given RoleValue list diff --git a/services/authorization/service/src/test/javadoc/org/collectionspace/services/authorization/test/package.html b/services/authorization-mgt/service/src/test/javadoc/org/collectionspace/services/authorization/test/package.html similarity index 100% rename from services/authorization/service/src/test/javadoc/org/collectionspace/services/authorization/test/package.html rename to services/authorization-mgt/service/src/test/javadoc/org/collectionspace/services/authorization/test/package.html diff --git a/services/authorization/pstore/src/main/resources/db/mysql/test_authorization.sql b/services/authorization/pstore/src/main/resources/db/mysql/test_authorization.sql index fdb980beb..de65dd610 100644 --- a/services/authorization/pstore/src/main/resources/db/mysql/test_authorization.sql +++ b/services/authorization/pstore/src/main/resources/db/mysql/test_authorization.sql @@ -15,9 +15,9 @@ insert into `accounts_roles`(`account_id`, `user_id`, `role_id`, `role_name`, `c insert into `accounts_roles`(`account_id`, `user_id`, `role_id`, `role_name`, `created_at`) values ('eeca40d7-dc77-4cc5-b489-16a53c75525a', 'test', '3', 'ROLE_COLLECTIONS_MANAGER', '2010-02-17 16:31:48'); -- Additional account introduced during integration on release 0.6, and currently relied upon by the Application Layer. -insert into `accounts_roles`(`account_id`, `user_id`, `role_id`, `role_name`, `created_at`) values ('251f98f3-0292-4f3e-aa95-455314050e1b', 'test', '1', 'ROLE_ADMINISTRATOR', '2010-05-03 12:35:00'); -insert into `accounts_roles`(`account_id`, `user_id`, `role_id`, `role_name`, `created_at`) values ('251f98f3-0292-4f3e-aa95-455314050e1b', 'test', '2', 'ROLE_USERS', '2010-05-03 12:35:00'); -insert into `accounts_roles`(`account_id`, `user_id`, `role_id`, `role_name`, `created_at`) values ('251f98f3-0292-4f3e-aa95-455314050e1b', 'test', '3', 'ROLE_COLLECTIONS_MANAGER', '2010-05-03 12:35:00'); +insert into `accounts_roles`(`account_id`, `user_id`, `role_id`, `role_name`, `created_at`) values ('251f98f3-0292-4f3e-aa95-455314050e1b', 'test@collectionspace.org', '1', 'ROLE_ADMINISTRATOR', '2010-05-03 12:35:00'); +insert into `accounts_roles`(`account_id`, `user_id`, `role_id`, `role_name`, `created_at`) values ('251f98f3-0292-4f3e-aa95-455314050e1b', 'test@collectionspace.org', '2', 'ROLE_USERS', '2010-05-03 12:35:00'); +insert into `accounts_roles`(`account_id`, `user_id`, `role_id`, `role_name`, `created_at`) values ('251f98f3-0292-4f3e-aa95-455314050e1b', 'test@collectionspace.org', '3', 'ROLE_COLLECTIONS_MANAGER', '2010-05-03 12:35:00'); -- todo: barney is created in security test but accountrole is not yet created there, so add fake account id insert into `accounts_roles`(`account_id`, `user_id`, `role_id`, `role_name`, `created_at`) values ('1', 'barney', '2', 'ROLE_USERS', '2010-02-17 16:31:48'); diff --git a/services/authorization/service/pom.xml b/services/authorization/service/pom.xml index e1d7e0570..a04763f03 100644 --- a/services/authorization/service/pom.xml +++ b/services/authorization/service/pom.xml @@ -1,7 +1,7 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0 org.collectionspace.services.authorization @@ -159,6 +159,7 @@ org.collectionspace.services org.collectionspace.services.authorization.jaxb ${project.version} + provided @@ -166,26 +167,7 @@ cspace-services-authz - - org.apache.maven.plugins - maven-surefire-plugin - - - - maven.basedir - ${basedir} - - - log4j.configuration - file:${project.build.directory}/test-classes/log4j.properties - - - spring-beans-config - applicationContext-authorization-test.xml - - - - + diff --git a/services/client/src/main/java/org/collectionspace/services/client/AbstractServiceClientImpl.java b/services/client/src/main/java/org/collectionspace/services/client/AbstractServiceClientImpl.java index 1aa2f0380..1b5b5c369 100644 --- a/services/client/src/main/java/org/collectionspace/services/client/AbstractServiceClientImpl.java +++ b/services/client/src/main/java/org/collectionspace/services/client/AbstractServiceClientImpl.java @@ -46,280 +46,305 @@ import org.slf4j.LoggerFactory; * @param */ public abstract class AbstractServiceClientImpl implements - CollectionSpaceClient { - - /** The logger. */ - protected final Logger logger = LoggerFactory - .getLogger(AbstractServiceClientImpl.class); - - /** - * The character used to separate the words in a part label - */ - public static final String PART_LABEL_SEPERATOR = "_"; - - /** The Constant PART_COMMON_LABEL. */ - public static final String PART_COMMON_LABEL = "common"; - - /** The properties. */ - private Properties properties = new Properties(); - - /** The url. */ - private URL url; - - /** The http client. */ - private HttpClient httpClient; - - /** - * Gets the common part name. - * - * @return the common part name - */ - public String getCommonPartName() { - return getCommonPartName(getServicePathComponent()); - } - - /** - * Gets the common part name. - * - * @param servicePathComponent - * the service path component - * @return the common part name - */ - public String getCommonPartName(String servicePathComponent) { - return servicePathComponent + PART_LABEL_SEPERATOR + PART_COMMON_LABEL; - } - - /** - * Gets the service path component. - * - * @return the service path component - */ - abstract public String getServicePathComponent(); - - /** - * Instantiates a new abstract service client impl. - */ - protected AbstractServiceClientImpl() { - readProperties(); - setupHttpClient(); - } - - /* - * (non-Javadoc) - * - * @see - * org.collectionspace.services.client.CollectionSpaceClient#getProperty - * (java.lang.String) - */ - @Override - public String getProperty(String propName) { - return properties.getProperty(propName); - } - - /* - * (non-Javadoc) - * - * @see - * org.collectionspace.services.client.CollectionSpaceClient#setProperty - * (java.lang.String, java.lang.String) - */ - @Override - public void setProperty(String propName, String value) { - properties.setProperty(propName, value); - } - - /* - * (non-Javadoc) - * - * @see - * org.collectionspace.services.client.CollectionSpaceClient#removeProperty - * (java.lang.String) - */ - @Override - public Object removeProperty(String propName) { - return properties.remove(propName); - } - - /** - * Prints the properties. - */ - public void printProperties() { - for (Object kobj : properties.keySet()) { - String key = (String) kobj; - logger.trace("begin property name=" + key + " value=" - + properties.get(key)); - } - } - - /* - * (non-Javadoc) - * - * @see - * org.collectionspace.services.client.CollectionSpaceClient#getBaseURL() - */ - @Override - public String getBaseURL() { - return properties.getProperty(URL_PROPERTY); - } - - /* - * (non-Javadoc) - * - * @see - * org.collectionspace.services.client.CollectionSpaceClient#getHttpClient() - */ - @Override - public HttpClient getHttpClient() { - return httpClient; - } - - /* - * (non-Javadoc) - * - * @see org.collectionspace.services.client.CollectionSpaceClient#useAuth() - */ - @Override - public boolean useAuth() { - String auth = properties.getProperty(AUTH_PROPERTY); - return Boolean.valueOf(auth); - } - - /* - * (non-Javadoc) - * - * @see org.collectionspace.services.client.CollectionSpaceClient#useSSL() - */ - @Override - public boolean useSSL() { - String ssl = properties.getProperty(SSL_PROPERTY); - return Boolean.valueOf(ssl); - } - - /** - * readProperties reads properties from system class path as well as it - * overrides properties made available using command line - * - * @exception RuntimeException - */ - private void readProperties() { - - ClassLoader cl = Thread.currentThread().getContextClassLoader(); - InputStream is = null; - try { - is = cl.getResourceAsStream("collectionspace-client.properties"); - properties.load(is); - if (logger.isDebugEnabled()) { - printProperties(); - } - String spec = System.getProperty(URL_PROPERTY); - if (spec != null && !"".equals(spec)) { - properties.setProperty(URL_PROPERTY, spec); - } - - spec = properties.getProperty(URL_PROPERTY); - url = new URL(spec); - if (logger.isInfoEnabled()) { - logger.info("readProperties() using url=" + url); - } - - String auth = System.getProperty(AUTH_PROPERTY); - if (auth != null && !"".equals(auth)) { - properties.setProperty(AUTH_PROPERTY, auth); - } - String ssl = System.getProperty(SSL_PROPERTY); - if (ssl != null && !"".equals(ssl)) { - properties.setProperty(AUTH_PROPERTY, ssl); - } - String user = System.getProperty(USER_PROPERTY); - if (user != null && !"".equals(user)) { - properties.setProperty(USER_PROPERTY, user); - } - String password = System.getProperty(PASSWORD_PROPERTY); - if (password != null && !"".equals(password)) { - properties.setProperty(PASSWORD_PROPERTY, password); - } - if (logger.isDebugEnabled()) { - printProperties(); - } - } catch (Exception e) { - logger.debug("Caught exception while reading properties", e); - throw new RuntimeException(e); - } finally { - if (is != null) { - try { - is.close(); - } catch (Exception e) { - if (logger.isDebugEnabled() == true) { - e.printStackTrace(); - } - } - } - } - } - - /** - * setupHttpClient sets up HTTP client for the service client the setup - * process relies on the following properties URL_PROPERTY USER_PROPERTY - * PASSWORD_PROPERTY AUTH_PROPERTY SSL_PROPERTY - */ - @Override - public void setupHttpClient() { - this.httpClient = new HttpClient(); - if (useAuth()) { - String user = properties.getProperty(USER_PROPERTY); - String password = properties.getProperty(PASSWORD_PROPERTY); - if (logger.isInfoEnabled()) { - logger.info("setupHttpClient() using url=" + url + " user=" - + user + " password=" + password); - } - - httpClient.getState().setCredentials( - new AuthScope(url.getHost(), url.getPort(), - AuthScope.ANY_REALM), - new UsernamePasswordCredentials(user, password)); - // JAXRS client library requires HTTP preemptive authentication - httpClient.getParams().setAuthenticationPreemptive(true); - if (logger.isInfoEnabled()) { - logger.info("setupHttpClient: set preemptive authentication"); - } - } else { - if (logger.isInfoEnabled()) { - logger.info("setupHttpClient() : no auth mode!"); - } - } - } - - /* - * (non-Javadoc) - * - * @see - * org.collectionspace.services.client.CollectionSpaceClient#isServerSecure - * () - */ - @Override - public boolean isServerSecure() { - return Boolean.getBoolean("cspace.server.secure"); - } - - /* - * (non-Javadoc) - * - * @see - * org.collectionspace.services.client.CollectionSpaceClient#readList(java - * .lang.String, java.lang.String) - */ - @Override - public ClientResponse readList(String pageSize, - String pageNumber) { - return getProxy().readList(pageSize, pageNumber); - } - - /* (non-Javadoc) - * @see org.collectionspace.services.client.CollectionSpaceClient#delete(java.lang.String) - */ + CollectionSpaceClient { + + /** The logger. */ + protected final Logger logger = LoggerFactory.getLogger(AbstractServiceClientImpl.class); + /** + * The character used to separate the words in a part label + */ + public static final String PART_LABEL_SEPERATOR = "_"; + /** The Constant PART_COMMON_LABEL. */ + public static final String PART_COMMON_LABEL = "common"; + /** The properties. */ + private Properties properties = new Properties(); + /** The url. */ + private URL url; + /** The http client. */ + private HttpClient httpClient; + + /** + * Gets the common part name. + * + * @return the common part name + */ + public String getCommonPartName() { + return getCommonPartName(getServicePathComponent()); + } + + /** + * Gets the common part name. + * + * @param servicePathComponent + * the service path component + * @return the common part name + */ + public String getCommonPartName(String servicePathComponent) { + return servicePathComponent + PART_LABEL_SEPERATOR + PART_COMMON_LABEL; + } + + /** + * Gets the service path component. + * + * @return the service path component + */ + abstract public String getServicePathComponent(); + + /** + * Instantiates a new abstract service client impl. + */ + protected AbstractServiceClientImpl() { + readProperties(); + setupHttpClient(); + } + + /* + * (non-Javadoc) + * + * @see + * org.collectionspace.services.client.CollectionSpaceClient#getProperty + * (java.lang.String) + */ @Override - public ClientResponse delete(String csid) { - // must override in children if you want behavior - throw new UnsupportedOperationException(); - } + public String getProperty(String propName) { + return properties.getProperty(propName); + } + + /* + * (non-Javadoc) + * + * @see + * org.collectionspace.services.client.CollectionSpaceClient#setProperty + * (java.lang.String, java.lang.String) + */ + @Override + public void setProperty(String propName, String value) { + properties.setProperty(propName, value); + } + + /* + * (non-Javadoc) + * + * @see + * org.collectionspace.services.client.CollectionSpaceClient#removeProperty + * (java.lang.String) + */ + @Override + public Object removeProperty(String propName) { + return properties.remove(propName); + } + + /** + * Prints the properties. + */ + public void printProperties() { + for (Object kobj : properties.keySet()) { + String key = (String) kobj; + logger.trace("begin property name=" + key + " value=" + + properties.get(key)); + } + } + + /* + * (non-Javadoc) + * + * @see + * org.collectionspace.services.client.CollectionSpaceClient#getBaseURL() + */ + @Override + public String getBaseURL() { + return properties.getProperty(URL_PROPERTY); + } + + /* + * (non-Javadoc) + * + * @see + * org.collectionspace.services.client.CollectionSpaceClient#getHttpClient() + */ + @Override + public HttpClient getHttpClient() { + return httpClient; + } + + /* + * (non-Javadoc) + * + * @see org.collectionspace.services.client.CollectionSpaceClient#useAuth() + */ + @Override + public boolean useAuth() { + String auth = properties.getProperty(AUTH_PROPERTY); + return Boolean.valueOf(auth); + } + + /* + * (non-Javadoc) + * + * @see org.collectionspace.services.client.CollectionSpaceClient#useSSL() + */ + @Override + public boolean useSSL() { + String ssl = properties.getProperty(SSL_PROPERTY); + return Boolean.valueOf(ssl); + } + + /** + * readProperties reads properties from system class path as well as it + * overrides properties made available using command line + * + * @exception RuntimeException + */ + private void readProperties() { + + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + InputStream is = null; + try { + is = cl.getResourceAsStream("collectionspace-client.properties"); + properties.load(is); + if (logger.isDebugEnabled()) { + printProperties(); + } + String spec = System.getProperty(URL_PROPERTY); + if (spec != null && !"".equals(spec)) { + properties.setProperty(URL_PROPERTY, spec); + } + + spec = properties.getProperty(URL_PROPERTY); + url = new URL(spec); + if (logger.isInfoEnabled()) { + logger.info("readProperties() using url=" + url); + } + + String auth = System.getProperty(AUTH_PROPERTY); + if (auth != null && !"".equals(auth)) { + properties.setProperty(AUTH_PROPERTY, auth); + } + String ssl = System.getProperty(SSL_PROPERTY); + if (ssl != null && !"".equals(ssl)) { + properties.setProperty(AUTH_PROPERTY, ssl); + } + String user = System.getProperty(USER_PROPERTY); + if (user != null && !"".equals(user)) { + properties.setProperty(USER_PROPERTY, user); + } + String password = System.getProperty(PASSWORD_PROPERTY); + if (password != null && !"".equals(password)) { + properties.setProperty(PASSWORD_PROPERTY, password); + } + if (logger.isDebugEnabled()) { + printProperties(); + } + } catch (Exception e) { + logger.debug("Caught exception while reading properties", e); + throw new RuntimeException(e); + } finally { + if (is != null) { + try { + is.close(); + } catch (Exception e) { + if (logger.isDebugEnabled() == true) { + e.printStackTrace(); + } + } + } + } + } + + /** + * setupHttpClient sets up HTTP client for the service client the setup + * process relies on the following properties URL_PROPERTY USER_PROPERTY + * PASSWORD_PROPERTY AUTH_PROPERTY SSL_PROPERTY + */ + @Override + public void setupHttpClient() { + this.httpClient = new HttpClient(); + if (useAuth()) { + String user = properties.getProperty(USER_PROPERTY); + String password = properties.getProperty(PASSWORD_PROPERTY); + if (logger.isInfoEnabled()) { + logger.info("setupHttpClient() using url=" + url + " user=" + + user + " password=" + password); + } + + httpClient.getState().setCredentials( + new AuthScope(url.getHost(), url.getPort(), + AuthScope.ANY_REALM), + new UsernamePasswordCredentials(user, password)); + // JAXRS client library requires HTTP preemptive authentication + httpClient.getParams().setAuthenticationPreemptive(true); + if (logger.isInfoEnabled()) { + logger.info("setupHttpClient: set preemptive authentication"); + } + } else { + if (logger.isInfoEnabled()) { + logger.info("setupHttpClient() : no auth mode!"); + } + } + } + + /* + * (non-Javadoc) + * + * @see + * org.collectionspace.services.client.CollectionSpaceClient#isServerSecure + * () + */ + @Override + public boolean isServerSecure() { + return Boolean.getBoolean("cspace.server.secure"); + } + + /* + * (non-Javadoc) + * + * @see + * org.collectionspace.services.client.CollectionSpaceClient#readList(java + * .lang.String, java.lang.String) + */ + @Override + public ClientResponse readList(String pageSize, + String pageNumber) { + return getProxy().readList(pageSize, pageNumber); + } + + /* (non-Javadoc) + * @see org.collectionspace.services.client.CollectionSpaceClient#delete(java.lang.String) + */ + @Override + public ClientResponse delete(String csid) { + // must override in children if you want behavior + throw new UnsupportedOperationException(); + } + + @Override + public abstract CollectionSpaceProxy getProxy(); + + @Override + public abstract void setProxy(); + + public void setAuth(boolean useAuth, + String user, boolean useUser, + String password, boolean usePassword) { + if (useAuth) { + setProperty(CollectionSpaceClient.AUTH_PROPERTY, + "true"); + if (useUser) { + setProperty(CollectionSpaceClient.USER_PROPERTY, + user); + } else { + removeProperty(CollectionSpaceClient.USER_PROPERTY); + } + if (usePassword) { + setProperty(CollectionSpaceClient.PASSWORD_PROPERTY, + password); + } else { + removeProperty(CollectionSpaceClient.PASSWORD_PROPERTY); + } + } else { + removeProperty(CollectionSpaceClient.AUTH_PROPERTY); + } + setupHttpClient(); + setProxy(); + } } diff --git a/services/client/src/main/java/org/collectionspace/services/client/CollectionSpaceClient.java b/services/client/src/main/java/org/collectionspace/services/client/CollectionSpaceClient.java index 82adaede4..d17653618 100644 --- a/services/client/src/main/java/org/collectionspace/services/client/CollectionSpaceClient.java +++ b/services/client/src/main/java/org/collectionspace/services/client/CollectionSpaceClient.java @@ -22,8 +22,8 @@ * limitations under the License. */ package org.collectionspace.services.client; -import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response; import org.apache.commons.httpclient.HttpClient; import org.collectionspace.services.jaxb.AbstractCommonList; import org.jboss.resteasy.client.ClientResponse; @@ -32,19 +32,15 @@ import org.jboss.resteasy.client.ClientResponse; * */ public interface CollectionSpaceClient { - + /** The AUT h_ property. */ String AUTH_PROPERTY = "cspace.auth"; - /** The PASSWOR d_ property. */ String PASSWORD_PROPERTY = "cspace.password"; - /** The SS l_ property. */ String SSL_PROPERTY = "cspace.ssl"; - /** The UR l_ property. */ String URL_PROPERTY = "cspace.url"; - /** The USE r_ property. */ String USER_PROPERTY = "cspace.user"; @@ -54,7 +50,7 @@ public interface CollectionSpaceClient { * @return the proxy */ CollectionSpaceProxy getProxy(); - + /** * Gets the base url. * @@ -104,6 +100,25 @@ public interface CollectionSpaceClient { */ void setupHttpClient(); + /** + * setProxy for the client + * might be useful to reset proxy (based on auth requirements) that is usually created at the time of + * constructing a client + */ + void setProxy(); + + /** + * setAuth sets up authentication properties based on given parameters + * @param useAuth + * @param user user name + * @param useUser indicates using user name + * @param password + * @param usePassword indicates using password + */ + void setAuth(boolean useAuth, + String user, boolean useUser, + String password, boolean usePassword); + /** * Use auth. * @@ -123,7 +138,7 @@ public interface CollectionSpaceClient { * @return boolean */ boolean isServerSecure(); - + /** * Read list. * diff --git a/services/client/src/main/java/org/collectionspace/services/client/TestServiceClient.java b/services/client/src/main/java/org/collectionspace/services/client/TestServiceClient.java index 8374ff208..faf5fdc16 100644 --- a/services/client/src/main/java/org/collectionspace/services/client/TestServiceClient.java +++ b/services/client/src/main/java/org/collectionspace/services/client/TestServiceClient.java @@ -20,7 +20,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.collectionspace.services.client; import javax.ws.rs.core.Response; @@ -33,23 +32,30 @@ import org.jboss.resteasy.client.ClientResponse; * @version $Revision:$ */ public class TestServiceClient extends AbstractServiceClientImpl { + /** - * - * Returning NULL since this class is a base-level client, used (only) - * to obtain the base service URL. - * - */ - @Override - public String getServicePathComponent() { - throw new UnsupportedOperationException(); - } - - @Override - public CollectionSpaceProxy getProxy() { - throw new UnsupportedOperationException(); - } - - public ClientResponse delete(String csid) { - throw new UnsupportedOperationException(); - } + * + * Returning NULL since this class is a base-level client, used (only) + * to obtain the base service URL. + * + */ + @Override + public String getServicePathComponent() { + throw new UnsupportedOperationException(); + } + + + @Override + public CollectionSpaceProxy getProxy() { + throw new UnsupportedOperationException(); + } + + @Override + public void setProxy() { + throw new UnsupportedOperationException(); + } + + public ClientResponse delete(String csid) { + throw new UnsupportedOperationException(); + } } diff --git a/services/client/src/main/java/org/collectionspace/services/client/test/BaseServiceTest.java b/services/client/src/main/java/org/collectionspace/services/client/test/BaseServiceTest.java index be29719c4..02747faa6 100644 --- a/services/client/src/main/java/org/collectionspace/services/client/test/BaseServiceTest.java +++ b/services/client/src/main/java/org/collectionspace/services/client/test/BaseServiceTest.java @@ -69,35 +69,26 @@ import org.collectionspace.services.jaxb.AbstractCommonList; */ public abstract class BaseServiceTest { - //Maven's base directory -i.e., the one containing the current pom.xml - static protected final String MAVEN_BASEDIR_PROPERTY = "maven.basedir"; - + //Maven's base directory -i.e., the one containing the current pom.xml + static protected final String MAVEN_BASEDIR_PROPERTY = "maven.basedir"; /** The Constant logger. */ static protected final Logger logger = LoggerFactory.getLogger(BaseServiceTest.class); - /** The Constant serviceClient. */ protected static final TestServiceClient serviceClient = new TestServiceClient(); - /** The NO n_ existen t_ id. */ protected final String NON_EXISTENT_ID = createNonExistentIdentifier(); - /** The EXPECTE d_ statu s_ code. */ protected int EXPECTED_STATUS_CODE = 0; - /** The REQUES t_ type. */ protected ServiceRequestType REQUEST_TYPE = ServiceRequestType.NON_EXISTENT; - /** The Constant XML_DECLARATION. */ protected static final String XML_DECLARATION = ""; - /** The Constant MALFORMED_XML_DATA. */ protected static final String MALFORMED_XML_DATA = XML_DECLARATION + "wrong schema contentswrong schema contents"; - /** The NUL l_ charset. */ final String NULL_CHARSET = null; @@ -107,16 +98,16 @@ public abstract class BaseServiceTest { * @return the client */ abstract protected CollectionSpaceClient getClientInstance(); - - /** - * Gets the abstract common list. - * - * @param response the response - * @return the abstract common list - */ - abstract protected AbstractCommonList getAbstractCommonList( - ClientResponse response); - + + /** + * Gets the abstract common list. + * + * @param response the response + * @return the abstract common list + */ + abstract protected AbstractCommonList getAbstractCommonList( + ClientResponse response); + /** * Returns the name of the currently running test. * diff --git a/services/common/src/main/java/org/collectionspace/services/common/security/SecurityInterceptor.java b/services/common/src/main/java/org/collectionspace/services/common/security/SecurityInterceptor.java index c0ac26821..2e56c89c5 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/security/SecurityInterceptor.java +++ b/services/common/src/main/java/org/collectionspace/services/common/security/SecurityInterceptor.java @@ -41,6 +41,8 @@ import javax.ws.rs.core.Response; import javax.ws.rs.ext.Provider; import org.collectionspace.authentication.AuthN; import org.collectionspace.services.authorization.AuthZ; +import org.collectionspace.services.authorization.CSpaceResource; +import org.collectionspace.services.authorization.URIResourceImpl; import org.collectionspace.services.common.document.JaxbUtils; import org.collectionspace.services.common.storage.jpa.JpaStorageUtils; import org.slf4j.Logger; @@ -66,19 +68,21 @@ public class SecurityInterceptor implements PreProcessInterceptor { logger.debug("received " + httpMethod + " on " + uriPath); } checkActive(); - AuthZ authZ = AuthZ.get(); -// CSpaceResource res = new URIResourceImpl(uriPath, httpMethod); -// if (!authZ.isAccessAllowed(res)) { -// logger.error("Access to " + res.getId() + " is NOT allowed to " + -// " user=" + AuthN.get().getUserId()); -// Response response = Response.status( -// Response.Status.FORBIDDEN).entity(uriPath + " " + httpMethod).type("text/plain").build(); -// throw new WebApplicationException(response); -// } -// if(logger.isDebugEnabled()) { -// logger.debug("Access to " + res.getId() + " is allowed to " + -// " user=" + AuthN.get().getUserId()); -// } + if (uriPath.startsWith("dimensions")) { + AuthZ authZ = AuthZ.get(); + CSpaceResource res = new URIResourceImpl(uriPath, httpMethod); + if (!authZ.isAccessAllowed(res)) { + logger.error("Access to " + res.getId() + " is NOT allowed to " + + " user=" + AuthN.get().getUserId()); + Response response = Response.status( + Response.Status.FORBIDDEN).entity(uriPath + " " + httpMethod).type("text/plain").build(); + throw new WebApplicationException(response); + } + if (logger.isDebugEnabled()) { + logger.debug("Access to " + res.getId() + " is allowed to " + + " user=" + AuthN.get().getUserId()); + } + } return null; } @@ -106,7 +110,7 @@ public class SecurityInterceptor implements PreProcessInterceptor { } Object status = JaxbUtils.getValue(account, "getStatus"); if (status != null) { - String value = (String)JaxbUtils.getValue(status, "value"); + String value = (String) JaxbUtils.getValue(status, "value"); if ("INACTIVE".equalsIgnoreCase(value)) { String msg = "User's account is inactive, userId=" + userId; Response response = Response.status( diff --git a/services/dimension/client/pom.xml b/services/dimension/client/pom.xml index 3bc8bcb70..f719cbe06 100644 --- a/services/dimension/client/pom.xml +++ b/services/dimension/client/pom.xml @@ -19,12 +19,10 @@ org.slf4j slf4j-api - test org.slf4j slf4j-log4j12 - test diff --git a/services/dimension/client/src/main/java/org/collectionspace/services/client/DimensionFactory.java b/services/dimension/client/src/main/java/org/collectionspace/services/client/DimensionFactory.java new file mode 100644 index 000000000..9afb62917 --- /dev/null +++ b/services/dimension/client/src/main/java/org/collectionspace/services/client/DimensionFactory.java @@ -0,0 +1,66 @@ +/** + * This document is a part of the source code and related artifacts + * for CollectionSpace, an open source collections management system + * for museums and related institutions: + + * http://www.collectionspace.org + * http://wiki.collectionspace.org + + * Copyright 2010 University of California at Berkeley + + * Licensed under the Educational Community License (ECL), Version 2.0. + * You may not use this file except in compliance with this License. + + * You may obtain a copy of the ECL 2.0 License at + + * https://source.collectionspace.org/collection-space/LICENSE.txt + + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package org.collectionspace.services.client; + + + + +import javax.ws.rs.core.MediaType; +import org.collectionspace.services.dimension.DimensionsCommon; +import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput; +import org.jboss.resteasy.plugins.providers.multipart.OutputPart; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * @author + */ +public class DimensionFactory { + + static private final Logger logger = + LoggerFactory.getLogger(DimensionFactory.class); + + /** + * Creates the dimension instance. + * + * @param commpnPartName + * @param dimension + * @return the multipart output + */ + public static MultipartOutput createDimensionInstance(String commonPartName, + DimensionsCommon dimension) { + + MultipartOutput multipart = new MultipartOutput(); + OutputPart commonPart = + multipart.addPart(dimension, MediaType.APPLICATION_XML_TYPE); + commonPart.getHeaders().add("label", commonPartName); + + return multipart; + } +} diff --git a/services/dimension/client/src/test/java/org/collectionspace/services/client/test/DimensionServiceTest.java b/services/dimension/client/src/test/java/org/collectionspace/services/client/test/DimensionServiceTest.java index 32310fba8..39105d7f6 100644 --- a/services/dimension/client/src/test/java/org/collectionspace/services/client/test/DimensionServiceTest.java +++ b/services/dimension/client/src/test/java/org/collectionspace/services/client/test/DimensionServiceTest.java @@ -29,6 +29,7 @@ import javax.ws.rs.core.Response; import org.collectionspace.services.client.CollectionSpaceClient; import org.collectionspace.services.client.DimensionClient; +import org.collectionspace.services.client.DimensionFactory; import org.collectionspace.services.dimension.DimensionsCommon; import org.collectionspace.services.dimension.DimensionsCommonList; import org.collectionspace.services.jaxb.AbstractCommonList; @@ -54,14 +55,12 @@ import org.slf4j.LoggerFactory; */ public class DimensionServiceTest extends AbstractServiceTestImpl { - /** The logger. */ - private final Logger logger = - LoggerFactory.getLogger(DimensionServiceTest.class); - + /** The logger. */ + private final Logger logger = + LoggerFactory.getLogger(DimensionServiceTest.class); // Instance variables specific to this test. /** The SERVIC e_ pat h_ component. */ final String SERVICE_PATH_COMPONENT = "dimensions"; - /** The known resource id. */ private String knownResourceId = null; @@ -70,15 +69,15 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { */ @Override protected CollectionSpaceClient getClientInstance() { - return new DimensionClient(); + return new DimensionClient(); } - + /* (non-Javadoc) * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse) */ @Override - protected AbstractCommonList getAbstractCommonList( - ClientResponse response) { + protected AbstractCommonList getAbstractCommonList( + ClientResponse response) { return response.getEntity(DimensionsCommonList.class); } @@ -90,7 +89,7 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String) */ @Override - @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class) + @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class) public void create(String testName) throws Exception { // Perform setup, such as initializing the type of service request @@ -101,7 +100,8 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { // Submit the request to the service and store the response. DimensionClient client = new DimensionClient(); String identifier = createIdentifier(); - MultipartOutput multipart = createDimensionInstance(identifier); + MultipartOutput multipart = createDimensionInstance(client.getCommonPartName(), + identifier); ClientResponse res = client.create(multipart); int statusCode = res.getStatus(); @@ -112,7 +112,7 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { // Specifically: // Does it fall within the set of valid status codes? // Does it exactly match the expected status code? - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { logger.debug(testName + ": status = " + statusCode); } Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), @@ -121,7 +121,7 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { // Store the ID returned from the first resource created // for additional tests below. - if (knownResourceId == null){ + if (knownResourceId == null) { knownResourceId = extractId(res); if (logger.isDebugEnabled()) { logger.debug(testName + ": knownResourceId=" + knownResourceId); @@ -137,10 +137,10 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String) */ @Override - @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class, - dependsOnMethods = {"create"}) + @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, + dependsOnMethods = {"create"}) public void createList(String testName) throws Exception { - for(int i = 0; i < 3; i++){ + for (int i = 0; i < 3; i++) { create(testName); } } @@ -153,7 +153,7 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { */ @Override public void createWithEmptyEntityBody(String testName) throws Exception { - //Should this really be empty? + //Should this really be empty? } /* (non-Javadoc) @@ -161,7 +161,7 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { */ @Override public void createWithMalformedXml(String testName) throws Exception { - //Should this really be empty? + //Should this really be empty? } /* (non-Javadoc) @@ -169,13 +169,13 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { */ @Override public void createWithWrongXmlSchema(String testName) throws Exception { - //Should this really be empty? + //Should this really be empty? } /* @Override @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class, - dependsOnMethods = {"create", "testSubmitRequest"}) + dependsOnMethods = {"create", "testSubmitRequest"}) public void createWithEmptyEntityBody(String testName) throws Exception { // Perform setup. @@ -191,9 +191,9 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { // Check the status code of the response: does it match // the expected response(s)? if(logger.isDebugEnabled()){ - logger.debug("createWithEmptyEntityBody url=" + url + - " status=" + statusCode); - } + logger.debug("createWithEmptyEntityBody url=" + url + + " status=" + statusCode); + } Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); @@ -201,7 +201,7 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { @Override @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class, - dependsOnMethods = {"create", "testSubmitRequest"}) + dependsOnMethods = {"create", "testSubmitRequest"}) public void createWithMalformedXml(String testName) throws Exception { // Perform setup. @@ -217,9 +217,9 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { // Check the status code of the response: does it match // the expected response(s)? if(logger.isDebugEnabled()){ - logger.debug(testName + ": url=" + url + - " status=" + statusCode); - } + logger.debug(testName + ": url=" + url + + " status=" + statusCode); + } Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); @@ -227,7 +227,7 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { @Override @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class, - dependsOnMethods = {"create", "testSubmitRequest"}) + dependsOnMethods = {"create", "testSubmitRequest"}) public void createWithWrongXmlSchema(String testName) throws Exception { // Perform setup. @@ -243,15 +243,14 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { // Check the status code of the response: does it match // the expected response(s)? if(logger.isDebugEnabled()){ - logger.debug(testName + ": url=" + url + - " status=" + statusCode); - } + logger.debug(testName + ": url=" + url + + " status=" + statusCode); + } Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); } */ - // --------------------------------------------------------------- // CRUD tests : READ tests // --------------------------------------------------------------- @@ -260,8 +259,8 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String) */ @Override - @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class, - dependsOnMethods = {"create"}) + @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, + dependsOnMethods = {"create"}) public void read(String testName) throws Exception { // Perform setup. @@ -274,7 +273,7 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { // Check the status code of the response: does it match // the expected response(s)? - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { logger.debug(testName + ": status = " + statusCode); } Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), @@ -292,8 +291,8 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String) */ @Override - @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class, - dependsOnMethods = {"read"}) + @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, + dependsOnMethods = {"read"}) public void readNonExistent(String testName) throws Exception { // Perform setup. @@ -306,7 +305,7 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { // Check the status code of the response: does it match // the expected response(s)? - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { logger.debug(testName + ": status = " + statusCode); } Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), @@ -322,8 +321,8 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String) */ @Override - @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class, - dependsOnMethods = {"read"}) + @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, + dependsOnMethods = {"read"}) public void readList(String testName) throws Exception { // Perform setup. @@ -337,7 +336,7 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { // Check the status code of the response: does it match // the expected response(s)? - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { logger.debug(testName + ": status = " + statusCode); } Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), @@ -346,17 +345,17 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { // Optionally output additional data about list members for debugging. boolean iterateThroughList = false; - if(iterateThroughList && logger.isDebugEnabled()){ + if (iterateThroughList && logger.isDebugEnabled()) { List items = list.getDimensionListItem(); int i = 0; - for(DimensionsCommonList.DimensionListItem item : items){ - logger.debug(testName + ": list-item[" + i + "] csid=" + - item.getCsid()); - logger.debug(testName + ": list-item[" + i + "] objectNumber=" + - item.getDimension()); - logger.debug(testName + ": list-item[" + i + "] URI=" + - item.getUri()); + for (DimensionsCommonList.DimensionListItem item : items) { + logger.debug(testName + ": list-item[" + i + "] csid=" + + item.getCsid()); + logger.debug(testName + ": list-item[" + i + "] objectNumber=" + + item.getDimension()); + logger.debug(testName + ": list-item[" + i + "] URI=" + + item.getUri()); i++; } } @@ -373,8 +372,8 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String) */ @Override - @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class, - dependsOnMethods = {"read"}) + @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, + dependsOnMethods = {"read"}) public void update(String testName) throws Exception { // Perform setup. @@ -384,12 +383,12 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { DimensionClient client = new DimensionClient(); ClientResponse res = client.read(knownResourceId); - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { logger.debug(testName + ": read status = " + res.getStatus()); } Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE); - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { logger.debug("got object to update with ID: " + knownResourceId); } MultipartInput input = (MultipartInput) res.getEntity(); @@ -400,7 +399,7 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { // Update the content of this resource. dimension.setValue("updated-" + dimension.getValue()); dimension.setValueDate("updated-" + dimension.getValueDate()); - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { logger.debug("to be updated object"); logger.debug(objectAsXmlString(dimension, DimensionsCommon.class)); } @@ -412,7 +411,7 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { res = client.update(knownResourceId, output); int statusCode = res.getStatus(); // Check the status code of the response: does it match the expected response(s)? - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { logger.debug(testName + ": status = " + statusCode); } Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), @@ -423,7 +422,7 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { input = (MultipartInput) res.getEntity(); DimensionsCommon updatedDimension = (DimensionsCommon) extractPart(input, - client.getCommonPartName(), DimensionsCommon.class); + client.getCommonPartName(), DimensionsCommon.class); Assert.assertNotNull(updatedDimension); Assert.assertEquals(updatedDimension.getValueDate(), @@ -439,30 +438,30 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String) */ @Override - public void updateWithEmptyEntityBody(String testName) throws Exception{ - //Should this really be empty? + public void updateWithEmptyEntityBody(String testName) throws Exception { + //Should this really be empty? } - + /* (non-Javadoc) * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String) */ @Override public void updateWithMalformedXml(String testName) throws Exception { - //Should this really be empty? + //Should this really be empty? } - + /* (non-Javadoc) * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String) */ @Override public void updateWithWrongXmlSchema(String testName) throws Exception { - //Should this really be empty? + //Should this really be empty? } /* @Override @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class, - dependsOnMethods = {"create", "update", "testSubmitRequest"}) + dependsOnMethods = {"create", "update", "testSubmitRequest"}) public void updateWithEmptyEntityBody(String testName) throws Exception { // Perform setup. @@ -478,9 +477,9 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { // Check the status code of the response: does it match // the expected response(s)? if(logger.isDebugEnabled()){ - logger.debug(testName + ": url=" + url + - " status=" + statusCode); - } + logger.debug(testName + ": url=" + url + + " status=" + statusCode); + } Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); @@ -488,7 +487,7 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { @Override @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class, - dependsOnMethods = {"create", "update", "testSubmitRequest"}) + dependsOnMethods = {"create", "update", "testSubmitRequest"}) public void updateWithMalformedXml(String testName) throws Exception { // Perform setup. @@ -504,9 +503,9 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { // Check the status code of the response: does it match // the expected response(s)? if(logger.isDebugEnabled()){ - logger.debug(testName + ": url=" + url + - " status=" + statusCode); - } + logger.debug(testName + ": url=" + url + + " status=" + statusCode); + } Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); @@ -514,7 +513,7 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { @Override @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class, - dependsOnMethods = {"create", "update", "testSubmitRequest"}) + dependsOnMethods = {"create", "update", "testSubmitRequest"}) public void updateWithWrongXmlSchema(String testName) throws Exception { // Perform setup. @@ -530,9 +529,9 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { // Check the status code of the response: does it match // the expected response(s)? if(logger.isDebugEnabled()){ - logger.debug(testName + ": url=" + url + - " status=" + statusCode); - } + logger.debug(testName + ": url=" + url + + " status=" + statusCode); + } Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); @@ -543,8 +542,8 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String) */ @Override - @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class, - dependsOnMethods = {"update", "testSubmitRequest"}) + @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, + dependsOnMethods = {"update", "testSubmitRequest"}) public void updateNonExistent(String testName) throws Exception { // Perform setup. @@ -554,14 +553,15 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { // Note: The ID used in this 'create' call may be arbitrary. // The only relevant ID may be the one used in update(), below. DimensionClient client = new DimensionClient(); - MultipartOutput multipart = createDimensionInstance(NON_EXISTENT_ID); + MultipartOutput multipart = createDimensionInstance(client.getCommonPartName(), + NON_EXISTENT_ID); ClientResponse res = client.update(NON_EXISTENT_ID, multipart); int statusCode = res.getStatus(); // Check the status code of the response: does it match // the expected response(s)? - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { logger.debug(testName + ": status = " + statusCode); } Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), @@ -577,8 +577,8 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String) */ @Override - @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class, - dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"}) + @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, + dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"}) public void delete(String testName) throws Exception { // Perform setup. @@ -591,7 +591,7 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { // Check the status code of the response: does it match // the expected response(s)? - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { logger.debug(testName + ": status = " + statusCode); } Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), @@ -604,8 +604,8 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String) */ @Override - @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class, - dependsOnMethods = {"delete"}) + @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, + dependsOnMethods = {"delete"}) public void deleteNonExistent(String testName) throws Exception { // Perform setup. @@ -618,7 +618,7 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { // Check the status code of the response: does it match // the expected response(s)? - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { logger.debug(testName + ": status = " + statusCode); } Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), @@ -646,9 +646,9 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { // Check the status code of the response: does it match // the expected response(s)? - if(logger.isDebugEnabled()){ - logger.debug("testSubmitRequest: url=" + url + - " status=" + statusCode); + if (logger.isDebugEnabled()) { + logger.debug("testSubmitRequest: url=" + url + + " status=" + statusCode); } Assert.assertEquals(statusCode, EXPECTED_STATUS); @@ -671,9 +671,9 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { * @param identifier the identifier * @return the multipart output */ - private MultipartOutput createDimensionInstance(String identifier) { - return createDimensionInstance( - "dimensionType-" + identifier, + private MultipartOutput createDimensionInstance(String commonPartName, String identifier) { + return createDimensionInstance(commonPartName, + "dimensionType-" + identifier, "entryNumber-" + identifier, "entryDate-" + identifier); } @@ -686,19 +686,18 @@ public class DimensionServiceTest extends AbstractServiceTestImpl { * @param entryDate the entry date * @return the multipart output */ - private MultipartOutput createDimensionInstance(String dimensionType, String entryNumber, String entryDate) { + private MultipartOutput createDimensionInstance(String commonPartName, String dimensionType, String entryNumber, String entryDate) { DimensionsCommon dimension = new DimensionsCommon(); dimension.setDimension(dimensionType); dimension.setValue(entryNumber); dimension.setValueDate(entryDate); - MultipartOutput multipart = new MultipartOutput(); - OutputPart commonPart = - multipart.addPart(dimension, MediaType.APPLICATION_XML_TYPE); - commonPart.getHeaders().add("label", new DimensionClient().getCommonPartName()); + MultipartOutput multipart = DimensionFactory.createDimensionInstance( + commonPartName, dimension); - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { logger.debug("to be created, dimension common"); - logger.debug(objectAsXmlString(dimension, DimensionsCommon.class)); + logger.debug(objectAsXmlString(dimension, + DimensionsCommon.class)); } return multipart; diff --git a/services/security/client/pom.xml b/services/security/client/pom.xml index 6dac20c73..eecd1ec78 100644 --- a/services/security/client/pom.xml +++ b/services/security/client/pom.xml @@ -47,6 +47,11 @@ org.collectionspace.services.collectionobject.client ${project.version} + + org.collectionspace.services + org.collectionspace.services.dimension.client + ${project.version} + org.testng diff --git a/services/security/client/src/test/java/org/collectionspace/services/security/client/test/AuthenticationServiceTest.java b/services/security/client/src/test/java/org/collectionspace/services/security/client/test/AuthenticationServiceTest.java index f47b5fe0e..79fbbcd84 100644 --- a/services/security/client/src/test/java/org/collectionspace/services/security/client/test/AuthenticationServiceTest.java +++ b/services/security/client/src/test/java/org/collectionspace/services/security/client/test/AuthenticationServiceTest.java @@ -105,12 +105,7 @@ public class AuthenticationServiceTest extends AbstractServiceTestImpl { // its associated HTTP method name (e.g. POST, DELETE). setupCreate(testName); AccountClient accountClient = new AccountClient(); - accountClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, - "true"); - accountClient.setProperty(CollectionSpaceClient.USER_PROPERTY, - "test"); - accountClient.setProperty( - CollectionSpaceClient.PASSWORD_PROPERTY, "test"); + accountClient.setAuth(true, "test", true, "test", true); // Submit the request to the service and store the response. AccountsCommon account = @@ -142,12 +137,7 @@ public class AuthenticationServiceTest extends AbstractServiceTestImpl { // its associated HTTP method name (e.g. POST, DELETE). setupCreate(testName); AccountClient accountClient = new AccountClient(); - accountClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, - "true"); - accountClient.setProperty(CollectionSpaceClient.USER_PROPERTY, - "test"); - accountClient.setProperty( - CollectionSpaceClient.PASSWORD_PROPERTY, "test"); + accountClient.setAuth(true, "test", true, "test", true); // Submit the request to the service and store the response. AccountsCommon account = @@ -201,23 +191,10 @@ public class AuthenticationServiceTest extends AbstractServiceTestImpl { public void create(String testName) { setupCreate(testName); CollectionObjectClient collectionObjectClient = new CollectionObjectClient(); + collectionObjectClient.setAuth(true, "barney", true, "barney08", true); String identifier = BaseServiceTest.createIdentifier(); MultipartOutput multipart = createCollectionObjectInstance( collectionObjectClient.getCommonPartName(), identifier); - - collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, - "true"); - collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY, - "barney"); - collectionObjectClient.setProperty( - CollectionSpaceClient.PASSWORD_PROPERTY, "barney08"); - try { - collectionObjectClient.setupHttpClient(); - collectionObjectClient.setProxy(); - } catch (Exception e) { - logger.error("create: caught " + e.getMessage()); - return; - } ClientResponse res = collectionObjectClient.create(multipart); if (logger.isDebugEnabled()) { logger.debug("create: status = " + res.getStatus()); @@ -233,27 +210,35 @@ public class AuthenticationServiceTest extends AbstractServiceTestImpl { } + @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, + dependsOnMethods = {"createActiveAccount"}) + public void createWithoutAuthn(String testName) { + setupCreate(testName); + CollectionObjectClient collectionObjectClient = new CollectionObjectClient(); + collectionObjectClient.setAuth(false, "test", true, "test", true); + String identifier = BaseServiceTest.createIdentifier(); + MultipartOutput multipart = createCollectionObjectInstance( + collectionObjectClient.getCommonPartName(), identifier); + ClientResponse res = collectionObjectClient.create(multipart); + if (logger.isDebugEnabled()) { + logger.debug("create: status = " + res.getStatus()); + } + Assert.assertEquals(res.getStatus(), + Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + + Response.Status.UNAUTHORIZED.getStatusCode()); + res.releaseConnection(); + + } + @Test(dataProvider = "testName", dependsOnMethods = {"createInactiveAccount"}) public void createWithInactiveAccount(String testName) { banner(testName); CollectionObjectClient collectionObjectClient = new CollectionObjectClient(); + collectionObjectClient.setAuth(true, "george", true, "george08", true); String identifier = BaseServiceTest.createIdentifier(); MultipartOutput multipart = createCollectionObjectInstance( collectionObjectClient.getCommonPartName(), identifier); - collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, - "true"); - collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY, - "george"); - collectionObjectClient.setProperty(CollectionSpaceClient.PASSWORD_PROPERTY, - "george08"); - try { - collectionObjectClient.setupHttpClient(); - collectionObjectClient.setProxy(); - } catch (Exception e) { - logger.error(testName + ": caught " + e.getMessage()); - return; - } ClientResponse res = collectionObjectClient.create(multipart); if (logger.isDebugEnabled()) { logger.debug(testName + ": status = " + res.getStatus()); @@ -271,22 +256,10 @@ public class AuthenticationServiceTest extends AbstractServiceTestImpl { public void createWithoutPassword(String testName) { banner(testName); CollectionObjectClient collectionObjectClient = new CollectionObjectClient(); + collectionObjectClient.setAuth(true, "test", true, "", false); String identifier = BaseServiceTest.createIdentifier(); MultipartOutput multipart = createCollectionObjectInstance( collectionObjectClient.getCommonPartName(), identifier); - - collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, - "true"); - collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY, - "test"); - collectionObjectClient.removeProperty(CollectionSpaceClient.PASSWORD_PROPERTY); - try { - collectionObjectClient.setupHttpClient(); - collectionObjectClient.setProxy(); - } catch (Exception e) { - logger.error(testName + ": caught " + e.getMessage()); - return; - } ClientResponse res = collectionObjectClient.create(multipart); if (logger.isDebugEnabled()) { logger.debug(testName + ": status = " + res.getStatus()); @@ -302,23 +275,10 @@ public class AuthenticationServiceTest extends AbstractServiceTestImpl { public void createWithUnknownUser(String testName) { banner(testName); CollectionObjectClient collectionObjectClient = new CollectionObjectClient(); + collectionObjectClient.setAuth(true, "foo", true, "bar", true); String identifier = BaseServiceTest.createIdentifier(); MultipartOutput multipart = createCollectionObjectInstance( collectionObjectClient.getCommonPartName(), identifier); - - collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, - "true"); - collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY, - "foo"); - collectionObjectClient.setProperty(CollectionSpaceClient.PASSWORD_PROPERTY, - "bar"); - try { - collectionObjectClient.setupHttpClient(); - collectionObjectClient.setProxy(); - } catch (Exception e) { - logger.error(testName + ": caught " + e.getMessage()); - return; - } ClientResponse res = collectionObjectClient.create(multipart); if (logger.isDebugEnabled()) { logger.debug(testName + ": status = " + res.getStatus()); @@ -334,23 +294,10 @@ public class AuthenticationServiceTest extends AbstractServiceTestImpl { public void createWithIncorrectPassword(String testName) { banner(testName); CollectionObjectClient collectionObjectClient = new CollectionObjectClient(); + collectionObjectClient.setAuth(true, "test", true, "bar", true); String identifier = BaseServiceTest.createIdentifier(); MultipartOutput multipart = createCollectionObjectInstance( collectionObjectClient.getCommonPartName(), identifier); - - collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, - "true"); - collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY, - "test"); - collectionObjectClient.setProperty( - CollectionSpaceClient.PASSWORD_PROPERTY, "bar"); - try { - collectionObjectClient.setupHttpClient(); - collectionObjectClient.setProxy(); - } catch (Exception e) { - logger.error(testName + ": caught " + e.getMessage()); - return; - } ClientResponse res = collectionObjectClient.create(multipart); if (logger.isDebugEnabled()) { logger.debug(testName + ": status = " + res.getStatus()); @@ -366,23 +313,10 @@ public class AuthenticationServiceTest extends AbstractServiceTestImpl { public void createWithIncorrectUserPassword(String testName) { banner(testName); CollectionObjectClient collectionObjectClient = new CollectionObjectClient(); + collectionObjectClient.setAuth(true, "foo", true, "bar", true); String identifier = BaseServiceTest.createIdentifier(); MultipartOutput multipart = createCollectionObjectInstance( collectionObjectClient.getCommonPartName(), identifier); - - collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, - "true"); - collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY, - "foo"); - collectionObjectClient.setProperty( - CollectionSpaceClient.PASSWORD_PROPERTY, "bar"); - try { - collectionObjectClient.setupHttpClient(); - collectionObjectClient.setProxy(); - } catch (Exception e) { - logger.error(testName + ": caught " + e.getMessage()); - return; - } ClientResponse res = collectionObjectClient.create(multipart); if (logger.isDebugEnabled()) { logger.debug(testName + ": status = " @@ -399,23 +333,10 @@ public class AuthenticationServiceTest extends AbstractServiceTestImpl { public void createWithoutTenant(String testName) { banner(testName); CollectionObjectClient collectionObjectClient = new CollectionObjectClient(); + collectionObjectClient.setAuth(true, "babybop", true, "babybop09", true); String identifier = BaseServiceTest.createIdentifier(); MultipartOutput multipart = createCollectionObjectInstance( collectionObjectClient.getCommonPartName(), identifier); - - collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, - "true"); - collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY, - "babybop"); - collectionObjectClient.setProperty( - CollectionSpaceClient.PASSWORD_PROPERTY, "babybop09"); - try { - collectionObjectClient.setupHttpClient(); - collectionObjectClient.setProxy(); - } catch (Exception e) { - logger.error(testName + ": caught " + e.getMessage()); - return; - } ClientResponse res = collectionObjectClient.create(multipart); if (logger.isDebugEnabled()) { logger.debug(testName + ": status = " @@ -435,20 +356,7 @@ public class AuthenticationServiceTest extends AbstractServiceTestImpl { setupDelete(testName); CollectionObjectClient collectionObjectClient = new CollectionObjectClient(); collectionObjectClient = new CollectionObjectClient(); - - collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, - "true"); - collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY, - "test"); - collectionObjectClient.setProperty( - CollectionSpaceClient.PASSWORD_PROPERTY, "test"); - try { - collectionObjectClient.setupHttpClient(); - collectionObjectClient.setProxy(); - } catch (Exception e) { - logger.error("deleteCollectionObject: caught " + e.getMessage()); - return; - } + collectionObjectClient.setAuth(true, "test", true, "test", true); if (logger.isDebugEnabled()) { logger.debug("Calling deleteCollectionObject:" + knownResourceId); } @@ -468,13 +376,7 @@ public class AuthenticationServiceTest extends AbstractServiceTestImpl { // Perform setup. setupDelete(testName); AccountClient accountClient = new AccountClient(); - - accountClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, - "true"); - accountClient.setProperty(CollectionSpaceClient.USER_PROPERTY, - "test"); - accountClient.setProperty( - CollectionSpaceClient.PASSWORD_PROPERTY, "test"); + accountClient.setAuth(true, "test", true, "test", true); // Submit the request to the service and store the response. ClientResponse res = accountClient.delete(barneyAccountId); int statusCode = res.getStatus(); @@ -507,7 +409,8 @@ public class AuthenticationServiceTest extends AbstractServiceTestImpl { */ private MultipartOutput createCollectionObjectInstance( String commonPartName, String identifier) { - return createCollectionObjectInstance(commonPartName, "objectNumber-" + identifier, "objectName-" + identifier); + return createCollectionObjectInstance(commonPartName, "objectNumber-" + + identifier, "objectName-" + identifier); } /** diff --git a/services/security/client/src/test/java/org/collectionspace/services/security/client/test/AuthorizationServiceTest.java b/services/security/client/src/test/java/org/collectionspace/services/security/client/test/AuthorizationServiceTest.java new file mode 100644 index 000000000..48df128f9 --- /dev/null +++ b/services/security/client/src/test/java/org/collectionspace/services/security/client/test/AuthorizationServiceTest.java @@ -0,0 +1,608 @@ +/** + * This document is a part of the source code and related artifacts + * for CollectionSpace, an open source collections management system + * for museums and related institutions: + + * http://www.collectionspace.org + * http://wiki.collectionspace.org + + * Copyright 2010 University of California at Berkeley + + * Licensed under the Educational Community License (ECL), Version 2.0. + * You may not use this file except in compliance with this License. + + * You may obtain a copy of the ECL 2.0 License at + + * https://source.collectionspace.org/collection-space/LICENSE.txt + + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package org.collectionspace.services.security.client.test; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Date; +import java.util.Hashtable; +import java.util.List; +import javax.ws.rs.core.Response; +import org.collectionspace.services.account.AccountsCommon; +import org.collectionspace.services.authorization.AccountRole; +import org.collectionspace.services.authorization.AccountValue; +import org.collectionspace.services.authorization.EffectType; + +import org.collectionspace.services.authorization.Permission; +import org.collectionspace.services.authorization.PermissionAction; +import org.collectionspace.services.authorization.PermissionRole; +import org.collectionspace.services.authorization.PermissionValue; +import org.collectionspace.services.authorization.Role; +import org.collectionspace.services.authorization.RoleValue; +import org.collectionspace.services.client.AccountClient; +import org.collectionspace.services.client.AccountFactory; +import org.collectionspace.services.client.AccountRoleClient; +import org.collectionspace.services.client.AccountRoleFactory; +import org.collectionspace.services.client.CollectionSpaceClient; +import org.collectionspace.services.client.DimensionClient; +import org.collectionspace.services.client.DimensionFactory; +import org.collectionspace.services.client.PermissionClient; +import org.collectionspace.services.client.PermissionFactory; +import org.collectionspace.services.client.PermissionRoleClient; +import org.collectionspace.services.client.PermissionRoleFactory; +import org.collectionspace.services.client.RoleClient; +import org.collectionspace.services.client.RoleFactory; +import org.collectionspace.services.client.test.AbstractServiceTestImpl; +import org.collectionspace.services.dimension.DimensionsCommon; +import org.collectionspace.services.jaxb.AbstractCommonList; +import org.jboss.resteasy.client.ClientResponse; +import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput; + +import org.testng.Assert; +import org.testng.annotations.Test; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; + +/** + * AuthorizationServiceTest, carries out tests against a + * deployed and running Permission, Role, AccountRole, PermissionRole and + * CollectionObject Services. + * + * $LastChangedRevision: 917 $ + * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $ + */ +public class AuthorizationServiceTest extends AbstractServiceTestImpl { + + static private final Logger logger = + LoggerFactory.getLogger(AuthorizationServiceTest.class); + // Instance variables specific to this test. + private String knownResourceId = null; + private List allResourceIdsCreated = new ArrayList(); + //key for accValues is userId + private Hashtable accValues = new Hashtable(); + //key for permValues is id as there could be several perms for the same resource + private Hashtable permValues = new Hashtable(); + //key for roleValues is roleName + private Hashtable roleValues = new Hashtable(); + private String bigbirdPermId; + private String elmoPermId; + /* + * This method is called only by the parent class, AbstractServiceTestImpl + */ + + @Override + protected String getServicePathComponent() { + return null; + } + + @BeforeClass(alwaysRun = true) + public void seedData() { + seedPermissions(); + seedRoles(); + seedAccounts(); + seedAccountRoles(); + seedPermissionRoles(); + } + + private void seedPermissions() { + String rc1 = "collectionobjects"; + bigbirdPermId = createPermission(rc1, EffectType.PERMIT); + PermissionValue bbpv = new PermissionValue(); + bbpv.setResourceName(rc1); + bbpv.setPermissionId(bigbirdPermId); + permValues.put(bbpv.getPermissionId(), bbpv); + + String rc2 = "collectionobjects"; + elmoPermId = createPermission(rc2, EffectType.DENY); + PermissionValue epv = new PermissionValue(); + epv.setResourceName(rc2); + epv.setPermissionId(elmoPermId); + permValues.put(epv.getPermissionId(), epv); + } + + private void seedRoles() { + String rn1 = "ROLE_MMI_CM"; + String r1RoleId = createRole(rn1); + RoleValue rv1 = new RoleValue(); + rv1.setRoleId(r1RoleId); + rv1.setRoleName(rn1); + roleValues.put(rv1.getRoleName(), rv1); + + String rn2 = "ROLE_MMI_INTERN"; + String r2RoleId = createRole(rn2); + RoleValue rv2 = new RoleValue(); + rv2.setRoleId(r2RoleId); + rv2.setRoleName(rn2); + roleValues.put(rv2.getRoleName(), rv2); + } + + private void seedAccounts() { + String userId = "bigbird2010"; + String accId = createAccount(userId, "bigbird@cspace.org"); + AccountValue ava = new AccountValue(); + ava.setScreenName(userId); + ava.setUserId(userId); + ava.setAccountId(accId); + accValues.put(ava.getUserId(), ava); + + String userId2 = "elmo2010"; + String coAccId = createAccount(userId2, "elmo@cspace.org"); + AccountValue avc = new AccountValue(); + avc.setScreenName(userId2); + avc.setUserId(userId2); + avc.setAccountId(coAccId); + accValues.put(avc.getUserId(), avc); + } + + private void seedAccountRoles() { + + List bigbirdRoleValues = new ArrayList(); + bigbirdRoleValues.add(roleValues.get("ROLE_MMI_CM")); + createAccountRole(accValues.get("bigbird2010"), bigbirdRoleValues); + + List elmoRoleValues = new ArrayList(); + elmoRoleValues.add(roleValues.get("ROLE_MMI_INTERN")); + createAccountRole(accValues.get("elmo2010"), elmoRoleValues); + } + + private void seedPermissionRoles() { + + List bigbirdRoleValues = new ArrayList(); + bigbirdRoleValues.add(roleValues.get("ROLE_MMI_CM")); + createPermissionRole(permValues.get(bigbirdPermId), bigbirdRoleValues); + + List elmoRoleValues = new ArrayList(); + elmoRoleValues.add(roleValues.get("ROLE_MMI_INTERN")); + createPermissionRole(permValues.get(elmoPermId), elmoRoleValues); + + } + + + /* (non-Javadoc) + * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance() + */ + @Override + protected CollectionSpaceClient getClientInstance() { + return null; + } + + /* (non-Javadoc) + * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse) + */ + @Override + protected AbstractCommonList getAbstractCommonList( + ClientResponse response) { + //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697 + throw new UnsupportedOperationException(); + } + + @Test(dataProvider = "testName") + @Override + public void readPaginatedList(String testName) throws Exception { + //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697 + } + // --------------------------------------------------------------- + // CRUD tests : CREATE tests + // --------------------------------------------------------------- + // Success outcomes + + @Override + @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class) + public void create(String testName) throws Exception { + setupCreate(testName); + + // Submit the request to the service and store the response. + DimensionClient client = new DimensionClient(); + client.setAuth(true, "bigbird2010", true, "bigbird2010", true); + String identifier = createIdentifier(); + DimensionsCommon dimension = new DimensionsCommon(); + dimension.setDimension("dimensionType"); + dimension.setValue("value-" + identifier); + dimension.setValueDate(new Date().toString()); + MultipartOutput multipart = DimensionFactory.createDimensionInstance(client.getCommonPartName(), + dimension); + ClientResponse res = client.create(multipart); + + int statusCode = res.getStatus(); + + if (logger.isDebugEnabled()) { + logger.debug(testName + ": status = " + statusCode); + } + Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + Assert.assertEquals(statusCode, Response.Status.FORBIDDEN.getStatusCode()); + } + + //to not cause uniqueness violation for permRole, createList is removed + @Override + @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, + dependsOnMethods = {"create"}) + public void createList(String testName) throws Exception { + } + + // Failure outcomes + // Placeholders until the three tests below can be uncommented. + // See Issue CSPACE-401. + @Override + public void createWithEmptyEntityBody(String testName) throws Exception { + } + + @Override + public void createWithMalformedXml(String testName) throws Exception { + } + + @Override + public void createWithWrongXmlSchema(String testName) throws Exception { + } + + // --------------------------------------------------------------- + // CRUD tests : READ tests + // --------------------------------------------------------------- + // Success outcomes + @Override + @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, + dependsOnMethods = {"create"}) + public void read(String testName) throws Exception { + + // Perform setup. + setupRead(testName); + + } + + // Failure outcomes + @Override + @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class) + public void readNonExistent(String testName) throws Exception { + + // Perform setup. + setupReadNonExistent(testName); + } + + // --------------------------------------------------------------- + // CRUD tests : READ_LIST tests + // --------------------------------------------------------------- + // Success outcomes + @Override + @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, + dependsOnMethods = {"createList", "read"}) + public void readList(String testName) throws Exception { + setupReadList(testName); + } + + // Failure outcomes + // None at present. + // --------------------------------------------------------------- + // CRUD tests : UPDATE tests + // --------------------------------------------------------------- + // Success outcomes + @Override + @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, + dependsOnMethods = {"read", "readList", "readNonExistent"}) + public void update(String testName) throws Exception { + setupUpdate(testName); + } + + // Failure outcomes + // Placeholders until the three tests below can be uncommented. + // See Issue CSPACE-401. + @Override + public void updateWithEmptyEntityBody(String testName) throws Exception { + } + + @Override + public void updateWithMalformedXml(String testName) throws Exception { + } + + @Override + public void updateWithWrongXmlSchema(String testName) throws Exception { + } + + @Override + @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, + dependsOnMethods = {"readNonExistent", "testSubmitRequest"}) + public void updateNonExistent(String testName) throws Exception { + } + + // --------------------------------------------------------------- + // CRUD tests : DELETE tests + // --------------------------------------------------------------- + // Success outcomes + @Override + @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, + dependsOnMethods = {"read"}) + public void delete(String testName) throws Exception { + // Perform setup. + setupDelete(testName); + + } + + // Failure outcomes + @Override + @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class) + public void deleteNonExistent(String testName) throws Exception { + //ignoring this test as the service side returns 200 now even if it does + //not find a record in the db + } + + // --------------------------------------------------------------- + // Utility tests : tests of code used in tests above + // --------------------------------------------------------------- + /** + * Tests the code for manually submitting data that is used by several + * of the methods above. + */ + @Test(dependsOnMethods = {"create"}) + public void testSubmitRequest() throws Exception { + } + + // --------------------------------------------------------------- + // Utility methods used by tests above + // --------------------------------------------------------------- + @AfterClass(alwaysRun = true) + public void cleanUp() { + setupDelete("cleanup"); + String noTest = System.getProperty("noTestCleanup"); + if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) { + if (logger.isDebugEnabled()) { + logger.debug("Skipping Cleanup phase ..."); + } + return; + } + if (logger.isDebugEnabled()) { + logger.debug("Cleaning up temporary resources created for testing ..."); + } + + deletePermissionRoles(); + deleteAccountRoles(); + deletePermissions(); + deleteRoles(); + deleteAccounts(); + } + + private String createPermission(String resName, EffectType effect) { + setupCreate("createPermission"); + PermissionClient permClient = new PermissionClient(); + List actions = PermissionFactory.createDefaultActions(); + Permission permission = PermissionFactory.createPermissionInstance(resName, + "default permissions for " + resName, + actions, effect, true, true, true); + ClientResponse res = permClient.create(permission); + int statusCode = res.getStatus(); + if (logger.isDebugEnabled()) { + logger.debug("createPermission: resName=" + resName + + " status = " + statusCode); + } + Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); + res.releaseConnection(); + return extractId(res); + } + + private void deletePermission(String permId) { + setupDelete("deletePermission"); + PermissionClient permClient = new PermissionClient(); + ClientResponse res = permClient.delete(permId); + int statusCode = res.getStatus(); + if (logger.isDebugEnabled()) { + logger.debug("deletePermission: delete permission id=" + + permId + " status=" + statusCode); + } + Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); + res.releaseConnection(); + } + + private String createRole(String roleName) { + setupCreate("createRole"); + RoleClient roleClient = new RoleClient(); + + Role role = RoleFactory.createRoleInstance(roleName, + "role for " + roleName, true); + ClientResponse res = roleClient.create(role); + int statusCode = res.getStatus(); + if (logger.isDebugEnabled()) { + logger.debug("createRole: name=" + roleName + + " status = " + statusCode); + } + Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); + res.releaseConnection(); + return extractId(res); + } + + private void deleteRole(String roleId) { + setupDelete("deleteRole"); + RoleClient roleClient = new RoleClient(); + ClientResponse res = roleClient.delete(roleId); + int statusCode = res.getStatus(); + if (logger.isDebugEnabled()) { + logger.debug("deleteRole: delete role id=" + roleId + + " status=" + statusCode); + } + Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); + res.releaseConnection(); + } + + private String createAccount(String userName, String email) { + setupCreate("createAccount"); + AccountClient accClient = new AccountClient(); + AccountsCommon account = AccountFactory.createAccountInstance( + userName, userName, userName, email, + true, true, false, true, true); + ClientResponse res = accClient.create(account); + int statusCode = res.getStatus(); + if (logger.isDebugEnabled()) { + logger.debug("createAccount: userName=" + userName + + " status = " + statusCode); + } + Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); + res.releaseConnection(); + return extractId(res); + } + + private void deleteAccount(String accId) { + setupDelete("deleteAccount"); + AccountClient accClient = new AccountClient(); + ClientResponse res = accClient.delete(accId); + int statusCode = res.getStatus(); + if (logger.isDebugEnabled()) { + logger.debug("deleteAccount: delete account id=" + + accId + " status=" + statusCode); + } + Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); + res.releaseConnection(); + } + + private String createAccountRole(AccountValue av, + Collection rvs) { + setupCreate("createAccountRole"); + + // Submit the request to the service and store the response. + AccountRole accRole = AccountRoleFactory.createAccountRoleInstance( + av, rvs, true, true); + AccountRoleClient client = new AccountRoleClient(); + ClientResponse res = client.create(av.getAccountId(), accRole); + int statusCode = res.getStatus(); + + if (logger.isDebugEnabled()) { + logger.debug("createAccountRole: status = " + statusCode); + } + Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); + res.releaseConnection(); + return extractId(res); + } + + private void deleteAccountRole(String screenName) { + // Perform setup. + setupDelete("deleteAccountRole"); + + // Submit the request to the service and store the response. + AccountRoleClient client = new AccountRoleClient(); + ClientResponse res = client.delete( + accValues.get(screenName).getAccountId(), "123"); + int statusCode = res.getStatus(); + + // Check the status code of the response: does it match + // the expected response(s)? + if (logger.isDebugEnabled()) { + logger.debug("deleteAccountRole: status = " + statusCode); + } + Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); + res.releaseConnection(); + } + + private String createPermissionRole(PermissionValue pv, + Collection rvs) { + setupCreate("createPermissionRole"); + PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance( + pv, rvs, true, true); + PermissionRoleClient client = new PermissionRoleClient(); + ClientResponse res = client.create(pv.getPermissionId(), permRole); + int statusCode = res.getStatus(); + + if (logger.isDebugEnabled()) { + logger.debug("createPermissionRole: status = " + statusCode); + } + Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); + res.releaseConnection(); + return extractId(res); + } + + private void deletePermissionRole(String permId) { + + // Perform setup. + setupDelete("deletePermissionRole"); + + // Submit the request to the service and store the response. + PermissionRoleClient client = new PermissionRoleClient(); + ClientResponse res = client.delete(permId, "123"); + int statusCode = res.getStatus(); + + // Check the status code of the response: does it match + // the expected response(s)? + if (logger.isDebugEnabled()) { + logger.debug("deletePermissionRole : status = " + statusCode); + } + Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); + res.releaseConnection(); + } + + private void deletePermissionRoles() { + + //first delete relationships between the entities + for (PermissionValue pv : permValues.values()) { + deletePermissionRole(pv.getPermissionId()); + } + } + + private void deleteAccountRoles() { + for (AccountValue av : accValues.values()) { + deleteAccountRole(av.getUserId()); + } + } + + private void deletePermissions() { + //delete entities + for (PermissionValue pv : permValues.values()) { + deletePermission(pv.getPermissionId()); + } + } + + private void deleteRoles() { + for (RoleValue rv : roleValues.values()) { + deleteRole(rv.getRoleId()); + } + } + + private void deleteAccounts() { + + for (AccountValue av1 : accValues.values()) { + deleteAccount(av1.getAccountId()); + } + } +}