]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-909 Add validation support. Added tests for same. Fixed bug in updateNonExiste...
authorPatrick Schmitz <pschmitz@berkeley.edu>
Tue, 16 Feb 2010 01:10:21 +0000 (01:10 +0000)
committerPatrick Schmitz <pschmitz@berkeley.edu>
Tue, 16 Feb 2010 01:10:21 +0000 (01:10 +0000)
services/common/src/main/config/tenant-bindings.xml
services/vocabulary/client/src/test/java/org/collectionspace/services/client/test/VocabularyServiceTest.java
services/vocabulary/service/src/main/java/org/collectionspace/services/vocabulary/VocabularyResource.java
services/vocabulary/service/src/main/java/org/collectionspace/services/vocabulary/nuxeo/VocabularyItemValidatorHandler.java [new file with mode: 0644]

index 5f63994572eaf1a752048d5786d561dcf9a5868b..a6ee5848555462c8f4802502db2e82e095e11562 100644 (file)
             <service:documentHandler xmlns:service='http://collectionspace.org/services/common/service'>
                 org.collectionspace.services.vocabulary.nuxeo.VocabularyItemDocumentModelHandler
             </service:documentHandler>
+            <service:validatorHandler xmlns:service='http://collectionspace.org/services/common/service'>
+                org.collectionspace.services.vocabulary.nuxeo.VocabularyItemValidatorHandler
+            </service:validatorHandler>
             <service:object name="Vocabularyitem" version="0.1"
                             xmlns:service='http://collectionspace.org/services/common/service'>
                 <service:part id="0" control_group="Managed"
index e390e05e2a1be5b0ce9ee88102b88393f0c8e87e..90c1f90335bf59c5b6bd3fac018704c910815357 100644 (file)
@@ -345,6 +345,70 @@ public class VocabularyServiceTest extends AbstractServiceTestImpl {
     }
 
     // Failure outcomes
+    @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
+            dependsOnMethods = {"updateItem"})
+    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 Person has expected displayName.
+        MultipartInput input = (MultipartInput) res.getEntity();
+        VocabularyitemsCommon vitem = (VocabularyitemsCommon) extractPart(input,
+                client.getItemCommonPartName(), VocabularyitemsCommon.class);
+        Assert.assertNotNull(vitem);
+        // Try to Update with null displayName
+        vitem.setDisplayName(null);
+
+        // Submit the updated resource to the service and store the response.
+        MultipartOutput output = new MultipartOutput();
+        OutputPart commonPart = output.addPart(vitem, 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, 
+                       "Expecting invalid message because of null displayName.");
+
+        // Now try to Update with 1-char displayName (too short)
+        vitem.setDisplayName("a");
+
+        // Submit the updated resource to the service and store the response.
+        output = new MultipartOutput();
+        commonPart = output.addPart(vitem, 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, 
+               "Expecting invalid message because of 1-char displayName.");
+    }
+
     @Override
     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
         dependsOnMethods = {"read"})
@@ -754,8 +818,10 @@ public class VocabularyServiceTest extends AbstractServiceTestImpl {
         // The only relevant ID may be the one used in update(), below.
         HashMap<String, String> itemInfo = new HashMap<String, String>();
         itemInfo.put(VocabularyItemJAXBSchema.DISPLAY_NAME, "nonex");
-        MultipartOutput multipart = VocabularyClientUtils.createVocabularyItemInstance(
-                       knownResourceId, NON_EXISTENT_ID, itemInfo, NON_EXISTENT_ID);
+        MultipartOutput multipart = 
+               VocabularyClientUtils.createVocabularyItemInstance(knownResourceId, 
+                       VocabularyClientUtils.createVocabularyItemRefName(NON_EXISTENT_ID, NON_EXISTENT_ID, true),
+                       itemInfo, client.getItemCommonPartName());
         ClientResponse<MultipartInput> res =
                 client.updateItem(knownResourceId, NON_EXISTENT_ID, multipart);
         int statusCode = res.getStatus();
@@ -798,7 +864,7 @@ public class VocabularyServiceTest extends AbstractServiceTestImpl {
 
    @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
         dependsOnMethods = {"createItem", "readItemList", "testItemSubmitRequest",
-            "updateItem"})
+            "updateItem", "verifyIllegalItemDisplayName"})
     public void deleteItem(String testName) throws Exception {
 
         // Perform setup.
index 4de20de8032f3fae59c94a652095ae7a3591a07f..b740f96e41330aa98524a1d94cb59faf6d1907c4 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 VocabularyResource 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();
@@ -480,13 +485,17 @@ public class VocabularyResource 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();
             throw new WebApplicationException(response);
         } catch (DocumentNotFoundException dnfe) {
             if (logger.isDebugEnabled()) {
-                logger.debug("caugth exception in updateVocabularyItem", dnfe);
+                logger.debug("caught DNF exception in updateVocabularyItem", dnfe);
             }
             Response response = Response.status(Response.Status.NOT_FOUND).entity(
                     "Update failed on VocabularyItem csid=" + itemcsid).type(
diff --git a/services/vocabulary/service/src/main/java/org/collectionspace/services/vocabulary/nuxeo/VocabularyItemValidatorHandler.java b/services/vocabulary/service/src/main/java/org/collectionspace/services/vocabulary/nuxeo/VocabularyItemValidatorHandler.java
new file mode 100644 (file)
index 0000000..f1cf2a2
--- /dev/null
@@ -0,0 +1,104 @@
+/**
+ *  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.vocabulary.nuxeo;
+
+import org.collectionspace.services.vocabulary.VocabularyitemsCommon;
+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 VocabularyItemValidatorHandler implements ValidatorHandler {
+
+    final Logger logger = LoggerFactory.getLogger(VocabularyItemValidatorHandler.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;
+            VocabularyitemsCommon vocabItem = (VocabularyitemsCommon) mctx.getInputPart(mctx.getCommonPartLabel(),
+                    VocabularyitemsCommon.class);
+            String msg = "";
+            boolean invalid = false;
+                                               String displayName = vocabItem.getDisplayName();
+            if((displayName==null)||(displayName.length()<2)) {
+                invalid = true;
+                msg += "displayName must be non-null, and at least 2 chars long!";
+            }
+            /*
+            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);
+        }
+    }
+}