]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
f01658c2c6ed3fc86d5abf0a27ebda3e7fd334fe
[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                 createItemInOrgAuthority(newOrgAuthorityId, orgAuthorityName, itemName, createRefName(itemName));
95         }
96     }
97     
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;
103
104         if(logger.isDebugEnabled()){
105                 logger.debug("Import: Create Item: \""+itemName+"\" in orgAuthorityulary: \"" + orgAuthorityName +"\"");
106         }
107         MultipartOutput multipart = createOrganizationInstance(itemName, refName);
108         ClientResponse<Response> res = client.createItem(vcsid, multipart);
109
110         int statusCode = res.getStatus();
111
112         if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
113                 throw new RuntimeException("Could not create Item: \""+itemName
114                                 +"\" in orgAuthority: \"" + orgAuthorityName
115                                 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
116         }
117         if(statusCode != EXPECTED_STATUS_CODE) {
118                 throw new RuntimeException("Unexpected Status when creating Item: \""+itemName
119                                 +"\" in orgAuthority: \"" + orgAuthorityName +"\", Status:"+ statusCode);
120         }
121
122         return extractId(res);
123     }
124
125     // ---------------------------------------------------------------
126     // Utility methods used by tests above
127     // ---------------------------------------------------------------
128
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());
138
139         if(logger.isDebugEnabled()){
140                 logger.debug("to be created, orgAuthority common ", 
141                                         orgAuthority, OrgauthoritiesCommon.class);
142         }
143
144         return multipart;
145     }
146
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());
155
156         if(logger.isDebugEnabled()){
157                 logger.debug("to be created, organization common ", organization, OrganizationsCommon.class);
158         }
159
160         return multipart;
161     }
162
163
164     /**
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.
168      *
169      * @param serviceRequestType  A type of service request (e.g. CREATE, DELETE).
170      *
171      * @param statusCode  The invalid status code that was returned in the response,
172      *                    from submitting that type of request to the service.
173      *
174      * @return An error message.
175      */
176     protected String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {
177         return "Status code '" + statusCode + "' in response is NOT within the expected set: " +
178                 requestType.validStatusCodesAsString();
179     }
180
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);
186         }
187         String[] segments = uri.split("/");
188         String id = segments[segments.length - 1];
189         if(logger.isDebugEnabled()){
190                 logger.debug("id=" + id);
191         }
192         return id;
193     }
194     
195     protected String createRefName(String displayName) {
196         return displayName.replaceAll("\\W", "");
197     }
198
199         public static void main(String[] args) {
200                 
201                 BasicConfigurator.configure();
202                 logger.info("OrgAuthorityBaseImport starting...");
203
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";
209
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");
221
222                 oabi.createOrgAuthority(acquisitionMethodsOrgAuthorityName, acquisitionMethodsEnumValues);
223                 oabi.createOrgAuthority(entryMethodsOrgAuthorityName, entryMethodsEnumValues);
224                 oabi.createOrgAuthority(entryReasonsOrgAuthorityName, entryReasonsEnumValues);
225                 oabi.createOrgAuthority(responsibleDeptsOrgAuthorityName, respDeptNamesEnumValues);
226
227                 logger.info("OrgAuthorityBaseImport complete.");
228         }
229 }