]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-905 Added ValidatorHandler, and tests to check displayNameComputed and display...
authorPatrick Schmitz <pschmitz@berkeley.edu>
Sat, 13 Feb 2010 00:27:57 +0000 (00:27 +0000)
committerPatrick Schmitz <pschmitz@berkeley.edu>
Sat, 13 Feb 2010 00:27:57 +0000 (00:27 +0000)
services/common/src/main/config/tenant-bindings.xml
services/organization/client/src/test/java/org/collectionspace/services/client/test/OrgAuthorityServiceTest.java
services/organization/service/src/main/java/org/collectionspace/services/organization/OrgAuthorityResource.java
services/organization/service/src/main/java/org/collectionspace/services/organization/nuxeo/OrganizationDocumentModelHandler.java
services/organization/service/src/main/java/org/collectionspace/services/organization/nuxeo/OrganizationValidatorHandler.java [new file with mode: 0644]

index 072bfdb3985c4448ce447f870a05db0a4a691930..33f41fc433182de1ce78d770e91e617a03b1cfaa 100644 (file)
             <service:documentHandler xmlns:service='http://collectionspace.org/services/common/service'>
                 org.collectionspace.services.organization.nuxeo.OrganizationDocumentModelHandler
             </service:documentHandler>
+            <service:validatorHandler xmlns:service='http://collectionspace.org/services/common/service'>
+                org.collectionspace.services.organization.nuxeo.OrganizationValidatorHandler
+            </service:validatorHandler>
             <service:object id="1" name="Organization" version="0.1"
                             xmlns:service='http://collectionspace.org/services/common/service'>
                 <service:part id="0" control_group="Managed"
index bd0c7fb9c60969cb04625edb211820be5fa2df1b..28fd8551b11a1971773aa0e7197ec9058bf1f612 100644 (file)
@@ -398,7 +398,7 @@ public class OrgAuthorityServiceTest extends AbstractServiceTestImpl {
     public void verifyItemDisplayName(String testName) throws Exception {
 
         // Perform setup.
-        setupRead(testName);
+        setupUpdate(testName);
 
         // Submit the request to the service and store the response.
         ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
@@ -499,6 +499,51 @@ public class OrgAuthorityServiceTest extends AbstractServiceTestImpl {
                 "Updated DisplayName (not computed) in Organization not stored.");
     }
 
+    @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
+            dependsOnMethods = {"verifyItemDisplayName"})
+    public void verifyIllegalItemDisplayName(String testName) throws Exception {
+
+        // Perform setup.
+       setupUpdateWithWrongXmlSchema(testName);
+
+        // Submit the request to the service and store the response.
+        ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
+        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, Response.Status.OK.getStatusCode());
+
+        // Check whether organization has expected displayName.
+        MultipartInput input = (MultipartInput) res.getEntity();
+        OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
+                client.getItemCommonPartName(), OrganizationsCommon.class);
+        Assert.assertNotNull(organization);
+        // Try to Update with computed false and no displayName
+       organization.setDisplayNameComputed(false);
+        organization.setDisplayName(null);
+
+        // Submit the updated resource to the service and store the response.
+        MultipartOutput output = new MultipartOutput();
+        OutputPart commonPart = output.addPart(organization, MediaType.APPLICATION_XML_TYPE);
+        commonPart.getHeaders().add("label", client.getItemCommonPartName());
+        res = client.updateItem(knownResourceId, knownItemResourceId, output);
+        statusCode = res.getStatus();
+
+        // Check the status code of the response: does it match the expected response(s)?
+        if(logger.isDebugEnabled()){
+            logger.debug("updateItem: status = " + statusCode);
+        }
+        Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
+                invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
+        Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
+    }
+
     // Failure outcomes
     @Override
     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
@@ -956,7 +1001,7 @@ public class OrgAuthorityServiceTest extends AbstractServiceTestImpl {
 
    @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
         dependsOnMethods = {"createItem", "readItemList", "testItemSubmitRequest",
-            "updateItem", "verifyItemDisplayName"})
+            "updateItem", "verifyIllegalItemDisplayName"})
     public void deleteItem(String testName) throws Exception {
 
         // Perform setup.
index 9aea39b41b14f116b0db3ff4da4cd0bc730f8605..15584b592d971dce6a605a64483e3d9d484db4bd 100644 (file)
@@ -46,6 +46,7 @@ import org.collectionspace.services.common.ServiceMain;
 import org.collectionspace.services.common.context.MultipartServiceContext;
 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
 import org.collectionspace.services.common.context.ServiceContext;
+import org.collectionspace.services.common.document.BadRequestException;
 import org.collectionspace.services.common.document.DocumentFilter;
 import org.collectionspace.services.common.document.DocumentHandler;
 import org.collectionspace.services.common.document.DocumentNotFoundException;
@@ -322,6 +323,10 @@ public class OrgAuthorityResource extends AbstractCollectionSpaceResourceImpl {
             path.path(parentcsid + "/items/" + itemcsid);
             Response response = Response.created(path.build()).build();
             return response;
+        } catch (BadRequestException bre) {
+            Response response = Response.status(
+                    Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
+            throw new WebApplicationException(response);
         } catch (UnauthorizedException ue) {
             Response response = Response.status(
                     Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
@@ -469,6 +474,10 @@ public class OrgAuthorityResource extends AbstractCollectionSpaceResourceImpl {
             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
             getRepositoryClient(ctx).update(ctx, itemcsid, handler);
             result = (MultipartOutput) ctx.getOutput();
+        } catch (BadRequestException bre) {
+            Response response = Response.status(
+                    Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
+            throw new WebApplicationException(response);
         } catch (UnauthorizedException ue) {
             Response response = Response.status(
                     Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
index 328c818b5e8961dca410f4ac040fb49aecde3648..2f07e2e637a597d5ec243b723566bbb820ac63ac 100644 (file)
@@ -104,10 +104,6 @@ public class OrganizationDocumentModelHandler
                                (String) docModel.getProperty(commonPartLabel,OrganizationJAXBSchema.FOUNDING_PLACE));
                        docModel.setProperty(commonPartLabel, OrganizationJAXBSchema.DISPLAY_NAME,
                                                displayName);
-       } else if(null ==  
-                               docModel.getProperty(commonPartLabel,OrganizationJAXBSchema.DISPLAY_NAME)) { 
-            throw new IllegalArgumentException("Must provide "+OrganizationJAXBSchema.DISPLAY_NAME
-                       +" if " + OrganizationJAXBSchema.DISPLAY_NAME_COMPUTED+" is declared false."); 
        }
     }
 
diff --git a/services/organization/service/src/main/java/org/collectionspace/services/organization/nuxeo/OrganizationValidatorHandler.java b/services/organization/service/src/main/java/org/collectionspace/services/organization/nuxeo/OrganizationValidatorHandler.java
new file mode 100644 (file)
index 0000000..6c579ca
--- /dev/null
@@ -0,0 +1,103 @@
+/**
+ *  This document is a part of the source code and related artifacts
+ *  for CollectionSpace, an open source collections management system
+ *  for museums and related institutions:
+
+ *  http://www.collectionspace.org
+ *  http://wiki.collectionspace.org
+
+ *  Copyright 2009 University of California at Berkeley
+
+ *  Licensed under the Educational Community License (ECL), Version 2.0.
+ *  You may not use this file except in compliance with this License.
+
+ *  You may obtain a copy of the ECL 2.0 License at
+
+ *  https://source.collectionspace.org/collection-space/LICENSE.txt
+
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *//**
+ *  This document is a part of the source code and related artifacts
+ *  for CollectionSpace, an open source collections management system
+ *  for museums and related institutions:
+
+ *  http://www.collectionspace.org
+ *  http://wiki.collectionspace.org
+
+ *  Copyright 2009 University of California at Berkeley
+
+ *  Licensed under the Educational Community License (ECL), Version 2.0.
+ *  You may not use this file except in compliance with this License.
+
+ *  You may obtain a copy of the ECL 2.0 License at
+
+ *  https://source.collectionspace.org/collection-space/LICENSE.txt
+
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package org.collectionspace.services.organization.nuxeo;
+
+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.document.InvalidDocumentException;
+import org.collectionspace.services.common.document.ValidatorHandler;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ *
+ * @author 
+ */
+public class OrganizationValidatorHandler implements ValidatorHandler {
+
+    final Logger logger = LoggerFactory.getLogger(OrganizationValidatorHandler.class);
+
+    @Override
+    public void validate(Action action, ServiceContext ctx)
+            throws InvalidDocumentException {
+        if(logger.isDebugEnabled()) {
+            logger.debug("validate() action=" + action.name());
+        }
+        try {
+            MultipartServiceContext mctx = (MultipartServiceContext) ctx;
+            OrganizationsCommon org = (OrganizationsCommon) mctx.getInputPart(mctx.getCommonPartLabel(),
+                    OrganizationsCommon.class);
+            String msg = "";
+            boolean invalid = false;
+            if(!org.isDisplayNameComputed() && (org.getDisplayName()==null)) {
+                invalid = true;
+                msg += "displayName must be non-null if displayNameComputed is false!";
+            }
+            /*
+            if(action.equals(Action.CREATE)) {
+                //create specific validation here
+            } else if(action.equals(Action.UPDATE)) {
+                //update specific validation here
+            }
+            */
+
+            if (invalid) {
+                logger.error(msg);
+                throw new InvalidDocumentException(msg);
+            }
+        } catch (InvalidDocumentException ide) {
+            throw ide;
+        } catch (Exception e) {
+            throw new InvalidDocumentException(e);
+        }
+    }
+}