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