From: Patrick Schmitz Date: Thu, 14 Jan 2010 01:52:24 +0000 (+0000) Subject: CSPACE-725 - More refactoring of common client work. X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=5a6e3e0f2f2453790acf2824d4d6480db9fd2a4f;p=tmp%2Fjakarta-migration.git CSPACE-725 - More refactoring of common client work. --- diff --git a/services/organization/client/src/main/java/org/collectionspace/services/client/OrgAuthorityClientUtils.java b/services/organization/client/src/main/java/org/collectionspace/services/client/OrgAuthorityClientUtils.java index 246917e43..dd21227b5 100644 --- a/services/organization/client/src/main/java/org/collectionspace/services/client/OrgAuthorityClientUtils.java +++ b/services/organization/client/src/main/java/org/collectionspace/services/client/OrgAuthorityClientUtils.java @@ -1,13 +1,18 @@ package org.collectionspace.services.client; +import java.util.ArrayList; import java.util.List; import java.util.Map; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.Response; import org.collectionspace.services.OrganizationJAXBSchema; +import org.collectionspace.services.client.test.ServiceRequestType; import org.collectionspace.services.organization.OrganizationsCommon; import org.collectionspace.services.organization.OrgauthoritiesCommon; +import org.jboss.resteasy.client.ClientResponse; import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput; import org.jboss.resteasy.plugins.providers.multipart.OutputPart; import org.slf4j.Logger; @@ -71,4 +76,49 @@ public class OrgAuthorityClientUtils { return multipart; } + /** + * Returns an error message indicating that the status code returned by a + * specific call to a service does not fall within a set of valid status + * codes for that service. + * + * @param serviceRequestType A type of service request (e.g. CREATE, DELETE). + * + * @param statusCode The invalid status code that was returned in the response, + * from submitting that type of request to the service. + * + * @return An error message. + */ + public static String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) { + return "Status code '" + statusCode + "' in response is NOT within the expected set: " + + requestType.validStatusCodesAsString(); + } + + public static String extractId(ClientResponse res) { + MultivaluedMap mvm = res.getMetadata(); + String uri = (String) ((ArrayList) mvm.get("Location")).get(0); + if(logger.isDebugEnabled()){ + logger.info("extractId:uri=" + uri); + } + String[] segments = uri.split("/"); + String id = segments[segments.length - 1]; + if(logger.isDebugEnabled()){ + logger.debug("id=" + id); + } + return id; + } + + public static String createRefName(String displayName) { + return displayName.replaceAll("\\W", ""); + } + + public static String createOrgAuthRefName(String orgAuthorityName) { + return "urn:cspace:org.collectionspace.demo:orgauthority:name(" + +orgAuthorityName+")"; + } + + public static String createOrganizationRefName( + String orgAuthRefName, String orgName) { + return orgAuthRefName+":organization:name("+orgName+")"; + } + } diff --git a/services/organization/import/src/main/java/org/collectionspace/services/organization/importer/OrgAuthorityBaseImport.java b/services/organization/import/src/main/java/org/collectionspace/services/organization/importer/OrgAuthorityBaseImport.java index 0d05865d8..c7c6883bf 100644 --- a/services/organization/import/src/main/java/org/collectionspace/services/organization/importer/OrgAuthorityBaseImport.java +++ b/services/organization/import/src/main/java/org/collectionspace/services/organization/importer/OrgAuthorityBaseImport.java @@ -69,7 +69,7 @@ public class OrgAuthorityBaseImport { if(logger.isDebugEnabled()){ logger.debug("Import: Create orgAuthority: \"" + orgAuthorityName +"\""); } - String baseOrgAuthRefName = createOrgAuthRefName(orgAuthorityName); + String baseOrgAuthRefName = OrgAuthorityClientUtils.createOrgAuthRefName(orgAuthorityName); String fullOrgAuthRefName = baseOrgAuthRefName+"'"+orgAuthorityName+"'"; MultipartOutput multipart = OrgAuthorityClientUtils.createOrgAuthorityInstance( @@ -81,7 +81,7 @@ public class OrgAuthorityBaseImport { if(!REQUEST_TYPE.isValidStatusCode(statusCode)) { throw new RuntimeException("Could not create enumeration: \""+orgAuthorityName - +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + +"\" "+ OrgAuthorityClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); } if(statusCode != EXPECTED_STATUS_CODE) { throw new RuntimeException("Unexpected Status when creating enumeration: \"" @@ -90,7 +90,7 @@ public class OrgAuthorityBaseImport { // Store the ID returned from this create operation // for additional tests below. - String newOrgAuthorityId = extractId(res); + String newOrgAuthorityId = OrgAuthorityClientUtils.extractId(res); if(logger.isDebugEnabled()){ logger.debug("Import: Created orgAuthorityulary: \"" + orgAuthorityName +"\" ID:" +newOrgAuthorityId ); @@ -108,7 +108,7 @@ public class OrgAuthorityBaseImport { // Type of service request being tested ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE; String shortName = orgInfo.get(OrganizationJAXBSchema.SHORT_NAME); - String refName = createOrganizationRefName( + String refName = OrgAuthorityClientUtils.createOrganizationRefName( orgAuthorityRefName, shortName)+"'"+shortName+"'"; if(logger.isDebugEnabled()){ @@ -125,61 +125,20 @@ public class OrgAuthorityBaseImport { if(!REQUEST_TYPE.isValidStatusCode(statusCode)) { throw new RuntimeException("Could not create Item: \""+shortName +"\" in orgAuthority: \"" + orgAuthorityRefName - +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + +"\" "+ OrgAuthorityClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); } if(statusCode != EXPECTED_STATUS_CODE) { throw new RuntimeException("Unexpected Status when creating Item: \""+shortName +"\" in orgAuthority: \"" + orgAuthorityRefName +"\", Status:"+ statusCode); } - return extractId(res); + return OrgAuthorityClientUtils.extractId(res); } // --------------------------------------------------------------- // Utility methods used by methods above // --------------------------------------------------------------- - /** - * Returns an error message indicating that the status code returned by a - * specific call to a service does not fall within a set of valid status - * codes for that service. - * - * @param serviceRequestType A type of service request (e.g. CREATE, DELETE). - * - * @param statusCode The invalid status code that was returned in the response, - * from submitting that type of request to the service. - * - * @return An error message. - */ - protected String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) { - return "Status code '" + statusCode + "' in response is NOT within the expected set: " + - requestType.validStatusCodesAsString(); - } - - protected String extractId(ClientResponse res) { - MultivaluedMap mvm = res.getMetadata(); - String uri = (String) ((ArrayList) mvm.get("Location")).get(0); - if(logger.isDebugEnabled()){ - logger.debug("extractId:uri=" + uri); - } - String[] segments = uri.split("/"); - String id = segments[segments.length - 1]; - if(logger.isDebugEnabled()){ - logger.debug("id=" + id); - } - return id; - } - - protected String createOrgAuthRefName(String orgAuthorityName) { - return "urn:cspace:org.collectionspace.demo:orgauthority:name(" - +orgAuthorityName+")"; - } - - protected String createOrganizationRefName( - String orgAuthRefName, String orgName) { - return orgAuthRefName+":organization:name("+orgName+")"; - } - public static void main(String[] args) { BasicConfigurator.configure(); diff --git a/services/organization/sample/sample/src/main/java/org/collectionspace/services/organization/client/sample/Sample.java b/services/organization/sample/sample/src/main/java/org/collectionspace/services/organization/client/sample/Sample.java index f4b6db598..91602d8c7 100644 --- a/services/organization/sample/sample/src/main/java/org/collectionspace/services/organization/client/sample/Sample.java +++ b/services/organization/sample/sample/src/main/java/org/collectionspace/services/organization/client/sample/Sample.java @@ -102,7 +102,7 @@ public class Sample { if(!REQUEST_TYPE.isValidStatusCode(statusCode)) { throw new RuntimeException("Could not create enumeration: \""+orgAuthName - +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + +"\" "+ OrgAuthorityClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); } if(statusCode != EXPECTED_STATUS_CODE) { throw new RuntimeException("Unexpected Status when creating enumeration: \"" @@ -111,7 +111,7 @@ public class Sample { // Store the ID returned from this create operation // for additional tests below. - String newOrgAuthId = extractId(res); + String newOrgAuthId = OrgAuthorityClientUtils.extractId(res); logger.info("Import: Created orgAuthority: \"" + orgAuthName +"\" ID:" +newOrgAuthId ); @@ -146,7 +146,7 @@ public class Sample { if(!REQUEST_TYPE.isValidStatusCode(statusCode)) { throw new RuntimeException("Could not create Item: \""+shortName +"\" in orgAuthority: \"" + orgAuthorityRefName - +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + +"\" "+ OrgAuthorityClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); } if(statusCode != EXPECTED_STATUS_CODE) { throw new RuntimeException("Unexpected Status when creating Item: \""+shortName @@ -154,7 +154,7 @@ public class Sample { "\", Status:"+ statusCode); } - return extractId(res); + return OrgAuthorityClientUtils.extractId(res); } @@ -176,7 +176,7 @@ public class Sample { int statusCode = res.getStatus(); if(!REQUEST_TYPE.isValidStatusCode(statusCode)) { throw new RuntimeException("Could not read list of orgAuthorities: " - + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + + OrgAuthorityClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); } if(statusCode != EXPECTED_STATUS_CODE) { throw new RuntimeException("Unexpected Status when reading " + @@ -211,7 +211,7 @@ public class Sample { int statusCode = res.getStatus(); if(!REQUEST_TYPE.isValidStatusCode(statusCode)) { throw new RuntimeException("Could not read orgAuthority" - + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + + OrgAuthorityClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); } if(statusCode != EXPECTED_STATUS_CODE) { throw new RuntimeException("Unexpected Status when reading " + @@ -243,7 +243,7 @@ public class Sample { if(!REQUEST_TYPE.isValidStatusCode(statusCode)) { throw new RuntimeException("Could not read items in orgAuthority: " - + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + + OrgAuthorityClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); } if(statusCode != EXPECTED_STATUS_CODE) { throw new RuntimeException("Unexpected Status when reading " + @@ -279,7 +279,7 @@ public class Sample { if(!REQUEST_TYPE.isValidStatusCode(statusCode)) { throw new RuntimeException("Could not delete orgAuthority: " - + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + + OrgAuthorityClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); } if(statusCode != EXPECTED_STATUS_CODE) { throw new RuntimeException("Unexpected Status when deleting " + @@ -305,7 +305,7 @@ public class Sample { if(!REQUEST_TYPE.isValidStatusCode(statusCode)) { throw new RuntimeException("Could not delete orgAuthority item: " - + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + + OrgAuthorityClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); } if(statusCode != EXPECTED_STATUS_CODE) { throw new RuntimeException("Unexpected Status when deleting " + @@ -394,41 +394,6 @@ public class Sample { return obj; } - /** - * Returns an error message indicating that the status code returned by a - * specific call to a service does not fall within a set of valid status - * codes for that service. - * - * @param serviceRequestType A type of service request (e.g. CREATE, DELETE). - * - * @param statusCode The invalid status code that was returned in the response, - * from submitting that type of request to the service. - * - * @return An error message. - */ - protected String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) { - return "Status code '" + statusCode + "' in response is NOT within the expected set: " + - requestType.validStatusCodesAsString(); - } - - protected String extractId(ClientResponse res) { - MultivaluedMap mvm = res.getMetadata(); - String uri = (String) ((ArrayList) mvm.get("Location")).get(0); - if(logger.isDebugEnabled()){ - logger.info("extractId:uri=" + uri); - } - String[] segments = uri.split("/"); - String id = segments[segments.length - 1]; - if(logger.isDebugEnabled()){ - logger.debug("id=" + id); - } - return id; - } - - protected String createRefName(String displayName) { - return displayName.replaceAll("\\W", ""); - } - public static void main(String[] args) { // Configure logging.