]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
662e3018c348572d5533087146a61af65b27fb91
[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 (c)) 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.io.PrintStream;
26 import java.io.UnsupportedEncodingException;
27 import java.net.URLEncoder;
28 import java.nio.charset.Charset;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33 import javax.ws.rs.core.Response;
34
35 import org.collectionspace.services.PersonJAXBSchema;
36 import org.collectionspace.services.client.CollectionSpaceClient;
37 import org.collectionspace.services.client.PersonAuthorityClient;
38 import org.collectionspace.services.client.PersonAuthorityClientUtils;
39 import org.collectionspace.services.client.PoxPayloadOut;
40 import org.collectionspace.services.jaxb.AbstractCommonList;
41 import org.jboss.resteasy.client.ClientResponse;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.testng.Assert;
45 import org.testng.annotations.AfterClass;
46 import org.testng.annotations.BeforeClass;
47 import org.testng.annotations.Test;
48
49 /**
50  * PersonAuthoritySearchTest, carries out search (e.g. partial
51  * term matching) tests against a deployed and running PersonAuthority Service.
52  *
53  * $LastChangedRevision: 753 $
54  * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
55  */
56 public class PersonAuthoritySearchTest extends BaseServiceTest {
57
58     private final String CLASS_NAME = PersonAuthoritySearchTest.class.getName();
59     private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
60     
61         @Override
62         public String getServicePathComponent() {
63                 return PersonAuthorityClient.SERVICE_PATH_COMPONENT;
64         }
65
66         @Override
67         protected String getServiceName() {
68                 return PersonAuthorityClient.SERVICE_NAME;
69         }
70     
71     final String UTF8_CHARSET_NAME = "UTF-8";
72     
73     // Test name for partial term matching: Lech Wałęsa
74     //
75     // For details regarding the łę characters in the last name, see:
76     // http://en.wikipedia.org/wiki/L_with_stroke
77     // http://en.wikipedia.org/wiki/%C4%98
78     //
79     // Forename
80     final String TEST_PARTIAL_TERM_FORE_NAME = "Lech";
81     //
82     // Surname (contains single quote character)
83     final String TEST_PARTIAL_TERM_SUR_NAME_QUOTE = "O'Hara";
84     //
85     // Surname (contains two non-USASCII range Unicode UTF-8 characters)
86     final String TEST_PARTIAL_TERM_SUR_NAME_UNICODE = "Wałęsa";
87         // Wrong: "Wa" + "\u0142" + "\u0119" + "sa";
88         // Should also work: "Wa" + '\u0142' + '\u0119' + "sa";
89         // Should also work: "Wa\u0142\u0119sa";
90     //
91     //
92     // Displayname
93     final String TEST_PARTIAL_TERM_DISPLAY_NAME_UNICODE =
94             TEST_PARTIAL_TERM_FORE_NAME + " " + TEST_PARTIAL_TERM_SUR_NAME_UNICODE;
95     //
96     // Displayname
97     final String TEST_PARTIAL_TERM_DISPLAY_NAME_QUOTE =
98             TEST_PARTIAL_TERM_FORE_NAME + " " + TEST_PARTIAL_TERM_SUR_NAME_QUOTE;
99     //
100     // shortId
101     final String TEST_SHORT_ID_UNICODE = "lechWalesa";
102     //
103     // shortId
104     final String TEST_SHORT_ID_QUOTE = "lechOHara";
105     
106     final String TEST_KWD_BIRTH_PLACE = "Popowo, Poland";
107
108     final String TEST_KWD_UTF8_STYLE = "Appliqu"+'\u00e8'+"d Arts";
109     
110     final String TEST_KWD_BIO_NOTE_NO_QUOTES = 
111         "This is a silly bionote with no so-called quotes.";
112     
113     final String TEST_KWD_BIO_NOTE_DBL_QUOTES = 
114         "This is a silly \"bionote\" for testing so called quote_handling";
115
116     final String TEST_KWD_NO_MATCH = "Foobar";
117
118     // Non-existent partial term name (first letters of each of the words
119     // in a pangram for the English alphabet).
120     private static final String TEST_PARTIAL_TERM_NON_EXISTENT = "jlmbsoq";
121
122     /** The known resource id. */
123     private String knownResourceId = null;
124     
125     /** The known resource ref name. */
126     //private String knownResourceRefName = null;
127     
128     /** The known item resource id. */
129     private String knownItemResourceId = null;
130
131     // The resource ID of an item resource used for partial term matching tests.
132     private String knownItemPartialTermResourceId = null;
133
134     private List<String> allResourceIdsCreated = new ArrayList<String>();
135     
136     /** The all item resource ids created. */
137     private Map<String, String> allItemResourceIdsCreated =
138         new HashMap<String, String>();
139
140     // The number of matches expected on each partial term.
141     final int NUM_MATCHES_EXPECTED_COMMON = 2;
142     final int NUM_MATCHES_EXPECTED_SPECIFIC = 1;
143
144     // The minimum number of characters that must be included
145     // a partial term, in order to permit matching to occur.
146     final int PARTIAL_TERM_MIN_LENGTH = 1;
147
148     /* (non-Javadoc)
149      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
150      */
151     @Override
152     protected CollectionSpaceClient getClientInstance() {
153         return new PersonAuthorityClient();
154     }
155     
156     /* (non-Javadoc)
157      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
158      */
159     @Override
160     protected AbstractCommonList getAbstractCommonList(
161                     ClientResponse<AbstractCommonList> response) {
162     return response.getEntity(AbstractCommonList.class);
163     }
164
165     private String getPartialTermCommon() {
166         return TEST_PARTIAL_TERM_FORE_NAME;
167     }
168
169     private String getPartialTermUtf8() {
170         return TEST_PARTIAL_TERM_SUR_NAME_UNICODE;
171     }
172
173     private String getPartialTermQuote() {
174         return TEST_PARTIAL_TERM_SUR_NAME_QUOTE;
175     }
176
177     private String getPartialTermNonExistent() {
178         return TEST_PARTIAL_TERM_NON_EXISTENT;
179     }
180
181     private String getPartialTermMinimumLength() {
182         String partialTerm = getPartialTermCommon();
183         if (partialTerm == null || partialTerm.trim().isEmpty()) {
184             return partialTerm;
185         }
186         if (partialTerm.length() > PARTIAL_TERM_MIN_LENGTH) {
187             return partialTerm.substring(0, PARTIAL_TERM_MIN_LENGTH);
188         } else {
189           return partialTerm;
190         }
191     }
192
193     private String getKwdTerm() {
194         return TEST_KWD_BIRTH_PLACE;
195     }
196
197     private String getKwdTermUTF8() {
198         return TEST_KWD_UTF8_STYLE;
199     }
200
201     private String getKwdTermNonExistent() {
202         return TEST_KWD_NO_MATCH;
203     }
204
205     @BeforeClass
206     public void setup() {
207         try {
208             createAuthority();
209         } catch (Exception e) {
210             Assert.fail("Could not create new Authority for search tests.", e);
211         }
212         try {
213             createItemsInAuthorityForPartialTermMatch(knownResourceId, null ); //knownResourceRefName);
214         } catch (Exception e) {
215             Assert.fail("Could not create new item in Authority for search tests.", e);
216         }
217     }
218  
219     // ---------------------------------------------------------------
220     // CRUD tests : READ_LIST tests by partial term match.
221     // ---------------------------------------------------------------
222
223     // Success outcomes
224
225     /**
226      * Reads an item list by partial term.
227      */
228     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
229         groups = {"readListByPartialTerm"})
230     public void partialTermMatch(String testName) {
231         if (logger.isDebugEnabled()) {
232             logger.debug(testBanner(testName, CLASS_NAME));
233         }
234         int numMatchesFound = 0;
235         String partialTerm = getPartialTermCommon();
236         if (logger.isDebugEnabled()) {
237             logger.debug("Attempting match on partial term '" + partialTerm + "' ...");
238         }
239         numMatchesFound = readItemListWithFilters(testName, knownResourceId, partialTerm, null);
240         if (logger.isDebugEnabled()) {
241             logger.debug("Found " + numMatchesFound + " match(es), expected " +
242                 NUM_MATCHES_EXPECTED_COMMON + " match(es).");
243         }
244         Assert.assertEquals(numMatchesFound, NUM_MATCHES_EXPECTED_COMMON);
245     }
246
247     /**
248      * Reads an item list by partial term, with a partial term that consists
249      * of an all-lowercase variation of the expected match, to test case-insensitive
250      * matching.
251      */
252     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
253         groups = {"readListByPartialTerm"}, dependsOnMethods = {"partialTermMatch"})
254     public void partialTermMatchCaseInsensitiveLowerCase(String testName) {
255         if (logger.isDebugEnabled()) {
256             logger.debug(testBanner(testName, CLASS_NAME));
257         }
258         int numMatchesFound = 0;
259
260         final String partialTerm = getPartialTermCommon().toLowerCase();
261         if (logger.isDebugEnabled()) {
262             logger.debug("Attempting match on partial term '" + partialTerm + "' ...");
263         }
264         numMatchesFound =
265                 readItemListWithFilters(testName, knownResourceId, partialTerm, null);
266                 if (logger.isDebugEnabled()) {
267         logger.debug("Found " + numMatchesFound + " match(es), expected " +
268                         NUM_MATCHES_EXPECTED_COMMON + " match(es).");
269         }
270         Assert.assertEquals(numMatchesFound, NUM_MATCHES_EXPECTED_COMMON);
271     }
272
273     /**
274      * Reads an item list by partial term, with a partial term that consists
275      * of an all-uppercase variation of the expected match, to test case-insensitive
276      * matching.
277      */
278     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
279         groups = {"readListByPartialTerm"}, dependsOnMethods = {"partialTermMatch"})
280     public void partialTermMatchCaseInsensitiveUpperCase(String testName) {
281         if (logger.isDebugEnabled()) {
282             logger.debug(testBanner(testName, CLASS_NAME));
283         }
284         int numMatchesFound = 0;
285
286         final String partialTerm = getPartialTermCommon().toUpperCase();
287         if (logger.isDebugEnabled()) {
288             logger.debug("Attempting match on partial term '" + partialTerm + "' ...");
289         }
290         numMatchesFound =
291                 readItemListWithFilters(testName, knownResourceId, partialTerm, null);
292         if (logger.isDebugEnabled()) {
293             logger.debug("Found " + numMatchesFound + " match(es), expected " +
294                         NUM_MATCHES_EXPECTED_COMMON + " match(es).");
295         }
296         Assert.assertEquals(numMatchesFound, NUM_MATCHES_EXPECTED_COMMON);
297     }
298
299     /**
300      * Reads an item list by partial term, with a partial term that is of
301      * the minimum character length that may be expected to be matched.
302      */
303     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
304         groups = {"readListByPartialTerm"}, dependsOnMethods = {"partialTermMatch"})
305     public void partialTermMatchMinimumLength(String testName) {
306         if (logger.isDebugEnabled()) {
307             logger.debug(testBanner(testName, CLASS_NAME));
308         }
309         int numMatchesFound = 0;
310         String partialTerm = getPartialTermMinimumLength();
311         if (logger.isDebugEnabled()) {
312             logger.debug("Attempting match on partial term '" + partialTerm + "' ...");
313         }
314         numMatchesFound = readItemListWithFilters(testName, knownResourceId, partialTerm, null);
315         // Zero matches are expected on a non-existent term.
316         if (logger.isDebugEnabled()) {
317             logger.debug("Found " + numMatchesFound + " match(es), expected " +
318                         NUM_MATCHES_EXPECTED_COMMON + " match(es).");
319         }
320         Assert.assertEquals(numMatchesFound, NUM_MATCHES_EXPECTED_COMMON);
321     }
322
323     /**
324      * Reads an item list by partial term, with a partial term that contains
325      * at least one Unicode UTF-8 character (outside the USASCII range).
326      */
327     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
328         groups = {"readListByPartialTerm"}, dependsOnMethods = {"partialTermMatch"})
329     public void partialTermMatchUTF8(String testName) {
330         if (logger.isDebugEnabled()) {
331             logger.debug(testBanner(testName, CLASS_NAME));
332         }
333         int numMatchesFound = 0;
334         String partialTerm = getPartialTermUtf8();
335         String ptEncoded;
336         try {
337                 ptEncoded = URLEncoder.encode(partialTerm, UTF8_CHARSET_NAME);
338         }
339         catch (UnsupportedEncodingException ex) {
340           throw new RuntimeException("Broken VM does not support UTF-8");
341         }
342         if (logger.isDebugEnabled()) {
343             logger.debug("Attempting match on partial term '" + partialTerm + "', Encoded:'"+ptEncoded+"' ...");
344         }
345         numMatchesFound =
346                 readItemListWithFilters(testName, knownResourceId, partialTerm, null);
347         if (logger.isDebugEnabled()) {
348             logger.debug("Found " + numMatchesFound + " match(es), expected " +
349                 NUM_MATCHES_EXPECTED_SPECIFIC + " match(es).");
350         }
351         Assert.assertEquals(numMatchesFound, NUM_MATCHES_EXPECTED_SPECIFIC);
352     }
353     
354     /**
355      * Reads an item list by partial term, with a partial term that contains
356      * at least one Unicode UTF-8 character (outside the USASCII range).
357      */
358     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
359         groups = {"readListByPartialTerm"}, dependsOnMethods = {"partialTermMatch"})
360     public void partialTermMatchQuote(String testName) {
361         if (logger.isDebugEnabled()) {
362             logger.debug(testBanner(testName, CLASS_NAME));
363         }
364         int numMatchesFound = 0;
365         String partialTerm = getPartialTermQuote();
366         if (logger.isDebugEnabled()) {
367             logger.debug("Attempting match on partial term '" + partialTerm + "' ...");
368         }
369         numMatchesFound =
370                 readItemListWithFilters(testName, knownResourceId, partialTerm, null);
371         if (logger.isDebugEnabled()) {
372             logger.debug("Found " + numMatchesFound + " match(es), expected " +
373                 NUM_MATCHES_EXPECTED_SPECIFIC + " match(es).");
374         }
375         Assert.assertEquals(numMatchesFound, NUM_MATCHES_EXPECTED_SPECIFIC);
376     }
377
378     /**
379      * Finds terms by keywords.
380      */
381     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
382             groups = {"readListByKwdTerm"})
383     public void keywordTermMatch(String testName) {
384         if (logger.isDebugEnabled()) {
385             logger.debug(testBanner(testName, CLASS_NAME));
386         }
387         int numMatchesFound = 0;
388         String kwdTerm = getKwdTerm();
389         if (logger.isDebugEnabled()) {
390             logger.debug("Attempting match on kwd term '" + kwdTerm + "' ...");
391         }
392         numMatchesFound = readItemListWithFilters(testName, knownResourceId, null, kwdTerm);
393         if (logger.isDebugEnabled()) {
394             logger.debug("Found " + numMatchesFound + " match(es), expected " +
395                     NUM_MATCHES_EXPECTED_COMMON + " match(es).");
396         }
397         Assert.assertEquals(numMatchesFound, NUM_MATCHES_EXPECTED_COMMON);
398     }
399
400     /**
401      * Finds terms by keywords.
402      */
403     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
404         groups = {"readListByKwdTerm"}, dependsOnMethods = {"keywordTermMatch"})
405     public void keywordTermMatchUTF8(String testName) {
406         if (logger.isDebugEnabled()) {
407             logger.debug(testBanner(testName, CLASS_NAME));
408         }
409         int numMatchesFound = 0;
410         String kwdTerm = getKwdTermUTF8();
411         if (logger.isDebugEnabled()) {
412             logger.debug("Attempting match on kwd term '" + kwdTerm + "' ...");
413         }
414         numMatchesFound = readItemListWithFilters(testName, knownResourceId, null, kwdTerm);
415         if (logger.isDebugEnabled()) {
416             logger.debug("Found " + numMatchesFound + " match(es), expected " +
417                 NUM_MATCHES_EXPECTED_COMMON + " match(es).");
418         }
419         Assert.assertEquals(numMatchesFound, NUM_MATCHES_EXPECTED_COMMON);
420     }
421
422     
423     
424     // Failure outcomes
425
426     /**
427      * Reads an item list by partial term, with a partial term that is not
428      * expected to be matched by any term in any resource.
429      */
430     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
431         groups = {"readListByPartialTerm"}, dependsOnMethods = {"partialTermMatch"})
432     public void partialTermMatchOnNonexistentTerm(String testName) {
433         if (logger.isDebugEnabled()) {
434             logger.debug(testBanner(testName, CLASS_NAME));
435         }
436         int numMatchesFound = 0;
437         int ZERO_MATCHES_EXPECTED = 0;
438         String partialTerm = getPartialTermNonExistent();
439         if (logger.isDebugEnabled()) {
440             logger.debug("Attempting match on partial term '" + partialTerm + "' ...");
441         }
442         numMatchesFound = readItemListWithFilters(testName, knownResourceId, partialTerm, null);
443         // Zero matches are expected on a non-existent term.
444         if (logger.isDebugEnabled()) {
445             logger.debug("Found " + numMatchesFound + " match(es), expected " +
446                 ZERO_MATCHES_EXPECTED + " match(es).");
447         }
448         Assert.assertEquals(numMatchesFound, ZERO_MATCHES_EXPECTED);
449     }
450
451     /**
452      * Reads an item list by partial term, with a partial term that is not
453      * expected to be matched by any term in any resource.
454      */
455     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
456         groups = {"readListByKwdTerm"}, dependsOnMethods = {"keywordTermMatch"})
457     public void keywordTermMatchOnNonexistentTerm(String testName) {
458         if (logger.isDebugEnabled()) {
459             logger.debug(testBanner(testName, CLASS_NAME));
460         }
461         int numMatchesFound = 0;
462         int ZERO_MATCHES_EXPECTED = 0;
463         String kwdTerm = getKwdTermNonExistent();
464         if (logger.isDebugEnabled()) {
465             logger.debug("Attempting match on kwd term '" + kwdTerm + "' ...");
466         }
467         numMatchesFound = readItemListWithFilters(testName, knownResourceId, null, kwdTerm);
468         // Zero matches are expected on a non-existent term.
469         if (logger.isDebugEnabled()) {
470             logger.debug("Found " + numMatchesFound + " match(es), expected " +
471                 ZERO_MATCHES_EXPECTED + " match(es).");
472         }
473         Assert.assertEquals(numMatchesFound, ZERO_MATCHES_EXPECTED);
474     }
475
476     /**
477      * Reads an item list by partial term or keywords, given an authority and a term.
478      * Only one of partialTerm or keywords should be specified. 
479      * If both are specified, keywords will be ignored.
480      * 
481      * @param testName Calling test name
482      * @param authorityCsid The CSID of the authority within which partial term matching
483      *     will be performed.
484      * @param partialTerm A partial term to match item resources.
485      * @param partialTerm A keyword list to match item resources.
486      * @return The number of item resources matched by the partial term.
487      */
488     private int readItemListWithFilters(String testName, 
489                 String authorityCsid, String partialTerm, String keywords) {
490
491         // Perform setup.
492         int expectedStatusCode = Response.Status.OK.getStatusCode();
493         ServiceRequestType requestType = ServiceRequestType.READ_LIST;
494         testSetup(expectedStatusCode, requestType);
495
496         // Submit the request to the service and store the response.
497         PersonAuthorityClient client = new PersonAuthorityClient();
498         ClientResponse<AbstractCommonList> res = null;
499         if (authorityCsid != null) {
500                 res = client.readItemList(authorityCsid, partialTerm, keywords);
501         } else {
502             Assert.fail("readItemListByPartialTerm passed null csid!");
503         }
504         AbstractCommonList list = null;
505         try {
506             int statusCode = res.getStatus();
507
508             // Check the status code of the response: does it match
509             // the expected response(s)?
510             if(logger.isDebugEnabled()){
511                 logger.debug(testName + ": status = " + statusCode);
512             }
513             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
514                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
515             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
516
517             list = res.getEntity();
518         } finally {
519             res.releaseConnection();
520         }
521
522         List<AbstractCommonList.ListItem> items = list.getListItem();
523         int nItemsReturned = items.size();
524
525         return nItemsReturned;
526     }
527     
528     // ---------------------------------------------------------------
529     // Cleanup of resources created during testing
530     // ---------------------------------------------------------------
531     
532     /**
533      * Deletes all resources created by tests, after all tests have been run.
534      *
535      * This cleanup method will always be run, even if one or more tests fail.
536      * For this reason, it attempts to remove all resources created
537      * at any point during testing, even if some of those resources
538      * may be expected to be deleted by certain tests.
539      */
540     @AfterClass(alwaysRun=true)
541     public void cleanUp() {
542         String noTest = System.getProperty("noTestCleanup");
543         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
544             if (logger.isDebugEnabled()) {
545                 logger.debug("Skipping Cleanup phase ...");
546             }
547             return;
548         }
549         if (logger.isDebugEnabled()) {
550             logger.debug("Cleaning up temporary resources created for testing ...");
551         }
552         String parentResourceId;
553         String itemResourceId;
554         PersonAuthorityClient client = new PersonAuthorityClient();
555         parentResourceId = knownResourceId;
556         // Clean up item resources.
557         for (Map.Entry<String, String> entry : allItemResourceIdsCreated.entrySet()) {
558             itemResourceId = entry.getKey();
559             parentResourceId = entry.getValue();
560             // Note: Any non-success responses from the delete operation
561             // below are ignored and not reported.
562             ClientResponse<Response> res =
563                 client.deleteItem(parentResourceId, itemResourceId);
564             res.releaseConnection();
565         }
566         // Clean up authority resources.
567         for (String resourceId : allResourceIdsCreated) {
568             // Note: Any non-success responses are ignored and not reported.
569             client.delete(resourceId).releaseConnection();
570         }
571     }
572
573     // ---------------------------------------------------------------
574     // Utility methods used by tests above
575     // ---------------------------------------------------------------
576
577     // ---------------------------------------------------------------
578     // Utilities: setup routines for search tests
579     // ---------------------------------------------------------------
580
581     public void createAuthority() throws Exception {
582
583         String testName = "createAuthority";
584         if (logger.isDebugEnabled()) {
585             logger.debug(testBanner(testName, CLASS_NAME));
586         }
587
588         // Perform setup.
589         int expectedStatusCode = Response.Status.CREATED.getStatusCode();
590         ServiceRequestType requestType = ServiceRequestType.CREATE;
591         testSetup(expectedStatusCode, requestType);
592
593         // Submit the request to the service and store the response.
594         PersonAuthorityClient client = new PersonAuthorityClient();
595         String shortId = createIdentifier();
596         String displayName = "displayName-" + shortId;
597         //String baseRefName = PersonAuthorityClientUtils.createPersonAuthRefName(shortId, null);
598         PoxPayloadOut multipart =
599             PersonAuthorityClientUtils.createPersonAuthorityInstance(
600             displayName, shortId, client.getCommonPartName());
601
602         String newID = null;
603         ClientResponse<Response> res = client.create(multipart);
604         try {
605             int statusCode = res.getStatus();
606             // Check the status code of the response: does it match
607             // the expected response(s)?
608             if(logger.isDebugEnabled()){
609                 logger.debug(testName + ": status = " + statusCode);
610             }
611             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
612                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
613             Assert.assertEquals(statusCode, this.EXPECTED_STATUS_CODE);
614             newID = PersonAuthorityClientUtils.extractId(res);
615         } finally {
616             res.releaseConnection();
617         }
618         // Store the refname from the first resource created
619         // for additional tests below.
620         //knownResourceRefName = baseRefName;
621         // Store the ID returned from the first resource created
622         // for additional tests below.
623         if (knownResourceId == null){
624             knownResourceId = newID;
625             //knownResourceRefName = baseRefName;
626         }
627
628         // Store the IDs from every resource created by tests,
629         // so they can be deleted after tests have been run.
630         allResourceIdsCreated.add(newID);
631     }
632
633      /**
634      * Creates an item in the authority, used for partial term matching tests.
635      *
636      * @param authorityCsid The CSID of the Authority in which the term will be created.
637      * @param authRefName The refName of the Authority in which the term will be created.
638      */
639     private void createItemsInAuthorityForPartialTermMatch(
640                 String authorityCsid, String authRefName)
641         throws Exception {
642             
643         String testName = "createItemsInAuthorityForPartialTermMatch";
644
645         int expectedStatusCode = Response.Status.CREATED.getStatusCode();
646         ServiceRequestType requestType = ServiceRequestType.CREATE;
647         testSetup(expectedStatusCode, requestType);
648
649         // Submit the request to the service and store the response.
650         PersonAuthorityClient client = new PersonAuthorityClient();
651         Map<String, String> partialTermPersonMap = new HashMap<String,String>();
652         //
653         // Fill the property map for the UNICODE item
654         //
655         partialTermPersonMap.put(PersonJAXBSchema.SHORT_IDENTIFIER, TEST_SHORT_ID_UNICODE );
656         partialTermPersonMap.put(PersonJAXBSchema.DISPLAY_NAME_COMPUTED, "false");
657         partialTermPersonMap.put(PersonJAXBSchema.DISPLAY_NAME, TEST_PARTIAL_TERM_DISPLAY_NAME_UNICODE);
658         partialTermPersonMap.put(PersonJAXBSchema.FORE_NAME, TEST_PARTIAL_TERM_FORE_NAME);
659         partialTermPersonMap.put(PersonJAXBSchema.SUR_NAME, TEST_PARTIAL_TERM_SUR_NAME_UNICODE);
660         partialTermPersonMap.put(PersonJAXBSchema.BIRTH_PLACE, TEST_KWD_BIRTH_PLACE);
661         partialTermPersonMap.put(PersonJAXBSchema.GENDER, "male");
662         partialTermPersonMap.put(PersonJAXBSchema.BIO_NOTE, TEST_KWD_BIO_NOTE_NO_QUOTES);
663
664         Map<String, List<String>> partialTermRepeatablesMap = new HashMap<String, List<String>>();
665         ArrayList<String> styles = new ArrayList<String>();
666         styles.add(TEST_KWD_UTF8_STYLE);
667         partialTermRepeatablesMap.put(PersonJAXBSchema.SCHOOLS_OR_STYLES, styles);
668
669         createItem(testName, authorityCsid, null /*authRefName*/, client, 
670                 partialTermPersonMap, partialTermRepeatablesMap);
671         //
672         // Adjust the property map for the QUOTE item
673         //
674         partialTermPersonMap.put(PersonJAXBSchema.SHORT_IDENTIFIER, TEST_SHORT_ID_QUOTE );
675         partialTermPersonMap.put(PersonJAXBSchema.DISPLAY_NAME, TEST_PARTIAL_TERM_DISPLAY_NAME_QUOTE);
676         partialTermPersonMap.put(PersonJAXBSchema.SUR_NAME, TEST_PARTIAL_TERM_SUR_NAME_QUOTE);
677         partialTermPersonMap.put(PersonJAXBSchema.BIO_NOTE, TEST_KWD_BIO_NOTE_DBL_QUOTES);
678
679         createItem(testName, authorityCsid, null /*authRefName*/, client, 
680                 partialTermPersonMap, partialTermRepeatablesMap);
681     }
682     
683     private void createItem(
684                 String testName, 
685                 String authorityCsid, 
686                 String authRefName,
687                 PersonAuthorityClient client,
688                 Map<String, String> partialTermPersonMap,
689                 Map<String, List<String>> partialTermRepeatablesMap) throws Exception {
690         PoxPayloadOut multipart =
691             PersonAuthorityClientUtils.createPersonInstance(authorityCsid, null, //authRefName, 
692                 partialTermPersonMap, partialTermRepeatablesMap, client.getItemCommonPartName() );
693
694         String newID = null;
695         ClientResponse<Response> res = client.createItem(authorityCsid, multipart);
696         try {
697             int statusCode = res.getStatus();
698             // Check the status code of the response: does it match
699             // the expected response(s)?
700             if(logger.isDebugEnabled()){
701                 logger.debug(testName + ": status = " + statusCode);
702             }
703             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
704                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
705             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
706
707             newID = PersonAuthorityClientUtils.extractId(res);
708         } finally {
709             res.releaseConnection();
710         }
711
712         // Store the ID returned from the first item resource created
713         // for additional tests below.
714         if (knownItemResourceId == null){
715             knownItemResourceId = newID;
716             if (logger.isDebugEnabled()) {
717                 logger.debug(testName + ": knownItemPartialTermResourceId=" + knownItemPartialTermResourceId);
718             }
719         }
720
721         // Store the IDs from any item resources created
722         // by tests, along with the IDs of their parents, so these items
723         // can be deleted after all tests have been run.
724         allItemResourceIdsCreated.put(newID, authorityCsid);
725     }
726
727 }