]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
9197df8ffd7104b9025a260fec347de12d12adcf
[tmp/jakarta-migration.git] /
1 /**
2  * This document is a part of the source code and related artifacts
3  * for CollectionSpace, an open source collections management system
4  * for museums and related institutions:
5  *
6  * http://www.collectionspace.org
7  * http://wiki.collectionspace.org
8  *
9  * Copyright © 2009 Regents of the University of California
10  *
11  * Licensed under the Educational Community License (ECL), Version 2.0.
12  * You may not use this file except in compliance with this License.
13  *
14  * You may obtain a copy of the ECL 2.0 License at
15  * https://source.collectionspace.org/collection-space/LICENSE.txt
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */
23 package org.collectionspace.services.client.test;
24
25 import javax.ws.rs.core.MediaType;
26 import javax.ws.rs.core.Response;
27
28 import org.collectionspace.services.client.CollectionSpaceClient;
29 import org.collectionspace.services.client.HeldintrustClient;
30 import org.collectionspace.services.client.PayloadInputPart;
31 import org.collectionspace.services.client.PayloadOutputPart;
32 import org.collectionspace.services.client.PoxPayloadIn;
33 import org.collectionspace.services.client.PoxPayloadOut;
34 import org.collectionspace.services.common.api.GregorianCalendarDateTimeUtils;
35 import org.collectionspace.services.heldintrust.HeldintrustsCommon;
36 import org.collectionspace.services.jaxb.AbstractCommonList;
37 import org.dom4j.Element;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.testng.Assert;
41 import org.testng.annotations.Test;
42
43 /**
44  * HeldintrustServiceTest, carries out tests against a deployed and running Hit Service.
45  *
46  * FIXME: http://issues.collectionspace.org/browse/CSPACE-1685
47  *
48  * $LastChangedRevision$
49  * $LastChangedDate$
50  */
51 public class HeldintrustServiceTest extends AbstractPoxServiceTestImpl<AbstractCommonList, HeldintrustsCommon> {
52
53     private final static String CURRENT_DATE_UTC = GregorianCalendarDateTimeUtils.currentDateUTC();
54
55     /**
56      * The logger
57      */
58     private final Logger logger = LoggerFactory.getLogger(HeldintrustServiceTest.class);
59
60     @Override
61     protected CollectionSpaceClient getClientInstance() throws Exception {
62         return new HeldintrustClient();
63     }
64
65     @Override
66     protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) throws Exception {
67         return new HeldintrustClient(clientPropertiesFilename);
68     }
69
70     @Override
71     protected String getServiceName() {
72         return HeldintrustClient.SERVICE_NAME;
73     }
74
75     // ---------------------------------------------------------------
76     // CRUD tests : READ tests
77     // ---------------------------------------------------------------
78     @Override
79     protected void compareReadInstances(HeldintrustsCommon original, HeldintrustsCommon fromRead) {
80         // Add test here
81     }
82
83     @Override
84     public void delete(String testName) {
85         // Do nothing because this test is not ready to delete the "knownResourceId".
86         // Instead, the method localDelete() will get called later in the dependency chain. The
87         // method localDelete() has a dependency on the test "verifyReadOnlyCoreFields".  Once the
88         // "verifyReadOnlyCoreFields"
89         // test is run, the localDelete() test/method will get run.  The localDelete() test/method in turn
90         // calls the inherited delete() test/method.
91     }
92
93     @Test(dataProvider = "testName", dependsOnMethods = {"CRUDTests", "verifyReadOnlyCoreFields"})
94     public void localDelete(String testName) throws Exception {
95         // Because of issues with TestNG not allowing @Test annotations on override methods,
96         // and because we want the "updateWrongUser" to run before the "delete" test, we need
97         // this method.  This method will call super.delete() after all the dependencies have been
98         // met.
99         super.delete(testName);
100     }
101
102     @Test(dataProvider = "testName", dependsOnMethods = {"CRUDTests"})
103     public void verifyReadOnlyCoreFields(String testName) throws Exception {
104         // TODO These should be in some core client utils
105         final String COLLECTIONSPACE_CORE_SCHEMA = "collectionspace_core";
106         final String COLLECTIONSPACE_CORE_TENANTID = "tenantId";
107         final String COLLECTIONSPACE_CORE_URI = "uri";
108         final String COLLECTIONSPACE_CORE_CREATED_AT = "createdAt";
109         final String COLLECTIONSPACE_CORE_CREATED_BY = "createdBy";
110
111         // Perform setup.
112         setupUpdate();
113
114         // Retrieve the contents of a resource to update.
115         HeldintrustClient client = new HeldintrustClient();
116         PoxPayloadIn input;
117         Response res = client.read(knownResourceId);
118         try {
119             logger.debug("{}: read status = {}", testName, res.getStatus());
120             Assert.assertEquals(res.getStatus(), testExpectedStatusCode);
121
122             input = new PoxPayloadIn(res.readEntity(String.class));
123         } finally {
124             res.close();
125         }
126
127         PayloadInputPart payloadInputPart = input.getPart(COLLECTIONSPACE_CORE_SCHEMA);
128         Element coreAsElement = null;
129         if (payloadInputPart != null) {
130             coreAsElement = payloadInputPart.getElementBody();
131         }
132         Assert.assertNotNull(coreAsElement);
133         logger.debug("Core part before update:");
134         logger.debug("{}", coreAsElement.asXML());
135
136         // Update the read-only elements
137         Element tenantId = coreAsElement.element(COLLECTIONSPACE_CORE_TENANTID);
138         String originalTenantId = tenantId.getText();
139         tenantId.setText("foo");
140         Element uri = coreAsElement.element(COLLECTIONSPACE_CORE_URI);
141         String originalUri = uri.getText();
142         uri.setText("foo");
143         Element createdAt = coreAsElement.element(COLLECTIONSPACE_CORE_CREATED_AT);
144         String originalCreatedAt = createdAt.getText();
145         String now = GregorianCalendarDateTimeUtils.timestampUTC();
146         if (originalCreatedAt.equalsIgnoreCase(now)) {
147             logger.warn("Cannot check createdAt read-only; too fast!");
148         }
149         createdAt.setText(now);
150         Element createdBy = coreAsElement.element(COLLECTIONSPACE_CORE_CREATED_BY);
151         String originalCreatedBy = createdBy.getText();
152         createdBy.setText("foo");
153
154         logger.debug("Core part to be updated:");
155         logger.debug("{}", coreAsElement.asXML());
156
157         // Create an output payload to send to the service, and add the common part
158         PoxPayloadOut output = new PoxPayloadOut(this.getServicePathComponent());
159         output.addPart(COLLECTIONSPACE_CORE_SCHEMA, coreAsElement);
160
161         // Submit the request to the service and store the response.
162         res = client.update(knownResourceId, output);
163         try {
164             int statusCode = res.getStatus();
165             // Check the status code of the response: does it match the expected response(s)?
166             logger.debug("{}: status = {}", testName, statusCode);
167             Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
168                               invalidStatusCodeMessage(testRequestType, statusCode));
169             Assert.assertEquals(statusCode, testExpectedStatusCode);
170
171             input = new PoxPayloadIn(res.readEntity(String.class));
172         } finally {
173             res.close();
174         }
175
176         PayloadInputPart updatedCorePart = input.getPart(COLLECTIONSPACE_CORE_SCHEMA);
177         Element updatedCoreAsElement = null;
178         if (updatedCorePart != null) {
179             updatedCoreAsElement = updatedCorePart.getElementBody();
180         }
181         Assert.assertNotNull(updatedCoreAsElement);
182
183         tenantId = updatedCoreAsElement.element(COLLECTIONSPACE_CORE_TENANTID);
184         String updatedTenantId = tenantId.getText();
185         Assert.assertEquals(updatedTenantId, originalTenantId, "CORE part TenantID was able to update!");
186         uri = updatedCoreAsElement.element(COLLECTIONSPACE_CORE_URI);
187         String updatedUri = uri.getText();
188         Assert.assertEquals(updatedUri, originalUri, "CORE part URI was able to update!");
189         createdAt = updatedCoreAsElement.element(COLLECTIONSPACE_CORE_CREATED_AT);
190         String updatedCreatedAt = createdAt.getText();
191         Assert.assertEquals(updatedCreatedAt, originalCreatedAt, "CORE part CreatedAt was able to update!");
192         createdBy = updatedCoreAsElement.element(COLLECTIONSPACE_CORE_CREATED_BY);
193         String updatedCreatedBy = createdBy.getText();
194         Assert.assertEquals(updatedCreatedBy, originalCreatedBy, "CORE part CreatedBy was able to update!");
195     }
196
197     // ---------------------------------------------------------------
198     // Utility tests : tests of code used in tests above
199     // ---------------------------------------------------------------
200
201     /* (non-Javadoc)
202      * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
203      */
204     @Override
205     public String getServicePathComponent() {
206         return HeldintrustClient.SERVICE_PATH_COMPONENT;
207     }
208
209     /**
210      * Creates the hit instance.
211      *
212      * @param identifier the identifier
213      * @return the multipart output
214      * @throws Exception
215      */
216     @Override
217     protected PoxPayloadOut createInstance(String identifier) throws Exception {
218         return createHitInstance("entryNumber-" + identifier);
219     }
220
221     /**
222      * Creates the hit instance.
223      *
224      * @param entryNumber the entry number
225      * @return the multipart output
226      * @throws Exception
227      */
228     private PoxPayloadOut createHitInstance(String entryNumber) throws Exception {
229         HeldintrustsCommon hit = new HeldintrustsCommon();
230         hit.setHeldInTrustNumber(entryNumber);
231
232         PoxPayloadOut multipart = new PoxPayloadOut(HeldintrustClient.SERVICE_PAYLOAD_NAME);
233         PayloadOutputPart commonPart = multipart.addPart(hit, MediaType.APPLICATION_XML_TYPE);
234         commonPart.setLabel(new HeldintrustClient().getCommonPartName());
235
236         logger.debug("to be created, HitsCommon instance");
237         logger.debug("{}", objectAsXmlString(hit, HeldintrustsCommon.class));
238
239         return multipart;
240     }
241
242     @Override
243     protected PoxPayloadOut createInstance(String commonPartName, String identifier) throws Exception {
244         return this.createInstance(identifier);
245     }
246
247     @Override
248     protected HeldintrustsCommon updateInstance(HeldintrustsCommon hitsCommon) {
249         HeldintrustsCommon result = new HeldintrustsCommon();
250
251         result.setHeldInTrustNumber("hits");
252
253         return result;
254     }
255
256     @Override
257     protected void compareUpdatedInstances(HeldintrustsCommon original,
258                                            HeldintrustsCommon updated) {
259         // put test here
260     }
261
262     /*
263      * For convenience and terseness, this test method is the base of the test execution dependency chain.  Other
264      * test methods may
265      * refer to this method in their @Test annotation declarations.
266      */
267     @Override
268     @Test(dataProvider = "testName",
269           dependsOnMethods = {
270               "org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})
271     public void CRUDTests(String testName) {
272         // Needed for TestNG dependency chain.
273     }
274 }