567de12cee823cc48ea17d6158a76f3f6dddc4b2
[ovsdb.git] / utils / mdsal-utils / src / test / java / org / opendaylight / ovsdb / utils / mdsal / utils / MdsalUtilsAsyncTest.java
1 /*
2  * Copyright © 2016, 2017 Inocybe 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.ovsdb.utils.mdsal.utils;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.fail;
14
15 import com.google.common.base.Optional;
16 import com.google.common.util.concurrent.CheckedFuture;
17 import com.google.common.util.concurrent.FutureCallback;
18 import com.google.common.util.concurrent.Futures;
19 import com.google.common.util.concurrent.MoreExecutors;
20 import java.util.Collections;
21 import java.util.concurrent.ExecutionException;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.Mockito;
26 import org.mockito.runners.MockitoJUnitRunner;
27 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
28 import org.opendaylight.controller.md.sal.binding.test.AbstractDataBrokerTest;
29 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
30 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
31 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.node.attributes.SupportingNode;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.node.attributes.SupportingNodeBuilder;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.node.attributes.SupportingNodeKey;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44
45 @RunWith(MockitoJUnitRunner.class)
46 public class MdsalUtilsAsyncTest extends AbstractDataBrokerTest {
47
48     private MdsalUtilsAsync mdsalUtilsAsync;
49     private DataBroker databroker;
50
51     private static final TopologyId TOPOLOGY_TEST = new TopologyId("test:1");
52
53     private static final NodeId NODE_ID = new NodeId("test");
54     private static final NodeKey NODE_KEY =  new NodeKey(NODE_ID);
55     private static final Node DATA = new NodeBuilder().setKey(NODE_KEY).setNodeId(NODE_ID).build();
56
57     private static final InstanceIdentifier<Node> TEST_IID = InstanceIdentifier
58             .create(NetworkTopology.class)
59             .child(Topology.class, new TopologyKey(TOPOLOGY_TEST))
60             .child(Node.class, NODE_KEY);
61
62     @Before
63     public void setUp() {
64         databroker = getDataBroker();
65         mdsalUtilsAsync = Mockito.spy(new MdsalUtilsAsync(databroker));
66     }
67
68     @Test
69     public void testDelete() {
70         final CheckedFuture<Void, TransactionCommitFailedException> fut = mdsalUtilsAsync.put(
71                 LogicalDatastoreType.CONFIGURATION, TEST_IID, DATA);
72         Futures.addCallback(fut, new FutureCallback<Void>() {
73
74             @Override
75             public void onSuccess(final Void result) {
76                 final CheckedFuture<Void, TransactionCommitFailedException> future =
77                         mdsalUtilsAsync.delete(LogicalDatastoreType.CONFIGURATION, TEST_IID);
78                 Futures.addCallback(future, new FutureCallback<Void>() {
79
80                     @Override
81                     public void onSuccess(final Void result) {
82                         assertNull(readDS());
83                     }
84
85                     @Override
86                     public void onFailure(final Throwable ex) {
87                         fail(ex.getMessage());
88                     }
89                 }, MoreExecutors.directExecutor());
90             }
91
92             @Override
93             public void onFailure(final Throwable ex) {
94                 fail(ex.getMessage());
95             }
96         }, MoreExecutors.directExecutor());
97     }
98
99     @Test
100     public void testPutWithoutCallback() {
101         final String operationDesc = "testPut";
102         final SupportingNode supportingNodeBuilder1 = new SupportingNodeBuilder().setKey(
103                 new SupportingNodeKey(new NodeId("id1"), TOPOLOGY_TEST)).build();
104         final SupportingNode supportingNodeBuilder2 = new SupportingNodeBuilder().setKey(
105                 new SupportingNodeKey(new NodeId("id2"), TOPOLOGY_TEST)).build();
106
107         final Node data1 = new NodeBuilder(DATA).setSupportingNode(
108                 Collections.singletonList(supportingNodeBuilder1)).build();
109         final Node data2 = new NodeBuilder(DATA).setSupportingNode(
110                 Collections.singletonList(supportingNodeBuilder2)).build();
111
112         mdsalUtilsAsync.put(LogicalDatastoreType.CONFIGURATION, TEST_IID, data1, operationDesc);
113         assertEquals(data1, readDS());
114
115         final CheckedFuture<Void, TransactionCommitFailedException> future = mdsalUtilsAsync.put(
116                 LogicalDatastoreType.CONFIGURATION, TEST_IID, data2);
117         Futures.addCallback(future, new FutureCallback<Void>() {
118
119             @Override
120             public void onSuccess(final Void result) {
121                 assertEquals(1, readDS().getSupportingNode().size());
122             }
123
124             @Override
125             public void onFailure(final Throwable ex) {
126                 fail(ex.getMessage());
127             }
128         }, MoreExecutors.directExecutor());
129     }
130
131     @Test
132     public void testMerge() {
133         final String operationDesc = "testMerge";
134         final SupportingNode supportingNodeBuilder1 = new SupportingNodeBuilder().setKey(
135                 new SupportingNodeKey(new NodeId("id1"), TOPOLOGY_TEST)).build();
136         final SupportingNode supportingNodeBuilder2 = new SupportingNodeBuilder().setKey(
137                 new SupportingNodeKey(new NodeId("id2"), TOPOLOGY_TEST)).build();
138
139         final Node data1 = new NodeBuilder(DATA).setSupportingNode(
140                 Collections.singletonList(supportingNodeBuilder1)).build();
141         final Node data2 = new NodeBuilder(DATA).setSupportingNode(
142                 Collections.singletonList(supportingNodeBuilder2)).build();
143
144         mdsalUtilsAsync.merge(LogicalDatastoreType.CONFIGURATION, TEST_IID, data1, operationDesc, true);
145         assertEquals(data1, readDS());
146
147         final CheckedFuture<Void, TransactionCommitFailedException> future =
148                 mdsalUtilsAsync.merge(LogicalDatastoreType.CONFIGURATION, TEST_IID, data2, true);
149         Futures.addCallback(future, new FutureCallback<Void>() {
150
151             @Override
152             public void onSuccess(final Void result) {
153                 assertEquals(2, readDS().getSupportingNode().size());
154             }
155
156             @Override
157             public void onFailure(final Throwable ex) {
158                 fail(ex.getMessage());
159             }
160         }, MoreExecutors.directExecutor());
161     }
162
163     @Test
164     public void testRead() {
165         final CheckedFuture<Void, TransactionCommitFailedException> fut =
166                 mdsalUtilsAsync.put(LogicalDatastoreType.CONFIGURATION, TEST_IID, DATA);
167
168         Futures.addCallback(fut, new FutureCallback<Void>() {
169             @Override
170             public void onSuccess(final Void result) {
171                 final CheckedFuture<Optional<Node>, ReadFailedException> future =
172                         mdsalUtilsAsync.read(LogicalDatastoreType.CONFIGURATION, TEST_IID);
173                 Optional<Node> optNode;
174                 try {
175                     optNode = future.get();
176                     if (optNode.isPresent()) {
177                         assertEquals(DATA, optNode.get());
178                     } else {
179                         fail("Couldn't read node");
180                     }
181                 } catch (InterruptedException | ExecutionException e) {
182                     fail(e.getMessage());
183                 }
184             }
185
186             @Override
187             public void onFailure(final Throwable ex) {
188                 fail(ex.getMessage());
189             }
190         }, MoreExecutors.directExecutor());
191     }
192
193     private Node readDS() {
194         try {
195             final Optional<Node> result = databroker.newReadOnlyTransaction().read(
196                     LogicalDatastoreType.CONFIGURATION, TEST_IID).get();
197             if (result.isPresent()) {
198                 return result.get();
199             }
200         } catch (InterruptedException | ExecutionException e) {
201             fail(e.getMessage());
202         }
203         return null;
204     }
205 }