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,
62 List<List<String>> orgInfo ) {
64 // Expected status code: 201 Created
65 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
66 // Type of service request being tested
67 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
69 if(logger.isDebugEnabled()){
70 logger.debug("Import: Create orgAuthority: \"" + orgAuthorityName +"\"");
72 String baseOrgAuthRefName = createOrgAuthRefName(orgAuthorityName);
73 String fullOrgAuthRefName = baseOrgAuthRefName+"'"+orgAuthorityName+"'";
74 MultipartOutput multipart = createOrgAuthorityInstance(orgAuthorityName,
76 ClientResponse<Response> res = client.create(multipart);
78 int statusCode = res.getStatus();
80 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
81 throw new RuntimeException("Could not create enumeration: \""+orgAuthorityName
82 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
84 if(statusCode != EXPECTED_STATUS_CODE) {
85 throw new RuntimeException("Unexpected Status when creating enumeration: \""
86 +orgAuthorityName +"\", Status:"+ statusCode);
89 // Store the ID returned from this create operation
90 // for additional tests below.
91 String newOrgAuthorityId = extractId(res);
92 if(logger.isDebugEnabled()){
93 logger.debug("Import: Created orgAuthorityulary: \"" + orgAuthorityName +"\" ID:"
96 for(List<String> orgStrings : orgInfo){
97 createItemInAuthority(newOrgAuthorityId, baseOrgAuthRefName, orgStrings);
101 private String createItemInAuthority(String vcsid,
102 String orgAuthorityRefName, List<String> orgStrings) {
103 // Expected status code: 201 Created
104 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
105 // Type of service request being tested
106 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
108 shortName, longName, nameAdditions, contactName,
109 foundingDate, dissolutionDate, foundingPlace, function, description
111 String shortName = orgStrings.get(0);
112 String refName = createOrganizationRefName(orgAuthorityRefName, shortName)+"'"+shortName+"'";
114 if(logger.isDebugEnabled()){
115 logger.debug("Import: Create Item: \""+shortName
116 +"\" in orgAuthorityulary: \"" + orgAuthorityRefName +"\"");
118 MultipartOutput multipart = createOrganizationInstance( vcsid, refName,
120 ClientResponse<Response> res = client.createItem(vcsid, multipart);
122 int statusCode = res.getStatus();
124 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
125 throw new RuntimeException("Could not create Item: \""+shortName
126 +"\" in orgAuthority: \"" + orgAuthorityRefName
127 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
129 if(statusCode != EXPECTED_STATUS_CODE) {
130 throw new RuntimeException("Unexpected Status when creating Item: \""+shortName
131 +"\" in orgAuthority: \"" + orgAuthorityRefName +"\", Status:"+ statusCode);
134 return extractId(res);
137 // ---------------------------------------------------------------
138 // Utility methods used by methods above
139 // ---------------------------------------------------------------
141 private MultipartOutput createOrgAuthorityInstance(
142 String displayName, String refName ) {
143 OrgauthoritiesCommon orgAuthority = new OrgauthoritiesCommon();
144 orgAuthority.setDisplayName(displayName);
145 orgAuthority.setRefName(refName);
146 orgAuthority.setVocabType("OrgAuthority");
147 MultipartOutput multipart = new MultipartOutput();
148 OutputPart commonPart = multipart.addPart(orgAuthority, MediaType.APPLICATION_XML_TYPE);
149 commonPart.getHeaders().add("label", client.getCommonPartName());
151 if(logger.isDebugEnabled()){
152 logger.debug("to be created, orgAuthority common ",
153 orgAuthority, OrgauthoritiesCommon.class);
159 private MultipartOutput createOrganizationInstance(String inAuthority,
160 String orgRefName, List<String> orgStrings){
161 OrganizationsCommon organization = new OrganizationsCommon();
162 organization.setInAuthority(inAuthority);
163 organization.setShortName(orgStrings.get(0));
164 organization.setRefName(orgRefName);
165 String longName = orgStrings.get(1);
167 organization.setLongName(longName);
168 String nameAdditions = orgStrings.get(2);
169 if(nameAdditions!=null)
170 organization.setNameAdditions(nameAdditions);
171 String contactName = orgStrings.get(3);
172 if(contactName!=null)
173 organization.setContactName(contactName);
174 String foundingDate = orgStrings.get(4);
175 if(foundingDate!=null)
176 organization.setFoundingDate(foundingDate);
177 String dissolutionDate = orgStrings.get(5);
178 if(dissolutionDate!=null)
179 organization.setDissolutionDate(dissolutionDate);
180 String foundingPlace = orgStrings.get(6);
181 if(foundingPlace!=null)
182 organization.setFoundingPlace(foundingPlace);
183 String function = orgStrings.get(7);
185 organization.setFunction(function);
186 String description = orgStrings.get(8);
187 if(description!=null)
188 organization.setDescription(description);
189 MultipartOutput multipart = new MultipartOutput();
190 OutputPart commonPart = multipart.addPart(organization,
191 MediaType.APPLICATION_XML_TYPE);
192 commonPart.getHeaders().add("label", client.getItemCommonPartName());
194 if(logger.isDebugEnabled()){
195 logger.debug("to be created, organization common ", organization, OrganizationsCommon.class);
203 * Returns an error message indicating that the status code returned by a
204 * specific call to a service does not fall within a set of valid status
205 * codes for that service.
207 * @param serviceRequestType A type of service request (e.g. CREATE, DELETE).
209 * @param statusCode The invalid status code that was returned in the response,
210 * from submitting that type of request to the service.
212 * @return An error message.
214 protected String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {
215 return "Status code '" + statusCode + "' in response is NOT within the expected set: " +
216 requestType.validStatusCodesAsString();
219 protected String extractId(ClientResponse<Response> res) {
220 MultivaluedMap<String, Object> mvm = res.getMetadata();
221 String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);
222 if(logger.isDebugEnabled()){
223 logger.debug("extractId:uri=" + uri);
225 String[] segments = uri.split("/");
226 String id = segments[segments.length - 1];
227 if(logger.isDebugEnabled()){
228 logger.debug("id=" + id);
233 protected String createOrgAuthRefName(String orgAuthorityName) {
234 return "urn:cspace:org.collectionspace.demo:orgauthority:name("
235 +orgAuthorityName+")";
238 protected String createOrganizationRefName(
239 String orgAuthRefName, String orgName) {
240 return orgAuthRefName+":organization:name("+orgName+")";
243 public static void main(String[] args) {
245 BasicConfigurator.configure();
246 logger.info("OrgAuthorityBaseImport starting...");
248 OrgAuthorityBaseImport oabi = new OrgAuthorityBaseImport();
249 final String demoOrgAuthorityName = "Demo Org Authority";
252 shortName, longName, nameAdditions, contactName,
253 foundingDate, dissolutionDate, foundingPlace, function, description
255 List<String> mmiOrgStrings =
256 Arrays.asList("MMI","Museum of the Moving Image",null,"Megan Forbes",
257 "1984", null, "Astoria, NY", null, null);
258 List<String> pahmaOrgStrings =
259 Arrays.asList("PAHMA","Phoebe A. Hearst Museum of Anthropology",
260 "University of California, Berkeley","Michael Black",
261 "1901", null, "Berkeley, CA", null, null);
262 List<String> savoyOrgStrings =
263 Arrays.asList("Savoy Theatre",null,null,null,
264 "1900", "1952", "New York, NY", null, null);
265 List<List<String>> orgsStrings =
266 Arrays.asList(mmiOrgStrings, pahmaOrgStrings, savoyOrgStrings );
268 oabi.createOrgAuthority(demoOrgAuthorityName, orgsStrings);
270 logger.info("OrgAuthorityBaseImport complete.");