]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
af7c7140936441f0e597924d05cfe47d021f34ee
[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, 
62                 List<List<String>> orgInfo ) {
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 = createOrgAuthorityInstance(orgAuthorityName, 
75                         fullOrgAuthRefName);
76         ClientResponse<Response> res = client.create(multipart);
77
78         int statusCode = res.getStatus();
79
80         if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
81                 throw new RuntimeException("Could not create enumeration: \""+orgAuthorityName
82                                 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
83         }
84         if(statusCode != EXPECTED_STATUS_CODE) {
85                 throw new RuntimeException("Unexpected Status when creating enumeration: \""
86                                 +orgAuthorityName +"\", Status:"+ statusCode);
87         }
88
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:"
94                                 +newOrgAuthorityId );
95         }
96         for(List<String> orgStrings : orgInfo){
97                 createItemInAuthority(newOrgAuthorityId, baseOrgAuthRefName, orgStrings);
98         }
99     }
100     
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;
107                 /* Strings are:  
108                         shortName, longName, nameAdditions, contactName, 
109                 foundingDate, dissolutionDate, foundingPlace, function, description
110                  */             
111         String shortName = orgStrings.get(0);
112         String refName = createOrganizationRefName(orgAuthorityRefName, shortName)+"'"+shortName+"'";
113
114         if(logger.isDebugEnabled()){
115                 logger.debug("Import: Create Item: \""+shortName
116                                 +"\" in orgAuthorityulary: \"" + orgAuthorityRefName +"\"");
117         }
118         MultipartOutput multipart = createOrganizationInstance( vcsid, refName,
119                         orgStrings );
120         ClientResponse<Response> res = client.createItem(vcsid, multipart);
121
122         int statusCode = res.getStatus();
123
124         if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
125                 throw new RuntimeException("Could not create Item: \""+shortName
126                                 +"\" in orgAuthority: \"" + orgAuthorityRefName
127                                 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
128         }
129         if(statusCode != EXPECTED_STATUS_CODE) {
130                 throw new RuntimeException("Unexpected Status when creating Item: \""+shortName
131                                 +"\" in orgAuthority: \"" + orgAuthorityRefName +"\", Status:"+ statusCode);
132         }
133
134         return extractId(res);
135     }
136
137     // ---------------------------------------------------------------
138     // Utility methods used by methods above
139     // ---------------------------------------------------------------
140
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());
150
151         if(logger.isDebugEnabled()){
152                 logger.debug("to be created, orgAuthority common ", 
153                                         orgAuthority, OrgauthoritiesCommon.class);
154         }
155
156         return multipart;
157     }
158
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);
166         if(longName!=null)
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);
184         if(function!=null)
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());
193
194         if(logger.isDebugEnabled()){
195                 logger.debug("to be created, organization common ", organization, OrganizationsCommon.class);
196         }
197
198         return multipart;
199     }
200
201
202     /**
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.
206      *
207      * @param serviceRequestType  A type of service request (e.g. CREATE, DELETE).
208      *
209      * @param statusCode  The invalid status code that was returned in the response,
210      *                    from submitting that type of request to the service.
211      *
212      * @return An error message.
213      */
214     protected String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {
215         return "Status code '" + statusCode + "' in response is NOT within the expected set: " +
216                 requestType.validStatusCodesAsString();
217     }
218
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);
224         }
225         String[] segments = uri.split("/");
226         String id = segments[segments.length - 1];
227         if(logger.isDebugEnabled()){
228                 logger.debug("id=" + id);
229         }
230         return id;
231     }
232     
233     protected String createOrgAuthRefName(String orgAuthorityName) {
234         return "urn:cspace:org.collectionspace.demo:orgauthority:name("
235                         +orgAuthorityName+")";
236     }
237
238     protected String createOrganizationRefName(
239                                                 String orgAuthRefName, String orgName) {
240         return orgAuthRefName+":organization:name("+orgName+")";
241     }
242
243         public static void main(String[] args) {
244                 
245                 BasicConfigurator.configure();
246                 logger.info("OrgAuthorityBaseImport starting...");
247
248                 OrgAuthorityBaseImport oabi = new OrgAuthorityBaseImport();
249                 final String demoOrgAuthorityName = "Demo Org Authority";
250
251                 /* Strings are:  
252                         shortName, longName, nameAdditions, contactName, 
253                 foundingDate, dissolutionDate, foundingPlace, function, description
254          */             
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 );
267
268                 oabi.createOrgAuthority(demoOrgAuthorityName, orgsStrings);
269
270                 logger.info("OrgAuthorityBaseImport complete.");
271         }
272 }