]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-720 - Completed fixing refName creation.
authorPatrick Schmitz <pschmitz@berkeley.edu>
Thu, 14 Jan 2010 20:40:24 +0000 (20:40 +0000)
committerPatrick Schmitz <pschmitz@berkeley.edu>
Thu, 14 Jan 2010 20:40:24 +0000 (20:40 +0000)
services/vocabulary/client/src/main/java/org/collectionspace/services/client/VocabularyClientUtils.java
services/vocabulary/client/src/test/java/org/collectionspace/services/client/test/VocabularyServiceTest.java
services/vocabulary/import/src/main/java/org/collectionspace/services/vocabulary/importer/VocabularyBaseImport.java

index d482a8d00ec81932f56a21ebdb427c81ad1f6048..0689bd9caddbb4fc8ae98f52623ce6ec981765f1 100644 (file)
@@ -61,6 +61,40 @@ public class VocabularyClientUtils {
         return multipart;\r
     }\r
 \r
+    public static String createItemInVocabulary(String vcsid, \r
+               String vocabularyRefName, Map<String,String> itemMap,\r
+               VocabularyClient client ) {\r
+       // Expected status code: 201 Created\r
+       int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();\r
+       // Type of service request being tested\r
+       ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;\r
+       String displayName = itemMap.get(VocabularyItemJAXBSchema.DISPLAY_NAME);\r
+       String refName = createVocabularyItemRefName(vocabularyRefName, displayName, true);\r
+\r
+       if(logger.isDebugEnabled()){\r
+               logger.debug("Import: Create Item: \""+displayName\r
+                               +"\" in personAuthorityulary: \"" + vocabularyRefName +"\"");\r
+       }\r
+       MultipartOutput multipart = \r
+               createVocabularyItemInstance( vcsid, refName,\r
+                               itemMap, client.getItemCommonPartName() );\r
+       ClientResponse<Response> res = client.createItem(vcsid, multipart);\r
+\r
+       int statusCode = res.getStatus();\r
+\r
+       if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
+               throw new RuntimeException("Could not create Item: \""+refName\r
+                               +"\" in personAuthority: \"" + vocabularyRefName\r
+                               +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
+       }\r
+       if(statusCode != EXPECTED_STATUS_CODE) {\r
+               throw new RuntimeException("Unexpected Status when creating Item: \""+refName\r
+                               +"\" in personAuthority: \"" + vocabularyRefName +"\", Status:"+ statusCode);\r
+       }\r
+\r
+       return extractId(res);\r
+    }\r
+\r
     /**\r
      * Returns an error message indicating that the status code returned by a\r
      * specific call to a service does not fall within a set of valid status\r
@@ -92,14 +126,20 @@ public class VocabularyClientUtils {
         return id;\r
     }\r
     \r
-    public static String createVocabularyRefName(String vocabularyName) {\r
-       return "urn:cspace:org.collectionspace.demo:vocabulary:name("\r
+    public static String createVocabularyRefName(String vocabularyName, boolean withDisplaySuffix) {\r
+       String refName = "urn:cspace:org.collectionspace.demo:vocabulary:name("\r
                        +vocabularyName+")";\r
+       if(withDisplaySuffix)\r
+               refName += "'"+vocabularyName+"'";\r
+       return refName;\r
     }\r
 \r
     public static String createVocabularyItemRefName(\r
-                                               String vocabularyRefName, String vocabItemName) {\r
-       return vocabularyRefName+":item:name("+vocabItemName+")";\r
+                                               String vocabularyRefName, String vocabItemName, boolean withDisplaySuffix) {\r
+       String refName = vocabularyRefName+":item:name("+vocabItemName+")";\r
+       if(withDisplaySuffix)\r
+               refName += "'"+vocabItemName+"'";\r
+       return refName;\r
     }\r
 \r
 }\r
index 95fc62a6a9ed6a791ab2d92cdab0a7849e4faf8b..3f06913283330e16597a3c26de2343b515f74a85 100644 (file)
@@ -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<Response> 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<String, String> itemInfo = new HashMap<String, String>();
         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<MultipartInput> res =
index e448af7d8574b8e04884034cb840f1a555cc778a..c5ee6e83d54fbed4456c0daae9beae28fdd5b608 100644 (file)
@@ -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<Response> 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<String, String> itemInfo = new HashMap<String, String>();
+            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<Response> 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<Response> res) {
-        MultivaluedMap<String, Object> mvm = res.getMetadata();
-        String uri = (String) ((ArrayList<Object>) 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();