From c6856bbdfc434cf9ca67b9aeea1fbb439ad91972 Mon Sep 17 00:00:00 2001 From: Patrick Schmitz Date: Thu, 14 Jan 2010 20:40:24 +0000 Subject: [PATCH] CSPACE-720 - Completed fixing refName creation. --- .../client/VocabularyClientUtils.java | 48 ++++++- .../client/test/VocabularyServiceTest.java | 6 +- .../importer/VocabularyBaseImport.java | 120 ++---------------- 3 files changed, 60 insertions(+), 114 deletions(-) diff --git a/services/vocabulary/client/src/main/java/org/collectionspace/services/client/VocabularyClientUtils.java b/services/vocabulary/client/src/main/java/org/collectionspace/services/client/VocabularyClientUtils.java index d482a8d00..0689bd9ca 100644 --- a/services/vocabulary/client/src/main/java/org/collectionspace/services/client/VocabularyClientUtils.java +++ b/services/vocabulary/client/src/main/java/org/collectionspace/services/client/VocabularyClientUtils.java @@ -61,6 +61,40 @@ public class VocabularyClientUtils { return multipart; } + public static String createItemInVocabulary(String vcsid, + String vocabularyRefName, Map itemMap, + VocabularyClient client ) { + // Expected status code: 201 Created + int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode(); + // Type of service request being tested + ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE; + String displayName = itemMap.get(VocabularyItemJAXBSchema.DISPLAY_NAME); + String refName = createVocabularyItemRefName(vocabularyRefName, displayName, true); + + if(logger.isDebugEnabled()){ + logger.debug("Import: Create Item: \""+displayName + +"\" in personAuthorityulary: \"" + vocabularyRefName +"\""); + } + MultipartOutput multipart = + createVocabularyItemInstance( vcsid, refName, + itemMap, client.getItemCommonPartName() ); + ClientResponse res = client.createItem(vcsid, multipart); + + int statusCode = res.getStatus(); + + if(!REQUEST_TYPE.isValidStatusCode(statusCode)) { + throw new RuntimeException("Could not create Item: \""+refName + +"\" in personAuthority: \"" + vocabularyRefName + +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + } + if(statusCode != EXPECTED_STATUS_CODE) { + throw new RuntimeException("Unexpected Status when creating Item: \""+refName + +"\" in personAuthority: \"" + vocabularyRefName +"\", Status:"+ statusCode); + } + + return extractId(res); + } + /** * 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 @@ -92,14 +126,20 @@ public class VocabularyClientUtils { return id; } - public static String createVocabularyRefName(String vocabularyName) { - return "urn:cspace:org.collectionspace.demo:vocabulary:name(" + public static String createVocabularyRefName(String vocabularyName, boolean withDisplaySuffix) { + String refName = "urn:cspace:org.collectionspace.demo:vocabulary:name(" +vocabularyName+")"; + if(withDisplaySuffix) + refName += "'"+vocabularyName+"'"; + return refName; } public static String createVocabularyItemRefName( - String vocabularyRefName, String vocabItemName) { - return vocabularyRefName+":item:name("+vocabItemName+")"; + String vocabularyRefName, String vocabItemName, boolean withDisplaySuffix) { + String refName = vocabularyRefName+":item:name("+vocabItemName+")"; + if(withDisplaySuffix) + refName += "'"+vocabItemName+"'"; + return refName; } } diff --git a/services/vocabulary/client/src/test/java/org/collectionspace/services/client/test/VocabularyServiceTest.java b/services/vocabulary/client/src/test/java/org/collectionspace/services/client/test/VocabularyServiceTest.java index 95fc62a6a..3f0691328 100644 --- a/services/vocabulary/client/src/test/java/org/collectionspace/services/client/test/VocabularyServiceTest.java +++ b/services/vocabulary/client/src/test/java/org/collectionspace/services/client/test/VocabularyServiceTest.java @@ -92,7 +92,7 @@ public class VocabularyServiceTest extends AbstractServiceTest { // Submit the request to the service and store the response. String identifier = createIdentifier(); String displayName = "displayName-" + identifier; - String refName = VocabularyClientUtils.createVocabularyRefName(displayName); + String refName = VocabularyClientUtils.createVocabularyRefName(displayName, false); MultipartOutput multipart = VocabularyClientUtils.createEnumerationInstance( displayName, refName, client.getCommonPartName()); ClientResponse res = client.create(multipart); @@ -146,7 +146,7 @@ public class VocabularyServiceTest extends AbstractServiceTest { // Submit the request to the service and store the response. String identifier = createIdentifier(); String refName = VocabularyClientUtils.createVocabularyItemRefName( - vocabRefName, identifier); + vocabRefName, identifier, true); HashMap itemInfo = new HashMap(); itemInfo.put(VocabularyItemJAXBSchema.DISPLAY_NAME, identifier); MultipartOutput multipart = VocabularyClientUtils.createVocabularyItemInstance( @@ -767,7 +767,7 @@ public class VocabularyServiceTest extends AbstractServiceTest { // The only relevant ID may be the one used in update(), below. String displayName = "displayName-" + NON_EXISTENT_ID; - String refName = VocabularyClientUtils.createVocabularyRefName(displayName); + String refName = VocabularyClientUtils.createVocabularyRefName(displayName, false); MultipartOutput multipart = VocabularyClientUtils.createEnumerationInstance( displayName, refName, client.getCommonPartName()); ClientResponse res = diff --git a/services/vocabulary/import/src/main/java/org/collectionspace/services/vocabulary/importer/VocabularyBaseImport.java b/services/vocabulary/import/src/main/java/org/collectionspace/services/vocabulary/importer/VocabularyBaseImport.java index e448af7d8..c5ee6e83d 100644 --- a/services/vocabulary/import/src/main/java/org/collectionspace/services/vocabulary/importer/VocabularyBaseImport.java +++ b/services/vocabulary/import/src/main/java/org/collectionspace/services/vocabulary/importer/VocabularyBaseImport.java @@ -25,6 +25,7 @@ package org.collectionspace.services.vocabulary.importer; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.List; import javax.ws.rs.core.MediaType; @@ -32,7 +33,9 @@ import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; import org.apache.log4j.BasicConfigurator; +import org.collectionspace.services.VocabularyItemJAXBSchema; import org.collectionspace.services.client.VocabularyClient; +import org.collectionspace.services.client.VocabularyClientUtils; import org.collectionspace.services.client.test.ServiceRequestType; import org.collectionspace.services.vocabulary.VocabulariesCommon; import org.collectionspace.services.vocabulary.VocabularyitemsCommon; @@ -68,15 +71,17 @@ public class VocabularyBaseImport { if(logger.isDebugEnabled()){ logger.debug("Import: Create vocabulary: \"" + vocabName +"\""); } - MultipartOutput multipart = createVocabularyInstance(vocabName, - createRefName(vocabName), "enum"); + String baseVocabRefName = VocabularyClientUtils.createVocabularyRefName(vocabName, false); + String fullVocabRefName = baseVocabRefName+"'"+vocabName+"'"; + MultipartOutput multipart = VocabularyClientUtils.createEnumerationInstance( + vocabName, fullVocabRefName, client.getCommonPartName()); ClientResponse res = client.create(multipart); int statusCode = res.getStatus(); if(!REQUEST_TYPE.isValidStatusCode(statusCode)) { throw new RuntimeException("Could not create enumeration: \""+vocabName - +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + +"\" "+ VocabularyClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); } if(statusCode != EXPECTED_STATUS_CODE) { throw new RuntimeException("Unexpected Status when creating enumeration: \"" @@ -85,118 +90,19 @@ public class VocabularyBaseImport { // Store the ID returned from this create operation // for additional tests below. - String newVocabId = extractId(res); + String newVocabId = VocabularyClientUtils.extractId(res); if(logger.isDebugEnabled()){ logger.debug("Import: Created vocabulary: \"" + vocabName +"\" ID:" +newVocabId ); } for(String itemName : enumValues){ - createItemInVocab(newVocabId, vocabName, itemName, createRefName(itemName)); + HashMap itemInfo = new HashMap(); + itemInfo.put(VocabularyItemJAXBSchema.DISPLAY_NAME, itemName); + VocabularyClientUtils.createItemInVocabulary(newVocabId, + baseVocabRefName, itemInfo, client); } } - private String createItemInVocab(String vcsid, String vocabName, String itemName, String refName) { - // Expected status code: 201 Created - int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode(); - // Type of service request being tested - ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE; - - if(logger.isDebugEnabled()){ - logger.debug("Import: Create Item: \""+itemName+"\" in vocabulary: \"" + vocabName +"\""); - } - MultipartOutput multipart = createVocabularyItemInstance(vcsid, itemName, refName); - ClientResponse res = client.createItem(vcsid, multipart); - - int statusCode = res.getStatus(); - - if(!REQUEST_TYPE.isValidStatusCode(statusCode)) { - throw new RuntimeException("Could not create Item: \""+itemName - +"\" in vocabulary: \"" + vocabName - +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); - } - if(statusCode != EXPECTED_STATUS_CODE) { - throw new RuntimeException("Unexpected Status when creating Item: \""+itemName - +"\" in vocabulary: \"" + vocabName +"\", Status:"+ statusCode); - } - - return extractId(res); - } - - // --------------------------------------------------------------- - // Utility methods used by tests above - // --------------------------------------------------------------- - - private MultipartOutput createVocabularyInstance( - String displayName, String refName, String vocabType) { - VocabulariesCommon vocabulary = new VocabulariesCommon(); - vocabulary.setDisplayName(displayName); - vocabulary.setRefName(refName); - vocabulary.setVocabType(vocabType); - MultipartOutput multipart = new MultipartOutput(); - OutputPart commonPart = multipart.addPart(vocabulary, MediaType.APPLICATION_XML_TYPE); - commonPart.getHeaders().add("label", client.getCommonPartName()); - - if(logger.isDebugEnabled()){ - logger.debug("to be created, vocabulary common ", - vocabulary, VocabulariesCommon.class); - } - - return multipart; - } - - private MultipartOutput createVocabularyItemInstance( - String inVocabulary, String displayName, String refName) { - VocabularyitemsCommon vocabularyItem = new VocabularyitemsCommon(); - vocabularyItem.setInVocabulary(inVocabulary); - vocabularyItem.setDisplayName(displayName); - vocabularyItem.setRefName(refName); - MultipartOutput multipart = new MultipartOutput(); - OutputPart commonPart = multipart.addPart(vocabularyItem, MediaType.APPLICATION_XML_TYPE); - commonPart.getHeaders().add("label", client.getItemCommonPartName()); - - if(logger.isDebugEnabled()){ - logger.debug("to be created, vocabularyitem common ", vocabularyItem, VocabularyitemsCommon.class); - } - - 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. - */ - 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 createRefName(String displayName) { - return displayName.replaceAll("\\W", ""); - } - public static void main(String[] args) { BasicConfigurator.configure(); -- 2.47.3