HONEYCOMB-359 - Wildcarded writers
[honeycomb.git] / infra / translate-impl / src / test / java / io / fd / honeycomb / translate / impl / write / registry / WildcardedSubtreeWriterTest.java
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
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 io.fd.honeycomb.translate.impl.write.registry;
18
19 import io.fd.honeycomb.translate.write.Writer;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.mockito.MockitoAnnotations;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hc.aug.test.rev161222.AugTarget;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hc.aug.test.rev161222.FromAugmentAugment;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hc.aug.test.rev161222.FromAugmentListAugment;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hc.aug.test.rev161222.SimpleNestedAugment;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hc.aug.test.rev161222.aug.target.FromAugment;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hc.aug.test.rev161222.aug.target.from.augment.FromAugmentEntry;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hc.test.rev150105.ContainerWithChoice;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hc.test.rev150105.ContainerWithList;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hc.test.rev150105.container.with.choice.choice.c3.C3;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hc.test.rev150105.container.with.list.ListInContainer;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hc.test.rev150105.container.with.list.list.in.container.ContainerInList;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hc.test.rev150105.container.with.list.list.in.container.container.in.list.NestedList;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36
37 import static org.junit.Assert.assertFalse;
38 import static org.junit.Assert.assertTrue;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.when;
41
42 public class WildcardedSubtreeWriterTest {
43
44     private static final InstanceIdentifier<ContainerWithList> C_WITH_LIST = InstanceIdentifier.create(ContainerWithList.class);
45     private static final InstanceIdentifier<ContainerWithChoice> C_WITH_CHOICE = InstanceIdentifier.create(ContainerWithChoice.class);
46     private static final InstanceIdentifier<AugTarget> C_AUG = InstanceIdentifier.create(AugTarget.class);
47
48     private static final InstanceIdentifier<ListInContainer> L_IN_CONTAINER = C_WITH_LIST.child(ListInContainer.class);
49     private static final InstanceIdentifier<ContainerInList> C_IN_LIST = L_IN_CONTAINER.child(ContainerInList.class);
50
51     private static final InstanceIdentifier<NestedList> N_LIST = C_IN_LIST.child(NestedList.class);
52
53     private Writer subtreeContainerWithList;
54     private Writer subtreeContainerWithChoice;
55     private Writer subtreeAugTarget;
56
57
58     @Before
59     public void init() {
60         MockitoAnnotations.initMocks(this);
61         Writer<ContainerWithList> writerContainerWithList = mock(Writer.class);
62         Writer<ContainerWithChoice> writerContainerWithChoice = mock(Writer.class);
63         Writer<AugTarget> writerAugTarget = mock(Writer.class);
64         when(writerContainerWithList.getManagedDataObjectType()).thenReturn(C_WITH_LIST);
65         when(writerContainerWithChoice.getManagedDataObjectType()).thenReturn(C_WITH_CHOICE);
66         when(writerAugTarget.getManagedDataObjectType()).thenReturn(C_AUG);
67         subtreeContainerWithList = SubtreeWriter.createWildcardedForWriter(writerContainerWithList);
68         subtreeContainerWithChoice = SubtreeWriter.createWildcardedForWriter(writerContainerWithChoice);
69         subtreeAugTarget = SubtreeWriter.createWildcardedForWriter(writerAugTarget);
70     }
71
72     @Test
73     public void testParent() {
74         assertTrue(subtreeContainerWithList.canProcess(C_WITH_LIST));
75         assertFalse(subtreeContainerWithList.canProcess(C_WITH_CHOICE));
76
77         assertTrue(subtreeContainerWithChoice.canProcess(C_WITH_CHOICE));
78         assertFalse(subtreeContainerWithChoice.canProcess(C_WITH_LIST));
79
80         assertTrue(subtreeAugTarget.canProcess(C_AUG));
81         assertFalse(subtreeAugTarget.canProcess(C_WITH_LIST));
82     }
83
84     @Test
85     public void testDirectChild() {
86         assertTrue(subtreeContainerWithList.canProcess(L_IN_CONTAINER));
87         assertFalse(subtreeContainerWithList.canProcess(C_WITH_CHOICE.child(C3.class)));
88
89         assertTrue(subtreeContainerWithChoice.canProcess(C_WITH_CHOICE.child(C3.class)));
90         assertFalse(subtreeContainerWithChoice.canProcess(L_IN_CONTAINER));
91     }
92
93     @Test
94     public void testIndirectChild() {
95         assertTrue(subtreeContainerWithList.canProcess(C_IN_LIST));
96         assertTrue(subtreeContainerWithList.canProcess(N_LIST));
97     }
98
99     @Test
100     public void testAugDirectChild() {
101         assertTrue(subtreeAugTarget.canProcess(C_AUG.augmentation(FromAugmentAugment.class).child(FromAugment.class)));
102         assertFalse(subtreeContainerWithList.canProcess(C_AUG.augmentation(FromAugmentAugment.class).child(FromAugment.class)));
103     }
104
105     @Test
106     public void testAugIndirectChild() {
107         assertTrue(subtreeAugTarget.canProcess(C_AUG.augmentation(FromAugmentAugment.class)
108                 .child(FromAugment.class)
109                 .augmentation(SimpleNestedAugment.class)));
110         assertFalse(subtreeContainerWithList.canProcess(C_AUG.augmentation(FromAugmentAugment.class)
111                 .child(FromAugment.class)
112                 .augmentation(FromAugmentListAugment.class)
113                 .child(FromAugmentEntry.class)));
114     }
115 }