Merge "Cleanup: Remove passing around of DataPersistenceProvider"
[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.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertSame;
14 import static org.mockito.Matchers.any;
15 import static org.mockito.Matchers.eq;
16 import static org.mockito.Mockito.inOrder;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.verify;
19 import static org.mockito.Mockito.verifyNoMoreInteractions;
20 import static org.mockito.Mockito.when;
21
22 import com.google.common.base.Optional;
23 import com.google.common.util.concurrent.CheckedFuture;
24 import com.google.common.util.concurrent.Futures;
25 import java.util.concurrent.Future;
26 import org.junit.Before;
27 import org.junit.Ignore;
28 import org.junit.Test;
29 import org.mockito.InOrder;
30 import org.mockito.Mock;
31 import org.mockito.MockitoAnnotations;
32 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
33 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
34 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
35 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
36 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
37 import org.opendaylight.controller.md.sal.dom.api.DOMDataChangeListener;
38 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
39 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
40 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
41 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
42 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
43 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
44 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
45 import org.opendaylight.controller.sal.core.api.Broker.ConsumerSession;
46 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
47 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
48 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
49 import org.opendaylight.controller.sal.restconf.impl.RestconfError;
50 import org.opendaylight.controller.sal.streams.listeners.ListenerAdapter;
51 import org.opendaylight.controller.sal.streams.listeners.Notificator;
52 import org.opendaylight.yangtools.concepts.ListenerRegistration;
53 import org.opendaylight.yangtools.yang.common.QName;
54 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
55 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
56 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
57 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
58 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
59
60 /**
61  * Unit tests for BrokerFacade.
62  *
63  * @author Thomas Pantelis
64  */
65 public class BrokerFacadeTest {
66
67     @Mock
68     DOMDataBroker domDataBroker;
69
70     @Mock
71     ConsumerSession context;
72
73     @Mock
74     DOMRpcService mockRpcService;
75
76     @Mock
77     DOMMountPoint mockMountInstance;
78
79     BrokerFacade brokerFacade = BrokerFacade.getInstance();
80
81     NormalizedNode<?, ?> dummyNode = createDummyNode("test:module", "2014-01-09", "interfaces");
82     CheckedFuture<Optional<NormalizedNode<?, ?>>,ReadFailedException> dummyNodeInFuture = wrapDummyNode(dummyNode);
83
84     QName qname = TestUtils.buildQName("interfaces","test:module", "2014-01-09");
85
86     SchemaPath type = SchemaPath.create(true, qname);
87
88     YangInstanceIdentifier instanceID = YangInstanceIdentifier.builder().node(qname).build();
89
90     @Mock
91     DOMDataReadOnlyTransaction rTransaction;
92
93     @Mock
94     DOMDataWriteTransaction wTransaction;
95
96     @Mock
97     DOMDataReadWriteTransaction rwTransaction;
98
99     @Before
100     public void setUp() throws Exception {
101         MockitoAnnotations.initMocks(this);
102         // TODO it is started before every test method
103         brokerFacade.setDomDataBroker(domDataBroker);
104         brokerFacade.setRpcService(mockRpcService);
105         brokerFacade.setContext(context);
106         when(domDataBroker.newReadOnlyTransaction()).thenReturn(rTransaction);
107         when(domDataBroker.newWriteOnlyTransaction()).thenReturn(wTransaction);
108         when(domDataBroker.newReadWriteTransaction()).thenReturn(rwTransaction);
109
110         ControllerContext.getInstance().setSchemas(TestUtils.loadSchemaContext("/full-versions/test-module"));
111
112     }
113
114     private CheckedFuture<Optional<NormalizedNode<?, ?>>,ReadFailedException> wrapDummyNode(final NormalizedNode<?, ?> dummyNode) {
115         return  Futures.immediateCheckedFuture(Optional.<NormalizedNode<?, ?>> of(dummyNode));
116     }
117
118     private CheckedFuture<Boolean,ReadFailedException> wrapExistence(final Boolean exists) {
119         return  Futures.immediateCheckedFuture(exists);
120     }
121
122
123     /**
124      * Value of this node shouldn't be important for testing purposes
125      */
126     private NormalizedNode<?, ?> createDummyNode(final String namespace, final String date, final String localName) {
127         return Builders.containerBuilder()
128                 .withNodeIdentifier(new NodeIdentifier(QName.create(namespace, date, localName))).build();
129     }
130
131     @Test
132     public void testReadConfigurationData() {
133         when(rTransaction.read(any(LogicalDatastoreType.class), any(YangInstanceIdentifier.class))).thenReturn(
134                 dummyNodeInFuture);
135
136         final NormalizedNode<?, ?> actualNode = brokerFacade.readConfigurationData(instanceID);
137
138         assertSame("readConfigurationData", dummyNode, actualNode);
139     }
140
141     @Test
142     public void testReadOperationalData() {
143         when(rTransaction.read(any(LogicalDatastoreType.class), any(YangInstanceIdentifier.class))).thenReturn(
144                 dummyNodeInFuture);
145
146         final NormalizedNode<?, ?> actualNode = brokerFacade.readOperationalData(instanceID);
147
148         assertSame("readOperationalData", dummyNode, actualNode);
149     }
150
151     @Test(expected = RestconfDocumentedException.class)
152     public void testReadOperationalDataWithNoDataBroker() {
153         brokerFacade.setDomDataBroker(null);
154
155         brokerFacade.readOperationalData(instanceID);
156     }
157
158     @Test
159     public void testInvokeRpc() throws Exception {
160         final DOMRpcResult expResult = mock(DOMRpcResult.class);
161         final CheckedFuture<DOMRpcResult, DOMRpcException> future = Futures.immediateCheckedFuture(expResult);
162         when(mockRpcService.invokeRpc(type, dummyNode)).thenReturn(future);
163
164         final CheckedFuture<DOMRpcResult, DOMRpcException> actualFuture = brokerFacade.invokeRpc(type, dummyNode);
165         assertNotNull("Future is null", actualFuture);
166         final DOMRpcResult actualResult = actualFuture.get();
167         assertSame("invokeRpc", expResult, actualResult);
168     }
169
170     @Test(expected = RestconfDocumentedException.class)
171     public void testInvokeRpcWithNoConsumerSession() {
172         brokerFacade.setContext(null);
173         brokerFacade.invokeRpc(type, dummyNode);
174     }
175
176     @Ignore
177     @Test
178     public void testCommitConfigurationDataPut() {
179         @SuppressWarnings("unchecked")
180         final CheckedFuture<Void, TransactionCommitFailedException> expFuture = mock(CheckedFuture.class);
181
182         when(wTransaction.submit()).thenReturn(expFuture);
183
184         final Future<Void> actualFuture = brokerFacade.commitConfigurationDataPut(instanceID, dummyNode);
185
186         assertSame("commitConfigurationDataPut", expFuture, actualFuture);
187
188         final InOrder inOrder = inOrder(domDataBroker, wTransaction);
189         inOrder.verify(domDataBroker).newWriteOnlyTransaction();
190         inOrder.verify(wTransaction).put(LogicalDatastoreType.CONFIGURATION, instanceID, dummyNode);
191         inOrder.verify(wTransaction).submit();
192     }
193
194     @Test
195     public void testCommitConfigurationDataPost() {
196         @SuppressWarnings("unchecked")
197         final CheckedFuture<Void, TransactionCommitFailedException> expFuture = mock(CheckedFuture.class);
198
199         final NormalizedNode<?, ?> dummyNode2 = createDummyNode("dummy:namespace2", "2014-07-01", "dummy local name2");
200
201         when(rwTransaction.read(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class))).thenReturn(
202                 wrapDummyNode(dummyNode2));
203
204         when(rwTransaction.exists(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class))).thenReturn(
205             wrapExistence(true));
206
207
208         when(rwTransaction.submit()).thenReturn(expFuture);
209
210         final CheckedFuture<Void, TransactionCommitFailedException> actualFuture = brokerFacade.commitConfigurationDataPost(
211                 YangInstanceIdentifier.builder().build(), dummyNode);
212
213         assertSame("commitConfigurationDataPost", expFuture, actualFuture);
214
215         final InOrder inOrder = inOrder(domDataBroker, rwTransaction);
216         inOrder.verify(domDataBroker).newReadWriteTransaction();
217         inOrder.verify(rwTransaction).merge(LogicalDatastoreType.CONFIGURATION, instanceID, dummyNode);
218         inOrder.verify(rwTransaction).submit();
219     }
220
221     @Test(expected = RestconfDocumentedException.class)
222     public void testCommitConfigurationDataPostAlreadyExists() {
223         when(rwTransaction.read(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class))).thenReturn(
224                 dummyNodeInFuture);
225         try {
226             brokerFacade.commitConfigurationDataPost(instanceID, dummyNode);
227         } catch (final RestconfDocumentedException e) {
228             assertEquals("getErrorTag", RestconfError.ErrorTag.DATA_EXISTS, e.getErrors().get(0).getErrorTag());
229             throw e;
230         }
231     }
232
233     @Test
234     public void testCommitConfigurationDataDelete() {
235         @SuppressWarnings("unchecked")
236         final CheckedFuture<Void, TransactionCommitFailedException> expFuture = mock(CheckedFuture.class);
237
238         when(wTransaction.submit()).thenReturn(expFuture);
239
240         final CheckedFuture<Void, TransactionCommitFailedException> actualFuture = brokerFacade
241                 .commitConfigurationDataDelete(instanceID);
242
243         assertSame("commitConfigurationDataDelete", expFuture, actualFuture);
244
245         final InOrder inOrder = inOrder(domDataBroker, wTransaction);
246         inOrder.verify(domDataBroker).newWriteOnlyTransaction();
247         inOrder.verify(wTransaction).delete(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
248         inOrder.verify(wTransaction).submit();
249     }
250
251     @SuppressWarnings("unchecked")
252     @Test
253     public void testRegisterToListenDataChanges() {
254         final ListenerAdapter listener = Notificator.createListener(instanceID, "stream");
255
256         final ListenerRegistration<DOMDataChangeListener> mockRegistration = mock(ListenerRegistration.class);
257
258         when(
259                 domDataBroker.registerDataChangeListener(any(LogicalDatastoreType.class), eq(instanceID), eq(listener),
260                         eq(DataChangeScope.BASE))).thenReturn(mockRegistration);
261
262         brokerFacade.registerToListenDataChanges(LogicalDatastoreType.CONFIGURATION, DataChangeScope.BASE, listener);
263
264         verify(domDataBroker).registerDataChangeListener(LogicalDatastoreType.CONFIGURATION, instanceID, listener,
265                 DataChangeScope.BASE);
266
267         assertEquals("isListening", true, listener.isListening());
268
269         brokerFacade.registerToListenDataChanges(LogicalDatastoreType.CONFIGURATION, DataChangeScope.BASE, listener);
270         verifyNoMoreInteractions(domDataBroker);
271
272     }
273 }

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