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
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
// 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);
// 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(
// 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 =
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.HashMap;
import java.util.List;
import javax.ws.rs.core.MediaType;
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;
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: \""
// 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();