Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.store.primitives.resources.impl; |
| 18 | |
| 19 | import org.junit.Assert; |
| 20 | import org.junit.Test; |
| 21 | import org.onosproject.store.service.DocumentPath; |
| 22 | import org.onosproject.store.service.DocumentTree; |
| 23 | import org.onosproject.store.service.IllegalDocumentModificationException; |
| 24 | import org.onosproject.store.service.NoSuchDocumentPathException; |
| 25 | import org.onosproject.store.service.Versioned; |
| 26 | |
| 27 | /** |
| 28 | * Tests for {@code DefaultDocumentTree}. |
| 29 | */ |
| 30 | public class DefaultDocumentTreeTest { |
| 31 | |
| 32 | @Test |
| 33 | public void testTreeConstructor() { |
| 34 | DocumentTree<String> tree = new DefaultDocumentTree<>(); |
| 35 | Assert.assertEquals(tree.root(), path("root")); |
| 36 | } |
| 37 | |
| 38 | @Test |
| 39 | public void testCreateNodeAtRoot() { |
| 40 | DocumentTree<String> tree = new DefaultDocumentTree<>(); |
| 41 | Assert.assertTrue(tree.create(path("root.a"), "bar")); |
| 42 | Assert.assertFalse(tree.create(path("root.a"), "baz")); |
| 43 | } |
| 44 | |
| 45 | @Test |
| 46 | public void testCreateNodeAtNonRoot() { |
| 47 | DocumentTree<String> tree = new DefaultDocumentTree<>(); |
| 48 | tree.create(path("root.a"), "bar"); |
| 49 | Assert.assertTrue(tree.create(path("root.a.b"), "baz")); |
| 50 | } |
| 51 | |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 52 | @Test |
| 53 | public void testCreateRecursive() { |
| 54 | DocumentTree<String> tree = new DefaultDocumentTree<>(); |
| 55 | tree.createRecursive(path("root.a.b.c"), "bar"); |
| 56 | Assert.assertEquals(tree.get(path("root.a.b.c")).value(), "bar"); |
| 57 | Assert.assertNull(tree.get(path("root.a.b")).value()); |
| 58 | Assert.assertNull(tree.get(path("root.a")).value()); |
| 59 | } |
| 60 | |
| 61 | @Test(expected = IllegalDocumentModificationException.class) |
| 62 | public void testCreateRecursiveRoot() { |
| 63 | DocumentTree<String> tree = new DefaultDocumentTree<>(); |
Madan Jampani | e59cc05 | 2016-09-16 13:07:07 -0700 | [diff] [blame] | 64 | tree.createRecursive(path("root"), "bar"); |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Madan Jampani | be68a83 | 2016-09-13 14:25:24 -0700 | [diff] [blame] | 67 | @Test(expected = IllegalDocumentModificationException.class) |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 68 | public void testCreateNodeFailure() { |
| 69 | DocumentTree<String> tree = new DefaultDocumentTree<>(); |
| 70 | tree.create(path("root.a.b"), "bar"); |
| 71 | } |
| 72 | |
| 73 | @Test |
| 74 | public void testGetRootValue() { |
| 75 | DocumentTree<String> tree = new DefaultDocumentTree<>(); |
| 76 | tree.create(path("root.a"), "bar"); |
| 77 | tree.create(path("root.a.b"), "baz"); |
| 78 | Versioned<String> root = tree.get(path("root")); |
| 79 | Assert.assertNotNull(root); |
| 80 | Assert.assertNull(root.value()); |
| 81 | } |
| 82 | |
| 83 | @Test |
| 84 | public void testGetInnerNode() { |
| 85 | DocumentTree<String> tree = new DefaultDocumentTree<>(); |
| 86 | tree.create(path("root.a"), "bar"); |
| 87 | tree.create(path("root.a.b"), "baz"); |
| 88 | Versioned<String> nodeValue = tree.get(path("root.a")); |
| 89 | Assert.assertNotNull(nodeValue); |
| 90 | Assert.assertEquals("bar", nodeValue.value()); |
| 91 | } |
| 92 | |
| 93 | @Test |
| 94 | public void testGetLeafNode() { |
| 95 | DocumentTree<String> tree = new DefaultDocumentTree<>(); |
| 96 | tree.create(path("root.a"), "bar"); |
| 97 | tree.create(path("root.a.b"), "baz"); |
| 98 | Versioned<String> nodeValue = tree.get(path("root.a.b")); |
| 99 | Assert.assertNotNull(nodeValue); |
| 100 | Assert.assertEquals("baz", nodeValue.value()); |
| 101 | } |
| 102 | |
| 103 | @Test |
| 104 | public void getMissingNode() { |
| 105 | DocumentTree<String> tree = new DefaultDocumentTree<>(); |
| 106 | tree.create(path("root.a"), "bar"); |
| 107 | tree.create(path("root.a.b"), "baz"); |
| 108 | Assert.assertNull(tree.get(path("root.x"))); |
| 109 | Assert.assertNull(tree.get(path("root.a.x"))); |
| 110 | Assert.assertNull(tree.get(path("root.a.b.x"))); |
| 111 | } |
| 112 | |
| 113 | @Test |
| 114 | public void testGetChildren() { |
| 115 | DocumentTree<String> tree = new DefaultDocumentTree<>(); |
| 116 | tree.create(path("root.a"), "bar"); |
| 117 | tree.create(path("root.a.b"), "alpha"); |
| 118 | tree.create(path("root.a.c"), "beta"); |
| 119 | Assert.assertEquals(2, tree.getChildren(path("root.a")).size()); |
| 120 | Assert.assertEquals(0, tree.getChildren(path("root.a.b")).size()); |
| 121 | } |
| 122 | |
Madan Jampani | be68a83 | 2016-09-13 14:25:24 -0700 | [diff] [blame] | 123 | @Test(expected = NoSuchDocumentPathException.class) |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 124 | public void testGetChildrenFailure() { |
| 125 | DocumentTree<String> tree = new DefaultDocumentTree<>(); |
| 126 | tree.create(path("root.a"), "bar"); |
| 127 | tree.getChildren(path("root.a.b")); |
| 128 | } |
| 129 | |
Madan Jampani | be68a83 | 2016-09-13 14:25:24 -0700 | [diff] [blame] | 130 | @Test(expected = IllegalDocumentModificationException.class) |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 131 | public void testSetRootFailure() { |
| 132 | DocumentTree<String> tree = new DefaultDocumentTree<>(); |
| 133 | tree.set(tree.root(), "bar"); |
| 134 | } |
| 135 | |
| 136 | @Test |
| 137 | public void testSet() { |
| 138 | DocumentTree<String> tree = new DefaultDocumentTree<>(); |
| 139 | tree.create(path("root.a"), "bar"); |
| 140 | Assert.assertNull(tree.set(path("root.a.b"), "alpha")); |
| 141 | Assert.assertEquals("alpha", tree.set(path("root.a.b"), "beta").value()); |
| 142 | Assert.assertEquals("beta", tree.get(path("root.a.b")).value()); |
| 143 | } |
| 144 | |
Madan Jampani | be68a83 | 2016-09-13 14:25:24 -0700 | [diff] [blame] | 145 | @Test(expected = IllegalDocumentModificationException.class) |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 146 | public void testSetInvalidNode() { |
| 147 | DocumentTree<String> tree = new DefaultDocumentTree<>(); |
| 148 | tree.set(path("root.a.b"), "alpha"); |
| 149 | } |
| 150 | |
| 151 | public void testReplaceWithVersion() { |
| 152 | DocumentTree<String> tree = new DefaultDocumentTree<>(); |
| 153 | tree.create(path("root.a"), "bar"); |
| 154 | tree.create(path("root.a.b"), "alpha"); |
| 155 | Versioned<String> value = tree.get(path("root.a.b")); |
| 156 | Assert.assertTrue(tree.replace(path("root.a.b"), "beta", value.version())); |
| 157 | Assert.assertFalse(tree.replace(path("root.a.b"), "beta", value.version())); |
| 158 | Assert.assertFalse(tree.replace(path("root.x"), "beta", 1)); |
| 159 | } |
| 160 | |
| 161 | public void testReplaceWithValue() { |
| 162 | DocumentTree<String> tree = new DefaultDocumentTree<>(); |
| 163 | tree.create(path("root.a"), "bar"); |
| 164 | tree.create(path("root.a.b"), "alpha"); |
| 165 | Assert.assertTrue(tree.replace(path("root.a.b"), "beta", "alpha")); |
| 166 | Assert.assertFalse(tree.replace(path("root.a.b"), "beta", "alpha")); |
| 167 | Assert.assertFalse(tree.replace(path("root.x"), "beta", "bar")); |
| 168 | Assert.assertTrue(tree.replace(path("root.x"), "beta", null)); |
| 169 | } |
| 170 | |
Madan Jampani | be68a83 | 2016-09-13 14:25:24 -0700 | [diff] [blame] | 171 | @Test(expected = IllegalDocumentModificationException.class) |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 172 | public void testRemoveRoot() { |
| 173 | DocumentTree<String> tree = new DefaultDocumentTree<>(); |
| 174 | tree.removeNode(tree.root()); |
| 175 | } |
| 176 | |
| 177 | @Test |
| 178 | public void testRemove() { |
| 179 | DocumentTree<String> tree = new DefaultDocumentTree<>(); |
| 180 | tree.create(path("root.a"), "bar"); |
| 181 | tree.create(path("root.a.b"), "alpha"); |
| 182 | Assert.assertEquals("alpha", tree.removeNode(path("root.a.b")).value()); |
| 183 | Assert.assertEquals(0, tree.getChildren(path("root.a")).size()); |
| 184 | } |
| 185 | |
Madan Jampani | be68a83 | 2016-09-13 14:25:24 -0700 | [diff] [blame] | 186 | @Test(expected = NoSuchDocumentPathException.class) |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 187 | public void testRemoveInvalidNode() { |
| 188 | DocumentTree<String> tree = new DefaultDocumentTree<>(); |
| 189 | tree.removeNode(path("root.a")); |
| 190 | } |
| 191 | |
| 192 | private static DocumentPath path(String path) { |
| 193 | return DocumentPath.from(path); |
| 194 | } |
| 195 | } |