2 * Copyright (c) 2022 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.dom.codec.impl;
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;
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.impl.schema.Builders;
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);
31 * Test when BAR is not initialized (its {@code null}) the {@code nonnullBar} method returns its empty instance.
34 public void testNonnullContainer() {
35 final var entry = codecContext.fromNormalizedNode(YangInstanceIdentifier.of(FOO),
36 Builders.containerBuilder().withNodeIdentifier(FOO).build());
38 assertEquals(InstanceIdentifier.create(Foo.class), entry.getKey());
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());
50 * Test when BAR is empty container the {@code getBar} and {@code nonnullBar} returns the same BAR instance.
53 public void testEmptyContainer() {
54 final var entry = codecContext.fromNormalizedNode(YangInstanceIdentifier.of(FOO),
55 Builders.containerBuilder()
56 .withNodeIdentifier(FOO)
57 .withChild(Builders.containerBuilder().withNodeIdentifier(BAR).build())
60 assertEquals(InstanceIdentifier.create(Foo.class), entry.getKey());
62 final var obj = entry.getValue();
63 assertThat(obj, instanceOf(Foo.class));
64 final var foo = (Foo) obj;
65 final var bar = foo.getBar();
67 assertSame(bar, foo.nonnullBar());
71 * Test when BAR is not empty container the {@code getBar} and {@code nonnullBar} returns the same BAR instance.
74 public void testNotEmptyContainer() {
75 // FIXME: MDSAL-670: these should get translated to YangInstanceIdentifier.of(FOO)
76 final var identifier = new YangInstanceIdentifier.NodeWithValue<>(Bar.QNAME, FOO);
77 final var data = Builders.containerBuilder()
78 .withNodeIdentifier(FOO)
79 .withChild(Builders.containerBuilder()
80 .withNodeIdentifier(BAR)
81 .withChild(Builders.leafSetBuilder()
82 .withNodeIdentifier(BAR)
83 .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(identifier).withValue(FOO).build())
87 final var entry = codecContext.fromNormalizedNode(YangInstanceIdentifier.of(FOO), data);
89 assertEquals(InstanceIdentifier.create(Foo.class), entry.getKey());
91 final var obj = entry.getValue();
92 assertThat(obj, instanceOf(Foo.class));
93 final var foo = (Foo) obj;
94 final var bar = foo.getBar();
96 assertSame(bar, foo.nonnullBar());