New test utility AssertDataObjects
[mdsal.git] / binding / mdsal-binding-test-utils / src / test / java / org / opendaylight / mdsal / binding / testutils / AugmentableExtensionTest.java
1 /*
2  * Copyright (c) 2016 Red Hat, Inc. 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.testutils;
9
10 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.OPERATIONAL;
11
12 import ch.vorburger.xtendbeans.AssertBeans;
13 import java.util.Map;
14 import org.junit.Test;
15 import org.opendaylight.mdsal.binding.api.ReadTransaction;
16 import org.opendaylight.mdsal.binding.api.WriteTransaction;
17 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractDataBrokerTest;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelList;
21 import org.opendaylight.yangtools.yang.binding.Augmentation;
22 import org.opendaylight.yangtools.yang.binding.DataObject;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24
25 /**
26  * Test for {@link AugmentableExtension}.
27  *
28  * @author Michael Vorburger
29  */
30 public class AugmentableExtensionTest extends AbstractDataBrokerTest {
31
32     private final AugmentableExtension augmentableExtension = new AugmentableExtension();
33
34     @Test
35     public void testAugmentableExtensionOnYangObjectByBuilder() {
36         TopLevelList topLevelList = ExampleYangObjects.topLevelList().getValue();
37         Map<Class<? extends Augmentation<?>>, Augmentation<?>> augmentations = augmentableExtension
38                 .getAugmentations(topLevelList);
39         AssertBeans.assertEqualByText("#{\n"
40                 + "    org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test"
41                 +        ".augment.rev140709.TreeComplexUsesAugment -> (new TreeComplexUsesAugmentBuilder => [\n"
42                 + "        containerWithUses = (new ContainerWithUsesBuilder => [\n"
43                 + "            leafFromGrouping = \"foo\"\n"
44                 + "        ]).build()\n"
45                 + "    ]).build()\n"
46                 + "}", augmentations);
47     }
48
49     @Test
50     public void testAugmentableExtensionWithDataBroker() throws Exception {
51         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
52         put(writeTx, OPERATIONAL, ExampleYangObjects.topLevelList());
53         writeTx.submit().checkedGet();
54
55         ReadTransaction readTx = getDataBroker().newReadOnlyTransaction();
56         InstanceIdentifier<Top> id = InstanceIdentifier.create(Top.class);
57         Top actualTop = readTx.read(OPERATIONAL, id).checkedGet().get();
58         AssertBeans.assertEqualByText("#{\n}", augmentableExtension.getAugmentations(actualTop));
59
60         TopLevelList topLevelList = actualTop.getTopLevelList().get(0);
61         AssertDataObjects.assertEqualByText("#{\n"
62                 + "    TreeComplexUsesAugment -> new TreeComplexUsesAugmentBuilder >> [\n"
63                 + "        containerWithUses = new ContainerWithUsesBuilder >> [\n"
64                 + "            leafFromGrouping = \"foo\"\n"
65                 + "        ]\n"
66                 + "    ]\n"
67                 + "}", augmentableExtension.getAugmentations(topLevelList));
68     }
69
70     <T extends DataObject> void put(WriteTransaction tx, LogicalDatastoreType store,
71             Map.Entry<InstanceIdentifier<T>, T> obj) {
72         tx.put(OPERATIONAL, obj.getKey(), obj.getValue());
73     }
74
75 }