Untangle the XML/Hello message decoders
[controller.git] / opendaylight / md-sal / sal-binding-it / src / test / java / org / opendaylight / controller / test / sal / binding / it / DataServiceTest.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.*;
11
12 import java.util.concurrent.Future;
13
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
19 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
20 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
21 import org.opendaylight.controller.sal.core.api.Broker;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
28 import org.opendaylight.yangtools.yang.binding.DataObject;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.opendaylight.yangtools.yang.common.RpcResult;
31
32 import com.google.inject.Inject;
33
34 public class DataServiceTest extends AbstractTest {
35
36     protected DataBrokerService consumerDataService;
37     
38     
39     @Inject
40     Broker broker2;
41
42     @Before
43     public void setUp() throws Exception {
44     }
45
46     @Test
47     public void test() throws Exception {
48         BindingAwareConsumer consumer1 = new BindingAwareConsumer() {
49
50             @Override
51             public void onSessionInitialized(ConsumerContext session) {
52                 consumerDataService = session.getSALService(DataBrokerService.class);
53             }
54         };
55         broker.registerConsumer(consumer1, getBundleContext());
56
57         assertNotNull(consumerDataService);
58
59         
60         DataModificationTransaction transaction = consumerDataService.beginTransaction();
61         assertNotNull(transaction);
62         
63         NodeRef node1 = createNodeRef("0");
64         DataObject  node = consumerDataService.readConfigurationData(node1.getValue());
65         assertNull(node);
66         Node nodeData1 = createNode("0");
67         
68         transaction.putConfigurationData(node1.getValue(), nodeData1);
69         Future<RpcResult<TransactionStatus>> commitResult = transaction.commit();
70         assertNotNull(commitResult);
71         
72         RpcResult<TransactionStatus> result = commitResult.get();
73         
74         assertNotNull(result);
75         assertNotNull(result.getResult());
76         assertEquals(TransactionStatus.COMMITED, result.getResult());
77         
78         Node readedData = (Node) consumerDataService.readConfigurationData(node1.getValue());
79         assertNotNull(readedData);
80         assertEquals(nodeData1.getKey(), readedData.getKey());
81         
82         
83         DataModificationTransaction transaction2 = consumerDataService.beginTransaction();
84         assertNotNull(transaction);
85         
86         transaction2.removeConfigurationData(node1.getValue());
87         
88         Future<RpcResult<TransactionStatus>> commitResult2 = transaction2.commit();
89         assertNotNull(commitResult2);
90         
91         RpcResult<TransactionStatus> result2 = commitResult2.get();
92         
93         assertNotNull(result2);
94         assertNotNull(result2.getResult());
95         assertEquals(TransactionStatus.COMMITED, result2.getResult());
96     
97         DataObject readedData2 = consumerDataService.readConfigurationData(node1.getValue());
98         assertNull(readedData2);
99         
100     
101     }
102
103     
104     private static NodeRef createNodeRef(String string) {
105         NodeKey key = new NodeKey(new NodeId(string));
106         InstanceIdentifier<Node> path = InstanceIdentifier.builder().node(Nodes.class).node(Node.class, key)
107                 .toInstance();
108
109         return new NodeRef(path);
110     }
111     
112     private static Node createNode(String string) {
113         NodeBuilder ret = new NodeBuilder();
114         NodeId id = new NodeId(string);
115         ret.setKey(new NodeKey(id));
116         ret.setId(id);
117         return ret.build();
118     }
119 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.