Bug 617: Remove extend files from sal-rest-connector
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / BrokerFacadeTest.java
1 /*
2  * Copyright (c) ${year} Brocade Communications 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
9 package org.opendaylight.controller.sal.restconf.impl.test;
10
11 import static org.junit.Assert.*;
12 import static org.mockito.Mockito.*;
13
14 import java.util.Collections;
15 import java.util.Map;
16 import java.util.concurrent.Future;
17
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.mockito.InOrder;
21 import org.mockito.Mock;
22 import org.mockito.MockitoAnnotations;
23 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
24 import org.opendaylight.controller.sal.core.api.Broker.ConsumerSession;
25 import org.opendaylight.controller.sal.core.api.data.DataBrokerService;
26 import org.opendaylight.controller.sal.core.api.data.DataChangeListener;
27 import org.opendaylight.controller.sal.core.api.data.DataModificationTransaction;
28 import org.opendaylight.controller.sal.core.api.mount.MountInstance;
29 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
30 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
31 import org.opendaylight.controller.sal.restconf.impl.ResponseException;
32 import org.opendaylight.controller.sal.streams.listeners.ListenerAdapter;
33 import org.opendaylight.controller.sal.streams.listeners.Notificator;
34 import org.opendaylight.yangtools.concepts.ListenerRegistration;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.opendaylight.yangtools.yang.common.RpcResult;
37 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
38 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
39
40 import com.google.common.collect.ImmutableMap;
41 import com.google.common.util.concurrent.Futures;
42
43 /**
44  * Unit tests for BrokerFacade.
45  *
46  * @author Thomas Pantelis
47  */
48 public class BrokerFacadeTest {
49
50     @Mock
51     DataBrokerService dataBroker;
52
53     @Mock
54     DataModificationTransaction mockTransaction;
55
56     @Mock
57     ConsumerSession mockConsumerSession;
58
59     @Mock
60     MountInstance mockMountInstance;
61
62     BrokerFacade brokerFacade = BrokerFacade.getInstance();
63
64     CompositeNode dataNode = TestUtils.readInputToCnSn( "/parts/ietf-interfaces_interfaces.xml",
65                                                         XmlToCompositeNodeProvider.INSTANCE );
66
67     QName qname = QName.create( "node" );
68
69     InstanceIdentifier instanceID = InstanceIdentifier.builder().node( qname ).toInstance();
70
71     @Before
72     public void setUp() throws Exception {
73         MockitoAnnotations.initMocks( this );
74
75         brokerFacade.setDataService( dataBroker );
76         brokerFacade.setContext( mockConsumerSession );
77     }
78
79     @Test
80     public void testReadConfigurationData() {
81         when( dataBroker.readConfigurationData( instanceID ) ).thenReturn( dataNode );
82
83         CompositeNode actualNode = brokerFacade.readConfigurationData( instanceID );
84
85         assertSame( "readConfigurationData", dataNode, actualNode );
86     }
87
88     @Test
89     public void testReadConfigurationDataBehindMountPoint() {
90         when( mockMountInstance.readConfigurationData( instanceID ) ).thenReturn( dataNode );
91
92         CompositeNode actualNode = brokerFacade.readConfigurationDataBehindMountPoint(
93                                                                               mockMountInstance, instanceID );
94
95         assertSame( "readConfigurationDataBehindMountPoint", dataNode, actualNode );
96     }
97
98     @Test
99     public void testReadOperationalData() {
100         when( dataBroker.readOperationalData( instanceID ) ).thenReturn( dataNode );
101
102         CompositeNode actualNode = brokerFacade.readOperationalData( instanceID );
103
104         assertSame( "readOperationalData", dataNode, actualNode );
105     }
106
107     @Test
108     public void testReadOperationalDataBehindMountPoint() {
109         when( mockMountInstance.readOperationalData( instanceID ) ).thenReturn( dataNode );
110
111         CompositeNode actualNode = brokerFacade.readOperationalDataBehindMountPoint(
112                                                                               mockMountInstance, instanceID );
113
114         assertSame( "readOperationalDataBehindMountPoint", dataNode, actualNode );
115     }
116
117     @Test(expected=ResponseException.class)
118     public void testReadOperationalDataWithNoDataBroker() {
119         brokerFacade.setDataService( null );
120
121         brokerFacade.readOperationalData( instanceID );
122     }
123
124     @SuppressWarnings("unchecked")
125     @Test
126     public void testInvokeRpc() {
127         RpcResult<CompositeNode> expResult = mock( RpcResult.class );
128         Future<RpcResult<CompositeNode>> future = Futures.immediateFuture( expResult );
129         when( mockConsumerSession.rpc( qname, dataNode ) ).thenReturn( future );
130
131         RpcResult<CompositeNode> actualResult = brokerFacade.invokeRpc( qname, dataNode );
132
133         assertSame( "invokeRpc", expResult, actualResult );
134     }
135
136     @Test(expected=ResponseException.class)
137     public void testInvokeRpcWithException() {
138         Exception mockEx = new Exception( "mock" );
139         Future<RpcResult<CompositeNode>> future = Futures.immediateFailedFuture( mockEx );
140         when( mockConsumerSession.rpc( qname, dataNode ) ).thenReturn( future );
141
142         brokerFacade.invokeRpc( qname, dataNode );
143     }
144
145     @Test(expected=ResponseException.class)
146     public void testInvokeRpcWithNoConsumerSession() {
147         brokerFacade.setContext( null );
148
149         brokerFacade.invokeRpc( qname, dataNode );
150     }
151
152     @Test
153     public void testCommitConfigurationDataPut() {
154         Future<RpcResult<TransactionStatus>> expFuture =  Futures.immediateFuture( null );
155
156         when( dataBroker.beginTransaction() ).thenReturn( mockTransaction );
157         mockTransaction.putConfigurationData( instanceID, dataNode );
158         when( mockTransaction.commit() ).thenReturn( expFuture );
159
160         Future<RpcResult<TransactionStatus>> actualFuture =
161                              brokerFacade.commitConfigurationDataPut( instanceID, dataNode );
162
163         assertSame( "invokeRpc", expFuture, actualFuture );
164
165         InOrder inOrder = inOrder( dataBroker, mockTransaction );
166         inOrder.verify( dataBroker ).beginTransaction();
167         inOrder.verify( mockTransaction ).putConfigurationData( instanceID, dataNode );
168         inOrder.verify( mockTransaction ).commit();
169     }
170
171     @Test
172     public void testCommitConfigurationDataPutBehindMountPoint() {
173         Future<RpcResult<TransactionStatus>> expFuture = Futures.immediateFuture( null );
174
175         when( mockMountInstance.beginTransaction() ).thenReturn( mockTransaction );
176         mockTransaction.putConfigurationData( instanceID, dataNode );
177         when( mockTransaction.commit() ).thenReturn( expFuture );
178
179         Future<RpcResult<TransactionStatus>> actualFuture =
180                  brokerFacade.commitConfigurationDataPutBehindMountPoint(
181                                                        mockMountInstance, instanceID, dataNode );
182
183         assertSame( "invokeRpc", expFuture, actualFuture );
184
185         InOrder inOrder = inOrder( mockMountInstance, mockTransaction );
186         inOrder.verify( mockMountInstance ).beginTransaction();
187         inOrder.verify( mockTransaction ).putConfigurationData( instanceID, dataNode );
188         inOrder.verify( mockTransaction ).commit();
189     }
190
191     @Test
192     public void testCommitConfigurationDataPost() {
193         Future<RpcResult<TransactionStatus>> expFuture = Futures.immediateFuture( null );
194
195         Map<InstanceIdentifier, CompositeNode> nodeMap =
196                 new ImmutableMap.Builder<InstanceIdentifier,CompositeNode>()
197                                                              .put( instanceID, dataNode ).build();
198
199         when( dataBroker.beginTransaction() ).thenReturn( mockTransaction );
200         mockTransaction.putConfigurationData( instanceID, dataNode );
201         when( mockTransaction.getCreatedConfigurationData() ).thenReturn( nodeMap );
202         when( mockTransaction.commit() ).thenReturn( expFuture );
203
204         Future<RpcResult<TransactionStatus>> actualFuture =
205                              brokerFacade.commitConfigurationDataPost( instanceID, dataNode );
206
207         assertSame( "commitConfigurationDataPut", expFuture, actualFuture );
208
209         InOrder inOrder = inOrder( dataBroker, mockTransaction );
210         inOrder.verify( dataBroker ).beginTransaction();
211         inOrder.verify( mockTransaction ).putConfigurationData( instanceID, dataNode );
212         inOrder.verify( mockTransaction ).commit();
213     }
214
215     @Test
216     public void testCommitConfigurationDataPostAlreadyExists() {
217         when( dataBroker.beginTransaction() ).thenReturn( mockTransaction );
218         mockTransaction.putConfigurationData( instanceID, dataNode );
219         when( mockTransaction.getCreatedConfigurationData() )
220             .thenReturn( Collections.<InstanceIdentifier,CompositeNode>emptyMap() );
221
222         Future<RpcResult<TransactionStatus>> actualFuture =
223                              brokerFacade.commitConfigurationDataPost( instanceID, dataNode );
224
225         assertNull( "Retruned non-null Future", actualFuture );
226         verify( mockTransaction, never() ).commit();
227     }
228
229     @Test
230     public void testCommitConfigurationDataPostBehindMountPoint() {
231         Future<RpcResult<TransactionStatus>> expFuture = Futures.immediateFuture( null );
232
233         Map<InstanceIdentifier, CompositeNode> nodeMap =
234                 new ImmutableMap.Builder<InstanceIdentifier,CompositeNode>()
235                                                            .put( instanceID, dataNode ).build();
236
237         when( mockMountInstance.beginTransaction() ).thenReturn( mockTransaction );
238         mockTransaction.putConfigurationData( instanceID, dataNode );
239         when( mockTransaction.getCreatedConfigurationData() ).thenReturn( nodeMap );
240         when( mockTransaction.commit() ).thenReturn( expFuture );
241
242         Future<RpcResult<TransactionStatus>> actualFuture =
243                 brokerFacade.commitConfigurationDataPostBehindMountPoint( mockMountInstance,
244                                                                           instanceID, dataNode );
245
246         assertSame( "commitConfigurationDataPostBehindMountPoint", expFuture, actualFuture );
247
248         InOrder inOrder = inOrder( mockMountInstance, mockTransaction );
249         inOrder.verify( mockMountInstance ).beginTransaction();
250         inOrder.verify( mockTransaction ).putConfigurationData( instanceID, dataNode );
251         inOrder.verify( mockTransaction ).commit();
252     }
253
254     @Test
255     public void testCommitConfigurationDataPostBehindMountPointAlreadyExists() {
256
257         when( mockMountInstance.beginTransaction() ).thenReturn( mockTransaction );
258         mockTransaction.putConfigurationData( instanceID, dataNode );
259         when( mockTransaction.getCreatedConfigurationData() )
260             .thenReturn( Collections.<InstanceIdentifier,CompositeNode>emptyMap() );
261
262         Future<RpcResult<TransactionStatus>> actualFuture =
263                 brokerFacade.commitConfigurationDataPostBehindMountPoint( mockMountInstance,
264                                                                           instanceID, dataNode );
265
266         assertNull( "Retruned non-null Future", actualFuture );
267         verify( mockTransaction, never() ).commit();
268     }
269
270     @Test
271     public void testCommitConfigurationDataDelete() {
272         Future<RpcResult<TransactionStatus>> expFuture =  Futures.immediateFuture( null );
273
274         when( dataBroker.beginTransaction() ).thenReturn( mockTransaction );
275         mockTransaction.removeConfigurationData( instanceID );
276         when( mockTransaction.commit() ).thenReturn( expFuture );
277
278         Future<RpcResult<TransactionStatus>> actualFuture =
279                              brokerFacade.commitConfigurationDataDelete( instanceID );
280
281         assertSame( "commitConfigurationDataDelete", expFuture, actualFuture );
282
283         InOrder inOrder = inOrder( dataBroker, mockTransaction );
284         inOrder.verify( dataBroker ).beginTransaction();
285         inOrder.verify( mockTransaction ).removeConfigurationData( instanceID );
286         inOrder.verify( mockTransaction ).commit();
287     }
288
289     @Test
290     public void testCommitConfigurationDataDeleteBehindMountPoint() {
291         Future<RpcResult<TransactionStatus>> expFuture =  Futures.immediateFuture( null );
292
293         when( mockMountInstance.beginTransaction() ).thenReturn( mockTransaction );
294         mockTransaction.removeConfigurationData( instanceID );
295         when( mockTransaction.commit() ).thenReturn( expFuture );
296
297         Future<RpcResult<TransactionStatus>> actualFuture =
298                              brokerFacade.commitConfigurationDataDeleteBehindMountPoint(
299                                                               mockMountInstance, instanceID );
300
301         assertSame( "commitConfigurationDataDeleteBehindMountPoint", expFuture, actualFuture );
302
303         InOrder inOrder = inOrder( mockMountInstance, mockTransaction );
304         inOrder.verify( mockMountInstance ).beginTransaction();
305         inOrder.verify( mockTransaction ).removeConfigurationData( instanceID );
306         inOrder.verify( mockTransaction ).commit();
307     }
308
309     @SuppressWarnings("unchecked")
310     @Test
311     public void testRegisterToListenDataChanges() {
312         ListenerAdapter listener = Notificator.createListener( instanceID, "stream" );
313
314         ListenerRegistration<DataChangeListener> mockRegistration = mock( ListenerRegistration.class );
315         when( dataBroker.registerDataChangeListener( instanceID, listener ) )
316             .thenReturn( mockRegistration );
317
318         brokerFacade.registerToListenDataChanges( listener );
319
320         verify( dataBroker ).registerDataChangeListener( instanceID, listener );
321
322         assertEquals( "isListening", true, listener.isListening() );
323
324         brokerFacade.registerToListenDataChanges( listener );
325         verifyNoMoreInteractions( dataBroker );
326     }
327 }

©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.