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