]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
6aa258349fd49c09e20ffea30634abe7eb635977
[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     // Failure outcomes
167
168     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
169     public void keywordSearchNonExistentKeyword(String testName) throws Exception {
170
171         if (logger.isDebugEnabled()) {
172             logger.debug(testBanner(testName, CLASS_NAME));
173         }
174
175         final long NUM_MATCHES_EXPECTED = 0;
176
177         testSetup(STATUS_OK, ServiceRequestType.SEARCH);
178         if (logger.isDebugEnabled()) {
179             logger.debug("Searching on keyword(s): " + NON_EXISTENT_KEYWORD + " ...");
180         }
181         CollectionObjectClient client = new CollectionObjectClient();
182         ClientResponse<CollectionobjectsCommonList> res =
183             client.keywordSearch(NON_EXISTENT_KEYWORD);
184         int statusCode = res.getStatus();
185
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);
190         }
191         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
192                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
193
194         CollectionobjectsCommonList list = (CollectionobjectsCommonList)
195             res.getEntity(CollectionobjectsCommonList.class);
196         long numMatched = list.getTotalItems();
197
198         if (logger.isDebugEnabled()) {
199             logger.debug("Keyword search matched " + numMatched +
200                 " resources, expected to match " + NUM_MATCHES_EXPECTED);
201         }
202
203         Assert.assertEquals(numMatched, NUM_MATCHES_EXPECTED);
204
205     }
206
207     // ---------------------------------------------------------------
208     // Cleanup of resources created during testing
209     // ---------------------------------------------------------------
210
211     /**
212      * Deletes all resources created by tests, after all tests have been run.
213      *
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.
218      */
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 ...");
225             }
226             return;
227         }
228         if (logger.isDebugEnabled()) {
229             logger.debug("Cleaning up temporary resources created for testing ...");
230         }
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();
235         }
236     }
237
238     // ---------------------------------------------------------------
239     // Utility methods used by tests above
240     // ---------------------------------------------------------------
241     
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);
248             try {
249                 int statusCode = res.getStatus();
250                 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
251                 allResourceIdsCreated.add(extractId(res));
252             } finally {
253                 res.releaseConnection();
254             }
255         }
256     }
257
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());
266         return multipart;
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 }