]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c5ee6e83d54fbed4456c0daae9beae28fdd5b608
[tmp/jakarta-migration.git] /
1 /**
2  * This document is a part of the source code and related artifacts
3  * for CollectionSpace, an open source collections management system
4  * for museums and related institutions:
5  *
6  * http://www.collectionspace.org
7  * http://wiki.collectionspace.org
8  *
9  * Copyright (c)) 2009 Regents of the University of California
10  *
11  * Licensed under the Educational Community License (ECL), Version 2.0.
12  * You may not use this file except in compliance with this License.
13  *
14  * You may obtain a copy of the ECL 2.0 License at
15  * https://source.collectionspace.org/collection-space/LICENSE.txt
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */
23
24 package org.collectionspace.services.vocabulary.importer;
25
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.HashMap;
29 import java.util.List;
30
31 import javax.ws.rs.core.MediaType;
32 import javax.ws.rs.core.MultivaluedMap;
33 import javax.ws.rs.core.Response;
34
35 import org.apache.log4j.BasicConfigurator;
36 import org.collectionspace.services.VocabularyItemJAXBSchema;
37 import org.collectionspace.services.client.VocabularyClient;
38 import org.collectionspace.services.client.VocabularyClientUtils;
39 import org.collectionspace.services.client.test.ServiceRequestType;
40 import org.collectionspace.services.vocabulary.VocabulariesCommon;
41 import org.collectionspace.services.vocabulary.VocabularyitemsCommon;
42 import org.jboss.resteasy.client.ClientResponse;
43 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
44 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 /**
49  * VocabularyServiceTest, carries out tests against a
50  * deployed and running Vocabulary Service.
51  *
52  * $LastChangedRevision: 753 $
53  * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
54  */
55 public class VocabularyBaseImport {
56     private static final Logger logger =
57         LoggerFactory.getLogger(VocabularyBaseImport.class);
58
59     // Instance variables specific to this test.
60     private VocabularyClient client = new VocabularyClient();
61     final String SERVICE_PATH_COMPONENT = "vocabularies";
62     final String ITEM_SERVICE_PATH_COMPONENT = "items";
63
64     public void createEnumeration(String vocabName, List<String> enumValues ) {
65
66         // Expected status code: 201 Created
67         int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
68         // Type of service request being tested
69         ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
70
71         if(logger.isDebugEnabled()){
72                 logger.debug("Import: Create vocabulary: \"" + vocabName +"\"");
73         }
74         String baseVocabRefName = VocabularyClientUtils.createVocabularyRefName(vocabName, false);
75         String fullVocabRefName = baseVocabRefName+"'"+vocabName+"'";
76         MultipartOutput multipart = VocabularyClientUtils.createEnumerationInstance(
77                         vocabName, fullVocabRefName, client.getCommonPartName());
78         ClientResponse<Response> res = client.create(multipart);
79
80         int statusCode = res.getStatus();
81
82         if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
83                 throw new RuntimeException("Could not create enumeration: \""+vocabName
84                                 +"\" "+ VocabularyClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
85         }
86         if(statusCode != EXPECTED_STATUS_CODE) {
87                 throw new RuntimeException("Unexpected Status when creating enumeration: \""
88                                 +vocabName +"\", Status:"+ statusCode);
89         }
90
91         // Store the ID returned from this create operation
92         // for additional tests below.
93         String newVocabId = VocabularyClientUtils.extractId(res);
94         if(logger.isDebugEnabled()){
95                 logger.debug("Import: Created vocabulary: \"" + vocabName +"\" ID:"
96                                 +newVocabId );
97         }
98         for(String itemName : enumValues){
99             HashMap<String, String> itemInfo = new HashMap<String, String>();
100             itemInfo.put(VocabularyItemJAXBSchema.DISPLAY_NAME, itemName);
101                 VocabularyClientUtils.createItemInVocabulary(newVocabId, 
102                                 baseVocabRefName, itemInfo, client);
103         }
104     }
105     
106         public static void main(String[] args) {
107                 
108                 BasicConfigurator.configure();
109                 logger.info("VocabularyBaseImport starting...");
110
111                 VocabularyBaseImport vbi = new VocabularyBaseImport();
112                 final String acquisitionMethodsVocabName = "Acquisition Methods";
113                 final String entryMethodsVocabName = "Entry Methods";
114                 final String entryReasonsVocabName = "Entry Reasons";
115                 final String responsibleDeptsVocabName = "Responsible Departments";
116
117                 List<String> acquisitionMethodsEnumValues = 
118                         Arrays.asList("Gift","Purchase","Exchange","Transfer","Treasure");
119                 List<String> entryMethodsEnumValues = 
120                         Arrays.asList("In person","Post","Found on doorstep");
121                 List<String> entryReasonsEnumValues = 
122                         Arrays.asList("Enquiry","Commission","Loan");
123                 List<String> respDeptNamesEnumValues = 
124                         Arrays.asList("Antiquities","Architecture and Design","Decorative Arts",
125                                                                         "Ethnography","Herpetology","Media and Performance Art",
126                                                                         "Paintings and Sculpture","Paleobotany","Photographs",
127                                                                         "Prints and Drawings");
128
129                 vbi.createEnumeration(acquisitionMethodsVocabName, acquisitionMethodsEnumValues);
130                 vbi.createEnumeration(entryMethodsVocabName, entryMethodsEnumValues);
131                 vbi.createEnumeration(entryReasonsVocabName, entryReasonsEnumValues);
132                 vbi.createEnumeration(responsibleDeptsVocabName, respDeptNamesEnumValues);
133
134                 logger.info("VocabularyBaseImport complete.");
135         }
136 }