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 {
166 private void createCollectionObjects(long numToCreate, String keywords) {
167 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
168 CollectionObjectClient client = new CollectionObjectClient();
169 for (long i = 0; i < numToCreate; i++) {
170 MultipartOutput multipart = createCollectionObjectInstance(keywords);
171 ClientResponse<Response> res = client.create(multipart);
173 int statusCode = res.getStatus();
174 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
175 allResourceIdsCreated.add(extractId(res));
177 res.releaseConnection();
182 private MultipartOutput createCollectionObjectInstance(String keywords) {
183 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
184 collectionObject.setObjectNumber(createIdentifier());
185 collectionObject.setTitle(keywords);
186 MultipartOutput multipart = new MultipartOutput();
187 OutputPart commonPart = multipart.addPart(collectionObject,
188 MediaType.APPLICATION_XML_TYPE);
189 commonPart.getHeaders().add("label", new CollectionObjectClient().getCommonPartName());
195 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
196 public void keywordSearchNonExistentKeyword(String testName) throws Exception {
198 if (logger.isDebugEnabled()) {
199 logger.debug(testBanner(testName, CLASS_NAME));
202 final long NUM_MATCHES_EXPECTED = 0;
204 testSetup(STATUS_OK, ServiceRequestType.SEARCH);
205 if (logger.isDebugEnabled()) {
206 logger.debug("Searching on keyword(s): " + NON_EXISTENT_KEYWORD + " ...");
208 CollectionObjectClient client = new CollectionObjectClient();
209 ClientResponse<CollectionobjectsCommonList> res =
210 client.keywordSearch(NON_EXISTENT_KEYWORD);
211 int statusCode = res.getStatus();
213 // Check the status code of the response: does it match
214 // the expected response(s)?
215 if (logger.isDebugEnabled()) {
216 logger.debug(testName + ": status = " + statusCode);
218 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
219 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
221 CollectionobjectsCommonList list = (CollectionobjectsCommonList)
222 res.getEntity(CollectionobjectsCommonList.class);
223 long numMatched = list.getTotalItems();
225 if (logger.isDebugEnabled()) {
226 logger.debug("Keyword search matched " + numMatched +
227 " resources, expected to match " + NUM_MATCHES_EXPECTED);
230 Assert.assertEquals(numMatched, NUM_MATCHES_EXPECTED);
234 // ---------------------------------------------------------------
235 // Cleanup of resources created during testing
236 // ---------------------------------------------------------------
239 * Deletes all resources created by tests, after all tests have been run.
241 * This cleanup method will always be run, even if one or more tests fail.
242 * For this reason, it attempts to remove all resources created
243 * at any point during testing, even if some of those resources
244 * may be expected to be deleted by certain tests.
246 @AfterClass(alwaysRun=true)
247 public void cleanUp() {
248 String noTest = System.getProperty("noTestCleanup");
249 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
250 if (logger.isDebugEnabled()) {
251 logger.debug("Skipping Cleanup phase ...");
255 if (logger.isDebugEnabled()) {
256 logger.debug("Cleaning up temporary resources created for testing ...");
258 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
259 for (String resourceId : allResourceIdsCreated) {
260 // Note: Any non-success responses are ignored and not reported.
261 collectionObjectClient.delete(resourceId).releaseConnection();
265 // ---------------------------------------------------------------
266 // Utility methods used by tests above
267 // ---------------------------------------------------------------
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="