Updates to DocumentTreeNode + Simple implementation of DocumentTree interface

Change-Id: Icc162201a50de8ae48abdb8e769fb6ed86138a03
diff --git a/core/api/src/main/java/org/onosproject/store/primitives/DocumentTreeNode.java b/core/api/src/main/java/org/onosproject/store/primitives/DocumentTreeNode.java
deleted file mode 100644
index 18c50d7..0000000
--- a/core/api/src/main/java/org/onosproject/store/primitives/DocumentTreeNode.java
+++ /dev/null
@@ -1,170 +0,0 @@
-/*
- * Copyright 2016-present Open Networking Laboratory
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * 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.
- */
-
-package org.onosproject.store.primitives;
-
-import static com.google.common.base.Preconditions.checkNotNull;
-
-import java.util.Comparator;
-import java.util.Iterator;
-import java.util.Objects;
-import java.util.TreeSet;
-
-import org.onosproject.store.service.DocumentPath;
-
-import com.google.common.base.MoreObjects;
-import com.google.common.collect.Sets;
-
-/**
- * A {@code DocumentTree} node.
- */
-public class DocumentTreeNode<V> {
-    private final DocumentPath key;
-    private V value;
-    private long version;
-    private final TreeSet<DocumentTreeNode<V>> children =
-            Sets.newTreeSet(new Comparator<DocumentTreeNode<V>>() {
-                @Override
-                public int compare(DocumentTreeNode<V> o1,
-                                   DocumentTreeNode<V> o2) {
-                    return o1.getKey().compareTo(o2.getKey());
-                }
-            });
-    private final DocumentTreeNode<V> parent;
-
-    public DocumentTreeNode(DocumentPath key,
-                            V value,
-                            long version,
-                            DocumentTreeNode<V> parent) {
-        this.key = checkNotNull(key);
-        this.value = checkNotNull(value);
-        this.version = version;
-        this.parent = parent;
-    }
-
-    /**
-     * Returns this node's key.
-     *
-     * @return the key
-     */
-    public DocumentPath getKey() {
-        return key;
-    }
-
-    /**
-     * Returns this node's value.
-     *
-     * @return the value
-     */
-    public V getValue() {
-        return value;
-    }
-
-    /**
-     * Returns this node's version.
-     *
-     * @return the version
-     */
-    public long getVersion() {
-        return version;
-    }
-
-    /**
-     * Updates this node.
-     *
-     * @param newValue new value to be set
-     * @param newVersion new version to be set
-     */
-    public void update(V newValue, long newVersion) {
-        this.value = newValue;
-        this.version = newVersion;
-    }
-
-    /**
-     * Returns a collection of the children of this node.
-     *
-     * @return iterator for the children of this node.
-     */
-    public Iterator<DocumentTreeNode<V>> getChildren() {
-        return children.iterator();
-    }
-
-    /**
-     * Adds a child to this node.
-     *
-     * @param child the child node to be added
-     * @return {@code true} if the child set was modified as a result of this call, {@code false} otherwise
-     */
-    public boolean addChild(DocumentTreeNode<V> child) {
-        return children.add(child);
-    }
-
-    /**
-     * Removes a child node.
-     *
-     * @param child the child node to be removed
-     * @return {@code true} if the child set was modified as a result of this call, {@code false} otherwise
-     */
-    public boolean removeChild(String child) {
-        return children.remove(child);
-    }
-
-    /**
-     * Returns the parent of this node.
-     *
-     * @return the parent node of this node, which may be null
-     */
-    public DocumentTreeNode<V> getParent() {
-        return parent;
-    }
-
-    @Override
-    public int hashCode() {
-        return Objects.hash(this.key);
-    }
-
-    @Override
-    public boolean equals(Object obj) {
-        if (obj instanceof DocumentTreeNode) {
-            DocumentTreeNode<V> that = (DocumentTreeNode<V>) obj;
-            if (this.parent.equals(that.parent)) {
-                if (this.children.size() == that.children.size()) {
-                    for (DocumentTreeNode<V> child : this.children) {
-                        if (!that.children.contains(child)) {
-                            return false;
-                        }
-                    }
-                    return true;
-                }
-            }
-        }
-        return false;
-    }
-
-    @Override
-    public String toString() {
-        MoreObjects.ToStringHelper helper =
-                MoreObjects.toStringHelper(getClass())
-                .add("parent", this.parent)
-                .add("key", this.key)
-                .add("value", this.value);
-        for (DocumentTreeNode<V> child : children) {
-            helper = helper.add("child", child.key);
-        }
-        return helper.toString();
-    }
-
-}
diff --git a/core/api/src/main/java/org/onosproject/store/service/DocumentPath.java b/core/api/src/main/java/org/onosproject/store/service/DocumentPath.java
index 4e97cd4..1c43f4d 100644
--- a/core/api/src/main/java/org/onosproject/store/service/DocumentPath.java
+++ b/core/api/src/main/java/org/onosproject/store/service/DocumentPath.java
@@ -16,6 +16,7 @@
 
 package org.onosproject.store.service;
 
+import java.util.Arrays;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Objects;
@@ -71,6 +72,16 @@
     }
 
     /**
+     * Creates a new {@code DocumentPath} from a period delimited path string.
+     *
+     * @param path path string
+     * @return {@code DocumentPath} instance
+     */
+    public static DocumentPath from(String path) {
+        return new DocumentPath(Arrays.asList(path.split("\\.")));
+    }
+
+    /**
      * Returns a path for the parent of this node.
      *
      * @return parent node path. If this path is for the root, returns {@code null}.
diff --git a/core/api/src/main/java/org/onosproject/store/service/DocumentTree.java b/core/api/src/main/java/org/onosproject/store/service/DocumentTree.java
index 35c6deb..4255b65 100644
--- a/core/api/src/main/java/org/onosproject/store/service/DocumentTree.java
+++ b/core/api/src/main/java/org/onosproject/store/service/DocumentTree.java
@@ -16,15 +16,16 @@
 
 package org.onosproject.store.service;
 
-import java.util.Iterator;
+import java.util.Map;
 
-import org.onosproject.store.primitives.DocumentTreeNode;
+import javax.annotation.concurrent.NotThreadSafe;
 
 /**
  * A hierarchical <a href="https://en.wikipedia.org/wiki/Document_Object_Model">document tree</a> data structure.
  *
  * @param <V> document tree value type
  */
+@NotThreadSafe
 public interface DocumentTree<V> {
 
     /**
@@ -35,13 +36,13 @@
     DocumentPath root();
 
     /**
-     * Returns an iterator for all the first order descendants of a node.
+     * Returns the child values for this node.
      *
      * @param path path to the node
-     * @return an iterator for the child nodes of the specified node path
+     * @return mapping from a child name to its value
      * @throws NoSuchDocumentPathException if the path does not point to a valid node
      */
-    Iterator<DocumentTreeNode<V>> getChildren(DocumentPath path);
+    Map<String, Versioned<V>> getChildren(DocumentPath path);
 
     /**
      * Returns a document tree node.
@@ -49,7 +50,7 @@
      * @param path path to node
      * @return node value or {@code null} if path does not point to a valid node
      */
-    DocumentTreeNode<V> getNode(DocumentPath path);
+    Versioned<V> get(DocumentPath path);
 
     /**
      * Creates or updates a document tree node.
@@ -59,7 +60,7 @@
      * @return the previous mapping or {@code null} if there was no previous mapping
      * @throws NoSuchDocumentPathException if the parent node (for the node to create/update) does not exist
      */
-    V putNode(DocumentPath path, V value);
+    Versioned<V> set(DocumentPath path, V value);
 
     /**
      * Creates a document tree node if one does not exist already.
@@ -69,7 +70,7 @@
      * @return returns {@code true} if the mapping could be added successfully, {@code false} otherwise
      * @throws NoSuchDocumentPathException if the parent node (for the node to create) does not exist
      */
-    boolean createNode(DocumentPath path, V value);
+    boolean create(DocumentPath path, V value);
 
     /**
      * Conditionally updates a tree node if the current version matches a specified version.
@@ -77,7 +78,7 @@
      * @param path path for the node to create
      * @param newValue the non-null value to be associated with the key
      * @param version current version of the value for update to occur
-     * @return returns {@code true} if the update was made, {@code false} otherwise
+     * @return returns {@code true} if the update was made and the tree was modified, {@code false} otherwise
      * @throws NoSuchDocumentPathException if the parent node (for the node to create) does not exist
      */
     boolean replace(DocumentPath path, V newValue, long version);
@@ -88,7 +89,8 @@
      * @param path path for the node to create
      * @param newValue the non-null value to be associated with the key
      * @param currentValue current value for update to occur
-     * @return returns {@code true} if the update was made, {@code false} otherwise
+     * @return returns {@code true} if the update was made and the tree was modified, {@code false} otherwise.
+     * This method returns {@code false} if the newValue and currentValue are same.
      * @throws NoSuchDocumentPathException if the parent node (for the node to create) does not exist
      */
     boolean replace(DocumentPath path, V newValue, V currentValue);
@@ -96,12 +98,11 @@
     /**
      * Removes the node with the specified path.
      *
-     * is not a leaf node i.e has one or more children
-     * @param key path for the node to remove
+     * @param path path for the node to remove
      * @return the previous value of the node or {@code null} if it did not exist
      * @throws IllegalDocumentModificationException if the remove to be removed
      */
-    V removeNode(DocumentPath key);
+    Versioned<V> removeNode(DocumentPath path);
 
     /**
      * Registers a listener to be notified when a subtree rooted at the specified path
diff --git a/core/api/src/main/java/org/onosproject/store/service/DocumentTreeNode.java b/core/api/src/main/java/org/onosproject/store/service/DocumentTreeNode.java
new file mode 100644
index 0000000..9de9780
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/store/service/DocumentTreeNode.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+
+package org.onosproject.store.service;
+
+import java.util.Iterator;
+
+import javax.annotation.concurrent.NotThreadSafe;
+
+/**
+ * A {@code DocumentTree} node.
+ *
+ * @param <V> value type
+ */
+@NotThreadSafe
+public interface DocumentTreeNode<V> {
+
+    /**
+     * Returns the path to this node in a {@code DocumentTree}.
+     *
+     * @return absolute path
+     */
+    DocumentPath path();
+
+    /**
+     * Returns the value of this node.
+     *
+     * @return node value (and version)
+     */
+    Versioned<V> value();
+
+    /**
+     * Returns the children of this node.
+     *
+     * @return iterator for this node's children
+     */
+    Iterator<DocumentTreeNode<V>> children();
+
+    /**
+     * Returns the child node of this node with the specified relative path name.
+     *
+     * @param relativePath relative path name for the child node.
+     * @return child node; this method returns {@code null} if no such child exists
+     */
+    DocumentTreeNode<V> child(String relativePath);
+
+    /**
+     * Returns if this node has one or more children.
+     * @return {@code true} if yes, {@code false} otherwise
+     */
+    default boolean hasChildren() {
+        return children().hasNext();
+    }
+}
diff --git a/core/store/primitives/src/main/java/org/onosproject/store/primitives/resources/impl/DefaultDocumentTree.java b/core/store/primitives/src/main/java/org/onosproject/store/primitives/resources/impl/DefaultDocumentTree.java
new file mode 100644
index 0000000..fdfe9dd
--- /dev/null
+++ b/core/store/primitives/src/main/java/org/onosproject/store/primitives/resources/impl/DefaultDocumentTree.java
@@ -0,0 +1,175 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+
+package org.onosproject.store.primitives.resources.impl;
+
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.onosproject.store.service.DocumentPath;
+import org.onosproject.store.service.DocumentTree;
+import org.onosproject.store.service.DocumentTreeListener;
+import org.onosproject.store.service.DocumentTreeNode;
+import org.onosproject.store.service.IllegalDocumentModificationException;
+import org.onosproject.store.service.NoSuchDocumentPathException;
+import org.onosproject.store.service.Versioned;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Maps;
+
+/**
+ * Simple implementation of a {@link DocumentTree}.
+ *
+ * @param <V> tree node value type
+ */
+public class DefaultDocumentTree<V> implements DocumentTree<V> {
+
+    private static final DocumentPath ROOT_PATH = DocumentPath.from("root");
+    private final DefaultDocumentTreeNode<V> root;
+    private final AtomicInteger versionCounter = new AtomicInteger(0);
+
+    public DefaultDocumentTree() {
+        root = new DefaultDocumentTreeNode<V>(ROOT_PATH, null, nextVersion(), null);
+    }
+
+    @Override
+    public DocumentPath root() {
+        return ROOT_PATH;
+    }
+
+    @Override
+    public Map<String, Versioned<V>> getChildren(DocumentPath path) {
+        DocumentTreeNode<V> node = getNode(path);
+        if (node != null) {
+            Map<String, Versioned<V>> childrenValues = Maps.newHashMap();
+            node.children().forEachRemaining(n -> childrenValues.put(simpleName(n.path()), n.value()));
+            return childrenValues;
+        }
+        throw new NoSuchDocumentPathException();
+    }
+
+    @Override
+    public Versioned<V> get(DocumentPath path) {
+        DocumentTreeNode<V> currentNode = getNode(path);
+        return currentNode != null ? currentNode.value() : null;
+    }
+
+    @Override
+    public Versioned<V> set(DocumentPath path, V value) {
+        checkRootModification(path);
+        DefaultDocumentTreeNode<V> node = getNode(path);
+        if (node != null) {
+            return node.update(value, nextVersion());
+        } else {
+            create(path, value);
+            return null;
+        }
+    }
+
+    @Override
+    public boolean create(DocumentPath path, V value) {
+        checkRootModification(path);
+        DocumentTreeNode<V> node = getNode(path);
+        if (node != null) {
+            return false;
+        }
+        DocumentPath parentPath = path.parent();
+        DefaultDocumentTreeNode<V> parentNode =  getNode(parentPath);
+        if (parentNode == null) {
+            throw new IllegalDocumentModificationException();
+        }
+        parentNode.addChild(simpleName(path), value, nextVersion());
+        return true;
+    }
+
+    @Override
+    public boolean replace(DocumentPath path, V newValue, long version) {
+        checkRootModification(path);
+        DocumentTreeNode<V> node = getNode(path);
+        if (node != null && node.value() != null && node.value().version() == version) {
+            if (!Objects.equals(newValue, node.value().value())) {
+                set(path, newValue);
+                return true;
+            }
+        }
+        return false;
+    }
+
+    @Override
+    public boolean replace(DocumentPath path, V newValue, V currentValue) {
+        checkRootModification(path);
+        if (Objects.equals(newValue, currentValue)) {
+            return false;
+        }
+        DocumentTreeNode<V> node = getNode(path);
+        if (node != null && Objects.equals(Versioned.valueOrNull(node.value()), currentValue)) {
+            set(path, newValue);
+            return true;
+        }
+        return false;
+    }
+
+    @Override
+    public Versioned<V> removeNode(DocumentPath path) {
+        checkRootModification(path);
+        DefaultDocumentTreeNode<V> nodeToRemove = getNode(path);
+        if (nodeToRemove == null) {
+            throw new NoSuchDocumentPathException();
+        }
+        if (nodeToRemove.hasChildren()) {
+            throw new IllegalDocumentModificationException();
+        }
+        DefaultDocumentTreeNode<V> parent = (DefaultDocumentTreeNode<V>) nodeToRemove.parent();
+        parent.removeChild(simpleName(path));
+        return nodeToRemove.value();
+    }
+
+    @Override
+    public void addListener(DocumentPath path, DocumentTreeListener<V> listener) {
+        // TODO Auto-generated method stub
+    }
+
+    @Override
+    public void removeListener(DocumentTreeListener<V> listener) {
+        // TODO Auto-generated method stub
+    }
+
+    private DefaultDocumentTreeNode<V> getNode(DocumentPath path) {
+        Iterator<String> pathElements = path.pathElements().iterator();
+        DefaultDocumentTreeNode<V> currentNode = root;
+        Preconditions.checkState("root".equals(pathElements.next()), "Path should start with root");
+        while (pathElements.hasNext() &&  currentNode != null) {
+            currentNode = (DefaultDocumentTreeNode<V>) currentNode.child(pathElements.next());
+        }
+        return currentNode;
+    }
+
+    private long nextVersion() {
+        return versionCounter.incrementAndGet();
+    }
+
+    private String simpleName(DocumentPath path) {
+        return path.pathElements().get(path.pathElements().size() - 1);
+    }
+
+    private void checkRootModification(DocumentPath path) {
+        if (ROOT_PATH.equals(path)) {
+            throw new IllegalDocumentModificationException();
+        }
+    }
+}
diff --git a/core/store/primitives/src/main/java/org/onosproject/store/primitives/resources/impl/DefaultDocumentTreeNode.java b/core/store/primitives/src/main/java/org/onosproject/store/primitives/resources/impl/DefaultDocumentTreeNode.java
new file mode 100644
index 0000000..2720db8
--- /dev/null
+++ b/core/store/primitives/src/main/java/org/onosproject/store/primitives/resources/impl/DefaultDocumentTreeNode.java
@@ -0,0 +1,148 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+
+package org.onosproject.store.primitives.resources.impl;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.util.Iterator;
+import java.util.Objects;
+import java.util.TreeMap;
+
+import org.onosproject.store.service.DocumentPath;
+import org.onosproject.store.service.DocumentTreeNode;
+import org.onosproject.store.service.Versioned;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
+
+/**
+ * A {@code DocumentTree} node.
+ */
+public class DefaultDocumentTreeNode<V> implements DocumentTreeNode<V> {
+    private final DocumentPath key;
+    private Versioned<V> value;
+    private final TreeMap<String, DocumentTreeNode<V>> children = Maps.newTreeMap();
+    private final DocumentTreeNode<V> parent;
+
+    public DefaultDocumentTreeNode(DocumentPath key,
+                            V value,
+                            long version,
+                            DocumentTreeNode<V> parent) {
+        this.key = checkNotNull(key);
+        this.value = new Versioned<>(value, version);
+        this.parent = parent;
+    }
+
+    @Override
+    public DocumentPath path() {
+        return key;
+    }
+
+    @Override
+    public Versioned<V> value() {
+        return value;
+    }
+
+    @Override
+    public Iterator<DocumentTreeNode<V>> children() {
+        return ImmutableList.copyOf(children.values()).iterator();
+    }
+
+    @Override
+    public DocumentTreeNode<V> child(String name) {
+        return children.get(name);
+    }
+
+
+    public DocumentTreeNode<V> parent() {
+        return parent;
+    }
+
+    /**
+     * Adds a new child only if one does not exist with the name.
+     * @param name relative path name of the child node
+     * @param newValue new value to set
+     * @param newVersion new version to set
+     * @return previous value; can be {@code null} if no child currently exists with that relative path name.
+     * a non null return value indicates child already exists and no modification occured.
+     */
+    public Versioned<V> addChild(String name, V newValue, long newVersion) {
+        DefaultDocumentTreeNode<V> child = (DefaultDocumentTreeNode<V>) children.get(name);
+        if (child != null) {
+            return child.value();
+        }
+        children.put(name, new DefaultDocumentTreeNode<>(new DocumentPath(name, path()), newValue, newVersion, this));
+        return null;
+    }
+
+    /**
+     * Updates the node value.
+     *
+     * @param newValue new value to set
+     * @param newVersion new version to set
+     * @return previous value
+     */
+    public Versioned<V> update(V newValue, long newVersion) {
+        Versioned<V> previousValue = value;
+        value = new Versioned<>(newValue, newVersion);
+        return previousValue;
+    }
+
+
+    /**
+     * Removes a child node.
+     *
+     * @param name the name of child node to be removed
+     * @return {@code true} if the child set was modified as a result of this call, {@code false} otherwise
+     */
+    public boolean removeChild(String name) {
+        return children.remove(name) != null;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(this.key);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (obj instanceof DefaultDocumentTreeNode) {
+            DefaultDocumentTreeNode<V> that = (DefaultDocumentTreeNode<V>) obj;
+            if (this.parent.equals(that.parent)) {
+                if (this.children.size() == that.children.size()) {
+                    return Sets.symmetricDifference(this.children.keySet(), that.children.keySet()).isEmpty();
+                }
+            }
+        }
+        return false;
+    }
+
+    @Override
+    public String toString() {
+        MoreObjects.ToStringHelper helper =
+                MoreObjects.toStringHelper(getClass())
+                .add("parent", this.parent)
+                .add("key", this.key)
+                .add("value", this.value);
+        for (DocumentTreeNode<V> child : children.values()) {
+            helper = helper.add("child", child);
+        }
+        return helper.toString();
+    }
+}
diff --git a/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/DefaultDocumentTreeTest.java b/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/DefaultDocumentTreeTest.java
new file mode 100644
index 0000000..d44447c
--- /dev/null
+++ b/core/store/primitives/src/test/java/org/onosproject/store/primitives/resources/impl/DefaultDocumentTreeTest.java
@@ -0,0 +1,180 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+
+package org.onosproject.store.primitives.resources.impl;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.onosproject.store.service.DocumentPath;
+import org.onosproject.store.service.DocumentTree;
+import org.onosproject.store.service.IllegalDocumentModificationException;
+import org.onosproject.store.service.NoSuchDocumentPathException;
+import org.onosproject.store.service.Versioned;
+
+/**
+ * Tests for {@code DefaultDocumentTree}.
+ */
+public class DefaultDocumentTreeTest {
+
+    @Test
+    public void testTreeConstructor() {
+        DocumentTree<String> tree = new DefaultDocumentTree<>();
+        Assert.assertEquals(tree.root(), path("root"));
+    }
+
+    @Test
+    public void testCreateNodeAtRoot() {
+        DocumentTree<String> tree = new DefaultDocumentTree<>();
+        Assert.assertTrue(tree.create(path("root.a"), "bar"));
+        Assert.assertFalse(tree.create(path("root.a"), "baz"));
+    }
+
+    @Test
+    public void testCreateNodeAtNonRoot() {
+        DocumentTree<String> tree = new DefaultDocumentTree<>();
+        tree.create(path("root.a"), "bar");
+        Assert.assertTrue(tree.create(path("root.a.b"), "baz"));
+    }
+
+    @Test(expected=IllegalDocumentModificationException.class)
+    public void testCreateNodeFailure() {
+        DocumentTree<String> tree = new DefaultDocumentTree<>();
+        tree.create(path("root.a.b"), "bar");
+    }
+
+    @Test
+    public void testGetRootValue() {
+        DocumentTree<String> tree = new DefaultDocumentTree<>();
+        tree.create(path("root.a"), "bar");
+        tree.create(path("root.a.b"), "baz");
+        Versioned<String> root = tree.get(path("root"));
+        Assert.assertNotNull(root);
+        Assert.assertNull(root.value());
+    }
+
+    @Test
+    public void testGetInnerNode() {
+        DocumentTree<String> tree = new DefaultDocumentTree<>();
+        tree.create(path("root.a"), "bar");
+        tree.create(path("root.a.b"), "baz");
+        Versioned<String> nodeValue = tree.get(path("root.a"));
+        Assert.assertNotNull(nodeValue);
+        Assert.assertEquals("bar", nodeValue.value());
+    }
+
+    @Test
+    public void testGetLeafNode() {
+        DocumentTree<String> tree = new DefaultDocumentTree<>();
+        tree.create(path("root.a"), "bar");
+        tree.create(path("root.a.b"), "baz");
+        Versioned<String> nodeValue = tree.get(path("root.a.b"));
+        Assert.assertNotNull(nodeValue);
+        Assert.assertEquals("baz", nodeValue.value());
+    }
+
+    @Test
+    public void getMissingNode() {
+        DocumentTree<String> tree = new DefaultDocumentTree<>();
+        tree.create(path("root.a"), "bar");
+        tree.create(path("root.a.b"), "baz");
+        Assert.assertNull(tree.get(path("root.x")));
+        Assert.assertNull(tree.get(path("root.a.x")));
+        Assert.assertNull(tree.get(path("root.a.b.x")));
+    }
+
+    @Test
+    public void testGetChildren() {
+        DocumentTree<String> tree = new DefaultDocumentTree<>();
+        tree.create(path("root.a"), "bar");
+        tree.create(path("root.a.b"), "alpha");
+        tree.create(path("root.a.c"), "beta");
+        Assert.assertEquals(2, tree.getChildren(path("root.a")).size());
+        Assert.assertEquals(0, tree.getChildren(path("root.a.b")).size());
+    }
+
+    @Test(expected=NoSuchDocumentPathException.class)
+    public void testGetChildrenFailure() {
+        DocumentTree<String> tree = new DefaultDocumentTree<>();
+        tree.create(path("root.a"), "bar");
+        tree.getChildren(path("root.a.b"));
+    }
+
+    @Test(expected=IllegalDocumentModificationException.class)
+    public void testSetRootFailure() {
+        DocumentTree<String> tree = new DefaultDocumentTree<>();
+        tree.set(tree.root(), "bar");
+    }
+
+    @Test
+    public void testSet() {
+        DocumentTree<String> tree = new DefaultDocumentTree<>();
+        tree.create(path("root.a"), "bar");
+        Assert.assertNull(tree.set(path("root.a.b"), "alpha"));
+        Assert.assertEquals("alpha", tree.set(path("root.a.b"), "beta").value());
+        Assert.assertEquals("beta", tree.get(path("root.a.b")).value());
+    }
+
+    @Test(expected=IllegalDocumentModificationException.class)
+    public void testSetInvalidNode() {
+        DocumentTree<String> tree = new DefaultDocumentTree<>();
+        tree.set(path("root.a.b"), "alpha");
+    }
+
+    public void testReplaceWithVersion() {
+        DocumentTree<String> tree = new DefaultDocumentTree<>();
+        tree.create(path("root.a"), "bar");
+        tree.create(path("root.a.b"), "alpha");
+        Versioned<String> value = tree.get(path("root.a.b"));
+        Assert.assertTrue(tree.replace(path("root.a.b"), "beta", value.version()));
+        Assert.assertFalse(tree.replace(path("root.a.b"), "beta", value.version()));
+        Assert.assertFalse(tree.replace(path("root.x"), "beta", 1));
+    }
+
+    public void testReplaceWithValue() {
+        DocumentTree<String> tree = new DefaultDocumentTree<>();
+        tree.create(path("root.a"), "bar");
+        tree.create(path("root.a.b"), "alpha");
+        Assert.assertTrue(tree.replace(path("root.a.b"), "beta", "alpha"));
+        Assert.assertFalse(tree.replace(path("root.a.b"), "beta", "alpha"));
+        Assert.assertFalse(tree.replace(path("root.x"), "beta", "bar"));
+        Assert.assertTrue(tree.replace(path("root.x"), "beta", null));
+    }
+
+    @Test(expected=IllegalDocumentModificationException.class)
+    public void testRemoveRoot() {
+        DocumentTree<String> tree = new DefaultDocumentTree<>();
+        tree.removeNode(tree.root());
+    }
+
+    @Test
+    public void testRemove() {
+        DocumentTree<String> tree = new DefaultDocumentTree<>();
+        tree.create(path("root.a"), "bar");
+        tree.create(path("root.a.b"), "alpha");
+        Assert.assertEquals("alpha", tree.removeNode(path("root.a.b")).value());
+        Assert.assertEquals(0, tree.getChildren(path("root.a")).size());
+    }
+
+    @Test(expected=NoSuchDocumentPathException.class)
+    public void testRemoveInvalidNode() {
+        DocumentTree<String> tree = new DefaultDocumentTree<>();
+        tree.removeNode(path("root.a"));
+    }
+
+    private static DocumentPath path(String path) {
+        return DocumentPath.from(path);
+    }
+}