blob: 13983002577cd59fb3374d378b41a4d1c41b5966 [file] [log] [blame]
Madan Jampaniad5b8c72016-09-12 15:05:01 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Madan Jampaniad5b8c72016-09-12 15:05:01 -07003 *
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
17package org.onosproject.store.primitives.resources.impl;
18
19import org.junit.Assert;
20import org.junit.Test;
21import org.onosproject.store.service.DocumentPath;
22import org.onosproject.store.service.DocumentTree;
23import org.onosproject.store.service.IllegalDocumentModificationException;
24import org.onosproject.store.service.NoSuchDocumentPathException;
25import org.onosproject.store.service.Versioned;
26
27/**
28 * Tests for {@code DefaultDocumentTree}.
29 */
30public 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 Jampani86983282016-09-15 14:55:48 -070052 @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 Jampanie59cc052016-09-16 13:07:07 -070064 tree.createRecursive(path("root"), "bar");
Madan Jampani86983282016-09-15 14:55:48 -070065 }
66
Madan Jampanibe68a832016-09-13 14:25:24 -070067 @Test(expected = IllegalDocumentModificationException.class)
Madan Jampaniad5b8c72016-09-12 15:05:01 -070068 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 Jampanibe68a832016-09-13 14:25:24 -0700123 @Test(expected = NoSuchDocumentPathException.class)
Madan Jampaniad5b8c72016-09-12 15:05:01 -0700124 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 Jampanibe68a832016-09-13 14:25:24 -0700130 @Test(expected = IllegalDocumentModificationException.class)
Madan Jampaniad5b8c72016-09-12 15:05:01 -0700131 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 Jampanibe68a832016-09-13 14:25:24 -0700145 @Test(expected = IllegalDocumentModificationException.class)
Madan Jampaniad5b8c72016-09-12 15:05:01 -0700146 public void testSetInvalidNode() {
147 DocumentTree<String> tree = new DefaultDocumentTree<>();
148 tree.set(path("root.a.b"), "alpha");
149 }
150
Yuta HIGUCHIfbd9ae92018-01-24 23:39:06 -0800151 @Test
Madan Jampaniad5b8c72016-09-12 15:05:01 -0700152 public void testReplaceWithVersion() {
153 DocumentTree<String> tree = new DefaultDocumentTree<>();
154 tree.create(path("root.a"), "bar");
155 tree.create(path("root.a.b"), "alpha");
156 Versioned<String> value = tree.get(path("root.a.b"));
157 Assert.assertTrue(tree.replace(path("root.a.b"), "beta", value.version()));
158 Assert.assertFalse(tree.replace(path("root.a.b"), "beta", value.version()));
159 Assert.assertFalse(tree.replace(path("root.x"), "beta", 1));
160 }
161
Yuta HIGUCHIfbd9ae92018-01-24 23:39:06 -0800162 @Test
Madan Jampaniad5b8c72016-09-12 15:05:01 -0700163 public void testReplaceWithValue() {
164 DocumentTree<String> tree = new DefaultDocumentTree<>();
165 tree.create(path("root.a"), "bar");
166 tree.create(path("root.a.b"), "alpha");
167 Assert.assertTrue(tree.replace(path("root.a.b"), "beta", "alpha"));
168 Assert.assertFalse(tree.replace(path("root.a.b"), "beta", "alpha"));
169 Assert.assertFalse(tree.replace(path("root.x"), "beta", "bar"));
170 Assert.assertTrue(tree.replace(path("root.x"), "beta", null));
171 }
172
Madan Jampanibe68a832016-09-13 14:25:24 -0700173 @Test(expected = IllegalDocumentModificationException.class)
Madan Jampaniad5b8c72016-09-12 15:05:01 -0700174 public void testRemoveRoot() {
175 DocumentTree<String> tree = new DefaultDocumentTree<>();
176 tree.removeNode(tree.root());
177 }
178
179 @Test
180 public void testRemove() {
181 DocumentTree<String> tree = new DefaultDocumentTree<>();
182 tree.create(path("root.a"), "bar");
183 tree.create(path("root.a.b"), "alpha");
184 Assert.assertEquals("alpha", tree.removeNode(path("root.a.b")).value());
185 Assert.assertEquals(0, tree.getChildren(path("root.a")).size());
186 }
187
Madan Jampanibe68a832016-09-13 14:25:24 -0700188 @Test(expected = NoSuchDocumentPathException.class)
Madan Jampaniad5b8c72016-09-12 15:05:01 -0700189 public void testRemoveInvalidNode() {
190 DocumentTree<String> tree = new DefaultDocumentTree<>();
191 tree.removeNode(path("root.a"));
192 }
193
194 private static DocumentPath path(String path) {
Thomas Vachuskae2bd1152017-03-23 13:42:32 -0700195 return DocumentPath.from(path.replace(".", DocumentPath.DEFAULT_SEPARATOR));
Madan Jampaniad5b8c72016-09-12 15:05:01 -0700196 }
197}