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 (c)) 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.HashMap;
27 import java.util.List;
29 import javax.ws.rs.core.Response;
31 import org.collectionspace.services.PersonJAXBSchema;
32 import org.collectionspace.services.client.CollectionSpaceClient;
33 import org.collectionspace.services.client.PersonAuthorityClient;
34 import org.collectionspace.services.client.PersonAuthorityClientUtils;
35 import org.collectionspace.services.client.PoxPayloadOut;
36 import org.collectionspace.services.jaxb.AbstractCommonList;
38 import org.jboss.resteasy.client.ClientResponse;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.testng.Assert;
42 import org.testng.annotations.AfterClass;
43 import org.testng.annotations.BeforeClass;
44 import org.testng.annotations.Test;
47 * VocabularyServiceTest, carries out tests against a
48 * deployed and running Vocabulary Service.
50 * $LastChangedRevision: 753 $
51 * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
53 public class PersonAuthorityServicePerfTest extends BaseServiceTest {
55 private final String CLASS_NAME = PersonAuthorityServicePerfTest.class.getName();
56 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
58 // Instance variables specific to this test.
59 // final String SERVICE_PATH_COMPONENT = "personauthorities";
60 // final String ITEM_SERVICE_PATH_COMPONENT = "items";
61 private String authId = null;
62 //private String authRefName = null;
63 private List<String> allItemIdsCreated = new ArrayList<String>();
64 private String[] firstNames = {
66 "George","Geoff","Georgia",
67 "John","Johanna","Jon",
68 "Patrick","Patty","Patrik"};
69 private String[] firstNameSuffixes = {
70 "1","2","3","4","5","6","7","8","9","0"};
71 private String[] lastNames = {
72 "Axis","Black","Cobbler",
73 "Dunne","England","France",
74 "Goldsmith","Hart","Indy",
75 "Jones","Kohn","Lark",
76 "Maven","Newhart","Overdunne",
77 "Plaxo","Queen","Roberts",
78 "Smith","Tate","Underdunne",
79 "Vicious","Waits","Xavier",
82 // Keep these two arrays in sync!!!
83 private String[] partialTerms = {"Ann","John","Patty2"};
84 private int[] nMatches = {
85 4*lastNames.length*firstNameSuffixes.length, // Johanna too!
86 lastNames.length*firstNameSuffixes.length,
88 private int shortTestLimit = 2; // Just use first three items in suffix and last name arrays
89 private int[] nMatchesShort = {
90 4*shortTestLimit*shortTestLimit, // Johanna too!
91 shortTestLimit*shortTestLimit,
93 private boolean runFullTest = false;
96 public String getServicePathComponent() {
97 return PersonAuthorityClient.SERVICE_PATH_COMPONENT;
101 protected String getServiceName() {
102 return PersonAuthorityClient.SERVICE_NAME;
106 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
109 protected CollectionSpaceClient getClientInstance() {
110 return new PersonAuthorityClient();
114 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
117 protected AbstractCommonList getAbstractCommonList(
118 ClientResponse<AbstractCommonList> response) {
119 return response.getEntity(AbstractCommonList.class);
123 public void setup() {
126 } catch (Exception e) {
127 Assert.fail("Could not create new Authority for search tests.", e);
130 long startTime = System.currentTimeMillis();
132 long stopTime = System.currentTimeMillis();
133 double runTime = (stopTime - startTime)/1000.0;
134 logger.info("Created {} items in {} seconds.",
135 allItemIdsCreated.size(), runTime);
136 } catch (Exception e) {
137 Assert.fail("Could not create new item in Authority for search tests.", e);
141 // ---------------------------------------------------------------
142 // Utilities: setup routines for search tests
143 // ---------------------------------------------------------------
145 public void createAuthority() throws Exception {
147 String testName = "createAuthority";
150 int expectedStatusCode = Response.Status.CREATED.getStatusCode();
151 ServiceRequestType requestType = ServiceRequestType.CREATE;
152 testSetup(expectedStatusCode, requestType);
154 // Submit the request to the service and store the response.
155 PersonAuthorityClient client = new PersonAuthorityClient();
156 String shortId = "perfTestPersons";
157 String displayName = "Perf Test Person Auth";
158 //String baseRefName = PersonAuthorityClientUtils.createPersonAuthRefName(shortId, null);
159 PoxPayloadOut multipart =
160 PersonAuthorityClientUtils.createPersonAuthorityInstance(
161 displayName, shortId, client.getCommonPartName());
164 ClientResponse<Response> res = client.create(multipart);
166 int statusCode = res.getStatus();
167 // Check the status code of the response: does it match
168 // the expected response(s)?
169 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
170 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
171 Assert.assertEquals(statusCode, this.EXPECTED_STATUS_CODE);
172 newID = PersonAuthorityClientUtils.extractId(res);
173 logger.info("{}: succeeded.", testName);
175 res.releaseConnection();
177 // Store the refname from the first resource created
178 // for additional tests below.
179 //authRefName = baseRefName;
180 // Store the ID returned from the first resource created
181 // for additional tests below.
186 * Creates an item in the authority, used for partial term matching tests.
188 * @param authorityCsid The CSID of the Authority in which the term will be created.
189 * @param authRefName The refName of the Authority in which the term will be created.
191 private void createItem(String firstName, String lastName, PersonAuthorityClient client )
194 int expectedStatusCode = Response.Status.CREATED.getStatusCode();
195 ServiceRequestType requestType = ServiceRequestType.CREATE;
196 testSetup(expectedStatusCode, requestType);
199 client = new PersonAuthorityClient();
202 // Submit the request to the service and store the response.
203 Map<String, String> personMap = new HashMap<String,String>();
205 // Fill the property map
207 String shortId = firstName+lastName;
208 personMap.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId );
209 personMap.put(PersonJAXBSchema.DISPLAY_NAME_COMPUTED, "true");
210 personMap.put(PersonJAXBSchema.FORE_NAME, firstName);
211 personMap.put(PersonJAXBSchema.SUR_NAME, lastName);
212 Map<String, List<String>> personRepeatablesMap = new HashMap<String, List<String>>();
213 PoxPayloadOut multipart =
214 PersonAuthorityClientUtils.createPersonInstance(authId, null, //authRefName,
215 personMap, personRepeatablesMap, client.getItemCommonPartName() );
218 ClientResponse<Response> res = client.createItem(authId, multipart);
220 int statusCode = res.getStatus();
221 // Check the status code of the response: does it match
222 // the expected response(s)?
223 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
224 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
225 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
227 newID = PersonAuthorityClientUtils.extractId(res);
229 res.releaseConnection();
232 // Store the IDs from any item resources created
233 // by tests, along with the IDs of their parents, so these items
234 // can be deleted after all tests have been run.
235 allItemIdsCreated.add(newID);
238 private void createItems()
240 PersonAuthorityClient client = new PersonAuthorityClient();
241 String fullTest = System.getProperty("runFullItemsTest");
242 runFullTest = Boolean.TRUE.toString().equalsIgnoreCase(fullTest);
243 int maxSuff = shortTestLimit;
244 int maxLN = shortTestLimit;
246 logger.debug("Creating full items set ...");
247 maxSuff = firstNameSuffixes.length;
248 maxLN = lastNames.length;
250 logger.debug("Creating short items set ...");
252 for(String fname:firstNames) {
253 for( int iSuff=0; iSuff<maxSuff; iSuff++ ) {
254 String fns = firstNameSuffixes[iSuff];
255 for( int iLN=0; iLN<maxLN; iLN++ ) {
256 String lname = lastNames[iLN];
257 createItem(fname+fns, lname, null);
261 logger.debug("createItems created {} items.", allItemIdsCreated.size());
265 * Reads an item list by partial term.
267 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
268 public void partialTermMatch(String testName) {
269 if (logger.isDebugEnabled()) {
270 logger.debug(testBanner(testName, CLASS_NAME));
272 for(int i=0; i<partialTerms.length; i++) {
273 long startTime = System.currentTimeMillis();
274 int numMatchesFound = readItemListWithFilters(testName, authId,
275 partialTerms[i], null);
276 Assert.assertEquals(numMatchesFound, (runFullTest?nMatches[i]:nMatchesShort[i]),
277 "Did not get expected number of partial term matches for "+partialTerms[i]);
278 long stopTime = System.currentTimeMillis();
279 double runTime = (stopTime - startTime)/1000.0;
280 if(logger.isInfoEnabled()){
281 logger.info("Got: "+numMatchesFound+
282 " matches for: \""+partialTerms[i]+"\" in "+
283 runTime+" seconds.");
289 * Reads an item list by partial term or keywords, given an authority and a term.
290 * Only one of partialTerm or keywords should be specified.
291 * If both are specified, keywords will be ignored.
293 * @param testName Calling test name
294 * @param authorityCsid The CSID of the authority within which partial term matching
296 * @param partialTerm A partial term to match item resources.
297 * @param partialTerm A keyword list to match item resources.
298 * @return The number of item resources matched by the partial term.
300 private int readItemListWithFilters(String testName,
301 String authorityCsid, String partialTerm, String keywords) {
304 int expectedStatusCode = Response.Status.OK.getStatusCode();
305 ServiceRequestType requestType = ServiceRequestType.READ_LIST;
306 testSetup(expectedStatusCode, requestType);
308 // Submit the request to the service and store the response.
309 PersonAuthorityClient client = new PersonAuthorityClient();
310 ClientResponse<AbstractCommonList> res = null;
311 if (authorityCsid != null) {
312 res = client.readItemList(authorityCsid, partialTerm, keywords);
314 Assert.fail(testName+" passed null csid!");
316 AbstractCommonList list = null;
318 int statusCode = res.getStatus();
319 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
320 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
321 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
323 list = res.getEntity();
325 res.releaseConnection();
328 List<AbstractCommonList.ListItem> items = list.getListItem();
329 return (int)list.getTotalItems();
331 // ---------------------------------------------------------------
332 // Utility methods used by tests above
333 // ---------------------------------------------------------------
335 * Deletes all resources created by tests, after all tests have been run.
337 * This cleanup method will always be run, even if one or more tests fail.
338 * For this reason, it attempts to remove all resources created
339 * at any point during testing, even if some of those resources
340 * may be expected to be deleted by certain tests.
342 @AfterClass(alwaysRun=true)
343 public void cleanUp() {
344 String noTest = System.getProperty("noTestCleanup");
345 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
346 if (logger.isDebugEnabled()) {
347 logger.debug("Skipping Cleanup phase ...");
351 // Note: Any non-success responses from the delete operations
352 // below are ignored and not reported.
353 PersonAuthorityClient client = new PersonAuthorityClient();
354 // Clean up item resources.
355 for (String itemId : allItemIdsCreated) {
356 client.deleteItem(authId, itemId).releaseConnection();
358 client.delete(authId).releaseConnection();