blob: 431f58f3163a83223a047523c4f49e6d937430c0 [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
Yuta HIGUCHI52e047f2017-09-11 16:43:21 -070019import static org.hamcrest.Matchers.equalTo;
20import static org.hamcrest.Matchers.greaterThan;
21import static org.hamcrest.Matchers.is;
22import static org.hamcrest.Matchers.lessThan;
Madan Jampani98094222016-09-15 21:12:46 -070023import static org.junit.Assert.assertEquals;
24import static org.junit.Assert.assertFalse;
Yuta HIGUCHI52e047f2017-09-11 16:43:21 -070025import static org.junit.Assert.assertThat;
Madan Jampani98094222016-09-15 21:12:46 -070026import static org.junit.Assert.assertTrue;
27
28import java.util.Arrays;
29
Sbhat353548bb22017-07-13 10:27:10 -070030import org.junit.Rule;
Madan Jampani98094222016-09-15 21:12:46 -070031import org.junit.Test;
Sbhat353548bb22017-07-13 10:27:10 -070032import org.junit.rules.ExpectedException;
Madan Jampani98094222016-09-15 21:12:46 -070033
34/**
35 * Unit tests for {@link DocumentPath}.
36 */
37public class DocumentPathTest {
38
39 @Test
40 public void testConstruction() {
Thomas Vachuskae2bd1152017-03-23 13:42:32 -070041 DocumentPath path = path("root.a.b");
Madan Jampani98094222016-09-15 21:12:46 -070042 assertEquals(path.pathElements(), Arrays.asList("root", "a", "b"));
Thomas Vachuskae2bd1152017-03-23 13:42:32 -070043 assertEquals(path("root.a"), path.parent());
Sbhat353548bb22017-07-13 10:27:10 -070044 assertEquals(path("b"), path.childPath());
Jordan Haltermanb0ac5902017-07-30 12:31:01 -070045 path = DocumentPath.from("root", "a", "b");
46 assertEquals(path.pathElements(), Arrays.asList("root", "a", "b"));
Madan Jampani98094222016-09-15 21:12:46 -070047 }
48
49 @Test
50 public void testAncestry() {
Sbhat353548bb22017-07-13 10:27:10 -070051 DocumentPath path = path("root");
52 assertEquals(path.childPath(), null);
Thomas Vachuskae2bd1152017-03-23 13:42:32 -070053 DocumentPath path1 = path("root.a.b");
54 DocumentPath path2 = path("root.a.d");
55 DocumentPath path3 = path("root.a.b.c");
Madan Jampani98094222016-09-15 21:12:46 -070056 DocumentPath lca = DocumentPath.leastCommonAncestor(Arrays.asList(path1, path2, path3));
Thomas Vachuskae2bd1152017-03-23 13:42:32 -070057 assertEquals(path("root.a"), lca);
Madan Jampani98094222016-09-15 21:12:46 -070058 assertTrue(path1.isAncestorOf(path3));
59 assertFalse(path1.isAncestorOf(path2));
60 assertTrue(path3.isDescendentOf(path3));
61 assertTrue(path3.isDescendentOf(path1));
62 assertFalse(path3.isDescendentOf(path2));
Yuta HIGUCHIb2a20d12018-02-14 10:25:55 -080063
64 assertFalse(path.isDescendentOf(null));
65 assertFalse(path.isAncestorOf(null));
Madan Jampani98094222016-09-15 21:12:46 -070066 }
Thomas Vachuskae2bd1152017-03-23 13:42:32 -070067
Sbhat353548bb22017-07-13 10:27:10 -070068 @Rule
69 public final ExpectedException exception = ExpectedException.none();
70
71 @Test
72 public void testExceptions() {
73 DocumentPath parentPath = path("root.a.b");
74 DocumentPath path2 = exceptions("nodeName", parentPath);
75 exception.expect(IllegalDocumentNameException.class);
76 DocumentPath path1 = exceptions("node|name", parentPath);
77 }
78
Yuta HIGUCHIfbd9ae92018-01-24 23:39:06 -080079 @SuppressWarnings("SelfComparison")
Sbhat353548bb22017-07-13 10:27:10 -070080 @Test
81 public void comparePaths() {
82 DocumentPath one = path("root");
83 DocumentPath four = path("root.a.b.c.d");
84 DocumentPath difFour = path("root.e.c.b.a");
Yuta HIGUCHI52e047f2017-09-11 16:43:21 -070085 assertThat(one.compareTo(four), is(lessThan(0)));
86 assertThat(four.compareTo(one), is(greaterThan(0)));
87 assertThat(difFour.compareTo(four), is(greaterThan(0)));
88 assertThat(difFour.compareTo(difFour), is(equalTo(0)));
Sbhat353548bb22017-07-13 10:27:10 -070089 }
90
91 private static DocumentPath exceptions(String nodeName, DocumentPath path) {
92 return new DocumentPath(nodeName, path);
93 }
94
Thomas Vachuskae2bd1152017-03-23 13:42:32 -070095 private static DocumentPath path(String path) {
96 return DocumentPath.from(path.replace(".", DocumentPath.DEFAULT_SEPARATOR));
97 }
Madan Jampani98094222016-09-15 21:12:46 -070098}