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.HashMap;
29 import java.util.List;
32 import javax.ws.rs.core.MultivaluedMap;
33 import javax.ws.rs.core.Response;
35 import org.apache.log4j.BasicConfigurator;
36 import org.collectionspace.services.OrganizationJAXBSchema;
37 import org.collectionspace.services.client.OrgAuthorityClient;
38 import org.collectionspace.services.client.OrgAuthorityClientUtils;
39 import org.collectionspace.services.client.test.ServiceRequestType;
40 import org.jboss.resteasy.client.ClientResponse;
41 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
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<Map<String,String>> orgInfos ) {
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 = OrgAuthorityClientUtils.createOrgAuthRefName(orgAuthorityName, false);
73 String fullOrgAuthRefName = OrgAuthorityClientUtils.createOrgAuthRefName(orgAuthorityName, true);
74 MultipartOutput multipart =
75 OrgAuthorityClientUtils.createOrgAuthorityInstance(
76 orgAuthorityName, fullOrgAuthRefName,
77 client.getCommonPartName());
78 ClientResponse<Response> res = client.create(multipart);
80 int statusCode = res.getStatus();
82 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
83 throw new RuntimeException("Could not create enumeration: \""+orgAuthorityName
84 +"\" "+ OrgAuthorityClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
86 if(statusCode != EXPECTED_STATUS_CODE) {
87 throw new RuntimeException("Unexpected Status when creating enumeration: \""
88 +orgAuthorityName +"\", Status:"+ statusCode);
91 // Store the ID returned from this create operation
92 // for additional tests below.
93 String newOrgAuthorityId = OrgAuthorityClientUtils.extractId(res);
94 if(logger.isDebugEnabled()){
95 logger.debug("Import: Created orgAuthorityulary: \"" + orgAuthorityName +"\" ID:"
98 for(Map<String,String> orgInfo : orgInfos){
99 createItemInAuthority(newOrgAuthorityId,
100 baseOrgAuthRefName, orgInfo );
104 private String createItemInAuthority(String vcsid,
105 String orgAuthorityRefName, Map<String, String> orgInfo) {
106 // Expected status code: 201 Created
107 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
108 // Type of service request being tested
109 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
110 String shortName = orgInfo.get(OrganizationJAXBSchema.SHORT_NAME);
111 String refName = OrgAuthorityClientUtils.createOrganizationRefName(
112 orgAuthorityRefName, shortName, true);
114 if(logger.isDebugEnabled()){
115 logger.debug("Import: Create Item: \""+shortName
116 +"\" in orgAuthorityulary: \"" + orgAuthorityRefName +"\"");
118 MultipartOutput multipart =
119 OrgAuthorityClientUtils.createOrganizationInstance( vcsid,
120 refName, orgInfo, client.getItemCommonPartName() );
121 ClientResponse<Response> res = client.createItem(vcsid, multipart);
123 int statusCode = res.getStatus();
125 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
126 throw new RuntimeException("Could not create Item: \""+shortName
127 +"\" in orgAuthority: \"" + orgAuthorityRefName
128 +"\" "+ OrgAuthorityClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
130 if(statusCode != EXPECTED_STATUS_CODE) {
131 throw new RuntimeException("Unexpected Status when creating Item: \""+shortName
132 +"\" in orgAuthority: \"" + orgAuthorityRefName +"\", Status:"+ statusCode);
135 return OrgAuthorityClientUtils.extractId(res);
138 // ---------------------------------------------------------------
139 // Utility methods used by methods above
140 // ---------------------------------------------------------------
142 public static void main(String[] args) {
144 BasicConfigurator.configure();
145 logger.info("OrgAuthorityBaseImport starting...");
147 OrgAuthorityBaseImport oabi = new OrgAuthorityBaseImport();
148 final String demoOrgAuthorityName = "Demo Org Authority";
150 Map<String, String> mmiOrgMap = new HashMap<String,String>();
151 mmiOrgMap.put(OrganizationJAXBSchema.SHORT_NAME, "MMI");
152 mmiOrgMap.put(OrganizationJAXBSchema.LONG_NAME, "Museum of the Moving Image");
153 mmiOrgMap.put(OrganizationJAXBSchema.CONTACT_NAME, "Megan Forbes");
154 mmiOrgMap.put(OrganizationJAXBSchema.FOUNDING_DATE, "1984");
155 mmiOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, "Astoria, NY");
156 Map<String, String> pahmaOrgMap = new HashMap<String,String>();
157 pahmaOrgMap.put(OrganizationJAXBSchema.SHORT_NAME, "PAHMA");
158 pahmaOrgMap.put(OrganizationJAXBSchema.LONG_NAME, "Phoebe A. Hearst Museum of Anthropology");
159 pahmaOrgMap.put(OrganizationJAXBSchema.NAME_ADDITIONS, "University of California, Berkeley");
160 pahmaOrgMap.put(OrganizationJAXBSchema.CONTACT_NAME, "Michael Black");
161 pahmaOrgMap.put(OrganizationJAXBSchema.FOUNDING_DATE, "1901");
162 pahmaOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, "Berkeley, CA");
163 Map<String, String> savoyOrgMap = new HashMap<String,String>();
164 savoyOrgMap.put(OrganizationJAXBSchema.SHORT_NAME, "Savoy Theatre");
165 savoyOrgMap.put(OrganizationJAXBSchema.FOUNDING_DATE, "1900");
166 savoyOrgMap.put(OrganizationJAXBSchema.DISSOLUTION_DATE, "1952");
167 savoyOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, "New York, NY");
168 List<Map<String, String>> orgMaps =
169 Arrays.asList(mmiOrgMap, pahmaOrgMap, savoyOrgMap );
171 oabi.createOrgAuthority(demoOrgAuthorityName, orgMaps);
173 logger.info("OrgAuthorityBaseImport complete.");