Merge "HostTracker hosts DB key scheme implementation"
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / test / java / org / opendaylight / controller / sal / binding / test / connect / dom / BrokerIntegrationTest.java
1 package org.opendaylight.controller.sal.binding.test.connect.dom;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertNull;
6
7
8 import java.util.concurrent.Future;
9
10
11 import org.junit.Test;
12 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
13 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
14
15 import org.opendaylight.controller.sal.binding.test.AbstractDataServiceTest;
16
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.opendaylight.yangtools.yang.common.RpcResult;
26
27 public class BrokerIntegrationTest extends AbstractDataServiceTest {
28
29     @Test
30     public void simpleModifyOperation() throws Exception {
31
32         NodeRef node1 = createNodeRef("0");
33         DataObject node = baDataService.readConfigurationData(node1.getValue());
34         assertNull(node);
35         Node nodeData1 = createNode("0");
36
37         DataModificationTransaction transaction = baDataService.beginTransaction();
38         transaction.putConfigurationData(node1.getValue(), nodeData1);
39         Future<RpcResult<TransactionStatus>> commitResult = transaction.commit();
40         assertNotNull(commitResult);
41
42         RpcResult<TransactionStatus> result = commitResult.get();
43
44         assertNotNull(result);
45         assertNotNull(result.getResult());
46         assertEquals(TransactionStatus.COMMITED, result.getResult());
47
48         Node readedData = (Node) baDataService.readConfigurationData(node1.getValue());
49         assertNotNull(readedData);
50         assertEquals(nodeData1.getKey(), readedData.getKey());
51
52         NodeRef nodeFoo = createNodeRef("foo");
53         NodeRef nodeBar = createNodeRef("bar");
54         Node nodeFooData = createNode("foo");
55         Node nodeBarData = createNode("bar");
56
57         DataModificationTransaction insertMoreTr = baDataService.beginTransaction();
58         insertMoreTr.putConfigurationData(nodeFoo.getValue(), nodeFooData);
59         insertMoreTr.putConfigurationData(nodeBar.getValue(), nodeBarData);
60         RpcResult<TransactionStatus> result2 = insertMoreTr.commit().get();
61
62         assertNotNull(result2);
63         assertNotNull(result2.getResult());
64         assertEquals(TransactionStatus.COMMITED, result.getResult());
65
66         Nodes allNodes = (Nodes) baDataService.readConfigurationData(InstanceIdentifier.builder(Nodes.class)
67                 .toInstance());
68         assertNotNull(allNodes);
69         assertNotNull(allNodes.getNode());
70         assertEquals(3, allNodes.getNode().size());
71
72         /**
73          * We create transaction no 2
74          * 
75          */
76         DataModificationTransaction removalTransaction = baDataService.beginTransaction();
77         assertNotNull(transaction);
78
79         /**
80          * We remove node 1
81          * 
82          */
83         removalTransaction.removeConfigurationData(node1.getValue());
84
85         /**
86          * We commit transaction
87          */
88         Future<RpcResult<TransactionStatus>> commitResult2 = removalTransaction.commit();
89         assertNotNull(commitResult2);
90
91         RpcResult<TransactionStatus> result3 = commitResult2.get();
92
93         assertNotNull(result3);
94         assertNotNull(result3.getResult());
95         assertEquals(TransactionStatus.COMMITED, result2.getResult());
96
97         DataObject readedData2 = baDataService.readConfigurationData(node1.getValue());
98         assertNull(readedData2);
99     }
100
101     private static NodeRef createNodeRef(String string) {
102         NodeKey key = new NodeKey(new NodeId(string));
103         InstanceIdentifier<Node> path = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
104                 .toInstance();
105         return new NodeRef(path);
106     }
107
108     private static Node createNode(String string) {
109         NodeBuilder ret = new NodeBuilder();
110         ret.setId(new NodeId(string));
111         ret.setKey(new NodeKey(ret.getId()));
112         return ret.build();
113     }
114 }