]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
e6b1832d4f6d971081e2c4bdc5c2bcc7003c3c55
[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.List;
29
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.MultivaluedMap;
32 import javax.ws.rs.core.Response;
33
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;
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, List<String> enumValues ) {
62
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;
67
68         if(logger.isDebugEnabled()){
69                 logger.debug("Import: Create orgAuthority: \"" + orgAuthorityName +"\"");
70         }
71         MultipartOutput multipart = createOrgAuthorityInstance(orgAuthorityName, 
72                         createRefName(orgAuthorityName), "enum");
73         ClientResponse<Response> res = client.create(multipart);
74
75         int statusCode = res.getStatus();
76
77         if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
78                 throw new RuntimeException("Could not create enumeration: \""+orgAuthorityName
79                                 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
80         }
81         if(statusCode != EXPECTED_STATUS_CODE) {
82                 throw new RuntimeException("Unexpected Status when creating enumeration: \""
83                                 +orgAuthorityName +"\", Status:"+ statusCode);
84         }
85
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:"
91                                 +newOrgAuthorityId );
92         }
93         for(String itemName : enumValues){
94                 createItemInAuthority(newOrgAuthorityId, orgAuthorityName, itemName, createRefName(itemName));
95         }
96     }
97     
98     private String createItemInAuthority(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;
103
104         if(logger.isDebugEnabled()){
105                 logger.debug("Import: Create Item: \""+itemName+"\" in orgAuthorityulary: \"" + orgAuthorityName +"\"");
106         }
107         MultipartOutput multipart = createOrganizationInstance(itemName, refName,
108                         null, null, null, null, null, null, null, null, null );
109         ClientResponse<Response> res = client.createItem(vcsid, multipart);
110
111         int statusCode = res.getStatus();
112
113         if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
114                 throw new RuntimeException("Could not create Item: \""+itemName
115                                 +"\" in orgAuthority: \"" + orgAuthorityName
116                                 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
117         }
118         if(statusCode != EXPECTED_STATUS_CODE) {
119                 throw new RuntimeException("Unexpected Status when creating Item: \""+itemName
120                                 +"\" in orgAuthority: \"" + orgAuthorityName +"\", Status:"+ statusCode);
121         }
122
123         return extractId(res);
124     }
125
126     // ---------------------------------------------------------------
127     // Utility methods used by tests above
128     // ---------------------------------------------------------------
129
130     private MultipartOutput createOrgAuthorityInstance(
131                 String displayName, String refName, String vocabType) {
132         OrgauthoritiesCommon orgAuthority = new OrgauthoritiesCommon();
133         orgAuthority.setDisplayName(displayName);
134         orgAuthority.setRefName(refName);
135         orgAuthority.setVocabType(vocabType);
136         MultipartOutput multipart = new MultipartOutput();
137         OutputPart commonPart = multipart.addPart(orgAuthority, MediaType.APPLICATION_XML_TYPE);
138         commonPart.getHeaders().add("label", client.getCommonPartName());
139
140         if(logger.isDebugEnabled()){
141                 logger.debug("to be created, orgAuthority common ", 
142                                         orgAuthority, OrgauthoritiesCommon.class);
143         }
144
145         return multipart;
146     }
147
148     private MultipartOutput createOrganizationInstance(String inAuthority,
149         String shortName, String refName, String longName, 
150         String nameAdditions, String contactName, 
151         String foundingDate, String dissolutionDate, String foundingPlace,
152         String function, String description ) {
153         OrganizationsCommon organization = new OrganizationsCommon();
154         organization.setShortName(shortName);
155         if(refName!=null)
156                 organization.setRefName(refName);
157         if(longName!=null)
158                 organization.setLongName(longName);
159         if(nameAdditions!=null)
160                 organization.setNameAdditions(nameAdditions);
161         if(contactName!=null)
162                 organization.setContactName(contactName);
163         if(foundingDate!=null)
164                 organization.setFoundingDate(foundingDate);
165         if(dissolutionDate!=null)
166                 organization.setDissolutionDate(dissolutionDate);
167         if(foundingPlace!=null)
168                 organization.setFoundingPlace(foundingPlace);
169         if(function!=null)
170                 organization.setFunction(function);
171         if(description!=null)
172                 organization.setDescription(description);
173         MultipartOutput multipart = new MultipartOutput();
174         OutputPart commonPart = multipart.addPart(organization,
175             MediaType.APPLICATION_XML_TYPE);
176         commonPart.getHeaders().add("label", client.getItemCommonPartName());
177
178         if(logger.isDebugEnabled()){
179                 logger.debug("to be created, organization common ", organization, OrganizationsCommon.class);
180         }
181
182         return multipart;
183     }
184
185
186     /**
187      * Returns an error message indicating that the status code returned by a
188      * specific call to a service does not fall within a set of valid status
189      * codes for that service.
190      *
191      * @param serviceRequestType  A type of service request (e.g. CREATE, DELETE).
192      *
193      * @param statusCode  The invalid status code that was returned in the response,
194      *                    from submitting that type of request to the service.
195      *
196      * @return An error message.
197      */
198     protected String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {
199         return "Status code '" + statusCode + "' in response is NOT within the expected set: " +
200                 requestType.validStatusCodesAsString();
201     }
202
203     protected String extractId(ClientResponse<Response> res) {
204         MultivaluedMap<String, Object> mvm = res.getMetadata();
205         String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);
206         if(logger.isDebugEnabled()){
207                 logger.debug("extractId:uri=" + uri);
208         }
209         String[] segments = uri.split("/");
210         String id = segments[segments.length - 1];
211         if(logger.isDebugEnabled()){
212                 logger.debug("id=" + id);
213         }
214         return id;
215     }
216     
217     protected String createRefName(String displayName) {
218         return displayName.replaceAll("\\W", "");
219     }
220
221         public static void main(String[] args) {
222                 
223                 BasicConfigurator.configure();
224                 logger.info("OrgAuthorityBaseImport starting...");
225
226                 OrgAuthorityBaseImport oabi = new OrgAuthorityBaseImport();
227                 final String acquisitionMethodsOrgAuthorityName = "Acquisition Methods";
228                 final String entryMethodsOrgAuthorityName = "Entry Methods";
229                 final String entryReasonsOrgAuthorityName = "Entry Reasons";
230                 final String responsibleDeptsOrgAuthorityName = "Responsible Departments";
231
232                 List<String> acquisitionMethodsEnumValues = 
233                         Arrays.asList("Gift","Purchase","Exchange","Transfer","Treasure");
234                 List<String> entryMethodsEnumValues = 
235                         Arrays.asList("In person","Post","Found on doorstep");
236                 List<String> entryReasonsEnumValues = 
237                         Arrays.asList("Enquiry","Commission","Loan");
238                 List<String> respDeptNamesEnumValues = 
239                         Arrays.asList("Antiquities","Architecture and Design","Decorative Arts",
240                                                                         "Ethnography","Herpetology","Media and Performance Art",
241                                                                         "Paintings and Sculpture","Paleobotany","Photographs",
242                                                                         "Prints and Drawings");
243
244                 oabi.createOrgAuthority(acquisitionMethodsOrgAuthorityName, acquisitionMethodsEnumValues);
245                 oabi.createOrgAuthority(entryMethodsOrgAuthorityName, entryMethodsEnumValues);
246                 oabi.createOrgAuthority(entryReasonsOrgAuthorityName, entryReasonsEnumValues);
247                 oabi.createOrgAuthority(responsibleDeptsOrgAuthorityName, respDeptNamesEnumValues);
248
249                 logger.info("OrgAuthorityBaseImport complete.");
250         }
251 }