]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
dd00fe2f9b72d4e13eb9ccd473e9a2af645e7822
[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 package org.collectionspace.services.vocabulary.importer;
24
25 import java.util.Arrays;
26 import java.util.HashMap;
27 import java.util.List;
28
29 import javax.ws.rs.core.Response;
30
31 import org.collectionspace.services.VocabularyItemJAXBSchema;
32 import org.collectionspace.services.client.VocabularyClient;
33 import org.collectionspace.services.client.VocabularyClientUtils;
34 import org.collectionspace.services.client.test.ServiceRequestType;
35 import org.jboss.resteasy.client.ClientResponse;
36 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * VocabularyServiceTest, carries out tests against a
42  * deployed and running Vocabulary Service.
43  *
44  * $LastChangedRevision: 753 $
45  * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
46  */
47 public class VocabularyBaseImport {
48
49     private static final Logger logger =
50             LoggerFactory.getLogger(VocabularyBaseImport.class);
51     // Instance variables specific to this test.
52     private VocabularyClient client = new VocabularyClient();
53     final String SERVICE_PATH_COMPONENT = "vocabularies";
54     final String ITEM_SERVICE_PATH_COMPONENT = "items";
55
56     public void createEnumeration(String vocabName, List<String> enumValues) {
57
58         // Expected status code: 201 Created
59         int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
60         // Type of service request being tested
61         ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
62
63         if (logger.isDebugEnabled()) {
64             logger.debug("Import: Create vocabulary: \"" + vocabName + "\"");
65         }
66         String baseVocabRefName = VocabularyClientUtils.createVocabularyRefName(vocabName, false);
67         String fullVocabRefName = baseVocabRefName + "'" + vocabName + "'";
68         MultipartOutput multipart = VocabularyClientUtils.createEnumerationInstance(
69                 vocabName, fullVocabRefName, client.getCommonPartName());
70         ClientResponse<Response> res = client.create(multipart);
71
72         int statusCode = res.getStatus();
73
74         if (!REQUEST_TYPE.isValidStatusCode(statusCode)) {
75             throw new RuntimeException("Could not create enumeration: \"" + vocabName
76                     + "\" " + VocabularyClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
77         }
78         if (statusCode != EXPECTED_STATUS_CODE) {
79             throw new RuntimeException("Unexpected Status when creating enumeration: \""
80                     + vocabName + "\", Status:" + statusCode);
81         }
82
83         // Store the ID returned from this create operation
84         // for additional tests below.
85         String newVocabId = VocabularyClientUtils.extractId(res);
86         if (logger.isDebugEnabled()) {
87             logger.debug("Import: Created vocabulary: \"" + vocabName + "\" ID:"
88                     + newVocabId);
89         }
90         for (String itemName : enumValues) {
91             HashMap<String, String> itemInfo = new HashMap<String, String>();
92             itemInfo.put(VocabularyItemJAXBSchema.DISPLAY_NAME, itemName);
93             VocabularyClientUtils.createItemInVocabulary(newVocabId,
94                     baseVocabRefName, itemInfo, client);
95         }
96     }
97
98     public static void main(String[] args) {
99
100         logger.info("VocabularyBaseImport starting...");
101
102         VocabularyBaseImport vbi = new VocabularyBaseImport();
103         final String acquisitionMethodsVocabName = "Acquisition Methods";
104         final String entryMethodsVocabName = "Entry Methods";
105         final String entryReasonsVocabName = "Entry Reasons";
106         final String responsibleDeptsVocabName = "Responsible Departments";
107         final String termStatusVocabName = "Term Status";
108
109         List<String> acquisitionMethodsEnumValues =
110                 Arrays.asList("Gift", "Purchase", "Exchange", "Transfer", "Treasure");
111         List<String> entryMethodsEnumValues =
112                 Arrays.asList("In person", "Post", "Found on doorstep");
113         List<String> entryReasonsEnumValues =
114                 Arrays.asList("Enquiry", "Commission", "Loan");
115         List<String> respDeptNamesEnumValues =
116                 Arrays.asList("Antiquities", "Architecture and Design", "Decorative Arts",
117                 "Ethnography", "Herpetology", "Media and Performance Art",
118                 "Paintings and Sculpture", "Paleobotany", "Photographs",
119                 "Prints and Drawings");
120         List<String> termStatusEnumValues =
121                 Arrays.asList("Provisional", "Under Review", "Accepted", "Rejected");
122
123         vbi.createEnumeration(acquisitionMethodsVocabName, acquisitionMethodsEnumValues);
124         vbi.createEnumeration(entryMethodsVocabName, entryMethodsEnumValues);
125         vbi.createEnumeration(entryReasonsVocabName, entryReasonsEnumValues);
126         vbi.createEnumeration(responsibleDeptsVocabName, respDeptNamesEnumValues);
127         vbi.createEnumeration(termStatusVocabName, termStatusEnumValues);
128
129         logger.info("VocabularyBaseImport complete.");
130     }
131 }