]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
008bae5a80afb8630630057e5ff3f78871adb270
[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 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;
43
44 /**
45  * CollectionObjectAuthRefsTest, carries out tests against a
46  * deployed and running CollectionObject Service.
47  *
48  * $LastChangedRevision: 1327 $
49  * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
50  */
51 public class CollectionObjectSearchTest extends BaseServiceTest {
52
53    /** The logger. */
54     private final String CLASS_NAME = CollectionObjectSearchTest.class.getName();
55     private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
56
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";
61
62     /* Use this to keep track of resources to delete */
63     private List<String> allResourceIdsCreated = new ArrayList<String>();
64
65     /* (non-Javadoc)
66      * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
67      */
68     @Override
69     protected String getServicePathComponent() {
70         return new CollectionObjectClient().getServicePathComponent();
71     }
72
73     /* (non-Javadoc)
74      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
75      */
76     @Override
77     protected CollectionSpaceClient getClientInstance() {
78         return new CollectionObjectClient();
79     }
80
81     /* (non-Javadoc)
82      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
83      */
84     @Override
85     protected AbstractCommonList getAbstractCommonList(ClientResponse<AbstractCommonList> response) {
86         return response.getEntity(CollectionobjectsCommonList.class);
87     }
88
89     /**
90      * Creates one or more resources containing a "noise" keyword,
91      * which should NOT be retrieved by keyword searches.
92      */
93     @BeforeClass(alwaysRun=true)
94     public void setup() {
95         long numNoiseWordResources = 2;
96         if (logger.isDebugEnabled()) {
97             logger.debug("Creating " + numNoiseWordResources +
98                 " 'noise word' resources ...");
99         }
100         createCollectionObjects(numNoiseWordResources, NOISE_WORD);
101     }
102
103
104     // ---------------------------------------------------------------
105     // Search tests
106     // ---------------------------------------------------------------
107
108     // Success outcomes
109
110     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
111     public void keywordSearchOneWord(String testName) throws Exception {
112
113         if (logger.isDebugEnabled()) {
114             logger.debug(testBanner(testName, CLASS_NAME));
115         }
116
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 ...");
123         }
124         createCollectionObjects(numKeywordRetrievableResources, KEYWORD);
125
126         testSetup(STATUS_OK, ServiceRequestType.SEARCH);
127         if (logger.isDebugEnabled()) {
128             logger.debug("Searching on keyword(s): " + KEYWORD + " ...");
129         }
130         CollectionObjectClient client = new CollectionObjectClient();
131         ClientResponse<CollectionobjectsCommonList> res =
132             client.keywordSearch(KEYWORD);
133         int statusCode = res.getStatus();
134
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);
139         }
140         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
141                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
142
143         CollectionobjectsCommonList list = (CollectionobjectsCommonList)
144             res.getEntity(CollectionobjectsCommonList.class);
145         long numMatched = list.getTotalItems();
146
147         if (logger.isDebugEnabled()) {
148             logger.debug("Keyword search matched " + numMatched +
149                 " resources, expected to match " + numKeywordRetrievableResources);
150         }
151
152         // Optionally output additional data about list members for debugging.
153         boolean iterateThroughList = false;
154         if (iterateThroughList && logger.isDebugEnabled()) {
155             itemizeListItems(list);
156         }
157
158         Assert.assertEquals(numMatched, numKeywordRetrievableResources);
159
160     }
161
162     // @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
163     public void keywordSearchRepeatableScalarField(String testName) throws Exception {
164     }
165
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);
172             try {
173                 int statusCode = res.getStatus();
174                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
175                 allResourceIdsCreated.add(extractId(res));
176             } finally {
177                 res.releaseConnection();
178             }
179         }
180     }
181
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());
190         return multipart;
191     }
192
193     // Failure outcomes
194
195     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
196     public void keywordSearchNonExistentKeyword(String testName) throws Exception {
197
198         if (logger.isDebugEnabled()) {
199             logger.debug(testBanner(testName, CLASS_NAME));
200         }
201
202         final long NUM_MATCHES_EXPECTED = 0;
203
204         testSetup(STATUS_OK, ServiceRequestType.SEARCH);
205         if (logger.isDebugEnabled()) {
206             logger.debug("Searching on keyword(s): " + NON_EXISTENT_KEYWORD + " ...");
207         }
208         CollectionObjectClient client = new CollectionObjectClient();
209         ClientResponse<CollectionobjectsCommonList> res =
210             client.keywordSearch(NON_EXISTENT_KEYWORD);
211         int statusCode = res.getStatus();
212
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);
217         }
218         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
219                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
220
221         CollectionobjectsCommonList list = (CollectionobjectsCommonList)
222             res.getEntity(CollectionobjectsCommonList.class);
223         long numMatched = list.getTotalItems();
224
225         if (logger.isDebugEnabled()) {
226             logger.debug("Keyword search matched " + numMatched +
227                 " resources, expected to match " + NUM_MATCHES_EXPECTED);
228         }
229
230         Assert.assertEquals(numMatched, NUM_MATCHES_EXPECTED);
231
232     }
233
234     // ---------------------------------------------------------------
235     // Cleanup of resources created during testing
236     // ---------------------------------------------------------------
237
238     /**
239      * Deletes all resources created by tests, after all tests have been run.
240      *
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.
245      */
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 ...");
252             }
253             return;
254         }
255         if (logger.isDebugEnabled()) {
256             logger.debug("Cleaning up temporary resources created for testing ...");
257         }
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();
262         }
263     }
264
265     // ---------------------------------------------------------------
266     // Utility methods used by tests above
267     // ---------------------------------------------------------------
268
269     private void itemizeListItems(CollectionobjectsCommonList list) {
270         List<CollectionobjectsCommonList.CollectionObjectListItem> items =
271             list.getCollectionObjectListItem();
272         int i = 0;
273         for (CollectionobjectsCommonList.CollectionObjectListItem item : items) {
274             logger.debug("list-item[" + i + "] title="
275                     + item.getTitle());
276             logger.debug("list-item[" + i + "] URI="
277                     + item.getUri());
278             i++;
279         }
280     }
281
282 }