blob: 71b274fb7c7db0281a9d2a50f7e453ee6dde8352 [file] [log] [blame]
Madan Jampani98094222016-09-15 21:12:46 -07001/*
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
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
25import org.junit.Test;
26
27/**
28 * Unit tests for {@link DocumentPath}.
29 */
30public class DocumentPathTest {
31
32 @Test
33 public void testConstruction() {
Thomas Vachuskae2bd1152017-03-23 13:42:32 -070034 DocumentPath path = path("root.a.b");
Madan Jampani98094222016-09-15 21:12:46 -070035 assertEquals(path.pathElements(), Arrays.asList("root", "a", "b"));
Thomas Vachuskae2bd1152017-03-23 13:42:32 -070036 assertEquals(path("root.a"), path.parent());
Madan Jampani98094222016-09-15 21:12:46 -070037 }
38
39 @Test
40 public void testAncestry() {
Thomas Vachuskae2bd1152017-03-23 13:42:32 -070041 DocumentPath path1 = path("root.a.b");
42 DocumentPath path2 = path("root.a.d");
43 DocumentPath path3 = path("root.a.b.c");
Madan Jampani98094222016-09-15 21:12:46 -070044 DocumentPath lca = DocumentPath.leastCommonAncestor(Arrays.asList(path1, path2, path3));
Thomas Vachuskae2bd1152017-03-23 13:42:32 -070045 assertEquals(path("root.a"), lca);
Madan Jampani98094222016-09-15 21:12:46 -070046 assertTrue(path1.isAncestorOf(path3));
47 assertFalse(path1.isAncestorOf(path2));
48 assertTrue(path3.isDescendentOf(path3));
49 assertTrue(path3.isDescendentOf(path1));
50 assertFalse(path3.isDescendentOf(path2));
51 }
Thomas Vachuskae2bd1152017-03-23 13:42:32 -070052
53 private static DocumentPath path(String path) {
54 return DocumentPath.from(path.replace(".", DocumentPath.DEFAULT_SEPARATOR));
55 }
Madan Jampani98094222016-09-15 21:12:46 -070056}