153d9d27203200cc66d46e3b1546df93106ed5af
[controller.git] / opendaylight / md-sal / sal-binding-dom-it / src / test / java / org / opendaylight / controller / sal / binding / test / bugfix / WriteParentReadChildTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, 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
9 package org.opendaylight.controller.sal.binding.test.bugfix;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14
15 import com.google.common.base.Optional;
16 import com.google.common.collect.ImmutableList;
17 import java.util.concurrent.TimeUnit;
18 import org.junit.Test;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.controller.sal.binding.test.AbstractDataServiceTest;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.TllComplexAugment;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.aug.grouping.List1;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.aug.grouping.List1Builder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.aug.grouping.List1Key;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.aug.grouping.list1.List11;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.aug.grouping.list1.List11Builder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.aug.grouping.list1.List11Key;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.Top;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelList;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelListKey;
33 import org.opendaylight.yangtools.yang.binding.DataObject;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35
36 public class WriteParentReadChildTest extends AbstractDataServiceTest {
37
38     private static final int LIST11_ID = 1234;
39     private static final String LIST1_NAME = "bar";
40     private static final String TLL_NAME = "foo";
41
42     private static final TopLevelListKey TLL_KEY = new TopLevelListKey(TLL_NAME);
43     private static final List11Key LIST11_KEY = new List11Key(LIST11_ID);
44     private static final List1Key LIST1_KEY = new List1Key(LIST1_NAME);
45
46     private static final InstanceIdentifier<TopLevelList> TLL_INSTANCE_ID_BA = InstanceIdentifier.builder(Top.class) //
47             .child(TopLevelList.class, TLL_KEY).build();
48
49     private static final InstanceIdentifier<List1> LIST1_INSTANCE_ID_BA = //
50             TLL_INSTANCE_ID_BA.builder() //
51             .augmentation(TllComplexAugment.class).child(List1.class, LIST1_KEY).build();
52
53     private static final InstanceIdentifier<? extends DataObject> LIST11_INSTANCE_ID_BA = //
54             LIST1_INSTANCE_ID_BA.child(List11.class, LIST11_KEY);
55
56     /**
57      * The scenario tests writing parent node, which also contains child items
58      * and then reading child directly, by specifying path to the child.
59      * Expected behaviour is child is returned.
60      */
61     @Test
62     public void writeParentReadChild() throws Exception {
63
64         DataBroker dataBroker = testContext.getDataBroker();
65         final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
66
67         List11 list11 = new List11Builder() //
68                 .setKey(LIST11_KEY) //
69                 .setAttrStr("primary")
70                 .build();
71
72         List1 list1 = new List1Builder().setKey(LIST1_KEY).setList11(ImmutableList.of(list11)).build();
73
74         transaction.put(LogicalDatastoreType.OPERATIONAL, LIST1_INSTANCE_ID_BA, list1, true);
75         transaction.submit().get(5, TimeUnit.SECONDS);
76
77         Optional<List1> readList1 = dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL,
78                 LIST1_INSTANCE_ID_BA).get(1000, TimeUnit.MILLISECONDS);
79         assertTrue(readList1.isPresent());
80
81         Optional<? extends DataObject> readList11 = dataBroker.newReadOnlyTransaction().read(
82                 LogicalDatastoreType.OPERATIONAL, LIST11_INSTANCE_ID_BA).get(5, TimeUnit.SECONDS);
83         assertNotNull("Readed flow should not be null.",readList11);
84         assertTrue(readList11.isPresent());
85         assertEquals(list11, readList11.get());
86     }
87 }