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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright © 2009 Regents of the University of California
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
23 package org.collectionspace.services.client.test;
25 import javax.ws.rs.core.MediaType;
26 import javax.ws.rs.core.Response;
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;
44 * HeldintrustServiceTest, carries out tests against a deployed and running Hit Service.
46 * FIXME: http://issues.collectionspace.org/browse/CSPACE-1685
48 * $LastChangedRevision$
51 public class HeldintrustServiceTest extends AbstractPoxServiceTestImpl<AbstractCommonList, HeldintrustsCommon> {
53 private final static String CURRENT_DATE_UTC = GregorianCalendarDateTimeUtils.currentDateUTC();
58 private final Logger logger = LoggerFactory.getLogger(HeldintrustServiceTest.class);
61 protected CollectionSpaceClient getClientInstance() throws Exception {
62 return new HeldintrustClient();
66 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) throws Exception {
67 return new HeldintrustClient(clientPropertiesFilename);
71 protected String getServiceName() {
72 return HeldintrustClient.SERVICE_NAME;
75 // ---------------------------------------------------------------
76 // CRUD tests : READ tests
77 // ---------------------------------------------------------------
79 protected void compareReadInstances(HeldintrustsCommon original, HeldintrustsCommon fromRead) {
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.
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
99 super.delete(testName);
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";
114 // Retrieve the contents of a resource to update.
115 HeldintrustClient client = new HeldintrustClient();
117 Response res = client.read(knownResourceId);
119 logger.debug("{}: read status = {}", testName, res.getStatus());
120 Assert.assertEquals(res.getStatus(), testExpectedStatusCode);
122 input = new PoxPayloadIn(res.readEntity(String.class));
127 PayloadInputPart payloadInputPart = input.getPart(COLLECTIONSPACE_CORE_SCHEMA);
128 Element coreAsElement = null;
129 if (payloadInputPart != null) {
130 coreAsElement = payloadInputPart.getElementBody();
132 Assert.assertNotNull(coreAsElement);
133 logger.debug("Core part before update:");
134 logger.debug("{}", coreAsElement.asXML());
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();
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!");
149 createdAt.setText(now);
150 Element createdBy = coreAsElement.element(COLLECTIONSPACE_CORE_CREATED_BY);
151 String originalCreatedBy = createdBy.getText();
152 createdBy.setText("foo");
154 logger.debug("Core part to be updated:");
155 logger.debug("{}", coreAsElement.asXML());
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);
161 // Submit the request to the service and store the response.
162 res = client.update(knownResourceId, output);
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);
171 input = new PoxPayloadIn(res.readEntity(String.class));
176 PayloadInputPart updatedCorePart = input.getPart(COLLECTIONSPACE_CORE_SCHEMA);
177 Element updatedCoreAsElement = null;
178 if (updatedCorePart != null) {
179 updatedCoreAsElement = updatedCorePart.getElementBody();
181 Assert.assertNotNull(updatedCoreAsElement);
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!");
197 // ---------------------------------------------------------------
198 // Utility tests : tests of code used in tests above
199 // ---------------------------------------------------------------
202 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
205 public String getServicePathComponent() {
206 return HeldintrustClient.SERVICE_PATH_COMPONENT;
210 * Creates the hit instance.
212 * @param identifier the identifier
213 * @return the multipart output
217 protected PoxPayloadOut createInstance(String identifier) throws Exception {
218 return createHitInstance("entryNumber-" + identifier);
222 * Creates the hit instance.
224 * @param entryNumber the entry number
225 * @return the multipart output
228 private PoxPayloadOut createHitInstance(String entryNumber) throws Exception {
229 HeldintrustsCommon hit = new HeldintrustsCommon();
230 hit.setHeldInTrustNumber(entryNumber);
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());
236 logger.debug("to be created, HitsCommon instance");
237 logger.debug("{}", objectAsXmlString(hit, HeldintrustsCommon.class));
243 protected PoxPayloadOut createInstance(String commonPartName, String identifier) throws Exception {
244 return this.createInstance(identifier);
248 protected HeldintrustsCommon updateInstance(HeldintrustsCommon hitsCommon) {
249 HeldintrustsCommon result = new HeldintrustsCommon();
251 result.setHeldInTrustNumber("hits");
257 protected void compareUpdatedInstances(HeldintrustsCommon original,
258 HeldintrustsCommon updated) {
263 * For convenience and terseness, this test method is the base of the test execution dependency chain. Other
265 * refer to this method in their @Test annotation declarations.
268 @Test(dataProvider = "testName",
270 "org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})
271 public void CRUDTests(String testName) {
272 // Needed for TestNG dependency chain.