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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright (c)) 2009 Regents of the University of California
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
24 package org.collectionspace.services.organization.importer;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.List;
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.MultivaluedMap;
32 import javax.ws.rs.core.Response;
34 import org.apache.log4j.BasicConfigurator;
35 import org.collectionspace.services.client.OrgAuthorityClient;
36 import org.collectionspace.services.client.test.ServiceRequestType;
37 import org.collectionspace.services.organization.OrgauthoritiesCommon;
38 import org.collectionspace.services.organization.OrganizationsCommon;
39 import org.jboss.resteasy.client.ClientResponse;
40 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
41 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * OrgAuthorityServiceTest, carries out tests against a
47 * deployed and running OrgAuthority Service.
49 * $LastChangedRevision: 753 $
50 * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
52 public class OrgAuthorityBaseImport {
53 private static final Logger logger =
54 LoggerFactory.getLogger(OrgAuthorityBaseImport.class);
56 // Instance variables specific to this test.
57 private OrgAuthorityClient client = new OrgAuthorityClient();
58 final String SERVICE_PATH_COMPONENT = "orgauthorities";
59 final String ITEM_SERVICE_PATH_COMPONENT = "items";
61 public void createOrgAuthority(String orgAuthorityName, List<String> enumValues ) {
63 // Expected status code: 201 Created
64 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
65 // Type of service request being tested
66 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
68 if(logger.isDebugEnabled()){
69 logger.debug("Import: Create orgAuthority: \"" + orgAuthorityName +"\"");
71 MultipartOutput multipart = createOrgAuthorityInstance(orgAuthorityName,
72 createRefName(orgAuthorityName), "enum");
73 ClientResponse<Response> res = client.create(multipart);
75 int statusCode = res.getStatus();
77 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
78 throw new RuntimeException("Could not create enumeration: \""+orgAuthorityName
79 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
81 if(statusCode != EXPECTED_STATUS_CODE) {
82 throw new RuntimeException("Unexpected Status when creating enumeration: \""
83 +orgAuthorityName +"\", Status:"+ statusCode);
86 // Store the ID returned from this create operation
87 // for additional tests below.
88 String newOrgAuthorityId = extractId(res);
89 if(logger.isDebugEnabled()){
90 logger.debug("Import: Created orgAuthorityulary: \"" + orgAuthorityName +"\" ID:"
93 for(String itemName : enumValues){
94 createItemInOrgAuthority(newOrgAuthorityId, orgAuthorityName, itemName, createRefName(itemName));
98 private String createItemInOrgAuthority(String vcsid, String orgAuthorityName, String itemName, String refName) {
99 // Expected status code: 201 Created
100 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
101 // Type of service request being tested
102 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
104 if(logger.isDebugEnabled()){
105 logger.debug("Import: Create Item: \""+itemName+"\" in orgAuthorityulary: \"" + orgAuthorityName +"\"");
107 MultipartOutput multipart = createOrganizationInstance(itemName, refName);
108 ClientResponse<Response> res = client.createItem(vcsid, multipart);
110 int statusCode = res.getStatus();
112 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
113 throw new RuntimeException("Could not create Item: \""+itemName
114 +"\" in orgAuthority: \"" + orgAuthorityName
115 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
117 if(statusCode != EXPECTED_STATUS_CODE) {
118 throw new RuntimeException("Unexpected Status when creating Item: \""+itemName
119 +"\" in orgAuthority: \"" + orgAuthorityName +"\", Status:"+ statusCode);
122 return extractId(res);
125 // ---------------------------------------------------------------
126 // Utility methods used by tests above
127 // ---------------------------------------------------------------
129 private MultipartOutput createOrgAuthorityInstance(
130 String displayName, String refName, String vocabType) {
131 OrgauthoritiesCommon orgAuthority = new OrgauthoritiesCommon();
132 orgAuthority.setDisplayName(displayName);
133 orgAuthority.setRefName(refName);
134 orgAuthority.setVocabType(vocabType);
135 MultipartOutput multipart = new MultipartOutput();
136 OutputPart commonPart = multipart.addPart(orgAuthority, MediaType.APPLICATION_XML_TYPE);
137 commonPart.getHeaders().add("label", client.getCommonPartName());
139 if(logger.isDebugEnabled()){
140 logger.debug("to be created, orgAuthority common ",
141 orgAuthority, OrgauthoritiesCommon.class);
147 private MultipartOutput createOrganizationInstance(
148 String displayName, String refName) {
149 OrganizationsCommon organization = new OrganizationsCommon();
150 organization.setDisplayName(displayName);
151 organization.setRefName(refName);
152 MultipartOutput multipart = new MultipartOutput();
153 OutputPart commonPart = multipart.addPart(organization, MediaType.APPLICATION_XML_TYPE);
154 commonPart.getHeaders().add("label", client.getItemCommonPartName());
156 if(logger.isDebugEnabled()){
157 logger.debug("to be created, organization common ", organization, OrganizationsCommon.class);
165 * Returns an error message indicating that the status code returned by a
166 * specific call to a service does not fall within a set of valid status
167 * codes for that service.
169 * @param serviceRequestType A type of service request (e.g. CREATE, DELETE).
171 * @param statusCode The invalid status code that was returned in the response,
172 * from submitting that type of request to the service.
174 * @return An error message.
176 protected String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {
177 return "Status code '" + statusCode + "' in response is NOT within the expected set: " +
178 requestType.validStatusCodesAsString();
181 protected String extractId(ClientResponse<Response> res) {
182 MultivaluedMap<String, Object> mvm = res.getMetadata();
183 String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);
184 if(logger.isDebugEnabled()){
185 logger.debug("extractId:uri=" + uri);
187 String[] segments = uri.split("/");
188 String id = segments[segments.length - 1];
189 if(logger.isDebugEnabled()){
190 logger.debug("id=" + id);
195 protected String createRefName(String displayName) {
196 return displayName.replaceAll("\\W", "");
199 public static void main(String[] args) {
201 BasicConfigurator.configure();
202 logger.info("OrgAuthorityBaseImport starting...");
204 OrgAuthorityBaseImport oabi = new OrgAuthorityBaseImport();
205 final String acquisitionMethodsOrgAuthorityName = "Acquisition Methods";
206 final String entryMethodsOrgAuthorityName = "Entry Methods";
207 final String entryReasonsOrgAuthorityName = "Entry Reasons";
208 final String responsibleDeptsOrgAuthorityName = "Responsible Departments";
210 List<String> acquisitionMethodsEnumValues =
211 Arrays.asList("Gift","Purchase","Exchange","Transfer","Treasure");
212 List<String> entryMethodsEnumValues =
213 Arrays.asList("In person","Post","Found on doorstep");
214 List<String> entryReasonsEnumValues =
215 Arrays.asList("Enquiry","Commission","Loan");
216 List<String> respDeptNamesEnumValues =
217 Arrays.asList("Antiquities","Architecture and Design","Decorative Arts",
218 "Ethnography","Herpetology","Media and Performance Art",
219 "Paintings and Sculpture","Paleobotany","Photographs",
220 "Prints and Drawings");
222 oabi.createOrgAuthority(acquisitionMethodsOrgAuthorityName, acquisitionMethodsEnumValues);
223 oabi.createOrgAuthority(entryMethodsOrgAuthorityName, entryMethodsEnumValues);
224 oabi.createOrgAuthority(entryReasonsOrgAuthorityName, entryReasonsEnumValues);
225 oabi.createOrgAuthority(responsibleDeptsOrgAuthorityName, respDeptNamesEnumValues);
227 logger.info("OrgAuthorityBaseImport complete.");