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 | |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 25 | import org.junit.Rule; |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 26 | import org.junit.Test; |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 27 | import org.junit.rules.ExpectedException; |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Unit tests for {@link DocumentPath}. |
| 31 | */ |
| 32 | public class DocumentPathTest { |
| 33 | |
| 34 | @Test |
| 35 | public void testConstruction() { |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 36 | DocumentPath path = path("root.a.b"); |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 37 | assertEquals(path.pathElements(), Arrays.asList("root", "a", "b")); |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 38 | assertEquals(path("root.a"), path.parent()); |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 39 | assertEquals(path("b"), path.childPath()); |
Jordan Halterman | b0ac590 | 2017-07-30 12:31:01 -0700 | [diff] [blame] | 40 | path = DocumentPath.from("root", "a", "b"); |
| 41 | assertEquals(path.pathElements(), Arrays.asList("root", "a", "b")); |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | @Test |
| 45 | public void testAncestry() { |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 46 | DocumentPath path = path("root"); |
| 47 | assertEquals(path.childPath(), null); |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 48 | DocumentPath path1 = path("root.a.b"); |
| 49 | DocumentPath path2 = path("root.a.d"); |
| 50 | DocumentPath path3 = path("root.a.b.c"); |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 51 | DocumentPath lca = DocumentPath.leastCommonAncestor(Arrays.asList(path1, path2, path3)); |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 52 | assertEquals(path("root.a"), lca); |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 53 | 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 Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 59 | |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 60 | @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 Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 86 | private static DocumentPath path(String path) { |
| 87 | return DocumentPath.from(path.replace(".", DocumentPath.DEFAULT_SEPARATOR)); |
| 88 | } |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 89 | } |