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 java.util.ArrayList;
26 import java.util.List;
27 import javax.ws.rs.core.MediaType;
28 import javax.ws.rs.core.Response;
29 import org.collectionspace.services.client.CollectionObjectClient;
30 import org.collectionspace.services.client.CollectionSpaceClient;
31 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
32 import org.collectionspace.services.collectionobject.CollectionobjectsCommonList;
33 import org.collectionspace.services.jaxb.AbstractCommonList;
34 import org.jboss.resteasy.client.ClientResponse;
35 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
36 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.testng.Assert;
40 import org.testng.annotations.AfterClass;
41 import org.testng.annotations.BeforeClass;
42 import org.testng.annotations.Test;
45 * CollectionObjectAuthRefsTest, carries out tests against a
46 * deployed and running CollectionObject Service.
48 * $LastChangedRevision: 1327 $
49 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
51 public class CollectionObjectSearchTest extends BaseServiceTest {
54 private final String CLASS_NAME = CollectionObjectSearchTest.class.getName();
55 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
57 final static String KEYWORD = "Tsolyani";
58 // final static String[] TWO_KEYWORDS = {"Cheggarra", "Ahoggya"};
59 final static String NOISE_WORD = "Mihalli";
60 final static String NON_EXISTENT_KEYWORD = "jlmbsoqjlmbsoq";
62 /* Use this to keep track of resources to delete */
63 private List<String> allResourceIdsCreated = new ArrayList<String>();
66 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
69 protected String getServicePathComponent() {
70 return new CollectionObjectClient().getServicePathComponent();
74 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
77 protected CollectionSpaceClient getClientInstance() {
78 return new CollectionObjectClient();
82 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
85 protected AbstractCommonList getAbstractCommonList(ClientResponse<AbstractCommonList> response) {
86 return response.getEntity(CollectionobjectsCommonList.class);
90 * Creates one or more resources containing a "noise" keyword,
91 * which should NOT be retrieved by keyword searches.
93 @BeforeClass(alwaysRun=true)
95 long numNoiseWordResources = 2;
96 if (logger.isDebugEnabled()) {
97 logger.debug("Creating " + numNoiseWordResources +
98 " 'noise word' resources ...");
100 createCollectionObjects(numNoiseWordResources, NOISE_WORD);
104 // ---------------------------------------------------------------
106 // ---------------------------------------------------------------
110 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
111 public void keywordSearchOneWord(String testName) throws Exception {
113 if (logger.isDebugEnabled()) {
114 logger.debug(testBanner(testName, CLASS_NAME));
117 // Create one or more keyword retrievable resources, each containing
118 // a specified keyword.
119 long numKeywordRetrievableResources = 3;
120 if (logger.isDebugEnabled()) {
121 logger.debug("Creating " + numKeywordRetrievableResources +
122 " keyword-retrievable resources ...");
124 createCollectionObjects(numKeywordRetrievableResources, KEYWORD);
126 testSetup(STATUS_OK, ServiceRequestType.SEARCH);
127 if (logger.isDebugEnabled()) {
128 logger.debug("Searching on keyword(s): " + KEYWORD + " ...");
130 CollectionObjectClient client = new CollectionObjectClient();
131 ClientResponse<CollectionobjectsCommonList> res =
132 client.keywordSearch(KEYWORD);
133 int statusCode = res.getStatus();
135 // Check the status code of the response: does it match
136 // the expected response(s)?
137 if (logger.isDebugEnabled()) {
138 logger.debug(testName + ": status = " + statusCode);
140 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
141 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
143 CollectionobjectsCommonList list = (CollectionobjectsCommonList)
144 res.getEntity(CollectionobjectsCommonList.class);
145 long numMatched = list.getTotalItems();
147 if (logger.isDebugEnabled()) {
148 logger.debug("Keyword search matched " + numMatched +
149 " resources, expected to match " + numKeywordRetrievableResources);
152 // Optionally output additional data about list members for debugging.
153 boolean iterateThroughList = false;
154 if (iterateThroughList && logger.isDebugEnabled()) {
155 itemizeListItems(list);
158 Assert.assertEquals(numMatched, numKeywordRetrievableResources);
162 // @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
163 public void keywordSearchRepeatableScalarField(String testName) throws Exception {
168 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
169 public void keywordSearchNonExistentKeyword(String testName) throws Exception {
171 if (logger.isDebugEnabled()) {
172 logger.debug(testBanner(testName, CLASS_NAME));
175 final long NUM_MATCHES_EXPECTED = 0;
177 testSetup(STATUS_OK, ServiceRequestType.SEARCH);
178 if (logger.isDebugEnabled()) {
179 logger.debug("Searching on keyword(s): " + NON_EXISTENT_KEYWORD + " ...");
181 CollectionObjectClient client = new CollectionObjectClient();
182 ClientResponse<CollectionobjectsCommonList> res =
183 client.keywordSearch(NON_EXISTENT_KEYWORD);
184 int statusCode = res.getStatus();
186 // Check the status code of the response: does it match
187 // the expected response(s)?
188 if (logger.isDebugEnabled()) {
189 logger.debug(testName + ": status = " + statusCode);
191 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
192 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
194 CollectionobjectsCommonList list = (CollectionobjectsCommonList)
195 res.getEntity(CollectionobjectsCommonList.class);
196 long numMatched = list.getTotalItems();
198 if (logger.isDebugEnabled()) {
199 logger.debug("Keyword search matched " + numMatched +
200 " resources, expected to match " + NUM_MATCHES_EXPECTED);
203 Assert.assertEquals(numMatched, NUM_MATCHES_EXPECTED);
207 // ---------------------------------------------------------------
208 // Cleanup of resources created during testing
209 // ---------------------------------------------------------------
212 * Deletes all resources created by tests, after all tests have been run.
214 * This cleanup method will always be run, even if one or more tests fail.
215 * For this reason, it attempts to remove all resources created
216 * at any point during testing, even if some of those resources
217 * may be expected to be deleted by certain tests.
219 @AfterClass(alwaysRun=true)
220 public void cleanUp() {
221 String noTest = System.getProperty("noTestCleanup");
222 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
223 if (logger.isDebugEnabled()) {
224 logger.debug("Skipping Cleanup phase ...");
228 if (logger.isDebugEnabled()) {
229 logger.debug("Cleaning up temporary resources created for testing ...");
231 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
232 for (String resourceId : allResourceIdsCreated) {
233 // Note: Any non-success responses are ignored and not reported.
234 collectionObjectClient.delete(resourceId).releaseConnection();
238 // ---------------------------------------------------------------
239 // Utility methods used by tests above
240 // ---------------------------------------------------------------
242 private void createCollectionObjects(long numToCreate, String keywords) {
243 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
244 CollectionObjectClient client = new CollectionObjectClient();
245 for (long i = 0; i < numToCreate; i++) {
246 MultipartOutput multipart = createCollectionObjectInstance(keywords);
247 ClientResponse<Response> res = client.create(multipart);
249 int statusCode = res.getStatus();
250 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
251 allResourceIdsCreated.add(extractId(res));
253 res.releaseConnection();
258 private MultipartOutput createCollectionObjectInstance(String keywords) {
259 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
260 collectionObject.setObjectNumber(createIdentifier());
261 collectionObject.setTitle(keywords);
262 MultipartOutput multipart = new MultipartOutput();
263 OutputPart commonPart = multipart.addPart(collectionObject,
264 MediaType.APPLICATION_XML_TYPE);
265 commonPart.getHeaders().add("label", new CollectionObjectClient().getCommonPartName());
269 private void itemizeListItems(CollectionobjectsCommonList list) {
270 List<CollectionobjectsCommonList.CollectionObjectListItem> items =
271 list.getCollectionObjectListItem();
273 for (CollectionobjectsCommonList.CollectionObjectListItem item : items) {
274 logger.debug("list-item[" + i + "] title="
276 logger.debug("list-item[" + i + "] URI="