]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-4685,CSPACE-3718,CSPACE-5155: Update Organization AuthRefsTest to reflect...
authorAron Roberts <aron@socrates.berkeley.edu>
Fri, 4 May 2012 03:28:19 +0000 (20:28 -0700)
committerAron Roberts <aron@socrates.berkeley.edu>
Fri, 4 May 2012 03:28:19 +0000 (20:28 -0700)
services/organization/client/src/test/java/org/collectionspace/services/client/test/OrgAuthorityAuthRefsTest.java
services/organization/service/src/main/java/org/collectionspace/services/organization/nuxeo/OrganizationValidatorHandler.java

index 903013576e5953629b45ac088cf5225aefc92b47..2125087e28da28ec2dd8ca30c9caf149b1e098bd 100644 (file)
@@ -179,9 +179,6 @@ public class OrgAuthorityAuthRefsTest extends BaseServiceTest<AbstractCommonList
         // Create all the person refs and entities
         createPersonRefs();
 
-        // Create all the organization sub-body refs and entities
-        createSubBodyOrgRefs();
-
         // Initialize values for a new Organization item, to be created within
         // the newly-created Organization Authority resource.
         //
@@ -296,46 +293,6 @@ public class OrgAuthorityAuthRefsTest extends BaseServiceTest<AbstractCommonList
        return result;
     }
 
-    private void createSubBodyOrgRefs() {
-        // Create a temporary sub-body Organization resource, and its corresponding refName
-        // by which it can be identified.
-        //
-        // This sub-body Organization resource will be created in the same
-        // Organization authority as its parent Organization resource.
-
-        String subBodyResourceId = createSubBodyOrganization("Test SubBody Organization");
-        allResourceItemIdsCreated.put(subBodyResourceId, knownResourceId);
-        subBodyRefName = OrgAuthorityClientUtils.getOrgRefName(knownResourceId, subBodyResourceId, null);
-    }
-
-    protected String createSubBodyOrganization(String subBodyName) {
-        OrgAuthorityClient orgAuthClient = new OrgAuthorityClient();
-        Map<String, String> subBodyOrgMap = new HashMap<String,String>();
-        String shortId = createIdentifier();
-        subBodyOrgMap.put(OrganizationJAXBSchema.SHORT_IDENTIFIER, shortId );
-        subBodyOrgMap.put(OrganizationJAXBSchema.SHORT_NAME,
-            subBodyName + "-" + shortId);
-        subBodyOrgMap.put(OrganizationJAXBSchema.LONG_NAME, subBodyName + " Long Name");
-        subBodyOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, subBodyName + " Founding Place");
-       PoxPayloadOut multipart =
-           OrgAuthorityClientUtils.createOrganizationInstance(
-               knownResourceRefName, subBodyOrgMap, orgAuthClient.getItemCommonPartName());
-
-       String result = null;
-       ClientResponse<Response> res = orgAuthClient.createItem(knownResourceId, multipart);
-       try {
-            int statusCode = res.getStatus();
-            Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
-                    invalidStatusCodeMessage(testRequestType, statusCode));
-            Assert.assertEquals(statusCode, STATUS_CREATED);
-            result = extractId(res);
-       } finally {
-           res.releaseConnection();
-       }
-
-       return result;
-    }
-
     // Success outcomes
     /**
      * Read and check auth refs.
@@ -372,8 +329,7 @@ public class OrgAuthorityAuthRefsTest extends BaseServiceTest<AbstractCommonList
                 organizationContactPersonRefName1);
         Assert.assertEquals(organization.getContactNames().getContactName().get(1),
                 organizationContactPersonRefName2);
-        Assert.assertEquals(organization.getSubBodies().getSubBody().get(0),
-                subBodyRefName);
+
 
         // Get the auth refs and check them
         // FIXME - need to create this method in the client
index 3c6c6a04b9aedf08271a5be750554b26a0e7cd57..10ea216f9dbb3348aed4fd9bdb4b1e74a29fb8a8 100644 (file)
@@ -6,7 +6,7 @@
  *  http://www.collectionspace.org
  *  http://wiki.collectionspace.org
 
- *  Copyright 2009 University of California at Berkeley
+ *  Copyright 2009 University of California, Berkeley
 
  *  Licensed under the Educational Community License (ECL), Version 2.0.
  *  You may not use this file except in compliance with this License.
  */
 package org.collectionspace.services.organization.nuxeo;
 
+import java.util.List;
 import java.util.regex.Pattern;
-import org.collectionspace.services.organization.OrganizationsCommon;
-import org.collectionspace.services.common.context.MultipartServiceContext;
-import org.collectionspace.services.common.context.ServiceContext;
-import org.collectionspace.services.common.document.DocumentHandler.Action;
+import org.collectionspace.services.common.api.Tools;
 import org.collectionspace.services.common.document.InvalidDocumentException;
-import org.collectionspace.services.common.document.ValidatorHandler;
+import org.collectionspace.services.common.document.ValidatorHandlerImpl;
+import org.collectionspace.services.organization.OrgTermGroup;
+import org.collectionspace.services.organization.OrgTermGroupList;
+import org.collectionspace.services.organization.OrganizationsCommon;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -37,63 +38,110 @@ import org.slf4j.LoggerFactory;
  * OrganizationValidatorHandler
  * 
  * Validates data supplied when attempting to create and/or update Organization records.
- * 
- * $LastChangedRevision$
- * $LastChangedDate$
  */
-public class OrganizationValidatorHandler implements ValidatorHandler {
+public class OrganizationValidatorHandler extends ValidatorHandlerImpl {
 
     final Logger logger = LoggerFactory.getLogger(OrganizationValidatorHandler.class);
-    private static final Pattern shortIdBadPattern = Pattern.compile("[\\W]"); //.matcher(input).matches()
+    // 'Bad pattern' for shortIdentifiers matches any non-word characters
+    private static final Pattern SHORT_ID_BAD_PATTERN = Pattern.compile("[\\W]"); //.matcher(input).matches()
+    private static final String VALIDATION_ERROR = "The record payload was invalid. See log file for more details.";
+    private static final String SHORT_ID_BAD_CHARS_ERROR =
+            "shortIdentifier must only contain standard word characters";
+    private static final String HAS_NO_TERMS_ERROR =
+            "Authority items must contain at least one term.";
+    private static final String HAS_AN_EMPTY_TERM_ERROR =
+            "Each term group in an authority item must contain "
+            + "a non-empty term name or "
+            + "a non-empty term display name.";
+    boolean invalid = false;
+    String msg = "";
 
     @Override
-    public void validate(Action action, ServiceContext ctx)
-            throws InvalidDocumentException {
-        if (logger.isDebugEnabled()) {
-            logger.debug("validate() action=" + action.name());
-        }
-        
-        // Bail out if the validation action is for delete.
-        if (action.equals(Action.DELETE)) {
-               return;
-        }
-        
-        try {
-            MultipartServiceContext mctx = (MultipartServiceContext) ctx;
-            OrganizationsCommon org = (OrganizationsCommon) mctx.getInputPart(mctx.getCommonPartLabel(),
-                    OrganizationsCommon.class);
-            String msg = "";
-            boolean invalid = false;
-            
-            // Validation occurring on both creates and updates
-            String displayName = org.getDisplayName();
-            if (!org.isDisplayNameComputed()  && ((displayName == null) || displayName.trim().isEmpty())) {
-                invalid = true;
-                msg += "displayName must be non-null and non-blank if displayNameComputed is false";
+    protected Class getCommonPartClass() {
+        return OrganizationsCommon.class;
+    }
+
+    @Override
+    protected void handleCreate() throws InvalidDocumentException {
+        OrganizationsCommon person = (OrganizationsCommon) getCommonPart();
+        // No guarantee that there is a common part in every post/update.
+        if (person != null) {
+            try {
+                String shortId = person.getShortIdentifier();
+                if (shortId != null) {
+                    CS_ASSERT(shortIdentifierContainsOnlyValidChars(shortId), SHORT_ID_BAD_CHARS_ERROR);
+                }
+                CS_ASSERT(containsAtLeastOneTerm(person), HAS_NO_TERMS_ERROR);
+                CS_ASSERT(allTermsContainNameOrDisplayName(person), HAS_AN_EMPTY_TERM_ERROR);
+            } catch (AssertionError e) {
+                if (logger.isErrorEnabled()) {
+                    logger.error(e.getMessage(), e);
+                }
+                throw new InvalidDocumentException(VALIDATION_ERROR, e);
             }
-            
-            // Validation specific to creates or updates
-            if (action.equals(Action.CREATE)) {
-                String shortId = org.getShortIdentifier();
-                // Per CSPACE-2215, shortIdentifier values that are null (missing)
-                // or the empty string are now legally accepted in create payloads.
-                // In either of those cases, a short identifier will be synthesized from
-                // a display name or supplied in another manner.
-                if ((shortId != null) && (shortIdBadPattern.matcher(shortId).find())) {
-                    invalid = true;
-                    msg += "shortIdentifier must only contain standard word characters";
+        }
+    }
+
+    @Override
+    protected void handleGet() throws InvalidDocumentException {
+    }
+
+    @Override
+    protected void handleGetAll() throws InvalidDocumentException {
+    }
+
+    @Override
+    protected void handleUpdate() throws InvalidDocumentException {
+        OrganizationsCommon person = (OrganizationsCommon) getCommonPart();
+        // No guarantee that there is a common part in every post/update.
+        if (person != null) {
+            try {
+                // shortIdentifier is among a set of fields that are
+                // prevented from being changed on an update, and thus
+                // we don't need to check its value here.
+                CS_ASSERT(containsAtLeastOneTerm(person), HAS_NO_TERMS_ERROR);
+                CS_ASSERT(allTermsContainNameOrDisplayName(person), HAS_AN_EMPTY_TERM_ERROR);
+            } catch (AssertionError e) {
+                if (logger.isErrorEnabled()) {
+                    logger.error(e.getMessage(), e);
                 }
-            } else if (action.equals(Action.UPDATE)) {
+                throw new InvalidDocumentException(VALIDATION_ERROR, e);
             }
+        }
+    }
+
+    @Override
+    protected void handleDelete() throws InvalidDocumentException {
+    }
+
+    private boolean shortIdentifierContainsOnlyValidChars(String shortId) {
+        // Check whether any characters match the 'bad' pattern
+        if (SHORT_ID_BAD_PATTERN.matcher(shortId).find()) {
+            return false;
+        }
+        return true;
+    }
+
+    private boolean containsAtLeastOneTerm(OrganizationsCommon person) {
+        OrgTermGroupList termGroupList = person.getOrgTermGroupList();
+        if (termGroupList == null) {
+            return false;
+        }
+        List<OrgTermGroup> termGroups = termGroupList.getOrgTermGroup();
+        if ((termGroups == null) || (termGroups.size() == 0)) {
+            return false;
+        }
+        return true;
+    }
 
-            if (invalid) {
-                logger.error(msg);
-                throw new InvalidDocumentException(msg);
+    private boolean allTermsContainNameOrDisplayName(OrganizationsCommon person) {
+        OrgTermGroupList termGroupList = person.getOrgTermGroupList();
+        List<OrgTermGroup> termGroups = termGroupList.getOrgTermGroup();
+        for (OrgTermGroup termGroup : termGroups) {
+            if (Tools.isBlank(termGroup.getTermName()) || Tools.isBlank(termGroup.getTermDisplayName())) {
+                return false;
             }
-        } catch (InvalidDocumentException ide) {
-            throw ide;
-        } catch (Exception e) {
-            throw new InvalidDocumentException(e);
         }
+        return true;
     }
 }