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;
30 import javax.ws.rs.core.Response;
32 import org.collectionspace.services.PersonJAXBSchema;
33 import org.collectionspace.services.client.CollectionSpaceClient;
34 import org.collectionspace.services.client.PersonAuthorityClient;
35 import org.collectionspace.services.client.PersonAuthorityClientUtils;
36 import org.collectionspace.services.client.PoxPayloadOut;
37 import org.collectionspace.services.jaxb.AbstractCommonList;
38 import org.collectionspace.services.person.PersonTermGroup;
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 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
116 return new PersonAuthorityClient(clientPropertiesFilename);
120 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
123 protected AbstractCommonList getCommonList(Response response) {
124 return response.readEntity(AbstractCommonList.class);
128 public void setup() {
131 } catch (Exception e) {
132 Assert.fail("Could not create new Authority for search tests.", e);
135 long startTime = System.currentTimeMillis();
137 long stopTime = System.currentTimeMillis();
138 double runTime = (stopTime - startTime)/1000.0;
139 logger.info("Created {} items in {} seconds.",
140 allItemIdsCreated.size(), runTime);
141 } catch (Exception e) {
142 Assert.fail("Could not create new item in Authority for search tests.", e);
146 // ---------------------------------------------------------------
147 // Utilities: setup routines for search tests
148 // ---------------------------------------------------------------
150 public void createAuthority() throws Exception {
152 String testName = "createAuthority";
155 int expectedStatusCode = Response.Status.CREATED.getStatusCode();
156 ServiceRequestType requestType = ServiceRequestType.CREATE;
157 testSetup(expectedStatusCode, requestType);
159 // Submit the request to the service and store the response.
160 PersonAuthorityClient client = new PersonAuthorityClient();
161 String shortId = "perfTestPersons";
162 String displayName = "Perf Test Person Auth";
163 //String baseRefName = PersonAuthorityClientUtils.createPersonAuthRefName(shortId, null);
164 PoxPayloadOut multipart =
165 PersonAuthorityClientUtils.createPersonAuthorityInstance(
166 displayName, shortId, client.getCommonPartName());
169 Response res = client.create(multipart);
171 assertStatusCode(res, testName);
172 newID = PersonAuthorityClientUtils.extractId(res);
173 logger.info("{}: succeeded.", testName);
179 // Store the refname from the first resource created
180 // for additional tests below.
181 //authRefName = baseRefName;
182 // Store the ID returned from the first resource created
183 // for additional tests below.
188 * Creates an item in the authority, used for partial term matching tests.
190 * @param authorityCsid The CSID of the Authority in which the term will be created.
191 * @param authRefName The refName of the Authority in which the term will be created.
193 private void createItem(String firstName, String lastName, PersonAuthorityClient client )
196 int expectedStatusCode = Response.Status.CREATED.getStatusCode();
197 ServiceRequestType requestType = ServiceRequestType.CREATE;
198 testSetup(expectedStatusCode, requestType);
201 client = new PersonAuthorityClient();
204 // Submit the request to the service and store the response.
205 Map<String, String> personMap = new HashMap<String,String>();
207 // Fill the property map
209 String shortId = firstName+lastName;
210 personMap.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId );
212 List<PersonTermGroup> terms = new ArrayList<PersonTermGroup>();
213 PersonTermGroup term = new PersonTermGroup();
214 term.setTermDisplayName(firstName + " " + lastName);
215 term.setTermName(firstName + " " + lastName);
216 term.setForeName(firstName);
217 term.setSurName(lastName);
220 Map<String, List<String>> personRepeatablesMap = new HashMap<String, List<String>>();
221 PoxPayloadOut multipart =
222 PersonAuthorityClientUtils.createPersonInstance(authId, null, //authRefName,
223 personMap, terms, personRepeatablesMap, client.getItemCommonPartName() );
226 Response res = client.createItem(authId, multipart);
228 assertStatusCode(res, "createItem");
229 newID = PersonAuthorityClientUtils.extractId(res);
236 // Store the IDs from any item resources created
237 // by tests, along with the IDs of their parents, so these items
238 // can be deleted after all tests have been run.
239 allItemIdsCreated.add(newID);
242 private void createItems()
244 PersonAuthorityClient client = new PersonAuthorityClient();
245 String fullTest = System.getProperty("runFullItemsTest");
246 runFullTest = Boolean.TRUE.toString().equalsIgnoreCase(fullTest);
247 int maxSuff = shortTestLimit;
248 int maxLN = shortTestLimit;
250 logger.debug("Creating full items set ...");
251 maxSuff = firstNameSuffixes.length;
252 maxLN = lastNames.length;
254 logger.debug("Creating short items set ...");
256 for(String fname:firstNames) {
257 for( int iSuff=0; iSuff<maxSuff; iSuff++ ) {
258 String fns = firstNameSuffixes[iSuff];
259 for( int iLN=0; iLN<maxLN; iLN++ ) {
260 String lname = lastNames[iLN];
261 createItem(fname+fns, lname, null);
265 logger.debug("createItems created {} items.", allItemIdsCreated.size());
269 * Reads an item list by partial term.
271 @Test(dataProvider="testName")
272 public void partialTermMatch(String testName) {
273 for(int i=0; i<partialTerms.length; i++) {
274 long startTime = System.currentTimeMillis();
275 int numMatchesFound = readItemListWithFilters(testName, authId,
276 partialTerms[i], null);
277 Assert.assertEquals(numMatchesFound, (runFullTest?nMatches[i]:nMatchesShort[i]),
278 "Did not get expected number of partial term matches for "+partialTerms[i]);
279 long stopTime = System.currentTimeMillis();
280 double runTime = (stopTime - startTime)/1000.0;
281 if(logger.isInfoEnabled()){
282 logger.info("Got: "+numMatchesFound+
283 " matches for: \""+partialTerms[i]+"\" in "+
284 runTime+" seconds.");
290 * Reads an item list by partial term or keywords, given an authority and a term.
291 * Only one of partialTerm or keywords should be specified.
292 * If both are specified, keywords will be ignored.
294 * @param testName Calling test name
295 * @param authorityCsid The CSID of the authority within which partial term matching
297 * @param partialTerm A partial term to match item resources.
298 * @param partialTerm A keyword list to match item resources.
299 * @return The number of item resources matched by the partial term.
301 private int readItemListWithFilters(String testName,
302 String authorityCsid, String partialTerm, String keywords) {
305 int expectedStatusCode = Response.Status.OK.getStatusCode();
306 ServiceRequestType requestType = ServiceRequestType.READ_LIST;
307 testSetup(expectedStatusCode, requestType);
309 // Submit the request to the service and store the response.
310 PersonAuthorityClient client = new PersonAuthorityClient();
312 if (authorityCsid != null) {
313 res = client.readItemList(authorityCsid, partialTerm, keywords);
315 Assert.fail(testName+" passed null csid!");
317 AbstractCommonList list = null;
319 assertStatusCode(res, testName);
320 list = res.readEntity(AbstractCommonList.class);
327 List<AbstractCommonList.ListItem> items = list.getListItem();
328 return (int)list.getTotalItems();
330 // ---------------------------------------------------------------
331 // Utility methods used by tests above
332 // ---------------------------------------------------------------
334 * Deletes all resources created by tests, after all tests have been run.
336 * This cleanup method will always be run, even if one or more tests fail.
337 * For this reason, it attempts to remove all resources created
338 * at any point during testing, even if some of those resources
339 * may be expected to be deleted by certain tests.
341 @AfterClass(alwaysRun=true)
342 public void cleanUp() {
343 String noTest = System.getProperty("noTestCleanup");
344 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
345 if (logger.isDebugEnabled()) {
346 logger.debug("Skipping Cleanup phase ...");
350 // Note: Any non-success responses from the delete operations
351 // below are ignored and not reported.
352 PersonAuthorityClient client = new PersonAuthorityClient();
353 // Clean up item resources.
354 for (String itemId : allItemIdsCreated) {
355 client.deleteItem(authId, itemId).close();
357 client.delete(authId).close();