2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.v3po.translate.util.read.registry;
19 import static org.hamcrest.CoreMatchers.hasItem;
20 import static org.hamcrest.CoreMatchers.hasItems;
21 import static org.hamcrest.CoreMatchers.is;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertThat;
24 import static org.junit.Assert.assertTrue;
26 import com.google.common.collect.Sets;
27 import org.junit.Test;
28 import org.opendaylight.yangtools.yang.binding.ChildOf;
29 import org.opendaylight.yangtools.yang.binding.DataObject;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 public class TypeHierarchyTest {
35 public void testHierarchy() throws Exception {
36 final TypeHierarchy typeHierarchy = TypeHierarchy.create(Sets.newHashSet(
37 DataObject3.DataObject31.DataObject311.IID,
38 DataObject3.DataObject31.IID,/* Included in previous already */
40 DataObject2.DataObject21.IID));
43 assertThat(typeHierarchy.getRoots().size(), is(3));
44 assertThat(typeHierarchy.getRoots(), hasItems(DataObject1.IID, DataObject2.IID, DataObject3.IID));
47 assertThat(typeHierarchy.getDirectChildren(DataObject1.IID).size(), is(0));
48 assertThat(typeHierarchy.getDirectChildren(DataObject2.DataObject21.IID).size(), is(0));
49 assertThat(typeHierarchy.getDirectChildren(DataObject3.DataObject31.DataObject311.IID).size(), is(0));
51 // Intermediate leaves
52 assertThat(typeHierarchy.getDirectChildren(DataObject2.IID).size(), is(1));
53 assertThat(typeHierarchy.getDirectChildren(DataObject2.IID), hasItem(DataObject2.DataObject21.IID));
54 assertEquals(typeHierarchy.getDirectChildren(DataObject2.IID), typeHierarchy.getAllChildren(DataObject2.IID));
56 assertThat(typeHierarchy.getDirectChildren(DataObject3.DataObject31.IID).size(), is(1));
57 assertThat(typeHierarchy.getDirectChildren(DataObject3.DataObject31.IID), hasItem(
58 DataObject3.DataObject31.DataObject311.IID));
59 assertEquals(typeHierarchy.getDirectChildren(DataObject3.DataObject31.IID), typeHierarchy.getAllChildren(
60 DataObject3.DataObject31.IID));
62 assertThat(typeHierarchy.getDirectChildren(DataObject3.IID).size(), is(1));
63 assertThat(typeHierarchy.getDirectChildren(DataObject3.IID), hasItem(DataObject3.DataObject31.IID));
64 assertThat(typeHierarchy.getAllChildren(DataObject3.IID).size(), is(2));
65 assertTrue(typeHierarchy.getAllChildren(DataObject3.IID).contains(DataObject3.DataObject31.IID));
66 assertTrue(typeHierarchy.getAllChildren(DataObject3.IID).contains(DataObject3.DataObject31.DataObject311.IID));
69 private abstract static class DataObject1 implements DataObject {
70 static InstanceIdentifier<DataObject1> IID = InstanceIdentifier.create(DataObject1.class);
72 private abstract static class DataObject2 implements DataObject {
73 static InstanceIdentifier<DataObject2> IID = InstanceIdentifier.create(DataObject2.class);
74 private abstract static class DataObject21 implements DataObject, ChildOf<DataObject2> {
75 static InstanceIdentifier<DataObject21> IID = DataObject2.IID.child(DataObject21.class);
78 private abstract static class DataObject3 implements DataObject {
79 static InstanceIdentifier<DataObject3> IID = InstanceIdentifier.create(DataObject3.class);
80 private abstract static class DataObject31 implements DataObject, ChildOf<DataObject3> {
81 static InstanceIdentifier<DataObject31> IID = DataObject3.IID.child(DataObject31.class);
82 private abstract static class DataObject311 implements DataObject, ChildOf<DataObject31> {
83 static InstanceIdentifier<DataObject311> IID = DataObject31.IID.child(DataObject311.class);