From c7376f7cb991e9ba1f0b2270e1ec8d2e6d07db11 Mon Sep 17 00:00:00 2001 From: Aron Roberts Date: Sat, 2 Oct 2010 00:45:59 +0000 Subject: [PATCH] CSPACE-2975: Services tenant bindings configuration now specifies validator handlers for LocationAuthority, OrgAuthority, and PersonAuthority; these had been inadvertently omitted. Added tests to Person service to verify that non-word chars in short identifiers / shortIds submitted in request payloads, when creating a Person authority or Person item, return the expected 400 BadRequest response. --- .../main/config/services/tenant-bindings.xml | 18 ++ .../client/PersonAuthorityClientUtils.java | 56 +++--- .../test/PersonAuthorityServiceTest.java | 160 +++++++++++++++++- 3 files changed, 202 insertions(+), 32 deletions(-) diff --git a/services/common/src/main/config/services/tenant-bindings.xml b/services/common/src/main/config/services/tenant-bindings.xml index fdce5391a..cb5e9f8b3 100644 --- a/services/common/src/main/config/services/tenant-bindings.xml +++ b/services/common/src/main/config/services/tenant-bindings.xml @@ -490,6 +490,9 @@ org.collectionspace.services.organization.nuxeo.OrgAuthorityDocumentModelHandler + + org.collectionspace.services.organization.nuxeo.OrgAuthorityValidatorHandler + org.collectionspace.services.person.nuxeo.PersonAuthorityDocumentModelHandler + + org.collectionspace.services.person.nuxeo.PersonAuthorityValidatorHandler + org.collectionspace.services.location.nuxeo.LocationAuthorityDocumentModelHandler + + org.collectionspace.services.location.nuxeo.LocationAuthorityValidatorHandler + org.collectionspace.services.organization.nuxeo.OrgAuthorityDocumentModelHandler + + org.collectionspace.services.organization.nuxeo.OrgAuthorityValidatorHandler + org.collectionspace.services.person.nuxeo.PersonAuthorityDocumentModelHandler + + org.collectionspace.services.person.nuxeo.PersonAuthorityValidatorHandler + org.collectionspace.services.location.nuxeo.LocationAuthorityDocumentModelHandler + + org.collectionspace.services.location.nuxeo.LocationAuthorityValidatorHandler + )personRepeatablesInfo.get(PersonJAXBSchema.GROUPS))!=null) { - GroupList groupsList = new GroupList(); - List groups = groupsList.getGroup(); - groups.addAll(values); - person.setGroups(groupsList); - } - if((values = (List)personRepeatablesInfo.get(PersonJAXBSchema.NATIONALITIES))!=null) { - NationalityList nationalitiesList = new NationalityList(); - List nationalities = nationalitiesList.getNationality(); - nationalities.addAll(values); - person.setNationalities(nationalitiesList); - } if((value = (String)personInfo.get(PersonJAXBSchema.GENDER))!=null) person.setGender(value); - if((values = (List)personRepeatablesInfo.get(PersonJAXBSchema.OCCUPATIONS))!=null) { - OccupationList occupationsList = new OccupationList(); - List occupations = occupationsList.getOccupation(); - occupations.addAll(values); - person.setOccupations(occupationsList); - } - if((values = (List)personRepeatablesInfo.get(PersonJAXBSchema.SCHOOLS_OR_STYLES))!=null) { - SchoolOrStyleList schoolOrStyleList = new SchoolOrStyleList(); - List schoolsOrStyles = schoolOrStyleList.getSchoolOrStyle(); - schoolsOrStyles.addAll(values); - person.setSchoolsOrStyles(schoolOrStyleList); - } - if((value = (String)personInfo.get(PersonJAXBSchema.BIO_NOTE))!=null) + if((value = (String)personInfo.get(PersonJAXBSchema.BIO_NOTE))!=null) person.setBioNote(value); if((value = (String)personInfo.get(PersonJAXBSchema.NAME_NOTE))!=null) person.setNameNote(value); + if (personRepeatablesInfo != null) { + if((values = (List)personRepeatablesInfo.get(PersonJAXBSchema.GROUPS))!=null) { + GroupList groupsList = new GroupList(); + List groups = groupsList.getGroup(); + groups.addAll(values); + person.setGroups(groupsList); + } + if((values = (List)personRepeatablesInfo.get(PersonJAXBSchema.NATIONALITIES))!=null) { + NationalityList nationalitiesList = new NationalityList(); + List nationalities = nationalitiesList.getNationality(); + nationalities.addAll(values); + person.setNationalities(nationalitiesList); + } + + if((values = (List)personRepeatablesInfo.get(PersonJAXBSchema.OCCUPATIONS))!=null) { + OccupationList occupationsList = new OccupationList(); + List occupations = occupationsList.getOccupation(); + occupations.addAll(values); + person.setOccupations(occupationsList); + } + if((values = (List)personRepeatablesInfo.get(PersonJAXBSchema.SCHOOLS_OR_STYLES))!=null) { + SchoolOrStyleList schoolOrStyleList = new SchoolOrStyleList(); + List schoolsOrStyles = schoolOrStyleList.getSchoolOrStyle(); + schoolsOrStyles.addAll(values); + person.setSchoolsOrStyles(schoolOrStyleList); + } + } + + MultipartOutput multipart = new MultipartOutput(); OutputPart commonPart = multipart.addPart(person, MediaType.APPLICATION_XML_TYPE); @@ -414,6 +419,7 @@ public class PersonAuthorityClientUtils { */ public static String extractId(ClientResponse res) { MultivaluedMap mvm = res.getMetadata(); + // FIXME: This may throw an NPE if the Location: header isn't present String uri = (String) ((ArrayList) mvm.get("Location")).get(0); if(logger.isDebugEnabled()){ logger.debug("extractId:uri=" + uri); diff --git a/services/person/client/src/test/java/org/collectionspace/services/client/test/PersonAuthorityServiceTest.java b/services/person/client/src/test/java/org/collectionspace/services/client/test/PersonAuthorityServiceTest.java index 5efbc897a..456710a52 100644 --- a/services/person/client/src/test/java/org/collectionspace/services/client/test/PersonAuthorityServiceTest.java +++ b/services/person/client/src/test/java/org/collectionspace/services/client/test/PersonAuthorityServiceTest.java @@ -223,7 +223,36 @@ public class PersonAuthorityServiceTest extends AbstractServiceTestImpl { } /** - * Creates the item in authority. + * Creates the item with an empty short identifier. + * + * @param testName the test name + */ + /* + @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class, + groups = {"create"}, dependsOnMethods = {"create"}) + public void createItemWithEmptyShortId(String testName) { + if (logger.isDebugEnabled()) { + logger.debug(testBanner(testName, CLASS_NAME)); + } + setupCreate(); + + // Fill the property map + String shortId = ""; + Map fieldProperties = new HashMap(); + fieldProperties.put(PersonJAXBSchema.DISPLAY_NAME_COMPUTED, "false"); + fieldProperties.put(PersonJAXBSchema.DISPLAY_NAME, "Rod Beck"); + fieldProperties.put(PersonJAXBSchema.SHORT_DISPLAY_NAME_COMPUTED, "false"); + fieldProperties.put(PersonJAXBSchema.SHORT_DISPLAY_NAME, "RodBeck"); + + final Map NULL_REPEATABLE_FIELD_PROPERTIES = null; + String newID = createItemInAuthority(knownResourceId, knownResourceRefName, + shortId, fieldProperties, NULL_REPEATABLE_FIELD_PROPERTIES); + allResourceIdsCreated.add(newID); + } +*/ + + /** + * Creates an item in an authority, using test data. * * @param vcsid the vcsid * @param authRefName the auth ref name @@ -236,9 +265,6 @@ public class PersonAuthorityServiceTest extends AbstractServiceTestImpl { logger.debug(testName + ":"+vcsid+"..."); } - // Submit the request to the service and store the response. - PersonAuthorityClient client = new PersonAuthorityClient(); - Map johnWayneMap = new HashMap(); // // Fill the property map @@ -249,7 +275,7 @@ public class PersonAuthorityServiceTest extends AbstractServiceTestImpl { johnWayneMap.put(PersonJAXBSchema.SHORT_DISPLAY_NAME_COMPUTED, "false"); johnWayneMap.put(PersonJAXBSchema.SHORT_DISPLAY_NAME, "JohnWayne"); johnWayneMap.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId); - + johnWayneMap.put(PersonJAXBSchema.FORE_NAME, TEST_FORE_NAME); johnWayneMap.put(PersonJAXBSchema.SUR_NAME, TEST_SUR_NAME); johnWayneMap.put(PersonJAXBSchema.GENDER, "male"); @@ -269,9 +295,33 @@ public class PersonAuthorityServiceTest extends AbstractServiceTestImpl { johnWayneGroups.add("Scottish"); johnWayneRepeatablesMap.put(PersonJAXBSchema.GROUPS, johnWayneGroups); + return createItemInAuthority(vcsid, authRefName, shortId, johnWayneMap, johnWayneRepeatablesMap); + + } + + /** + * Creates an item in an authority. + * + * @param vcsid the vcsid + * @param authRefName the auth ref name + * @param itemFieldProperties a set of properties specifying the values of fields. + * @param itemRepeatableFieldProperties a set of properties specifying the values of repeatable fields. + * @return the string + */ + private String createItemInAuthority(String vcsid, String authRefName, String shortId, + Map itemFieldProperties, Map itemRepeatableFieldProperties) { + + final String testName = "createItemInAuthority"; + if(logger.isDebugEnabled()){ + logger.debug(testName + ":"+vcsid+"..."); + } + + // Submit the request to the service and store the response. + PersonAuthorityClient client = new PersonAuthorityClient(); + MultipartOutput multipart = - PersonAuthorityClientUtils.createPersonInstance(vcsid, authRefName, johnWayneMap, - johnWayneRepeatablesMap, client.getItemCommonPartName() ); + PersonAuthorityClientUtils.createPersonInstance(vcsid, authRefName, itemFieldProperties, + itemRepeatableFieldProperties, client.getItemCommonPartName() ); String newID = null; ClientResponse res = client.createItem(vcsid, multipart); @@ -502,6 +552,102 @@ public class PersonAuthorityServiceTest extends AbstractServiceTestImpl { } */ + /** + * Attempts to create an authority with an short identifier that contains + * non-word characters. + * + * @param testName the test name + */ + @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class, + groups = {"create", "nonWordCharsInShortId"}) + public void createWithShortIdNonWordChars(String testName) throws Exception { + if (logger.isDebugEnabled()) { + logger.debug(testBanner(testName, CLASS_NAME)); + } + EXPECTED_STATUS_CODE = STATUS_BAD_REQUEST; + REQUEST_TYPE = ServiceRequestType.CREATE; + testSetup(EXPECTED_STATUS_CODE, REQUEST_TYPE); + + // Create the payload to be included in the body of the request + PersonAuthorityClient client = new PersonAuthorityClient(); + String shortId = createIdentifier() + "*" + createIdentifier(); + String displayName = "displayName-" + shortId; + MultipartOutput multipart = + PersonAuthorityClientUtils.createPersonAuthorityInstance( + displayName, shortId, client.getCommonPartName()); + + // Submit the request to the service and store the response. + ClientResponse res = client.create(multipart); + + // Check the status code of the response: does it match + // the expected response(s)? + try { + int statusCode = res.getStatus(); + if(logger.isDebugEnabled()){ + logger.debug(testName + ": status = " + statusCode); + } + Assert.assertTrue(this.REQUEST_TYPE.isValidStatusCode(statusCode), + invalidStatusCodeMessage(this.REQUEST_TYPE, statusCode)); + Assert.assertEquals(statusCode, this.EXPECTED_STATUS_CODE); + } finally { + res.releaseConnection(); + } + + } + + /** + * Attempts to create an item with an short identifier that contains + * non-word characters. + * + * @param testName the test name + */ + @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class, + groups = {"create", "nonWordCharsInShortId"}, dependsOnMethods = {"create"}) + public void createItemWithShortIdNonWordChars(String testName) { + if (logger.isDebugEnabled()) { + logger.debug(testBanner(testName, CLASS_NAME)); + } + EXPECTED_STATUS_CODE = STATUS_BAD_REQUEST; + REQUEST_TYPE = ServiceRequestType.CREATE; + testSetup(EXPECTED_STATUS_CODE, REQUEST_TYPE); + + PersonAuthorityClient client = new PersonAuthorityClient(); + + // Create the payload to be included in the body of the request + String shortId = "7-Eleven"; + Map fieldProperties = new HashMap(); + fieldProperties.put(PersonJAXBSchema.DISPLAY_NAME_COMPUTED, "false"); + fieldProperties.put(PersonJAXBSchema.DISPLAY_NAME, shortId); + fieldProperties.put(PersonJAXBSchema.SHORT_DISPLAY_NAME_COMPUTED, "false"); + fieldProperties.put(PersonJAXBSchema.SHORT_DISPLAY_NAME, shortId); + fieldProperties.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId); + final Map NULL_REPEATABLE_FIELD_PROPERTIES = null; + MultipartOutput multipart = + PersonAuthorityClientUtils.createPersonInstance(knownResourceId, + knownResourceRefName, fieldProperties, + NULL_REPEATABLE_FIELD_PROPERTIES, client.getItemCommonPartName()); + + // Send the request and receive a response + ClientResponse res = client.createItem(knownResourceId, multipart); + + // Check the status code of the response: does it match + // the expected response(s)? + try { + int statusCode = res.getStatus(); + // Check the status code of the response: does it match + // the expected response(s)? + if(logger.isDebugEnabled()){ + logger.debug(testName + ": status = " + statusCode); + } + Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); + } finally { + res.releaseConnection(); + + } + } + // --------------------------------------------------------------- // CRUD tests : CREATE LIST tests // --------------------------------------------------------------- -- 2.47.3