New test utility AssertDataObjects
[mdsal.git] / binding / mdsal-binding-test-utils / src / test / java / org / opendaylight / mdsal / binding / testutils / AssertDataObjectsTest.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 java.util.Map;
13 import org.junit.Test;
14 import org.opendaylight.mdsal.binding.api.ReadTransaction;
15 import org.opendaylight.mdsal.binding.api.WriteTransaction;
16 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractDataBrokerTest;
17 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListKey;
20 import org.opendaylight.yangtools.yang.binding.DataObject;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22
23 /**
24  * Tests the {@link AssertDataObjects} utility.
25  *
26  * @author Michael Vorburger
27  */
28 public class AssertDataObjectsTest extends AbstractDataBrokerTest {
29
30     private static final String HEADER = "import static extension org.opendaylight.mdsal.binding.testutils."
31             + "XtendBuilderExtensions.operator_doubleGreaterThan\n\n";
32
33     @Test
34     public void testAssertDataObjectsWithTopLevelListKey() {
35         AssertDataObjects.assertEqualByText("new TopLevelListKey(\"test\")\n", new TopLevelListKey("test"));
36     }
37
38     @Test
39     public void testAssertDataObjectsWithEmptyTop() {
40         AssertDataObjects.assertEqualByText(HEADER + "new TopBuilder\n", ExampleYangObjects.topEmpty().getValue());
41     }
42
43     @Test
44     public void testAssertDataObjectsWithComplexTopWithKey() {
45         AssertDataObjects.assertEqualByText(HEADER + "new TopBuilder >> [\n"
46                 + "    topLevelList = #[\n"
47                 + "        new TopLevelListBuilder >> [\n"
48                 + "            name = \"foo\"\n"
49                 + "            addAugmentation(TreeComplexUsesAugment, new TreeComplexUsesAugmentBuilder >> [\n"
50                 + "                containerWithUses = new ContainerWithUsesBuilder >> [\n"
51                 + "                    leafFromGrouping = \"foo\"\n"
52                 + "                ]\n"
53                 + "            ])\n"
54                 + "        ]\n"
55                 + "    ]\n"
56                 + "]", ExpectedObjects.top());
57     }
58
59     @Test
60     public void testAssertDataObjectsWithTopLevelList() {
61         AssertDataObjects.assertEqualBeans(ExpectedObjects.topLevelList(),
62                 ExampleYangObjects.topLevelList().getValue());
63         AssertDataObjects.assertEqualByText(HEADER + "new TopLevelListBuilder >> [\n"
64                 + "    name = \"foo\"\n"
65                 + "    addAugmentation(TreeComplexUsesAugment, new TreeComplexUsesAugmentBuilder >> [\n"
66                 + "        containerWithUses = new ContainerWithUsesBuilder >> [\n"
67                 + "            leafFromGrouping = \"foo\"\n"
68                 + "        ]\n"
69                 + "    ])\n"
70                 + "]", ExampleYangObjects.topLevelList().getValue());
71     }
72
73     @Test
74     public void testAssertDataObjectsWithDataBroker() throws Exception {
75         WriteTransaction initialTx = getDataBroker().newWriteOnlyTransaction();
76         put(initialTx, OPERATIONAL, ExampleYangObjects.topEmpty());
77         put(initialTx, OPERATIONAL, ExampleYangObjects.topLevelList());
78         initialTx.submit().checkedGet();
79
80         ReadTransaction readTx = getDataBroker().newReadOnlyTransaction();
81         InstanceIdentifier<Top> id = InstanceIdentifier.create(Top.class);
82         Top actualTop = readTx.read(OPERATIONAL, id).checkedGet().get();
83
84         AssertDataObjects.assertEqualBeans(ExpectedObjects.top(), actualTop);
85
86         String expectedTopText = "import static extension org.opendaylight.mdsal.binding.testutils."
87                 + "XtendBuilderExtensions.operator_doubleGreaterThan\n\n"
88                 + "new TopBuilder >> [\n"
89                 + "    topLevelList = #[\n"
90                 + "        new TopLevelListBuilder >> [\n"
91                 + "            name = \"foo\"\n"
92                 + "            addAugmentation(TreeComplexUsesAugment, new TreeComplexUsesAugmentBuilder >> [\n"
93                 + "                containerWithUses = new ContainerWithUsesBuilder >> [\n"
94                 + "                    leafFromGrouping = \"foo\"\n"
95                 + "                ]\n"
96                 + "            ])\n"
97                 + "        ]\n"
98                 + "    ]\n"
99                 + "]";
100         AssertDataObjects.assertEqualByText(expectedTopText, actualTop);
101     }
102
103     <T extends DataObject> void put(WriteTransaction tx, LogicalDatastoreType store,
104             Map.Entry<InstanceIdentifier<T>, T> obj) {
105         tx.put(OPERATIONAL, obj.getKey(), obj.getValue());
106     }
107
108 }