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;
37 import org.collectionspace.services.person.PersonTermGroup;
39 import org.jboss.resteasy.client.ClientResponse;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.testng.Assert;
43 import org.testng.annotations.AfterClass;
44 import org.testng.annotations.BeforeClass;
45 import org.testng.annotations.Test;
48 * VocabularyServiceTest, carries out tests against a
49 * deployed and running Vocabulary Service.
51 * $LastChangedRevision: 753 $
52 * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
54 public class PersonAuthorityServicePerfTest extends BaseServiceTest<AbstractCommonList> {
56 private final String CLASS_NAME = PersonAuthorityServicePerfTest.class.getName();
57 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
59 // Instance variables specific to this test.
60 // final String SERVICE_PATH_COMPONENT = "personauthorities";
61 // final String ITEM_SERVICE_PATH_COMPONENT = "items";
62 private String authId = null;
63 //private String authRefName = null;
64 private List<String> allItemIdsCreated = new ArrayList<String>();
65 private String[] firstNames = {
67 "George","Geoff","Georgia",
68 "John","Johanna","Jon",
69 "Patrick","Patty","Patrik"};
70 private String[] firstNameSuffixes = {
71 "1","2","3","4","5","6","7","8","9","0"};
72 private String[] lastNames = {
73 "Axis","Black","Cobbler",
74 "Dunne","England","France",
75 "Goldsmith","Hart","Indy",
76 "Jones","Kohn","Lark",
77 "Maven","Newhart","Overdunne",
78 "Plaxo","Queen","Roberts",
79 "Smith","Tate","Underdunne",
80 "Vicious","Waits","Xavier",
83 // Keep these two arrays in sync!!!
84 private String[] partialTerms = {"Ann","John","Patty2"};
85 private int[] nMatches = {
86 4*lastNames.length*firstNameSuffixes.length, // Johanna too!
87 lastNames.length*firstNameSuffixes.length,
89 private int shortTestLimit = 2; // Just use first three items in suffix and last name arrays
90 private int[] nMatchesShort = {
91 4*shortTestLimit*shortTestLimit, // Johanna too!
92 shortTestLimit*shortTestLimit,
94 private boolean runFullTest = false;
97 public String getServicePathComponent() {
98 return PersonAuthorityClient.SERVICE_PATH_COMPONENT;
102 protected String getServiceName() {
103 return PersonAuthorityClient.SERVICE_NAME;
107 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
110 protected CollectionSpaceClient getClientInstance() {
111 return new PersonAuthorityClient();
115 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
118 protected AbstractCommonList getCommonList(
119 ClientResponse<AbstractCommonList> response) {
120 return response.getEntity(AbstractCommonList.class);
124 public void setup() {
127 } catch (Exception e) {
128 Assert.fail("Could not create new Authority for search tests.", e);
131 long startTime = System.currentTimeMillis();
133 long stopTime = System.currentTimeMillis();
134 double runTime = (stopTime - startTime)/1000.0;
135 logger.info("Created {} items in {} seconds.",
136 allItemIdsCreated.size(), runTime);
137 } catch (Exception e) {
138 Assert.fail("Could not create new item in Authority for search tests.", e);
142 // ---------------------------------------------------------------
143 // Utilities: setup routines for search tests
144 // ---------------------------------------------------------------
146 public void createAuthority() throws Exception {
148 String testName = "createAuthority";
151 int expectedStatusCode = Response.Status.CREATED.getStatusCode();
152 ServiceRequestType requestType = ServiceRequestType.CREATE;
153 testSetup(expectedStatusCode, requestType);
155 // Submit the request to the service and store the response.
156 PersonAuthorityClient client = new PersonAuthorityClient();
157 String shortId = "perfTestPersons";
158 String displayName = "Perf Test Person Auth";
159 //String baseRefName = PersonAuthorityClientUtils.createPersonAuthRefName(shortId, null);
160 PoxPayloadOut multipart =
161 PersonAuthorityClientUtils.createPersonAuthorityInstance(
162 displayName, shortId, client.getCommonPartName());
165 Response res = client.create(multipart);
167 assertStatusCode(res, testName);
168 newID = PersonAuthorityClientUtils.extractId(res);
169 logger.info("{}: succeeded.", testName);
175 // Store the refname from the first resource created
176 // for additional tests below.
177 //authRefName = baseRefName;
178 // Store the ID returned from the first resource created
179 // for additional tests below.
184 * Creates an item in the authority, used for partial term matching tests.
186 * @param authorityCsid The CSID of the Authority in which the term will be created.
187 * @param authRefName The refName of the Authority in which the term will be created.
189 private void createItem(String firstName, String lastName, PersonAuthorityClient client )
192 int expectedStatusCode = Response.Status.CREATED.getStatusCode();
193 ServiceRequestType requestType = ServiceRequestType.CREATE;
194 testSetup(expectedStatusCode, requestType);
197 client = new PersonAuthorityClient();
200 // Submit the request to the service and store the response.
201 Map<String, String> personMap = new HashMap<String,String>();
203 // Fill the property map
205 String shortId = firstName+lastName;
206 personMap.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId );
208 List<PersonTermGroup> terms = new ArrayList<PersonTermGroup>();
209 PersonTermGroup term = new PersonTermGroup();
210 term.setTermDisplayName(firstName + " " + lastName);
211 term.setTermName(firstName + " " + lastName);
212 term.setForeName(firstName);
213 term.setSurName(lastName);
216 Map<String, List<String>> personRepeatablesMap = new HashMap<String, List<String>>();
217 PoxPayloadOut multipart =
218 PersonAuthorityClientUtils.createPersonInstance(authId, null, //authRefName,
219 personMap, terms, personRepeatablesMap, client.getItemCommonPartName() );
222 ClientResponse<Response> res = client.createItem(authId, multipart);
224 assertStatusCode(res, "createItem");
225 newID = PersonAuthorityClientUtils.extractId(res);
228 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")
268 public void partialTermMatch(String testName) {
269 for(int i=0; i<partialTerms.length; i++) {
270 long startTime = System.currentTimeMillis();
271 int numMatchesFound = readItemListWithFilters(testName, authId,
272 partialTerms[i], null);
273 Assert.assertEquals(numMatchesFound, (runFullTest?nMatches[i]:nMatchesShort[i]),
274 "Did not get expected number of partial term matches for "+partialTerms[i]);
275 long stopTime = System.currentTimeMillis();
276 double runTime = (stopTime - startTime)/1000.0;
277 if(logger.isInfoEnabled()){
278 logger.info("Got: "+numMatchesFound+
279 " matches for: \""+partialTerms[i]+"\" in "+
280 runTime+" seconds.");
286 * Reads an item list by partial term or keywords, given an authority and a term.
287 * Only one of partialTerm or keywords should be specified.
288 * If both are specified, keywords will be ignored.
290 * @param testName Calling test name
291 * @param authorityCsid The CSID of the authority within which partial term matching
293 * @param partialTerm A partial term to match item resources.
294 * @param partialTerm A keyword list to match item resources.
295 * @return The number of item resources matched by the partial term.
297 private int readItemListWithFilters(String testName,
298 String authorityCsid, String partialTerm, String keywords) {
301 int expectedStatusCode = Response.Status.OK.getStatusCode();
302 ServiceRequestType requestType = ServiceRequestType.READ_LIST;
303 testSetup(expectedStatusCode, requestType);
305 // Submit the request to the service and store the response.
306 PersonAuthorityClient client = new PersonAuthorityClient();
307 ClientResponse<AbstractCommonList> res = null;
308 if (authorityCsid != null) {
309 res = client.readItemList(authorityCsid, partialTerm, keywords);
311 Assert.fail(testName+" passed null csid!");
313 AbstractCommonList list = null;
315 assertStatusCode(res, testName);
316 list = res.getEntity();
319 res.releaseConnection();
323 List<AbstractCommonList.ListItem> items = list.getListItem();
324 return (int)list.getTotalItems();
326 // ---------------------------------------------------------------
327 // Utility methods used by tests above
328 // ---------------------------------------------------------------
330 * Deletes all resources created by tests, after all tests have been run.
332 * This cleanup method will always be run, even if one or more tests fail.
333 * For this reason, it attempts to remove all resources created
334 * at any point during testing, even if some of those resources
335 * may be expected to be deleted by certain tests.
337 @AfterClass(alwaysRun=true)
338 public void cleanUp() {
339 String noTest = System.getProperty("noTestCleanup");
340 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
341 if (logger.isDebugEnabled()) {
342 logger.debug("Skipping Cleanup phase ...");
346 // Note: Any non-success responses from the delete operations
347 // below are ignored and not reported.
348 PersonAuthorityClient client = new PersonAuthorityClient();
349 // Clean up item resources.
350 for (String itemId : allItemIdsCreated) {
351 client.deleteItem(authId, itemId).releaseConnection();
353 client.delete(authId).releaseConnection();