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.ConditionGroupList;
\r
18 import org.collectionspace.services.location.ConditionGroup;
\r
19 import org.collectionspace.services.location.LocationauthoritiesCommon;
\r
20 import org.dom4j.DocumentException;
\r
21 import org.jboss.resteasy.client.ClientResponse;
\r
22 import org.slf4j.Logger;
\r
23 import org.slf4j.LoggerFactory;
\r
24 import org.testng.Assert;
\r
26 public class LocationAuthorityClientUtils {
\r
27 private static final Logger logger =
\r
28 LoggerFactory.getLogger(LocationAuthorityClientUtils.class);
\r
31 * Creates a new Location Authority
\r
32 * @param displayName The displayName used in UI, etc.
\r
33 * @param refName The proper refName for this authority
\r
34 * @param headerLabel The common part label
\r
35 * @return The PoxPayloadOut payload for the create call
\r
37 public static PoxPayloadOut createLocationAuthorityInstance(
\r
38 String displayName, String shortIdentifier, String headerLabel ) {
\r
39 LocationauthoritiesCommon locationAuthority = new LocationauthoritiesCommon();
\r
40 locationAuthority.setDisplayName(displayName);
\r
41 locationAuthority.setShortIdentifier(shortIdentifier);
\r
42 // String refName = createLocationAuthRefName(shortIdentifier, displayName);
\r
43 // locationAuthority.setRefName(refName);
\r
44 locationAuthority.setVocabType("LocationAuthority"); //FIXME: REM - Should this really be hard-coded?
\r
45 PoxPayloadOut multipart = new PoxPayloadOut(LocationAuthorityClient.SERVICE_PAYLOAD_NAME);
\r
46 PayloadOutputPart commonPart = multipart.addPart(locationAuthority, MediaType.APPLICATION_XML_TYPE);
\r
47 commonPart.setLabel(headerLabel);
\r
49 if(logger.isDebugEnabled()){
\r
50 logger.debug("to be created, locationAuthority common ",
\r
51 locationAuthority, LocationauthoritiesCommon.class);
\r
58 * @param locationRefName The proper refName for this authority
\r
59 * @param locationInfo the properties for the new Location. Can pass in one condition
\r
60 * note and date string.
\r
61 * @param headerLabel The common part label
\r
62 * @return The PoxPayloadOut payload for the create call
\r
64 public static PoxPayloadOut createLocationInstance(
\r
65 String locationAuthRefName, Map<String, String> locationInfo,
\r
66 String headerLabel){
\r
67 LocationsCommon location = new LocationsCommon();
\r
68 String shortId = locationInfo.get(LocationJAXBSchema.SHORT_IDENTIFIER);
\r
69 String displayName = locationInfo.get(LocationJAXBSchema.DISPLAY_NAME);
\r
70 location.setShortIdentifier(shortId);
\r
71 // String locationRefName = createLocationRefName(locationAuthRefName, shortId, displayName);
\r
72 // location.setRefName(locationRefName);
\r
73 String value = null;
\r
74 value = locationInfo.get(LocationJAXBSchema.DISPLAY_NAME_COMPUTED);
\r
75 boolean displayNameComputed = (value==null) || value.equalsIgnoreCase("true");
\r
76 location.setDisplayNameComputed(displayNameComputed);
\r
77 if((value = (String)locationInfo.get(LocationJAXBSchema.NAME))!=null)
\r
78 location.setName(value);
\r
79 if((value = (String)locationInfo.get(LocationJAXBSchema.CONDITION_NOTE))!=null) {
\r
80 ConditionGroupList conditionGroupList = new ConditionGroupList();
\r
81 List<ConditionGroup> conditionGroups = conditionGroupList.getConditionGroup();
\r
82 ConditionGroup conditionGroup = new ConditionGroup();
\r
83 conditionGroup.setConditionNote(value);
\r
84 if((value = (String)locationInfo.get(LocationJAXBSchema.CONDITION_NOTE_DATE))!=null)
\r
85 conditionGroup.setConditionNoteDate(value);
\r
86 conditionGroups.add(conditionGroup);
\r
87 location.setConditionGroupList(conditionGroupList);
\r
89 if((value = (String)locationInfo.get(LocationJAXBSchema.SECURITY_NOTE))!=null)
\r
90 location.setSecurityNote(value);
\r
91 if((value = (String)locationInfo.get(LocationJAXBSchema.ACCESS_NOTE))!=null)
\r
92 location.setAccessNote(value);
\r
93 if((value = (String)locationInfo.get(LocationJAXBSchema.LOCATION_TYPE))!=null)
\r
94 location.setLocationType(value);
\r
95 if((value = (String)locationInfo.get(LocationJAXBSchema.ADDRESS))!=null)
\r
96 location.setAddress(value);
\r
97 if((value = (String)locationInfo.get(LocationJAXBSchema.TERM_STATUS))!=null)
\r
98 location.setTermStatus(value);
\r
100 PoxPayloadOut multipart = new PoxPayloadOut(LocationAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
\r
101 PayloadOutputPart commonPart = multipart.addPart(location,
\r
102 MediaType.APPLICATION_XML_TYPE);
\r
103 commonPart.setLabel(headerLabel);
\r
105 if(logger.isDebugEnabled()){
\r
106 logger.debug("to be created, location common ", location, LocationsCommon.class);
\r
113 * @param vcsid CSID of the authority to create a new location in
\r
114 * @param locationAuthorityRefName The refName for the authority
\r
115 * @param locationMap the properties for the new Location
\r
116 * @param client the service client
\r
117 * @return the CSID of the new item
\r
119 public static String createItemInAuthority(String vcsid,
\r
120 String locationAuthorityRefName, Map<String,String> locationMap,
\r
121 LocationAuthorityClient client ) {
\r
122 // Expected status code: 201 Created
\r
123 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
\r
124 // Type of service request being tested
\r
125 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
\r
127 String displayName = locationMap.get(LocationJAXBSchema.DISPLAY_NAME);
\r
128 String displayNameComputedStr = locationMap.get(LocationJAXBSchema.DISPLAY_NAME_COMPUTED);
\r
129 boolean displayNameComputed = (displayNameComputedStr==null) || displayNameComputedStr.equalsIgnoreCase("true");
\r
130 if( displayName == null ) {
\r
131 if(!displayNameComputed) {
\r
132 throw new RuntimeException(
\r
133 "CreateItem: Must supply a displayName if displayNameComputed is set to false.");
\r
136 prepareDefaultDisplayName(
\r
137 locationMap.get(LocationJAXBSchema.NAME));
\r
140 if(logger.isDebugEnabled()){
\r
141 logger.debug("Import: Create Item: \""+displayName
\r
142 +"\" in locationAuthority: \"" + locationAuthorityRefName +"\"");
\r
144 PoxPayloadOut multipart =
\r
145 createLocationInstance( locationAuthorityRefName,
\r
146 locationMap, client.getItemCommonPartName() );
\r
147 String newID = null;
\r
148 ClientResponse<Response> res = client.createItem(vcsid, multipart);
\r
150 int statusCode = res.getStatus();
\r
152 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
\r
153 throw new RuntimeException("Could not create Item: \""
\r
154 +locationMap.get(LocationJAXBSchema.SHORT_IDENTIFIER)
\r
155 +"\" in locationAuthority: \"" + locationAuthorityRefName
\r
156 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
158 if(statusCode != EXPECTED_STATUS_CODE) {
\r
159 throw new RuntimeException("Unexpected Status when creating Item: \""
\r
160 +locationMap.get(LocationJAXBSchema.SHORT_IDENTIFIER)
\r
161 +"\" in locationAuthority: \"" + locationAuthorityRefName +"\", Status:"+ statusCode);
\r
163 newID = extractId(res);
\r
165 res.releaseConnection();
\r
171 public static PoxPayloadOut createLocationInstance(
\r
172 String commonPartXML, String headerLabel) throws DocumentException {
\r
173 PoxPayloadOut multipart = new PoxPayloadOut(LocationAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
\r
174 PayloadOutputPart commonPart = multipart.addPart(commonPartXML,
\r
175 MediaType.APPLICATION_XML_TYPE);
\r
176 commonPart.setLabel(headerLabel);
\r
178 if(logger.isDebugEnabled()){
\r
179 logger.debug("to be created, location common ", commonPartXML);
\r
185 public static String createItemInAuthority(String vcsid,
\r
186 String commonPartXML,
\r
187 LocationAuthorityClient client ) throws DocumentException {
\r
188 // Expected status code: 201 Created
\r
189 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
\r
190 // Type of service request being tested
\r
191 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
\r
193 PoxPayloadOut multipart =
\r
194 createLocationInstance(commonPartXML, client.getItemCommonPartName());
\r
195 String newID = null;
\r
196 ClientResponse<Response> res = client.createItem(vcsid, multipart);
\r
198 int statusCode = res.getStatus();
\r
200 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
\r
201 throw new RuntimeException("Could not create Item: \""+commonPartXML
\r
202 +"\" in locationAuthority: \"" + vcsid
\r
203 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
205 if(statusCode != EXPECTED_STATUS_CODE) {
\r
206 throw new RuntimeException("Unexpected Status when creating Item: \""+commonPartXML
\r
207 +"\" in locationAuthority: \"" + vcsid +"\", Status:"+ statusCode);
\r
209 newID = extractId(res);
\r
211 res.releaseConnection();
\r
218 * Creates the from xml file.
\r
220 * @param fileName the file name
\r
221 * @return new CSID as string
\r
222 * @throws Exception the exception
\r
224 private String createItemInAuthorityFromXmlFile(String vcsid, String commonPartFileName,
\r
225 LocationAuthorityClient client) throws Exception {
\r
226 byte[] b = FileUtils.readFileToByteArray(new File(commonPartFileName));
\r
227 String commonPartXML = new String(b);
\r
228 return createItemInAuthority(vcsid, commonPartXML, client );
\r
232 * Creates the locationAuthority ref name.
\r
234 * @param shortId the locationAuthority shortIdentifier
\r
235 * @param displaySuffix displayName to be appended, if non-null
\r
236 * @return the string
\r
238 public static String createLocationAuthRefName(String shortId, String displaySuffix) {
\r
239 String refName = "urn:cspace:org.collectionspace.demo:locationauthority:name("
\r
241 if(displaySuffix!=null&&!displaySuffix.isEmpty())
\r
242 refName += "'"+displaySuffix+"'";
\r
247 * Creates the location ref name.
\r
249 * @param locationAuthRefName the locationAuthority ref name
\r
250 * @param shortId the location shortIdentifier
\r
251 * @param displaySuffix displayName to be appended, if non-null
\r
252 * @return the string
\r
254 public static String createLocationRefName(
\r
255 String locationAuthRefName, String shortId, String displaySuffix) {
\r
256 String refName = locationAuthRefName+":location:name("+shortId+")";
\r
257 if(displaySuffix!=null&&!displaySuffix.isEmpty())
\r
258 refName += "'"+displaySuffix+"'";
\r
262 public static String extractId(ClientResponse<Response> res) {
\r
263 MultivaluedMap<String, Object> mvm = res.getMetadata();
\r
264 String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);
\r
265 if(logger.isDebugEnabled()){
\r
266 logger.debug("extractId:uri=" + uri);
\r
268 String[] segments = uri.split("/");
\r
269 String id = segments[segments.length - 1];
\r
270 if(logger.isDebugEnabled()){
\r
271 logger.debug("id=" + id);
\r
277 * Returns an error message indicating that the status code returned by a
\r
278 * specific call to a service does not fall within a set of valid status
\r
279 * codes for that service.
\r
281 * @param serviceRequestType A type of service request (e.g. CREATE, DELETE).
\r
283 * @param statusCode The invalid status code that was returned in the response,
\r
284 * from submitting that type of request to the service.
\r
286 * @return An error message.
\r
288 public static String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {
\r
289 return "Status code '" + statusCode + "' in response is NOT within the expected set: " +
\r
290 requestType.validStatusCodesAsString();
\r
296 * Produces a default displayName from the basic name and dates fields.
\r
297 * @see LocationDocumentModelHandler.prepareDefaultDisplayName() which
\r
298 * duplicates this logic, until we define a service-general utils package
\r
299 * that is neither client nor service specific.
\r
303 public static String prepareDefaultDisplayName(
\r
305 StringBuilder newStr = new StringBuilder();
\r
306 newStr.append(name);
\r
307 return newStr.toString();
\r