]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
49d570dea59059cc751d8af0f22d100c413ea7d2
[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.organization.importer;
25
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31
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.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;
44
45 /**
46  * OrgAuthorityServiceTest, carries out tests against a
47  * deployed and running OrgAuthority Service.
48  *
49  * $LastChangedRevision: 753 $
50  * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
51  */
52 public class OrgAuthorityBaseImport {
53     private static final Logger logger =
54         LoggerFactory.getLogger(OrgAuthorityBaseImport.class);
55
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";
60
61     public void createOrgAuthority(String orgAuthorityName, 
62                 List<Map<String,String>> orgInfos ) {
63
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;
68
69         if(logger.isDebugEnabled()){
70                 logger.debug("Import: Create orgAuthority: \"" + orgAuthorityName +"\"");
71         }
72         String baseOrgAuthRefName = createOrgAuthRefName(orgAuthorityName);
73         String fullOrgAuthRefName = baseOrgAuthRefName+"'"+orgAuthorityName+"'";
74         MultipartOutput multipart = 
75                 OrgAuthorityClientUtils.createOrgAuthorityInstance(
76                                 orgAuthorityName, fullOrgAuthRefName, 
77                                 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: \""+orgAuthorityName
84                                 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
85         }
86         if(statusCode != EXPECTED_STATUS_CODE) {
87                 throw new RuntimeException("Unexpected Status when creating enumeration: \""
88                                 +orgAuthorityName +"\", Status:"+ statusCode);
89         }
90
91         // Store the ID returned from this create operation
92         // for additional tests below.
93         String newOrgAuthorityId = extractId(res);
94         if(logger.isDebugEnabled()){
95                 logger.debug("Import: Created orgAuthorityulary: \"" + orgAuthorityName +"\" ID:"
96                                 +newOrgAuthorityId );
97         }
98         for(Map<String,String> orgInfo : orgInfos){
99                 createItemInAuthority(newOrgAuthorityId, 
100                                 baseOrgAuthRefName, orgInfo );
101         }
102     }
103     
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 = createOrganizationRefName(
112                                                 orgAuthorityRefName, shortName)+"'"+shortName+"'";
113
114         if(logger.isDebugEnabled()){
115                 logger.debug("Import: Create Item: \""+shortName
116                                 +"\" in orgAuthorityulary: \"" + orgAuthorityRefName +"\"");
117         }
118         MultipartOutput multipart = 
119                 OrgAuthorityClientUtils.createOrganizationInstance( vcsid, 
120                                 refName, orgInfo, client.getCommonPartName() );
121         ClientResponse<Response> res = client.createItem(vcsid, multipart);
122
123         int statusCode = res.getStatus();
124
125         if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
126                 throw new RuntimeException("Could not create Item: \""+shortName
127                                 +"\" in orgAuthority: \"" + orgAuthorityRefName
128                                 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
129         }
130         if(statusCode != EXPECTED_STATUS_CODE) {
131                 throw new RuntimeException("Unexpected Status when creating Item: \""+shortName
132                                 +"\" in orgAuthority: \"" + orgAuthorityRefName +"\", Status:"+ statusCode);
133         }
134
135         return extractId(res);
136     }
137
138     // ---------------------------------------------------------------
139     // Utility methods used by methods above
140     // ---------------------------------------------------------------
141
142     /**
143      * Returns an error message indicating that the status code returned by a
144      * specific call to a service does not fall within a set of valid status
145      * codes for that service.
146      *
147      * @param serviceRequestType  A type of service request (e.g. CREATE, DELETE).
148      *
149      * @param statusCode  The invalid status code that was returned in the response,
150      *                    from submitting that type of request to the service.
151      *
152      * @return An error message.
153      */
154     protected String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {
155         return "Status code '" + statusCode + "' in response is NOT within the expected set: " +
156                 requestType.validStatusCodesAsString();
157     }
158
159     protected String extractId(ClientResponse<Response> res) {
160         MultivaluedMap<String, Object> mvm = res.getMetadata();
161         String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);
162         if(logger.isDebugEnabled()){
163                 logger.debug("extractId:uri=" + uri);
164         }
165         String[] segments = uri.split("/");
166         String id = segments[segments.length - 1];
167         if(logger.isDebugEnabled()){
168                 logger.debug("id=" + id);
169         }
170         return id;
171     }
172     
173     protected String createOrgAuthRefName(String orgAuthorityName) {
174         return "urn:cspace:org.collectionspace.demo:orgauthority:name("
175                         +orgAuthorityName+")";
176     }
177
178     protected String createOrganizationRefName(
179                                                 String orgAuthRefName, String orgName) {
180         return orgAuthRefName+":organization:name("+orgName+")";
181     }
182
183         public static void main(String[] args) {
184                 
185                 BasicConfigurator.configure();
186                 logger.info("OrgAuthorityBaseImport starting...");
187
188                 OrgAuthorityBaseImport oabi = new OrgAuthorityBaseImport();
189                 final String demoOrgAuthorityName = "Demo Org Authority";
190
191         Map<String, String> mmiOrgMap = new HashMap<String,String>();
192         mmiOrgMap.put(OrganizationJAXBSchema.SHORT_NAME, "MMI");
193         mmiOrgMap.put(OrganizationJAXBSchema.LONG_NAME, "Museum of the Moving Image");
194         mmiOrgMap.put(OrganizationJAXBSchema.CONTACT_NAME, "Megan Forbes");
195         mmiOrgMap.put(OrganizationJAXBSchema.FOUNDING_DATE, "1984");
196         mmiOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, "Astoria, NY");
197         Map<String, String> pahmaOrgMap = new HashMap<String,String>();
198         pahmaOrgMap.put(OrganizationJAXBSchema.SHORT_NAME, "PAHMA");
199         pahmaOrgMap.put(OrganizationJAXBSchema.LONG_NAME, "Phoebe A. Hearst Museum of Anthropology");
200         pahmaOrgMap.put(OrganizationJAXBSchema.NAME_ADDITIONS, "University of California, Berkeley");
201         pahmaOrgMap.put(OrganizationJAXBSchema.CONTACT_NAME, "Michael Black");
202         pahmaOrgMap.put(OrganizationJAXBSchema.FOUNDING_DATE, "1901");
203         pahmaOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, "Berkeley, CA");
204         Map<String, String> savoyOrgMap = new HashMap<String,String>();
205         savoyOrgMap.put(OrganizationJAXBSchema.SHORT_NAME, "Savoy Theatre");
206         savoyOrgMap.put(OrganizationJAXBSchema.FOUNDING_DATE, "1900");
207         savoyOrgMap.put(OrganizationJAXBSchema.DISSOLUTION_DATE, "1952");
208         savoyOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, "New York, NY");
209         List<Map<String, String>> orgMaps = 
210                 Arrays.asList(mmiOrgMap, pahmaOrgMap, savoyOrgMap );
211
212                 oabi.createOrgAuthority(demoOrgAuthorityName, orgMaps);
213
214                 logger.info("OrgAuthorityBaseImport complete.");
215         }
216 }