Madan Jampani | 9809422 | 2016-09-15 21:12:46 -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.service; |
| 18 | |
| 19 | import static org.junit.Assert.assertEquals; |
| 20 | import static org.junit.Assert.assertFalse; |
| 21 | import static org.junit.Assert.assertTrue; |
| 22 | |
| 23 | import java.util.Arrays; |
| 24 | |
| 25 | import org.junit.Test; |
| 26 | |
| 27 | /** |
| 28 | * Unit tests for {@link DocumentPath}. |
| 29 | */ |
| 30 | public class DocumentPathTest { |
| 31 | |
| 32 | @Test |
| 33 | public void testConstruction() { |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 34 | DocumentPath path = path("root.a.b"); |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 35 | assertEquals(path.pathElements(), Arrays.asList("root", "a", "b")); |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 36 | assertEquals(path("root.a"), path.parent()); |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | @Test |
| 40 | public void testAncestry() { |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 41 | DocumentPath path1 = path("root.a.b"); |
| 42 | DocumentPath path2 = path("root.a.d"); |
| 43 | DocumentPath path3 = path("root.a.b.c"); |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 44 | DocumentPath lca = DocumentPath.leastCommonAncestor(Arrays.asList(path1, path2, path3)); |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 45 | assertEquals(path("root.a"), lca); |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 46 | 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 Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 52 | |
| 53 | private static DocumentPath path(String path) { |
| 54 | return DocumentPath.from(path.replace(".", DocumentPath.DEFAULT_SEPARATOR)); |
| 55 | } |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 56 | } |