blob: 6e69eb6b0f72e645052241886ae10b6821daf4ba [file] [log] [blame]
Madan Jampani98094222016-09-15 21:12:46 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Madan Jampani98094222016-09-15 21:12:46 -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.service;
18
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertFalse;
21import static org.junit.Assert.assertTrue;
22
23import java.util.Arrays;
24
Sbhat353548bb22017-07-13 10:27:10 -070025import org.junit.Rule;
Madan Jampani98094222016-09-15 21:12:46 -070026import org.junit.Test;
Sbhat353548bb22017-07-13 10:27:10 -070027import org.junit.rules.ExpectedException;
Madan Jampani98094222016-09-15 21:12:46 -070028
29/**
30 * Unit tests for {@link DocumentPath}.
31 */
32public class DocumentPathTest {
33
34 @Test
35 public void testConstruction() {
Thomas Vachuskae2bd1152017-03-23 13:42:32 -070036 DocumentPath path = path("root.a.b");
Madan Jampani98094222016-09-15 21:12:46 -070037 assertEquals(path.pathElements(), Arrays.asList("root", "a", "b"));
Thomas Vachuskae2bd1152017-03-23 13:42:32 -070038 assertEquals(path("root.a"), path.parent());
Sbhat353548bb22017-07-13 10:27:10 -070039 assertEquals(path("b"), path.childPath());
Jordan Haltermanb0ac5902017-07-30 12:31:01 -070040 path = DocumentPath.from("root", "a", "b");
41 assertEquals(path.pathElements(), Arrays.asList("root", "a", "b"));
Madan Jampani98094222016-09-15 21:12:46 -070042 }
43
44 @Test
45 public void testAncestry() {
Sbhat353548bb22017-07-13 10:27:10 -070046 DocumentPath path = path("root");
47 assertEquals(path.childPath(), null);
Thomas Vachuskae2bd1152017-03-23 13:42:32 -070048 DocumentPath path1 = path("root.a.b");
49 DocumentPath path2 = path("root.a.d");
50 DocumentPath path3 = path("root.a.b.c");
Madan Jampani98094222016-09-15 21:12:46 -070051 DocumentPath lca = DocumentPath.leastCommonAncestor(Arrays.asList(path1, path2, path3));
Thomas Vachuskae2bd1152017-03-23 13:42:32 -070052 assertEquals(path("root.a"), lca);
Madan Jampani98094222016-09-15 21:12:46 -070053 assertTrue(path1.isAncestorOf(path3));
54 assertFalse(path1.isAncestorOf(path2));
55 assertTrue(path3.isDescendentOf(path3));
56 assertTrue(path3.isDescendentOf(path1));
57 assertFalse(path3.isDescendentOf(path2));
58 }
Thomas Vachuskae2bd1152017-03-23 13:42:32 -070059
Sbhat353548bb22017-07-13 10:27:10 -070060 @Rule
61 public final ExpectedException exception = ExpectedException.none();
62
63 @Test
64 public void testExceptions() {
65 DocumentPath parentPath = path("root.a.b");
66 DocumentPath path2 = exceptions("nodeName", parentPath);
67 exception.expect(IllegalDocumentNameException.class);
68 DocumentPath path1 = exceptions("node|name", parentPath);
69 }
70
71 @Test
72 public void comparePaths() {
73 DocumentPath one = path("root");
74 DocumentPath four = path("root.a.b.c.d");
75 DocumentPath difFour = path("root.e.c.b.a");
76 assertEquals(-1, one.compareTo(four));
77 assertEquals(1, four.compareTo(one));
78 assertEquals(4, difFour.compareTo(four));
79 assertEquals(0, difFour.compareTo(difFour));
80 }
81
82 private static DocumentPath exceptions(String nodeName, DocumentPath path) {
83 return new DocumentPath(nodeName, path);
84 }
85
Thomas Vachuskae2bd1152017-03-23 13:42:32 -070086 private static DocumentPath path(String path) {
87 return DocumentPath.from(path.replace(".", DocumentPath.DEFAULT_SEPARATOR));
88 }
Madan Jampani98094222016-09-15 21:12:46 -070089}