Fix test failures in sal-binding-it
[controller.git] / opendaylight / md-sal / sal-binding-it / src / test / java / org / opendaylight / controller / test / sal / binding / it / DataServiceIT.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 package org.opendaylight.controller.test.sal.binding.it;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import java.util.concurrent.Future;
14 import org.junit.Test;
15 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
18 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
19 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.Lists;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.UnorderedContainer;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.unordered.container.UnorderedList;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.unordered.container.UnorderedListBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.unordered.container.UnorderedListKey;
25 import org.opendaylight.yangtools.yang.binding.DataObject;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.opendaylight.yangtools.yang.common.RpcResult;
28
29 /**
30  * covers creating, reading and deleting of an item in dataStore
31  */
32 public class DataServiceIT extends AbstractIT {
33     protected DataBrokerService consumerDataService;
34
35     /**
36      *
37      * Ignored this, because classes here are constructed from
38      * very different class loader as MD-SAL is run into,
39      * this is code is run from different classloader.
40      *
41      * @throws Exception
42      */
43     @Test
44     public void test() throws Exception {
45         BindingAwareConsumer consumer = new BindingAwareConsumer() {
46
47             @Override
48             public void onSessionInitialized(final ConsumerContext session) {
49                 consumerDataService = session.getSALService(DataBrokerService.class);
50             }
51         };
52
53         broker.registerConsumer(consumer);
54
55         assertNotNull(consumerDataService);
56
57
58         DataModificationTransaction transaction = consumerDataService.beginTransaction();
59         assertNotNull(transaction);
60
61         InstanceIdentifier<UnorderedList> node1 = createNodeRef("0");
62         DataObject node = consumerDataService.readOperationalData(node1);
63         assertNull(node);
64         UnorderedList nodeData1 = createNode("0");
65
66         transaction.putOperationalData(node1, nodeData1);
67         Future<RpcResult<TransactionStatus>> commitResult = transaction.commit();
68         assertNotNull(commitResult);
69
70         RpcResult<TransactionStatus> result = commitResult.get();
71
72         assertNotNull(result);
73         assertNotNull(result.getResult());
74         assertEquals(TransactionStatus.COMMITED, result.getResult());
75
76         UnorderedList readedData = (UnorderedList) consumerDataService.readOperationalData(node1);
77         assertNotNull(readedData);
78         assertEquals(nodeData1.getKey(), readedData.getKey());
79
80         DataModificationTransaction transaction2 = consumerDataService.beginTransaction();
81         assertNotNull(transaction2);
82
83         transaction2.removeOperationalData(node1);
84
85         Future<RpcResult<TransactionStatus>> commitResult2 = transaction2.commit();
86         assertNotNull(commitResult2);
87
88         RpcResult<TransactionStatus> result2 = commitResult2.get();
89
90         assertNotNull(result2);
91         assertNotNull(result2.getResult());
92         assertEquals(TransactionStatus.COMMITED, result2.getResult());
93
94         DataObject readedData2 = consumerDataService.readOperationalData(node1);
95         assertNull(readedData2);
96     }
97
98
99     private static InstanceIdentifier<UnorderedList> createNodeRef(final String string) {
100         UnorderedListKey key = new UnorderedListKey(string);
101         return  InstanceIdentifier.builder(Lists.class).child(UnorderedContainer.class).child(UnorderedList.class, key).build();
102     }
103
104     private static UnorderedList createNode(final String string) {
105         UnorderedListBuilder ret = new UnorderedListBuilder();
106         UnorderedListKey nodeKey = new UnorderedListKey(string);
107         ret.setKey(nodeKey);
108         ret.setName("name of " + string);
109         ret.setName("value of " + string);
110         return ret.build();
111     }
112 }