curator-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From randg...@apache.org
Subject [14/35] curator git commit: Include path in data validator - tests for data validator
Date Thu, 19 May 2016 19:02:29 GMT
Include path in data validator - tests for data validator


Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/450976f3
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/450976f3
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/450976f3

Branch: refs/heads/CURATOR-3.0
Commit: 450976f3d5c629b83d556b4f840ac844bb620830
Parents: 30d83b8
Author: randgalt <randgalt@apache.org>
Authored: Mon May 2 23:51:51 2016 -0500
Committer: randgalt <randgalt@apache.org>
Committed: Mon May 2 23:51:51 2016 -0500

----------------------------------------------------------------------
 .../imps/CuratorMultiTransactionImpl.java       |  2 +-
 .../framework/imps/SetDataBuilderImpl.java      |  2 +-
 .../curator/framework/schema/DataValidator.java |  2 +-
 .../framework/schema/DefaultDataValidator.java  |  2 +-
 .../apache/curator/framework/schema/Schema.java |  8 +--
 .../curator/framework/schema/TestSchema.java    | 57 ++++++++++++++++++--
 .../src/test/resources/schema3.json             |  7 +++
 7 files changed, 69 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/450976f3/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorMultiTransactionImpl.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorMultiTransactionImpl.java
b/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorMultiTransactionImpl.java
index 49824da..bfdf40f 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorMultiTransactionImpl.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorMultiTransactionImpl.java
@@ -138,7 +138,7 @@ public class CuratorMultiTransactionImpl implements
             else if ( curatorOp.get().getType() == ZooDefs.OpCode.setData )
             {
                 SetDataRequest setDataRequest = (SetDataRequest)curatorOp.get().toRequestRecord();
-                schema.validateData(setDataRequest.getData());
+                schema.validateData(setDataRequest.getPath(), setDataRequest.getData());
             }
         }
 

http://git-wip-us.apache.org/repos/asf/curator/blob/450976f3/curator-framework/src/main/java/org/apache/curator/framework/imps/SetDataBuilderImpl.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/imps/SetDataBuilderImpl.java
b/curator-framework/src/main/java/org/apache/curator/framework/imps/SetDataBuilderImpl.java
index 1f9b335..6a54146 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/imps/SetDataBuilderImpl.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/SetDataBuilderImpl.java
@@ -241,7 +241,7 @@ class SetDataBuilderImpl implements SetDataBuilder, BackgroundOperation<PathAndB
     @Override
     public Stat forPath(String path, byte[] data) throws Exception
     {
-        client.getSchemaSet().getSchema(path).validateData(data);
+        client.getSchemaSet().getSchema(path).validateData(path, data);
 
         if ( compress )
         {

http://git-wip-us.apache.org/repos/asf/curator/blob/450976f3/curator-framework/src/main/java/org/apache/curator/framework/schema/DataValidator.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/DataValidator.java
b/curator-framework/src/main/java/org/apache/curator/framework/schema/DataValidator.java
index 84e7933..8109f3b 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/DataValidator.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/DataValidator.java
@@ -20,5 +20,5 @@ package org.apache.curator.framework.schema;
 
 public interface DataValidator
 {
-    boolean isValid(byte[] data);
+    boolean isValid(String path, byte[] data);
 }

http://git-wip-us.apache.org/repos/asf/curator/blob/450976f3/curator-framework/src/main/java/org/apache/curator/framework/schema/DefaultDataValidator.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/DefaultDataValidator.java
b/curator-framework/src/main/java/org/apache/curator/framework/schema/DefaultDataValidator.java
index afc38d5..404c389 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/DefaultDataValidator.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/DefaultDataValidator.java
@@ -24,7 +24,7 @@ package org.apache.curator.framework.schema;
 public class DefaultDataValidator implements DataValidator
 {
     @Override
-    public boolean isValid(byte[] data)
+    public boolean isValid(String path, byte[] data)
     {
         return true;
     }

http://git-wip-us.apache.org/repos/asf/curator/blob/450976f3/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
b/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
index d7e9be7..6d08244 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
@@ -140,18 +140,20 @@ public class Schema
             throw new SchemaViolation(this, "Must be sequential");
         }
 
-        validateData(data);
+        validateData(path, data);
     }
 
     /**
      * Validate that this schema validates the data
      *
+     *
+     * @param path the znode full path
      * @param data data being set
      * @throws SchemaViolation if data is invalid
      */
-    public void validateData(byte[] data)
+    public void validateData(String path, byte[] data)
     {
-        if ( !dataValidator.isValid(data) )
+        if ( !dataValidator.isValid(path, data) )
         {
             throw new SchemaViolation(this, "Data is not valid");
         }

http://git-wip-us.apache.org/repos/asf/curator/blob/450976f3/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java
b/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java
index 7f29148..eadae80 100644
--- a/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java
+++ b/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java
@@ -35,7 +35,7 @@ public class TestSchema extends BaseClassForTests
     @Test
     public void testBasics() throws Exception
     {
-        SchemaSet schemaSet = loadSchemaSet("schema1.json");
+        SchemaSet schemaSet = loadSchemaSet("schema1.json", null);
         CuratorFramework client = newClient(schemaSet);
         try
         {
@@ -60,9 +60,52 @@ public class TestSchema extends BaseClassForTests
     }
 
     @Test
+    public void testDataValidator() throws Exception
+    {
+        final DataValidator dataValidator = new DataValidator()
+        {
+            @Override
+            public boolean isValid(String path, byte[] data)
+            {
+                return data.length > 0;
+            }
+        };
+        SchemaSetLoader.DataValidatorMapper dataValidatorMapper = new SchemaSetLoader.DataValidatorMapper()
+        {
+            @Override
+            public DataValidator getDataValidator(String name)
+            {
+                return dataValidator;
+            }
+        };
+        SchemaSet schemaSet = loadSchemaSet("schema3.json", dataValidatorMapper);
+        CuratorFramework client = newClient(schemaSet);
+        try
+        {
+            client.start();
+
+            try
+            {
+                client.create().forPath("/test", new byte[0]);
+                Assert.fail("Should've violated schema");
+            }
+            catch ( SchemaViolation dummy )
+            {
+                // expected
+            }
+
+            client.create().forPath("/test", "good".getBytes());
+        }
+        finally
+        {
+            CloseableUtils.closeQuietly(client);
+        }
+    }
+
+    @Test
     public void testMulti() throws Exception
     {
-        SchemaSet schemaSet = loadSchemaSet("schema2.json");
+        SchemaSet schemaSet = loadSchemaSet("schema2.json", null);
         CuratorFramework client = newClient(schemaSet);
         try
         {
@@ -94,6 +137,12 @@ public class TestSchema extends BaseClassForTests
         }
     }
 
+    @Override
+    protected boolean enabledSessionExpiredStateAware()
+    {
+        return true;
+    }
+
     private CuratorFramework newClient(SchemaSet schemaSet)
     {
         return CuratorFrameworkFactory.builder()
@@ -103,9 +152,9 @@ public class TestSchema extends BaseClassForTests
             .build();
     }
 
-    private SchemaSet loadSchemaSet(String name) throws IOException
+    private SchemaSet loadSchemaSet(String name, SchemaSetLoader.DataValidatorMapper dataValidatorMapper)
throws IOException
     {
         String json = Resources.toString(Resources.getResource(name), Charsets.UTF_8);
-        return new SchemaSetLoader(json, null).toSchemaSet(true);
+        return new SchemaSetLoader(json, dataValidatorMapper).toSchemaSet(true);
     }
 }

http://git-wip-us.apache.org/repos/asf/curator/blob/450976f3/curator-framework/src/test/resources/schema3.json
----------------------------------------------------------------------
diff --git a/curator-framework/src/test/resources/schema3.json b/curator-framework/src/test/resources/schema3.json
new file mode 100644
index 0000000..e777a11
--- /dev/null
+++ b/curator-framework/src/test/resources/schema3.json
@@ -0,0 +1,7 @@
+[
+  {
+    "name": "test",
+    "path": "/test",
+    "dataValidator": "test"
+  }
+]
\ No newline at end of file


Mime
View raw message