Migrate users of Builders/ImmutableNodes
[mdsal.git] / binding / mdsal-binding-dom-codec / src / test / java / org / opendaylight / mdsal / binding / dom / codec / impl / Mdsal673Test.java
1 /*
2  * Copyright (c) 2022 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 org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertSame;
16
17 import org.junit.Test;
18 import org.opendaylight.yang.gen.v1.mdsal668.norev.Foo;
19 import org.opendaylight.yang.gen.v1.mdsal668.norev.bar.Bar;
20 import org.opendaylight.yang.gen.v1.mdsal668.norev.bar.BarBuilder;
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.YangInstanceIdentifier.NodeIdentifier;
24 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
25
26 public class Mdsal673Test extends AbstractBindingCodecTest {
27     private static final NodeIdentifier FOO = new NodeIdentifier(Foo.QNAME);
28     private static final NodeIdentifier BAR = new NodeIdentifier(Bar.QNAME);
29
30     /**
31      * Test when BAR is not initialized (its {@code null}) the {@code nonnullBar} method returns its empty instance.
32      */
33     @Test
34     public void testNonnullContainer() {
35         final var entry = codecContext.fromNormalizedNode(YangInstanceIdentifier.of(FOO),
36             ImmutableNodes.newContainerBuilder().withNodeIdentifier(FOO).build());
37         assertNotNull(entry);
38         assertEquals(InstanceIdentifier.create(Foo.class), entry.getKey());
39
40         final var obj = entry.getValue();
41         assertThat(obj, instanceOf(Foo.class));
42         final var foo = (Foo) obj;
43         assertNull(foo.getBar());
44         // We check if nonnullBar() returns empty Bar.
45         // But we don't want to rely on provided builder in codec so the objects are not same.
46         assertEquals(BarBuilder.empty(), foo.nonnullBar());
47     }
48
49     /**
50      * Test when BAR is empty container the {@code getBar} and {@code nonnullBar} returns the same BAR instance.
51      */
52     @Test
53     public void testEmptyContainer() {
54         final var entry = codecContext.fromNormalizedNode(YangInstanceIdentifier.of(FOO),
55             ImmutableNodes.newContainerBuilder()
56                 .withNodeIdentifier(FOO)
57                 .withChild(ImmutableNodes.newContainerBuilder().withNodeIdentifier(BAR).build())
58                 .build());
59         assertNotNull(entry);
60         assertEquals(InstanceIdentifier.create(Foo.class), entry.getKey());
61
62         final var obj = entry.getValue();
63         assertThat(obj, instanceOf(Foo.class));
64         final var foo = (Foo) obj;
65         final var bar = foo.getBar();
66         assertNotNull(bar);
67         assertSame(bar, foo.nonnullBar());
68     }
69
70     /**
71      * Test when BAR is not empty container the {@code getBar} and {@code nonnullBar} returns the same BAR instance.
72      */
73     @Test
74     public void testNotEmptyContainer() {
75         // FIXME: MDSAL-670: these should get translated to YangInstanceIdentifier.of(FOO)
76         final var data = ImmutableNodes.newContainerBuilder()
77             .withNodeIdentifier(FOO)
78             .withChild(ImmutableNodes.newContainerBuilder()
79                 .withNodeIdentifier(BAR)
80                 .withChild(ImmutableNodes.newSystemLeafSetBuilder()
81                     .withNodeIdentifier(BAR)
82                     .withChild(ImmutableNodes.leafSetEntry(Bar.QNAME, FOO))
83                     .build())
84                 .build())
85             .build();
86         final var entry = codecContext.fromNormalizedNode(YangInstanceIdentifier.of(FOO), data);
87         assertNotNull(entry);
88         assertEquals(InstanceIdentifier.create(Foo.class), entry.getKey());
89
90         final var obj = entry.getValue();
91         assertThat(obj, instanceOf(Foo.class));
92         final var foo = (Foo) obj;
93         final var bar = foo.getBar();
94         assertNotNull(bar);
95         assertSame(bar, foo.nonnullBar());
96     }
97 }