Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 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 | |
Yuta HIGUCHI | 52e047f | 2017-09-11 16:43:21 -0700 | [diff] [blame] | 19 | import static org.hamcrest.Matchers.equalTo; |
| 20 | import static org.hamcrest.Matchers.greaterThan; |
| 21 | import static org.hamcrest.Matchers.is; |
| 22 | import static org.hamcrest.Matchers.lessThan; |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 23 | import static org.junit.Assert.assertEquals; |
| 24 | import static org.junit.Assert.assertFalse; |
Yuta HIGUCHI | 52e047f | 2017-09-11 16:43:21 -0700 | [diff] [blame] | 25 | import static org.junit.Assert.assertThat; |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 26 | import static org.junit.Assert.assertTrue; |
| 27 | |
| 28 | import java.util.Arrays; |
| 29 | |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 30 | import org.junit.Rule; |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 31 | import org.junit.Test; |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 32 | import org.junit.rules.ExpectedException; |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Unit tests for {@link DocumentPath}. |
| 36 | */ |
| 37 | public class DocumentPathTest { |
| 38 | |
| 39 | @Test |
| 40 | public void testConstruction() { |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 41 | DocumentPath path = path("root.a.b"); |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 42 | assertEquals(path.pathElements(), Arrays.asList("root", "a", "b")); |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 43 | assertEquals(path("root.a"), path.parent()); |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 44 | assertEquals(path("b"), path.childPath()); |
Jordan Halterman | b0ac590 | 2017-07-30 12:31:01 -0700 | [diff] [blame] | 45 | path = DocumentPath.from("root", "a", "b"); |
| 46 | assertEquals(path.pathElements(), Arrays.asList("root", "a", "b")); |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | @Test |
| 50 | public void testAncestry() { |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 51 | DocumentPath path = path("root"); |
| 52 | assertEquals(path.childPath(), null); |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 53 | DocumentPath path1 = path("root.a.b"); |
| 54 | DocumentPath path2 = path("root.a.d"); |
| 55 | DocumentPath path3 = path("root.a.b.c"); |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 56 | DocumentPath lca = DocumentPath.leastCommonAncestor(Arrays.asList(path1, path2, path3)); |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 57 | assertEquals(path("root.a"), lca); |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 58 | 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 HIGUCHI | b2a20d1 | 2018-02-14 10:25:55 -0800 | [diff] [blame] | 63 | |
| 64 | assertFalse(path.isDescendentOf(null)); |
| 65 | assertFalse(path.isAncestorOf(null)); |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 66 | } |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 67 | |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 68 | @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 HIGUCHI | fbd9ae9 | 2018-01-24 23:39:06 -0800 | [diff] [blame] | 79 | @SuppressWarnings("SelfComparison") |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 80 | @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 HIGUCHI | 52e047f | 2017-09-11 16:43:21 -0700 | [diff] [blame] | 85 | 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))); |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | private static DocumentPath exceptions(String nodeName, DocumentPath path) { |
| 92 | return new DocumentPath(nodeName, path); |
| 93 | } |
| 94 | |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 95 | private static DocumentPath path(String path) { |
| 96 | return DocumentPath.from(path.replace(".", DocumentPath.DEFAULT_SEPARATOR)); |
| 97 | } |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 98 | } |