1 package org.collectionspace.services.client;
4 import java.util.ArrayList;
5 import java.util.Arrays;
9 import javax.ws.rs.core.MediaType;
10 import javax.ws.rs.core.MultivaluedMap;
11 import javax.ws.rs.core.Response;
13 import org.apache.commons.io.FileUtils;
14 import org.collectionspace.services.TaxonomyJAXBSchema;
15 import org.collectionspace.services.client.test.ServiceRequestType;
16 import org.collectionspace.services.taxonomy.TaxonomyCommon;
17 import org.collectionspace.services.taxonomy.TaxonomyauthorityCommon;
18 import org.dom4j.DocumentException;
19 import org.jboss.resteasy.client.ClientResponse;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import org.testng.Assert;
24 public class TaxonomyAuthorityClientUtils {
25 private static final Logger logger =
26 LoggerFactory.getLogger(TaxonomyAuthorityClientUtils.class);
29 * Creates a new Taxonomy Authority
30 * @param displayName The displayName used in UI, etc.
31 * @param refName The proper refName for this authority
32 * @param headerLabel The common part label
33 * @return The PoxPayloadOut payload for the create call
35 public static PoxPayloadOut createTaxonomyAuthorityInstance(
36 String displayName, String shortIdentifier, String headerLabel ) {
37 TaxonomyauthorityCommon Taxonomyauthority = new TaxonomyauthorityCommon();
38 Taxonomyauthority.setDisplayName(displayName);
39 Taxonomyauthority.setShortIdentifier(shortIdentifier);
40 String refName = createTaxonomyAuthRefName(shortIdentifier, displayName);
41 Taxonomyauthority.setRefName(refName);
42 Taxonomyauthority.setVocabType("Taxonomyauthority"); //FIXME: REM - Should this really be hard-coded?
43 PoxPayloadOut multipart = new PoxPayloadOut(TaxonomyAuthorityClient.SERVICE_PAYLOAD_NAME);
44 PayloadOutputPart commonPart = multipart.addPart(Taxonomyauthority, MediaType.APPLICATION_XML_TYPE);
45 commonPart.setLabel(headerLabel);
47 if(logger.isDebugEnabled()){
48 logger.debug("to be created, Taxonomyauthority common ",
49 Taxonomyauthority, TaxonomyauthorityCommon.class);
56 * @param taxonomyRefName The proper refName for this authority
57 * @param taxonomyInfo the properties for the new Taxonomy. Can pass in one condition
58 * note and date string.
59 * @param headerLabel The common part label
60 * @return The PoxPayloadOut payload for the create call
62 public static PoxPayloadOut createTaxonomyInstance(
63 String taxonomyAuthRefName, Map<String, String> taxonomyInfo,
65 TaxonomyCommon taxonomy = new TaxonomyCommon();
66 String shortId = taxonomyInfo.get(TaxonomyJAXBSchema.SHORT_IDENTIFIER);
67 String displayName = taxonomyInfo.get(TaxonomyJAXBSchema.DISPLAY_NAME);
68 taxonomy.setShortIdentifier(shortId);
69 String taxonomyRefName = createTaxonomyRefName(taxonomyAuthRefName, shortId, displayName);
70 taxonomy.setRefName(taxonomyRefName);
72 value = taxonomyInfo.get(TaxonomyJAXBSchema.DISPLAY_NAME_COMPUTED);
73 boolean displayNameComputed = (value==null) || value.equalsIgnoreCase("true");
74 taxonomy.setDisplayNameComputed(displayNameComputed);
75 if((value = (String)taxonomyInfo.get(TaxonomyJAXBSchema.FULL_NAME))!=null)
76 taxonomy.setTaxonFullName(value);
77 if((value = (String)taxonomyInfo.get(TaxonomyJAXBSchema.TAXON_RANK))!=null)
78 taxonomy.setTaxonRank(value);
79 if((value = (String)taxonomyInfo.get(TaxonomyJAXBSchema.TERM_STATUS))!=null)
80 taxonomy.setTermStatus(value);
82 PoxPayloadOut multipart = new PoxPayloadOut(TaxonomyAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
83 PayloadOutputPart commonPart = multipart.addPart(taxonomy,
84 MediaType.APPLICATION_XML_TYPE);
85 commonPart.setLabel(headerLabel);
87 if(logger.isDebugEnabled()){
88 logger.debug("to be created, taxonomy common ", taxonomy, TaxonomyCommon.class);
95 * @param vcsid CSID of the authority to create a new taxonomy in
96 * @param TaxonomyauthorityRefName The refName for the authority
97 * @param taxonomyMap the properties for the new Taxonomy
98 * @param client the service client
99 * @return the CSID of the new item
101 public static String createItemInAuthority(String vcsid,
102 String TaxonomyauthorityRefName, Map<String,String> taxonomyMap,
103 TaxonomyAuthorityClient client ) {
104 // Expected status code: 201 Created
105 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
106 // Type of service request being tested
107 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
109 String displayName = taxonomyMap.get(TaxonomyJAXBSchema.DISPLAY_NAME);
110 String displayNameComputedStr = taxonomyMap.get(TaxonomyJAXBSchema.DISPLAY_NAME_COMPUTED);
111 boolean displayNameComputed = (displayNameComputedStr==null) || displayNameComputedStr.equalsIgnoreCase("true");
112 if( displayName == null ) {
113 if(!displayNameComputed) {
114 throw new RuntimeException(
115 "CreateItem: Must supply a displayName if displayNameComputed is set to false.");
118 prepareDefaultDisplayName(
119 taxonomyMap.get(TaxonomyJAXBSchema.FULL_NAME));
122 if(logger.isDebugEnabled()){
123 logger.debug("Import: Create Item: \""+displayName
124 +"\" in Taxonomyauthority: \"" + TaxonomyauthorityRefName +"\"");
126 PoxPayloadOut multipart =
127 createTaxonomyInstance( TaxonomyauthorityRefName,
128 taxonomyMap, client.getItemCommonPartName() );
130 ClientResponse<Response> res = client.createItem(vcsid, multipart);
132 int statusCode = res.getStatus();
134 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
135 throw new RuntimeException("Could not create Item: \""
136 +taxonomyMap.get(TaxonomyJAXBSchema.SHORT_IDENTIFIER)
137 +"\" in Taxonomyauthority: \"" + TaxonomyauthorityRefName
138 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
140 if(statusCode != EXPECTED_STATUS_CODE) {
141 throw new RuntimeException("Unexpected Status when creating Item: \""
142 +taxonomyMap.get(TaxonomyJAXBSchema.SHORT_IDENTIFIER)
143 +"\" in Taxonomyauthority: \"" + TaxonomyauthorityRefName +"\", Status:"+ statusCode);
145 newID = extractId(res);
147 res.releaseConnection();
153 public static PoxPayloadOut createTaxonomyInstance(
154 String commonPartXML, String headerLabel) throws DocumentException {
155 PoxPayloadOut multipart = new PoxPayloadOut(TaxonomyAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
156 PayloadOutputPart commonPart = multipart.addPart(commonPartXML,
157 MediaType.APPLICATION_XML_TYPE);
158 commonPart.setLabel(headerLabel);
160 if(logger.isDebugEnabled()){
161 logger.debug("to be created, taxonomy common ", commonPartXML);
167 public static String createItemInAuthority(String vcsid,
168 String commonPartXML,
169 TaxonomyAuthorityClient client ) throws DocumentException {
170 // Expected status code: 201 Created
171 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
172 // Type of service request being tested
173 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
175 PoxPayloadOut multipart =
176 createTaxonomyInstance(commonPartXML, client.getItemCommonPartName());
178 ClientResponse<Response> res = client.createItem(vcsid, multipart);
180 int statusCode = res.getStatus();
182 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
183 throw new RuntimeException("Could not create Item: \""+commonPartXML
184 +"\" in Taxonomyauthority: \"" + vcsid
185 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
187 if(statusCode != EXPECTED_STATUS_CODE) {
188 throw new RuntimeException("Unexpected Status when creating Item: \""+commonPartXML
189 +"\" in Taxonomyauthority: \"" + vcsid +"\", Status:"+ statusCode);
191 newID = extractId(res);
193 res.releaseConnection();
200 * Creates the from xml file.
202 * @param fileName the file name
203 * @return new CSID as string
204 * @throws Exception the exception
206 private String createItemInAuthorityFromXmlFile(String vcsid, String commonPartFileName,
207 TaxonomyAuthorityClient client) throws Exception {
208 byte[] b = FileUtils.readFileToByteArray(new File(commonPartFileName));
209 String commonPartXML = new String(b);
210 return createItemInAuthority(vcsid, commonPartXML, client );
214 * Creates the Taxonomyauthority ref name.
216 * @param shortId the Taxonomyauthority shortIdentifier
217 * @param displaySuffix displayName to be appended, if non-null
220 public static String createTaxonomyAuthRefName(String shortId, String displaySuffix) {
221 String refName = "urn:cspace:org.collectionspace.demo:taxonomyauthority:name("
223 if(displaySuffix!=null&&!displaySuffix.isEmpty())
224 refName += "'"+displaySuffix+"'";
229 * Creates the taxonomy ref name.
231 * @param taxonomyAuthRefName the Taxonomyauthority ref name
232 * @param shortId the taxonomy shortIdentifier
233 * @param displaySuffix displayName to be appended, if non-null
236 public static String createTaxonomyRefName(
237 String taxonomyAuthRefName, String shortId, String displaySuffix) {
238 String refName = taxonomyAuthRefName+":taxonomy:name("+shortId+")";
239 if(displaySuffix!=null&&!displaySuffix.isEmpty())
240 refName += "'"+displaySuffix+"'";
244 public static String extractId(ClientResponse<Response> res) {
245 MultivaluedMap<String, Object> mvm = res.getMetadata();
246 String uri = (String) ((ArrayList<Object>) mvm.get("Taxonomy")).get(0);
247 if(logger.isDebugEnabled()){
248 logger.debug("extractId:uri=" + uri);
250 String[] segments = uri.split("/");
251 String id = segments[segments.length - 1];
252 if(logger.isDebugEnabled()){
253 logger.debug("id=" + id);
259 * Returns an error message indicating that the status code returned by a
260 * specific call to a service does not fall within a set of valid status
261 * codes for that service.
263 * @param serviceRequestType A type of service request (e.g. CREATE, DELETE).
265 * @param statusCode The invalid status code that was returned in the response,
266 * from submitting that type of request to the service.
268 * @return An error message.
270 public static String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {
271 return "Status code '" + statusCode + "' in response is NOT within the expected set: " +
272 requestType.validStatusCodesAsString();
278 * Produces a default displayName from the basic name and dates fields.
279 * @see TaxonomyDocumentModelHandler.prepareDefaultDisplayName() which
280 * duplicates this logic, until we define a service-general utils package
281 * that is neither client nor service specific.
285 public static String prepareDefaultDisplayName(
287 StringBuilder newStr = new StringBuilder();
289 return newStr.toString();