2 * Copyright (c) 2022 PANTHEON.tech s.r.o. 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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
15 import java.util.Optional;
16 import org.junit.Test;
17 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer.NodeResult;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.yang.union.test.rev220428.IdentOne;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.yang.union.test.rev220428.IdentTwo;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.yang.union.test.rev220428.Top;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.yang.union.test.rev220428.TopBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.yang.union.test.rev220428.UnionType;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
30 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
31 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
33 public class UnionTypeWithMultipleIdentityrefsTest extends AbstractBindingCodecTest {
35 public static final QName MODULE_QNAME = QName.create("urn:opendaylight:yang:union:test",
36 "2022-04-28", "union-with-multi-identityref");
37 public static final QName TOP_QNAME = QName.create(MODULE_QNAME, "top");
38 public static final QName UNION_LEAF_QNAME = QName.create(TOP_QNAME, "test-union-leaf");
39 public static final QName IDENTITY_ONE_QNAME = QName.create(MODULE_QNAME, "ident-one");
40 public static final QName IDENTITY_TWO_QNAME = QName.create(MODULE_QNAME, "ident-two");
44 public void fromBindingToNNTest() {
45 verifyIdentityWasTranslatedToNNCorrectly(new UnionType(IdentOne.VALUE), IdentOne.QNAME);
46 verifyIdentityWasTranslatedToNNCorrectly(new UnionType(IdentTwo.VALUE), IdentTwo.QNAME);
50 public void fromNNToBindingTest() throws NoSuchFieldException {
51 verifyIdentityWasTranslatedToBindingCorrectly(IDENTITY_ONE_QNAME, new UnionType(IdentOne.VALUE));
52 verifyIdentityWasTranslatedToBindingCorrectly(IDENTITY_TWO_QNAME, new UnionType(IdentTwo.VALUE));
56 public void bindingToNNAndBackAgain() {
57 final Top topIdentOne = new TopBuilder().setTestUnionLeaf(new UnionType(IdentOne.VALUE)).build();
58 final Top topIdentOneReturned = thereAndBackAgain(InstanceIdentifier.builder(Top.class).build(), topIdentOne);
59 assertNull(topIdentOneReturned.getTestUnionLeaf().getIdentTwo());
60 assertEquals(topIdentOneReturned.getTestUnionLeaf().getIdentOne().implementedInterface(), IdentOne.class);
61 final Top topIdentTwo = new TopBuilder().setTestUnionLeaf(new UnionType(IdentTwo.VALUE)).build();
62 final Top topIdentTwoReturned = thereAndBackAgain(InstanceIdentifier.builder(Top.class).build(), topIdentTwo);
63 assertNull(topIdentTwoReturned.getTestUnionLeaf().getIdentOne());
64 assertEquals(topIdentTwoReturned.getTestUnionLeaf().getIdentTwo().implementedInterface(), IdentTwo.class);
67 private void verifyIdentityWasTranslatedToBindingCorrectly(final QName identityQname, final UnionType union) {
68 final ContainerNode top = Builders.containerBuilder()
69 .withNodeIdentifier(new NodeIdentifier(TOP_QNAME))
70 .withChild(ImmutableNodes.leafNode(NodeIdentifier.create(UNION_LEAF_QNAME), identityQname))
72 final var translated = codecContext.fromNormalizedNode(YangInstanceIdentifier.of(TOP_QNAME), top);
73 assertNotNull(translated);
74 assertNotNull(translated.getValue());
75 assertTrue(translated.getValue() instanceof Top);
76 assertEquals(new TopBuilder().setTestUnionLeaf(union).build(), translated.getValue());
79 private void verifyIdentityWasTranslatedToNNCorrectly(final UnionType chosenIdentity, final QName identityQname) {
80 // create binding instance with identity
81 final Top topContainer = new TopBuilder().setTestUnionLeaf(chosenIdentity).build();
82 // translate via codec into NN
83 final var translated = (NodeResult) codecContext.toNormalizedNode(InstanceIdentifier.builder(Top.class).build(),
85 assertNotNull(translated);
86 // verify translation worked
87 final var translatedNN = translated.node();
88 assertNotNull(translatedNN);
89 // verify the union leaf is present
90 // verify the leaf is the correct identity
91 assertEquals(Optional.of(identityQname),
92 NormalizedNodes.findNode(translatedNN, NodeIdentifier.create(UNION_LEAF_QNAME)).map(NormalizedNode::body));