From ecb6d67745f01dc2748b968db34c4f594acd6929 Mon Sep 17 00:00:00 2001 From: Richard Millet Date: Mon, 23 Nov 2009 20:47:09 +0000 Subject: [PATCH] CSPACE-610: Adding a sample for the Vocabulary service's Java client API. --- services/vocabulary/pom.xml | 1 + services/vocabulary/sample/.classpath | 6 + services/vocabulary/sample/.project | 23 ++ .../.settings/org.eclipse.jdt.core.prefs | 5 + .../.settings/org.maven.ide.eclipse.prefs | 9 + services/vocabulary/sample/pom.xml | 14 ++ services/vocabulary/sample/sample/.classpath | 10 + services/vocabulary/sample/sample/.project | 27 ++ .../.settings/org.eclipse.jdt.core.prefs | 5 + .../.settings/org.maven.ide.eclipse.prefs | 9 + services/vocabulary/sample/sample/pom.xml | 90 +++++++ .../vocabulary/client/sample/Sample.java | 230 ++++++++++++++++++ .../collectionspace-client.properties | 6 + 13 files changed, 435 insertions(+) create mode 100644 services/vocabulary/sample/.classpath create mode 100644 services/vocabulary/sample/.project create mode 100644 services/vocabulary/sample/.settings/org.eclipse.jdt.core.prefs create mode 100644 services/vocabulary/sample/.settings/org.maven.ide.eclipse.prefs create mode 100644 services/vocabulary/sample/pom.xml create mode 100644 services/vocabulary/sample/sample/.classpath create mode 100644 services/vocabulary/sample/sample/.project create mode 100644 services/vocabulary/sample/sample/.settings/org.eclipse.jdt.core.prefs create mode 100644 services/vocabulary/sample/sample/.settings/org.maven.ide.eclipse.prefs create mode 100644 services/vocabulary/sample/sample/pom.xml create mode 100644 services/vocabulary/sample/sample/src/main/java/org/collectionspace/services/vocabulary/client/sample/Sample.java create mode 100644 services/vocabulary/sample/sample/src/main/resources/collectionspace-client.properties diff --git a/services/vocabulary/pom.xml b/services/vocabulary/pom.xml index 19943eca4..8b0d325bf 100644 --- a/services/vocabulary/pom.xml +++ b/services/vocabulary/pom.xml @@ -18,6 +18,7 @@ 3rdparty client import + sample diff --git a/services/vocabulary/sample/.classpath b/services/vocabulary/sample/.classpath new file mode 100644 index 000000000..d8f291998 --- /dev/null +++ b/services/vocabulary/sample/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/services/vocabulary/sample/.project b/services/vocabulary/sample/.project new file mode 100644 index 000000000..cdab43654 --- /dev/null +++ b/services/vocabulary/sample/.project @@ -0,0 +1,23 @@ + + + org.collectionspace.services.vocabulary.samples + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.maven.ide.eclipse.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.maven.ide.eclipse.maven2Nature + + diff --git a/services/vocabulary/sample/.settings/org.eclipse.jdt.core.prefs b/services/vocabulary/sample/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 000000000..9fa2bf3ac --- /dev/null +++ b/services/vocabulary/sample/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,5 @@ +#Mon Nov 23 11:54:01 PST 2009 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.2 +org.eclipse.jdt.core.compiler.compliance=1.4 +org.eclipse.jdt.core.compiler.source=1.3 diff --git a/services/vocabulary/sample/.settings/org.maven.ide.eclipse.prefs b/services/vocabulary/sample/.settings/org.maven.ide.eclipse.prefs new file mode 100644 index 000000000..e3f71bde4 --- /dev/null +++ b/services/vocabulary/sample/.settings/org.maven.ide.eclipse.prefs @@ -0,0 +1,9 @@ +#Mon Nov 23 11:53:50 PST 2009 +activeProfiles= +eclipse.preferences.version=1 +fullBuildGoals=process-test-resources +includeModules=false +resolveWorkspaceProjects=true +resourceFilterGoals=process-resources resources\:testResources +skipCompilerPlugin=true +version=1 diff --git a/services/vocabulary/sample/pom.xml b/services/vocabulary/sample/pom.xml new file mode 100644 index 000000000..e5aec84a3 --- /dev/null +++ b/services/vocabulary/sample/pom.xml @@ -0,0 +1,14 @@ + + + 4.0.0 + org.collectionspace.services + org.collectionspace.services.vocabulary.client.samples + pom + 1.0 + vocabulary.client.samples + + + sample + + + diff --git a/services/vocabulary/sample/sample/.classpath b/services/vocabulary/sample/sample/.classpath new file mode 100644 index 000000000..425cd1620 --- /dev/null +++ b/services/vocabulary/sample/sample/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/services/vocabulary/sample/sample/.project b/services/vocabulary/sample/sample/.project new file mode 100644 index 000000000..da9d6526e --- /dev/null +++ b/services/vocabulary/sample/sample/.project @@ -0,0 +1,27 @@ + + + org.collectionspace.services.vocabulary.client.sample + + + org.collectionspace.services.client + org.collectionspace.services.vocabulary.client + org.collectionspace.services.vocabulary.jaxb + org.collectionspace.services.common + + + + org.eclipse.jdt.core.javabuilder + + + + + org.maven.ide.eclipse.maven2Builder + + + + + + org.maven.ide.eclipse.maven2Nature + org.eclipse.jdt.core.javanature + + diff --git a/services/vocabulary/sample/sample/.settings/org.eclipse.jdt.core.prefs b/services/vocabulary/sample/sample/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 000000000..8ee16c274 --- /dev/null +++ b/services/vocabulary/sample/sample/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,5 @@ +#Fri Oct 02 14:55:57 PDT 2009 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/services/vocabulary/sample/sample/.settings/org.maven.ide.eclipse.prefs b/services/vocabulary/sample/sample/.settings/org.maven.ide.eclipse.prefs new file mode 100644 index 000000000..0624c1a1a --- /dev/null +++ b/services/vocabulary/sample/sample/.settings/org.maven.ide.eclipse.prefs @@ -0,0 +1,9 @@ +#Wed Sep 16 16:14:17 PDT 2009 +activeProfiles= +eclipse.preferences.version=1 +fullBuildGoals=process-test-resources +includeModules=false +resolveWorkspaceProjects=true +resourceFilterGoals=process-resources resources\:testResources +skipCompilerPlugin=true +version=1 diff --git a/services/vocabulary/sample/sample/pom.xml b/services/vocabulary/sample/sample/pom.xml new file mode 100644 index 000000000..47c331e0e --- /dev/null +++ b/services/vocabulary/sample/sample/pom.xml @@ -0,0 +1,90 @@ + + + 4.0.0 + org.collectionspace.services + org.collectionspace.services.vocabulary.client.sample + jar + 1.0 + vocabulary.client.sample + + + + org.collectionspace.services + org.collectionspace.services.vocabulary.jaxb + 1.0 + + + org.collectionspace.services + org.collectionspace.services.client + 1.0 + + + org.collectionspace.services + org.collectionspace.services.vocabulary.client + 1.0 + + + + + + libs-releases-local + libs-releases-local + http://source.collectionspace.org:8081/artifactory/libs-releases-local + + true + + + false + + + + + libs-snapshots-local + libs-snapshots-local + http://source.collectionspace.org:8081/artifactory/libs-snapshots-local + + false + + + true + + + + collectionspace-releases + collectionspace-releases + http://source.collectionspace.org:8081/artifactory/public-nuxeo + + true + + + false + + + + collectionspace-snapshots + collectionspace-snapshots + http://source.collectionspace.org:8081/artifactory/public-snapshot-nuxeo + + false + + + true + + + + + + vocabulary-client-sample + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.6 + 1.6 + + + + + + diff --git a/services/vocabulary/sample/sample/src/main/java/org/collectionspace/services/vocabulary/client/sample/Sample.java b/services/vocabulary/sample/sample/src/main/java/org/collectionspace/services/vocabulary/client/sample/Sample.java new file mode 100644 index 000000000..ca54c6078 --- /dev/null +++ b/services/vocabulary/sample/sample/src/main/java/org/collectionspace/services/vocabulary/client/sample/Sample.java @@ -0,0 +1,230 @@ +/** + * This document is a part of the source code and related artifacts + * for CollectionSpace, an open source collections management system + * for museums and related institutions: + * + * http://www.collectionspace.org + * http://wiki.collectionspace.org + * + * Copyright (c)) 2009 Regents of the University of California + * + * Licensed under the Educational Community License (ECL), Version 2.0. + * You may not use this file except in compliance with this License. + * + * You may obtain a copy of the ECL 2.0 License at + * https://source.collectionspace.org/collection-space/LICENSE.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.collectionspace.services.vocabulary.client.sample; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.Response; + +import org.apache.log4j.BasicConfigurator; +import org.collectionspace.services.client.VocabularyClient; +import org.collectionspace.services.client.test.ServiceRequestType; +import org.collectionspace.services.vocabulary.VocabulariesCommon; +import org.collectionspace.services.vocabulary.VocabularyitemsCommon; +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; +import org.slf4j.LoggerFactory; + +/** + * VocabularyServiceTest, carries out tests against a + * deployed and running Vocabulary Service. + * + * $LastChangedRevision: 753 $ + * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $ + */ +public class Sample { + private static final Logger logger = + LoggerFactory.getLogger(Sample.class); + + // Instance variables specific to this test. + private VocabularyClient client = new VocabularyClient(); + final String SERVICE_PATH_COMPONENT = "vocabularies"; + final String ITEM_SERVICE_PATH_COMPONENT = "items"; + + public void createEnumeration(String vocabName, List enumValues ) { + + // 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 vocabulary: \"" + vocabName +"\""); + } + MultipartOutput multipart = createVocabularyInstance(vocabName, + createRefName(vocabName), "enum"); + 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)); + } + if(statusCode != EXPECTED_STATUS_CODE) { + throw new RuntimeException("Unexpected Status when creating enumeration: \"" + +vocabName +"\", Status:"+ statusCode); + } + + // Store the ID returned from this create operation + // for additional tests below. + String newVocabId = extractId(res); + if(logger.isDebugEnabled()){ + logger.debug("Import: Created vocabulary: \"" + vocabName +"\" ID:" + +newVocabId ); + } + for(String itemName : enumValues){ + createItemInVocab(newVocabId, vocabName, itemName, createRefName(itemName)); + } + } + + 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(); + logger.info("VocabularyBaseImport starting..."); + + Sample vbi = new Sample(); + final String acquisitionMethodsVocabName = "Acquisition Methods"; + final String entryMethodsVocabName = "Entry Methods"; + final String entryReasonsVocabName = "Entry Reasons"; + final String responsibleDeptsVocabName = "Responsible Departments"; + + List acquisitionMethodsEnumValues = + Arrays.asList("Gift","Purchase","Exchange","Transfer","Treasure"); + List entryMethodsEnumValues = + Arrays.asList("In person","Post","Found on doorstep"); + List entryReasonsEnumValues = + Arrays.asList("Enquiry","Commission","Loan"); + List respDeptNamesEnumValues = + Arrays.asList("Antiquities","Architecture and Design","Decorative Arts", + "Ethnography","Herpetology","Media and Performance Art", + "Paintings and Sculpture","Paleobotany","Photographs", + "Prints and Drawings"); + + vbi.createEnumeration(acquisitionMethodsVocabName, acquisitionMethodsEnumValues); + vbi.createEnumeration(entryMethodsVocabName, entryMethodsEnumValues); + vbi.createEnumeration(entryReasonsVocabName, entryReasonsEnumValues); + vbi.createEnumeration(responsibleDeptsVocabName, respDeptNamesEnumValues); + + logger.info("VocabularyBaseImport complete."); + } +} diff --git a/services/vocabulary/sample/sample/src/main/resources/collectionspace-client.properties b/services/vocabulary/sample/sample/src/main/resources/collectionspace-client.properties new file mode 100644 index 000000000..429b9f6fe --- /dev/null +++ b/services/vocabulary/sample/sample/src/main/resources/collectionspace-client.properties @@ -0,0 +1,6 @@ +#url of the collectionspace server +cspace.url=http://localhost:8180/cspace-services/ +cspace.ssl=false +cspace.auth=false +cspace.user=test +cspace.password=test \ No newline at end of file -- 2.47.3