]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
NOJIRA Updated sample code to match refactered utils code.
authorPatrick Schmitz <pschmitz@berkeley.edu>
Fri, 12 Feb 2010 22:34:11 +0000 (22:34 +0000)
committerPatrick Schmitz <pschmitz@berkeley.edu>
Fri, 12 Feb 2010 22:34:11 +0000 (22:34 +0000)
services/vocabulary/sample/sample/src/main/java/org/collectionspace/services/vocabulary/client/sample/Sample.java

index f55a4a203a1ed0aa3708fd5c8b95dd14491e4b78..9bfcad16a355008c84bd78dd46908a032610b6ad 100644 (file)
@@ -25,6 +25,7 @@ package org.collectionspace.services.vocabulary.client.sample;
 \r
 import java.util.ArrayList;\r
 import java.util.Arrays;\r
+import java.util.HashMap;\r
 import java.util.List;\r
 \r
 import javax.ws.rs.core.MediaType;\r
@@ -32,7 +33,9 @@ import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.Response;\r
 \r
 import org.apache.log4j.BasicConfigurator;\r
+import org.collectionspace.services.VocabularyItemJAXBSchema;\r
 import org.collectionspace.services.client.VocabularyClient;\r
+import org.collectionspace.services.client.VocabularyClientUtils;\r
 import org.collectionspace.services.client.test.ServiceRequestType;\r
 import org.collectionspace.services.vocabulary.VocabulariesCommon;\r
 import org.collectionspace.services.vocabulary.VocabulariesCommonList;\r
@@ -74,16 +77,20 @@ public class Sample {
        // Type of service request being tested\r
        ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;\r
 \r
-       logger.info("Import: Create vocabulary: \"" + vocabName +"\"");\r
-       MultipartOutput multipart = createVocabularyInstance(vocabName, \r
-                       createRefName(vocabName), "enum");\r
+       if(logger.isDebugEnabled()){\r
+               logger.debug("Import: Create vocabulary: \"" + vocabName +"\"");\r
+       }\r
+       String baseVocabRefName = VocabularyClientUtils.createVocabularyRefName(vocabName, false);\r
+       String fullVocabRefName = VocabularyClientUtils.createVocabularyRefName(vocabName, true);\r
+       MultipartOutput multipart = VocabularyClientUtils.createEnumerationInstance(\r
+                       vocabName, fullVocabRefName, client.getCommonPartName());\r
        ClientResponse<Response> res = client.create(multipart);\r
 \r
        int statusCode = res.getStatus();\r
 \r
        if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
                throw new RuntimeException("Could not create enumeration: \""+vocabName\r
-                               +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
+                               +"\" "+ VocabularyClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
        }\r
        if(statusCode != EXPECTED_STATUS_CODE) {\r
                throw new RuntimeException("Unexpected Status when creating enumeration: \""\r
@@ -92,43 +99,19 @@ public class Sample {
 \r
        // Store the ID returned from this create operation\r
        // for additional tests below.\r
-       String newVocabId = extractId(res);\r
-        logger.info("Import: Created vocabulary: \"" + vocabName +"\" ID:"\r
+       String newVocabId = VocabularyClientUtils.extractId(res);\r
+       if(logger.isDebugEnabled()){\r
+               logger.debug("Import: Created vocabulary: \"" + vocabName +"\" ID:"\r
                                +newVocabId );\r
-        \r
-        // Add items to the vocabulary\r
+       }\r
        for(String itemName : enumValues){\r
-               createItemInVocab(newVocabId, vocabName, itemName, createRefName(itemName));\r
+            HashMap<String, String> itemInfo = new HashMap<String, String>();\r
+            itemInfo.put(VocabularyItemJAXBSchema.DISPLAY_NAME, itemName);\r
+               VocabularyClientUtils.createItemInVocabulary(newVocabId, \r
+                               baseVocabRefName, itemInfo, client);\r
        }\r
-        \r
     }\r
     \r
-    private String createItemInVocab(String vcsid, String vocabName, String itemName, String refName) {\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
-\r
-       logger.info("Import: Create Item: \""+itemName+"\" in vocabulary: \"" + vocabName +"\"");\r
-       MultipartOutput multipart = createVocabularyItemInstance(vcsid, itemName, refName);\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: \""+itemName\r
-                               +"\" in vocabulary: \"" + vocabName\r
-                               +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
-       }\r
-       if(statusCode != EXPECTED_STATUS_CODE) {\r
-               throw new RuntimeException("Unexpected Status when creating Item: \""+itemName\r
-                               +"\" in vocabulary: \"" + vocabName +"\", Status:"+ statusCode);\r
-       }\r
-\r
-       return extractId(res);\r
-    }\r
-\r
-\r
    // ---------------------------------------------------------------\r
    // Read\r
    // ---------------------------------------------------------------\r
@@ -147,7 +130,7 @@ public class Sample {
         int statusCode = res.getStatus();\r
        if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
                throw new RuntimeException("Could not read list of vocabularies: "\r
-                + invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
+                + VocabularyClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
        }\r
        if(statusCode != EXPECTED_STATUS_CODE) {\r
                throw new RuntimeException("Unexpected Status when reading " +\r
@@ -182,7 +165,7 @@ public class Sample {
             int statusCode = res.getStatus();\r
             if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
                 throw new RuntimeException("Could not read vocabulary"\r
-                    + invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
+                    + VocabularyClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
             }\r
             if(statusCode != EXPECTED_STATUS_CODE) {\r
                 throw new RuntimeException("Unexpected Status when reading " +\r
@@ -214,7 +197,7 @@ public class Sample {
 \r
        if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
                throw new RuntimeException("Could not read items in vocabulary: "\r
-                + invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
+                + VocabularyClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
        }\r
        if(statusCode != EXPECTED_STATUS_CODE) {\r
                throw new RuntimeException("Unexpected Status when reading " +\r
@@ -250,7 +233,7 @@ public class Sample {
 \r
        if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
                throw new RuntimeException("Could not delete vocabulary: "\r
-                + invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
+                + VocabularyClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
        }\r
        if(statusCode != EXPECTED_STATUS_CODE) {\r
                throw new RuntimeException("Unexpected Status when deleting " +\r
@@ -276,7 +259,7 @@ public class Sample {
 \r
        if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
                throw new RuntimeException("Could not delete vocabulary item: "\r
-                + invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
+                + VocabularyClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
        }\r
        if(statusCode != EXPECTED_STATUS_CODE) {\r
                throw new RuntimeException("Unexpected Status when deleting " +\r
@@ -295,41 +278,6 @@ public class Sample {
     // Utility methods used by tests above\r
     // ---------------------------------------------------------------\r
 \r
-    private MultipartOutput createVocabularyInstance(\r
-               String displayName, String refName, String vocabType) {\r
-        VocabulariesCommon vocabulary = new VocabulariesCommon();\r
-        vocabulary.setDisplayName(displayName);\r
-        vocabulary.setRefName(refName);\r
-        vocabulary.setVocabType(vocabType);\r
-        MultipartOutput multipart = new MultipartOutput();\r
-        OutputPart commonPart = multipart.addPart(vocabulary, MediaType.APPLICATION_XML_TYPE);\r
-        commonPart.getHeaders().add("label", client.getCommonPartName());\r
-\r
-        if(logger.isDebugEnabled()){\r
-               logger.debug("to be created, vocabulary common ",\r
-                                       vocabulary, VocabulariesCommon.class);\r
-        }\r
-\r
-        return multipart;\r
-    }\r
-\r
-    private MultipartOutput createVocabularyItemInstance(\r
-               String inVocabulary, String displayName, String refName) {\r
-       VocabularyitemsCommon vocabularyItem = new VocabularyitemsCommon();\r
-       vocabularyItem.setInVocabulary(inVocabulary);\r
-       vocabularyItem.setDisplayName(displayName);\r
-       vocabularyItem.setRefName(refName);\r
-        MultipartOutput multipart = new MultipartOutput();\r
-        OutputPart commonPart = multipart.addPart(vocabularyItem, MediaType.APPLICATION_XML_TYPE);\r
-        commonPart.getHeaders().add("label", client.getItemCommonPartName());\r
-\r
-        if(logger.isDebugEnabled()){\r
-               logger.debug("to be created, vocabularyitem common ", vocabularyItem, VocabularyitemsCommon.class);\r
-        }\r
-\r
-        return multipart;\r
-    }\r
-\r
     // Retrieve individual fields of vocabulary records.\r
 \r
     private String displayAllVocabularies(VocabulariesCommonList list) {\r
@@ -379,6 +327,7 @@ public class Sample {
         return sb.toString();\r
     }\r
 \r
+    // TODO this should be moved to a common utils class\r
     private Object extractPart(MultipartInput input, String label,\r
         Class clazz) throws Exception {\r
         Object obj = null;\r
@@ -399,41 +348,6 @@ public class Sample {
         return obj;\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
-     * codes for that service.\r
-     *\r
-     * @param serviceRequestType  A type of service request (e.g. CREATE, DELETE).\r
-     *\r
-     * @param statusCode  The invalid status code that was returned in the response,\r
-     *                    from submitting that type of request to the service.\r
-     *\r
-     * @return An error message.\r
-     */\r
-    protected String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {\r
-        return "Status code '" + statusCode + "' in response is NOT within the expected set: " +\r
-                requestType.validStatusCodesAsString();\r
-    }\r
-\r
-    protected String extractId(ClientResponse<Response> res) {\r
-        MultivaluedMap<String, Object> mvm = res.getMetadata();\r
-        String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);\r
-        if(logger.isDebugEnabled()){\r
-               logger.info("extractId:uri=" + uri);\r
-        }\r
-        String[] segments = uri.split("/");\r
-        String id = segments[segments.length - 1];\r
-        if(logger.isDebugEnabled()){\r
-               logger.debug("id=" + id);\r
-        }\r
-        return id;\r
-    }\r
-    \r
-    protected String createRefName(String displayName) {\r
-       return displayName.replaceAll("\\W", "");\r
-    }\r
-\r
        public static void main(String[] args) {\r
 \r
         // Configure logging.\r