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