Specialize relative leafref types during instantiation
[mdsal.git] / binding / mdsal-binding-dom-codec / src / test / java / org / opendaylight / mdsal / binding / dom / codec / impl / SpecializingLeafrefTest.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.mdsal.binding.dom.codec.impl;
9
10 import static junit.framework.TestCase.assertTrue;
11 import static org.junit.Assert.assertEquals;
12
13 import com.google.common.collect.ImmutableList;
14 import java.util.List;
15 import java.util.Map;
16 import org.junit.Test;
17 import org.opendaylight.yang.gen.v1.mdsal426.norev.BarCont;
18 import org.opendaylight.yang.gen.v1.mdsal426.norev.BarContBuilder;
19 import org.opendaylight.yang.gen.v1.mdsal426.norev.BooleanCont;
20 import org.opendaylight.yang.gen.v1.mdsal426.norev.BooleanContBuilder;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24
25 public class SpecializingLeafrefTest extends AbstractBindingCodecTest {
26     private static final InstanceIdentifier<BooleanCont> BOOLEAN_CONT_II = InstanceIdentifier
27             .builder(BooleanCont.class).build();
28
29     private static final InstanceIdentifier<BarCont> BAR_CONT_II = InstanceIdentifier
30             .builder(BarCont.class).build();
31
32     @Test
33     public void specifiedBooleanLeafTest() {
34         final BooleanCont booleanCont  = new BooleanContBuilder().setIsFoo(true).build();
35
36         final Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> res = codecContext
37                 .toNormalizedNode(BOOLEAN_CONT_II, booleanCont);
38
39         final BooleanCont booleanContBinding = (BooleanCont)codecContext
40                 .fromNormalizedNode(res.getKey(), res.getValue()).getValue();
41
42         assertTrue(booleanContBinding.isIsFoo());
43     }
44
45     @Test
46     public void specifiedCommonLeafTest() {
47         final BarCont barCont  = new BarContBuilder().setLeaf2("foo").build();
48
49         final Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> res = codecContext
50                 .toNormalizedNode(BAR_CONT_II, barCont);
51
52         final BarCont booleanContBinding = (BarCont)codecContext
53                 .fromNormalizedNode(res.getKey(), res.getValue()).getValue();
54
55         assertEquals(booleanContBinding.getLeaf2(), "foo");
56     }
57
58     @Test
59     public void specifiedLeafListTest() {
60         final List<String> testList = ImmutableList.of("test");
61         final BarCont barCont  = new BarContBuilder().setLeafList1(testList).build();
62
63         final Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> res = codecContext
64                 .toNormalizedNode(BAR_CONT_II, barCont);
65
66         final BarCont barContAfterConverting = (BarCont)codecContext
67                 .fromNormalizedNode(res.getKey(), res.getValue()).getValue();
68
69         assertEquals(barContAfterConverting.getLeafList1(), testList);
70     }
71 }