Work on testing modeled schema
Project: http://git-wip-us.apache.org/repos/asf/curator/repo Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/23a14879 Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/23a14879 Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/23a14879 Branch: refs/heads/CURATOR-397 Commit: 23a1487984b576b878e3a88287f74d9381e5dec7 Parents: 555e1d4 Author: randgalt <[email protected]> Authored: Mon May 8 19:11:22 2017 +0200 Committer: randgalt <[email protected]> Committed: Mon May 8 19:11:22 2017 +0200 ---------------------------------------------------------------------- .../apache/curator/framework/schema/Schema.java | 2 +- .../x/async/modeled/details/ModelSpecImpl.java | 24 ++++++++++------ .../x/async/modeled/TestModeledFramework.java | 30 ++++++++++++++++++++ 3 files changed, 47 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/curator/blob/23a14879/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 e9f4f18..bcb35d3 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 @@ -318,7 +318,7 @@ public class Schema ", pathRegex=" + pathRegex + ", path='" + fixedPath + '\'' + ", documentation='" + documentation + '\'' + - ", dataValidator=" + schemaValidator + + ", dataValidator=" + schemaValidator.getClass() + ", ephemeral=" + ephemeral + ", sequential=" + sequential + ", watched=" + watched + http://git-wip-us.apache.org/repos/asf/curator/blob/23a14879/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/details/ModelSpecImpl.java ---------------------------------------------------------------------- diff --git a/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/details/ModelSpecImpl.java b/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/details/ModelSpecImpl.java index 3a4a504..847ce61 100644 --- a/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/details/ModelSpecImpl.java +++ b/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/details/ModelSpecImpl.java @@ -29,6 +29,7 @@ import org.apache.curator.x.async.modeled.ModelSerializer; import org.apache.curator.x.async.modeled.ModelSpec; import org.apache.curator.x.async.modeled.ZPath; import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.ZooDefs; import org.apache.zookeeper.data.ACL; import java.util.List; import java.util.Objects; @@ -207,18 +208,25 @@ public class ModelSpecImpl<T> implements ModelSpec<T>, SchemaValidator @Override public boolean isValid(Schema schema, String path, byte[] data, List<ACL> acl) { - if ( !acl.equals(aclList) ) + if ( acl != null ) { - throw new SchemaViolation(schema, new SchemaViolation.ViolatorData(path, data, acl), "ACLs do not match model ACLs"); + List<ACL> localAclList = (aclList.size() > 0) ? aclList : ZooDefs.Ids.OPEN_ACL_UNSAFE; + if ( !acl.equals(localAclList) ) + { + throw new SchemaViolation(schema, new SchemaViolation.ViolatorData(path, data, acl), "ACLs do not match model ACLs"); + } } - try + if ( data != null ) { - serializer.deserialize(data); - } - catch ( RuntimeException e ) - { - throw new SchemaViolation(schema, new SchemaViolation.ViolatorData(path, data, acl), "Data cannot be deserialized into a model"); + try + { + serializer.deserialize(data); + } + catch ( RuntimeException e ) + { + throw new SchemaViolation(schema, new SchemaViolation.ViolatorData(path, data, acl), "Data cannot be deserialized into a model"); + } } return true; } http://git-wip-us.apache.org/repos/asf/curator/blob/23a14879/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/TestModeledFramework.java ---------------------------------------------------------------------- diff --git a/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/TestModeledFramework.java b/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/TestModeledFramework.java index a7884ee..209c463 100644 --- a/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/TestModeledFramework.java +++ b/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/TestModeledFramework.java @@ -21,6 +21,9 @@ package org.apache.curator.x.async.modeled; import com.google.common.collect.Sets; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.framework.schema.Schema; +import org.apache.curator.framework.schema.SchemaSet; +import org.apache.curator.framework.schema.SchemaViolation; import org.apache.curator.retry.RetryOneTime; import org.apache.curator.utils.CloseableUtils; import org.apache.curator.x.async.AsyncCuratorFramework; @@ -33,6 +36,7 @@ import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import java.math.BigInteger; +import java.util.Collections; import java.util.Set; import java.util.concurrent.CountDownLatch; @@ -129,4 +133,30 @@ public class TestModeledFramework extends CompletableBaseClassForTests ModeledFramework<TestModel> client = ModeledFramework.builder(async, modelSpec).watched().build(); complete(client.read().whenComplete((model, e) -> Assert.assertTrue(e instanceof RuntimeException))); } + + @Test + public void testSchema() throws Exception + { + Schema schema = modelSpec.schema(); + try ( CuratorFramework schemaClient = CuratorFrameworkFactory.builder() + .connectString(server.getConnectString()) + .retryPolicy(new RetryOneTime(1)) + .schemaSet(new SchemaSet(Collections.singletonList(schema), false)) + .build() ) { + schemaClient.start(); + + try + { + schemaClient.create().forPath(modelSpec.path().fullPath(), "asflasfas".getBytes()); + Assert.fail("Should've thrown SchemaViolation"); + } + catch ( SchemaViolation dummy ) + { + // expected + } + + ModeledFramework<TestModel> modeledSchemaClient = ModeledFramework.wrap(AsyncCuratorFramework.wrap(schemaClient), modelSpec); + complete(modeledSchemaClient.set(new TestModel("one", "two", "three", 4, BigInteger.ONE)), (dummy, e) -> Assert.assertNull(e)); + } + } }
