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