Migrate users of Builders/ImmutableNodes
[mdsal.git] / binding / mdsal-binding-dom-codec / src / test / java / org / opendaylight / mdsal / binding / dom / codec / impl / LeafrefSerializeDeserializeTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies 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 org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12
13 import org.junit.Test;
14 import org.opendaylight.yang.gen.v1.bug8449.rev170516.Cont;
15 import org.opendaylight.yang.gen.v1.bug8449.rev170516.Cont.Ref;
16 import org.opendaylight.yang.gen.v1.bug8449.rev170516.ContBuilder;
17 import org.opendaylight.yang.gen.v1.bug8449.rev170516.ContInt32;
18 import org.opendaylight.yang.gen.v1.bug8449.rev170516.ContInt32.RefUnionInt32;
19 import org.opendaylight.yang.gen.v1.bug8449.rev170516.ContInt32Builder;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 import org.opendaylight.yangtools.yang.common.Uint32;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23
24 public class LeafrefSerializeDeserializeTest extends AbstractBindingCodecTest {
25
26     @Test
27     public void listReferenceTest() {
28         final YangInstanceIdentifier contYII = YangInstanceIdentifier.builder().node(Cont.QNAME).build();
29         final InstanceIdentifier<?> fromYangInstanceIdentifier = codecContext.fromYangInstanceIdentifier(contYII);
30         assertNotNull(fromYangInstanceIdentifier);
31
32         final InstanceIdentifier<Cont> BA_II_CONT = InstanceIdentifier.builder(Cont.class).build();
33         final Ref refVal = new Ref("myvalue");
34         final Cont data = new ContBuilder().setRef(refVal).build();
35         final var normalizedNode = codecContext.toNormalizedDataObject(BA_II_CONT, data);
36         assertNotNull(normalizedNode);
37
38         final var fromNormalizedNode = codecContext.fromNormalizedNode(contYII, normalizedNode.node());
39         assertNotNull(fromNormalizedNode);
40         final Cont value = (Cont) fromNormalizedNode.getValue();
41         assertEquals(refVal, value.getRef());
42     }
43
44     @Test
45     public void uint32LeafrefTest() {
46         final YangInstanceIdentifier contYII = YangInstanceIdentifier.builder().node(ContInt32.QNAME).build();
47         final InstanceIdentifier<?> fromYangInstanceIdentifier = codecContext.fromYangInstanceIdentifier(contYII);
48         assertNotNull(fromYangInstanceIdentifier);
49
50         final InstanceIdentifier<ContInt32> BA_II_CONT = InstanceIdentifier.builder(ContInt32.class).build();
51         final RefUnionInt32 refVal = new RefUnionInt32(Uint32.valueOf(5));
52         final ContInt32 data = new ContInt32Builder().setRefUnionInt32(refVal).build();
53         final var normalizedNode = codecContext.toNormalizedDataObject(BA_II_CONT, data);
54         assertNotNull(normalizedNode);
55
56         final var fromNormalizedNode = codecContext.fromNormalizedNode(contYII, normalizedNode.node());
57         assertNotNull(fromNormalizedNode);
58         final ContInt32 value = (ContInt32) fromNormalizedNode.getValue();
59         assertEquals(refVal, value.getRefUnionInt32());
60     }
61 }
62