1 package org.collectionspace.services.client;
\r
4 import java.util.ArrayList;
\r
5 import java.util.Arrays;
\r
6 import java.util.List;
\r
7 import java.util.Map;
\r
9 import javax.ws.rs.core.MediaType;
\r
10 import javax.ws.rs.core.MultivaluedMap;
\r
11 import javax.ws.rs.core.Response;
\r
13 import org.apache.commons.io.FileUtils;
\r
14 import org.collectionspace.services.LocationJAXBSchema;
\r
15 import org.collectionspace.services.client.test.ServiceRequestType;
\r
16 import org.collectionspace.services.location.LocationsCommon;
\r
17 import org.collectionspace.services.location.LocationauthoritiesCommon;
\r
18 import org.jboss.resteasy.client.ClientResponse;
\r
19 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
\r
20 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
\r
21 import org.slf4j.Logger;
\r
22 import org.slf4j.LoggerFactory;
\r
23 import org.testng.Assert;
\r
25 public class LocationAuthorityClientUtils {
\r
26 private static final Logger logger =
\r
27 LoggerFactory.getLogger(LocationAuthorityClientUtils.class);
\r
30 * Creates a new Location Authority
\r
31 * @param displayName The displayName used in UI, etc.
\r
32 * @param refName The proper refName for this authority
\r
33 * @param headerLabel The common part label
\r
34 * @return The MultipartOutput payload for the create call
\r
36 public static MultipartOutput createLocationAuthorityInstance(
\r
37 String displayName, String refName, String headerLabel ) {
\r
38 LocationauthoritiesCommon locationAuthority = new LocationauthoritiesCommon();
\r
39 locationAuthority.setDisplayName(displayName);
\r
40 locationAuthority.setRefName(refName);
\r
41 locationAuthority.setVocabType("LocationAuthority");
\r
42 MultipartOutput multipart = new MultipartOutput();
\r
43 OutputPart commonPart = multipart.addPart(locationAuthority, MediaType.APPLICATION_XML_TYPE);
\r
44 commonPart.getHeaders().add("label", headerLabel);
\r
46 if(logger.isDebugEnabled()){
\r
47 logger.debug("to be created, locationAuthority common ",
\r
48 locationAuthority, LocationauthoritiesCommon.class);
\r
55 * @param inAuthority CSID of the authority to create a new location in
\r
56 * @param locationRefName The proper refName for this authority
\r
57 * @param locationInfo the properties for the new Location
\r
58 * @param headerLabel The common part label
\r
59 * @return The MultipartOutput payload for the create call
\r
61 public static MultipartOutput createLocationInstance(String inAuthority,
\r
62 String locationRefName, Map<String, String> locationInfo, String headerLabel){
\r
63 LocationsCommon location = new LocationsCommon();
\r
64 location.setInAuthority(inAuthority);
\r
65 location.setRefName(locationRefName);
\r
66 String value = null;
\r
67 value = locationInfo.get(LocationJAXBSchema.DISPLAY_NAME_COMPUTED);
\r
68 boolean displayNameComputed = (value==null) || value.equalsIgnoreCase("true");
\r
69 location.setDisplayNameComputed(displayNameComputed);
\r
70 if((value = (String)locationInfo.get(LocationJAXBSchema.NAME))!=null)
\r
71 location.setName(value);
\r
72 if((value = (String)locationInfo.get(LocationJAXBSchema.CONDITION_NOTE))!=null)
\r
73 location.setConditionNote(value);
\r
74 if((value = (String)locationInfo.get(LocationJAXBSchema.CONDITION_NOTE_DATE))!=null)
\r
75 location.setConditionNoteDate(value);
\r
76 if((value = (String)locationInfo.get(LocationJAXBSchema.SECURITY_NOTE))!=null)
\r
77 location.setSecurityNote(value);
\r
78 if((value = (String)locationInfo.get(LocationJAXBSchema.LOCATION_TYPE))!=null)
\r
79 location.setLocationType(value);
\r
80 if((value = (String)locationInfo.get(LocationJAXBSchema.STATUS))!=null)
\r
81 location.setStatus(value);
\r
82 MultipartOutput multipart = new MultipartOutput();
\r
83 OutputPart commonPart = multipart.addPart(location,
\r
84 MediaType.APPLICATION_XML_TYPE);
\r
85 commonPart.getHeaders().add("label", headerLabel);
\r
87 if(logger.isDebugEnabled()){
\r
88 logger.debug("to be created, location common ", location, LocationsCommon.class);
\r
95 * @param vcsid CSID of the authority to create a new location in
\r
96 * @param locationAuthorityRefName The refName for the authority
\r
97 * @param locationMap the properties for the new Location
\r
98 * @param client the service client
\r
99 * @return the CSID of the new item
\r
101 public static String createItemInAuthority(String vcsid,
\r
102 String locationAuthorityRefName, Map<String,String> locationMap,
\r
103 LocationAuthorityClient client ) {
\r
104 // Expected status code: 201 Created
\r
105 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
\r
106 // Type of service request being tested
\r
107 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
\r
109 String displayName = locationMap.get(LocationJAXBSchema.DISPLAY_NAME);
\r
110 String displayNameComputedStr = locationMap.get(LocationJAXBSchema.DISPLAY_NAME_COMPUTED);
\r
111 boolean displayNameComputed = (displayNameComputedStr==null) || displayNameComputedStr.equalsIgnoreCase("true");
\r
112 if( displayName == null ) {
\r
113 if(!displayNameComputed) {
\r
114 throw new RuntimeException(
\r
115 "CreateItem: Must supply a displayName if displayNameComputed is set to false.");
\r
118 prepareDefaultDisplayName(
\r
119 locationMap.get(LocationJAXBSchema.NAME));
\r
122 String refName = createLocationRefName(locationAuthorityRefName, displayName, true);
\r
124 if(logger.isDebugEnabled()){
\r
125 logger.debug("Import: Create Item: \""+displayName
\r
126 +"\" in locationAuthority: \"" + locationAuthorityRefName +"\"");
\r
128 MultipartOutput multipart =
\r
129 createLocationInstance( vcsid, refName,
\r
130 locationMap, client.getItemCommonPartName() );
\r
131 ClientResponse<Response> res = client.createItem(vcsid, multipart);
\r
133 int statusCode = res.getStatus();
\r
135 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
\r
136 throw new RuntimeException("Could not create Item: \""+refName
\r
137 +"\" in locationAuthority: \"" + locationAuthorityRefName
\r
138 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
140 if(statusCode != EXPECTED_STATUS_CODE) {
\r
141 throw new RuntimeException("Unexpected Status when creating Item: \""+refName
\r
142 +"\" in locationAuthority: \"" + locationAuthorityRefName +"\", Status:"+ statusCode);
\r
145 return extractId(res);
\r
148 public static MultipartOutput createLocationInstance(
\r
149 String commonPartXML, String headerLabel){
\r
150 MultipartOutput multipart = new MultipartOutput();
\r
151 OutputPart commonPart = multipart.addPart(commonPartXML,
\r
152 MediaType.APPLICATION_XML_TYPE);
\r
153 commonPart.getHeaders().add("label", headerLabel);
\r
155 if(logger.isDebugEnabled()){
\r
156 logger.debug("to be created, location common ", commonPartXML);
\r
162 public static String createItemInAuthority(String vcsid,
\r
163 String commonPartXML,
\r
164 LocationAuthorityClient client ) {
\r
165 // Expected status code: 201 Created
\r
166 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
\r
167 // Type of service request being tested
\r
168 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
\r
170 MultipartOutput multipart =
\r
171 createLocationInstance( commonPartXML, client.getItemCommonPartName() );
\r
172 ClientResponse<Response> res = client.createItem(vcsid, multipart);
\r
174 int statusCode = res.getStatus();
\r
176 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
\r
177 throw new RuntimeException("Could not create Item: \""+commonPartXML
\r
178 +"\" in locationAuthority: \"" + vcsid
\r
179 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
181 if(statusCode != EXPECTED_STATUS_CODE) {
\r
182 throw new RuntimeException("Unexpected Status when creating Item: \""+commonPartXML
\r
183 +"\" in locationAuthority: \"" + vcsid +"\", Status:"+ statusCode);
\r
186 return extractId(res);
\r
190 * Creates the from xml file.
\r
192 * @param fileName the file name
\r
193 * @return new CSID as string
\r
194 * @throws Exception the exception
\r
196 private String createItemInAuthorityFromXmlFile(String vcsid, String commonPartFileName,
\r
197 LocationAuthorityClient client) throws Exception {
\r
198 byte[] b = FileUtils.readFileToByteArray(new File(commonPartFileName));
\r
199 String commonPartXML = new String(b);
\r
200 return createItemInAuthority(vcsid, commonPartXML, client );
\r
203 public static String createLocationAuthRefName(String locationAuthorityName, boolean withDisplaySuffix) {
\r
204 String refName = "urn:cspace:org.collectionspace.demo:locationauthority:name("
\r
205 +locationAuthorityName+")";
\r
206 if(withDisplaySuffix)
\r
207 refName += "'"+locationAuthorityName+"'";
\r
211 public static String createLocationRefName(
\r
212 String locationAuthRefName, String locationName, boolean withDisplaySuffix) {
\r
213 String refName = locationAuthRefName+":location:name("+locationName+")";
\r
214 if(withDisplaySuffix)
\r
215 refName += "'"+locationName+"'";
\r
219 public static String extractId(ClientResponse<Response> res) {
\r
220 MultivaluedMap<String, Object> mvm = res.getMetadata();
\r
221 String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);
\r
222 if(logger.isDebugEnabled()){
\r
223 logger.debug("extractId:uri=" + uri);
\r
225 String[] segments = uri.split("/");
\r
226 String id = segments[segments.length - 1];
\r
227 if(logger.isDebugEnabled()){
\r
228 logger.debug("id=" + id);
\r
234 * Returns an error message indicating that the status code returned by a
\r
235 * specific call to a service does not fall within a set of valid status
\r
236 * codes for that service.
\r
238 * @param serviceRequestType A type of service request (e.g. CREATE, DELETE).
\r
240 * @param statusCode The invalid status code that was returned in the response,
\r
241 * from submitting that type of request to the service.
\r
243 * @return An error message.
\r
245 public static String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {
\r
246 return "Status code '" + statusCode + "' in response is NOT within the expected set: " +
\r
247 requestType.validStatusCodesAsString();
\r
253 * Produces a default displayName from the basic name and dates fields.
\r
254 * @see LocationDocumentModelHandler.prepareDefaultDisplayName() which
\r
255 * duplicates this logic, until we define a service-general utils package
\r
256 * that is neither client nor service specific.
\r
260 public static String prepareDefaultDisplayName(
\r
262 StringBuilder newStr = new StringBuilder();
\r
263 newStr.append(name);
\r
264 return newStr.toString();
\r