]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
24a7eb1b93ba37f9555d6abe3ce910d148f8d5bc
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.client.test;
2
3 import javax.ws.rs.core.Response;
4
5 import org.collectionspace.services.client.AuthorityClient;
6 import org.collectionspace.services.client.CollectionSpaceClient;
7 import org.collectionspace.services.client.PayloadInputPart;
8 import org.collectionspace.services.client.PayloadOutputPart;
9 import org.collectionspace.services.client.PoxPayloadIn;
10 import org.collectionspace.services.client.PoxPayloadOut;
11 import org.collectionspace.services.client.AbstractCommonListUtils;
12 import org.collectionspace.services.client.XmlTools;
13 import org.collectionspace.services.client.workflow.WorkflowClient;
14 import org.collectionspace.services.jaxb.AbstractCommonList;
15 import org.dom4j.Document;
16 import org.jboss.resteasy.client.ClientResponse;
17 import org.testng.Assert;
18
19 /*
20  * <CLT> - Common list type
21  * <CPT> - Common part type
22  */
23 public abstract class AbstractPoxServiceTestImpl<CLT extends AbstractCommonList, CPT>
24                 extends AbstractServiceTestImpl<CLT, CPT, PoxPayloadOut, String> {
25                 
26         @Override
27         public CPT extractCommonPartValue(Response res) throws Exception {
28                 CPT result = null;
29                 
30                 CollectionSpaceClient client = getClientInstance();
31                 PayloadInputPart payloadInputPart = extractPart(res, client.getCommonPartName());
32                 if (payloadInputPart != null) {
33                         result = (CPT) payloadInputPart.getBody();
34                 }
35                 Assert.assertNotNull(result,
36                                 "Part or body of part " + client.getCommonPartName() + " was unexpectedly null.");
37                 
38                 return result;
39         }
40         
41     protected void printList(String testName, CLT list) {
42         if (getLogger().isDebugEnabled()){
43                 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, getLogger(), testName);
44         }
45     }
46     
47     @Override
48     protected long getSizeOfList(CLT list) {
49         return list.getTotalItems();            
50     }
51     
52     /**
53      * Extracts the workflow state of PoxPayloadIn
54      * 
55      * @param res
56      * @return
57      * @throws Exception
58      */
59         protected String extractAuthorityWorkflowState(PoxPayloadIn input) throws Exception {
60                 String result = null;
61                 
62                 Document document = input.getDOMDocument();
63                 result = XmlTools.getElementValue(document, "//" + WorkflowClient.WORKFLOWSTATE_XML_ELEMENT_NAME);
64
65                 return result;
66         }
67     
68     /**
69      * Extracts the workflow state of a response payload
70      * 
71      * @param res
72      * @return
73      * @throws Exception
74      */
75         protected String extractAuthorityWorkflowState(Response res) throws Exception {
76                 String result = null;
77                 
78         PoxPayloadIn input = new PoxPayloadIn((String)res.readEntity(getEntityResponseType()));         
79         result = extractAuthorityWorkflowState(input);
80                         
81                 return result;
82         }
83     
84     /**
85      * The entity type expected from the JAX-RS Response object
86      */
87     public Class<String> getEntityResponseType() {
88         return String.class;
89     }
90         
91         @Override
92     public CPT extractCommonPartValue(PoxPayloadOut payloadOut) throws Exception {
93         CPT result = null;
94         
95         CollectionSpaceClient client = getClientInstance();
96         PayloadOutputPart payloadOutputPart = payloadOut.getPart(client.getCommonPartName());
97         if (payloadOutputPart != null) {
98                 result = (CPT) payloadOutputPart.getBody();
99         }
100         Assert.assertNotNull(result,
101                 "Part or body of part " + client.getCommonPartName() + " was unexpectedly null.");
102         
103         return result;
104     }
105                 
106         @Override
107         public PoxPayloadOut createRequestTypeInstance(CPT commonPartTypeInstance) throws Exception {
108                 PoxPayloadOut result = null;
109                 
110                 CollectionSpaceClient client = this.getClientInstance();
111         PoxPayloadOut payloadOut = new PoxPayloadOut(this.getServicePathComponent());
112         PayloadOutputPart part = payloadOut.addPart(client.getCommonPartName(), commonPartTypeInstance);
113         result = payloadOut;
114                 
115                 return result;
116         }
117     
118     protected PayloadInputPart extractPart(Response res, String partLabel)
119             throws Exception {
120             if (getLogger().isDebugEnabled()) {
121                 getLogger().debug("Reading part " + partLabel + " ...");
122             }
123             PoxPayloadIn input = new PoxPayloadIn((String)res.readEntity(getEntityResponseType()));
124             PayloadInputPart payloadInputPart = input.getPart(partLabel);
125             Assert.assertNotNull(payloadInputPart,
126                     "Part " + partLabel + " was unexpectedly null.");
127             return payloadInputPart;
128     }
129 }