Remove UriInfo from JSONRestconfService API methods
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / JSONRestconfServiceImplTest.java
1 /*
2  * Copyright (c) 2015 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 package org.opendaylight.controller.sal.restconf.impl.test;
9
10 import static org.hamcrest.CoreMatchers.containsString;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertThat;
14 import static org.junit.Assert.assertTrue;
15 import static org.mockito.Matchers.any;
16 import static org.mockito.Matchers.eq;
17 import static org.mockito.Matchers.isNull;
18 import static org.mockito.Matchers.notNull;
19 import static org.mockito.Matchers.same;
20 import static org.mockito.Mockito.doReturn;
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.reset;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
25
26 import com.google.common.base.Optional;
27 import com.google.common.io.Resources;
28 import com.google.common.util.concurrent.Futures;
29 import java.io.FileNotFoundException;
30 import java.io.IOException;
31 import java.nio.charset.StandardCharsets;
32 import java.util.List;
33 import java.util.Map;
34 import javax.ws.rs.core.Response.Status;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.mockito.ArgumentCaptor;
39 import org.mockito.Mockito;
40 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
41 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
42 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
43 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
44 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
45 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationNotAvailableException;
46 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
47 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
48 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
49 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
50 import org.opendaylight.netconf.sal.restconf.impl.JSONRestconfServiceImpl;
51 import org.opendaylight.netconf.sal.restconf.impl.PutResult;
52 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
53 import org.opendaylight.yangtools.yang.common.OperationFailedException;
54 import org.opendaylight.yangtools.yang.common.QName;
55 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
56 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
57 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
58 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
59 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
60 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
61 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
62 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
63 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
64 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
65 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
66 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
67 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
68 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
69 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
70
71 /**
72  * Unit tests for JSONRestconfServiceImpl.
73  *
74  * @author Thomas Pantelis
75  */
76 @Deprecated
77 public class JSONRestconfServiceImplTest {
78     static final String IETF_INTERFACES_NS = "urn:ietf:params:xml:ns:yang:ietf-interfaces";
79     static final String IETF_INTERFACES_VERSION = "2013-07-04";
80     static final QName INTERFACES_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "interfaces");
81     static final QName INTERFACE_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "interface");
82     static final QName NAME_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "name");
83     static final QName TYPE_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "type");
84     static final QName ENABLED_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "enabled");
85     static final QName DESC_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "description");
86
87     static final String TEST_MODULE_NS = "test:module";
88     static final String TEST_MODULE_VERSION = "2014-01-09";
89     static final QName TEST_CONT_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "cont");
90     static final QName TEST_CONT1_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "cont1");
91     static final QName TEST_LF11_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "lf11");
92     static final QName TEST_LF12_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "lf12");
93
94     static final String TOASTER_MODULE_NS = "http://netconfcentral.org/ns/toaster";
95     static final String TOASTER_MODULE_VERSION = "2009-11-20";
96     static final QName TOASTER_DONENESS_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "toasterDoneness");
97     static final QName TOASTER_TYPE_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "toasterToastType");
98     static final QName WHEAT_BREAD_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "wheat-bread");
99     static final QName MAKE_TOAST_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "make-toast");
100     static final QName CANCEL_TOAST_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "cancel-toast");
101     static final QName TEST_OUTPUT_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "testOutput");
102     static final QName TEXT_OUT_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "textOut");
103
104     private static BrokerFacade brokerFacade;
105
106     private final JSONRestconfServiceImpl service = new JSONRestconfServiceImpl();
107
108     @BeforeClass
109     public static void init() throws IOException, ReactorException {
110         ControllerContext.getInstance().setSchemas(TestUtils.loadSchemaContext("/full-versions/yangs"));
111         brokerFacade = mock(BrokerFacade.class);
112         RestconfImpl.getInstance().setBroker(brokerFacade);
113         RestconfImpl.getInstance().setControllerContext(ControllerContext.getInstance());
114     }
115
116     @Before
117     public void setup() {
118         reset(brokerFacade);
119     }
120
121     private static String loadData(final String path) throws IOException {
122         return Resources.asCharSource(JSONRestconfServiceImplTest.class.getResource(path), StandardCharsets.UTF_8).read();
123     }
124
125     @SuppressWarnings("rawtypes")
126     @Test
127     public void testPut() throws Exception {
128         final PutResult result = mock(PutResult.class);
129         when(brokerFacade.commitConfigurationDataPut(notNull(SchemaContext.class),
130                 notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class), Mockito.anyString(),
131                 Mockito.anyString())).thenReturn(result);
132         when(result.getFutureOfPutData())
133                 .thenReturn(Futures.immediateCheckedFuture(null));
134         when(result.getStatus()).thenReturn(Status.OK);
135         final String uriPath = "ietf-interfaces:interfaces/interface/eth0";
136         final String payload = loadData("/parts/ietf-interfaces_interfaces.json");
137         this.service.put(uriPath, payload);
138
139         final ArgumentCaptor<YangInstanceIdentifier> capturedPath = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
140         final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
141         verify(brokerFacade).commitConfigurationDataPut(notNull(SchemaContext.class), capturedPath.capture(),
142                 capturedNode.capture(), Mockito.anyString(), Mockito.anyString());
143
144         verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
145                 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
146
147         assertTrue("Expected MapEntryNode. Actual " + capturedNode.getValue().getClass(),
148                 capturedNode.getValue() instanceof MapEntryNode);
149         final MapEntryNode actualNode = (MapEntryNode) capturedNode.getValue();
150         assertEquals("MapEntryNode node type", INTERFACE_QNAME, actualNode.getNodeType());
151         verifyLeafNode(actualNode, NAME_QNAME, "eth0");
152         verifyLeafNode(actualNode, TYPE_QNAME, "ethernetCsmacd");
153         verifyLeafNode(actualNode, ENABLED_QNAME, Boolean.FALSE);
154         verifyLeafNode(actualNode, DESC_QNAME, "some interface");
155     }
156
157     @SuppressWarnings("rawtypes")
158     @Test
159     public void testPutBehindMountPoint() throws Exception {
160         final DOMMountPoint mockMountPoint = setupTestMountPoint();
161         final PutResult result = mock(PutResult.class);
162         when(brokerFacade.commitMountPointDataPut(notNull(DOMMountPoint.class),
163                 notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class), Mockito.anyString(),
164                 Mockito.anyString())).thenReturn(result);
165         when(result.getFutureOfPutData()).thenReturn(Futures.immediateCheckedFuture(null));
166         when(result.getStatus()).thenReturn(Status.OK);
167         final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont/cont1";
168         final String payload = loadData("/full-versions/testCont1Data.json");
169
170         this.service.put(uriPath, payload);
171
172         final ArgumentCaptor<YangInstanceIdentifier> capturedPath = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
173         final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
174         verify(brokerFacade).commitMountPointDataPut(same(mockMountPoint), capturedPath.capture(),
175                 capturedNode.capture(), Mockito.anyString(), Mockito.anyString());
176
177         verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
178
179         assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
180         final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
181         assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getNodeType());
182         verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
183         verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
184     }
185
186     @Test(expected = OperationFailedException.class)
187     public void testPutFailure() throws Throwable {
188         final PutResult result = mock(PutResult.class);
189
190         when(result.getFutureOfPutData())
191                 .thenReturn(Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("mock")));
192         when(result.getStatus()).thenReturn(Status.OK);
193         when(brokerFacade.commitConfigurationDataPut(notNull(SchemaContext.class),
194                 notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class), Mockito.anyString(),
195                 Mockito.anyString())).thenReturn(result);
196
197         final String uriPath = "ietf-interfaces:interfaces/interface/eth0";
198         final String payload = loadData("/parts/ietf-interfaces_interfaces.json");
199
200         this.service.put(uriPath, payload);
201     }
202
203     @SuppressWarnings("rawtypes")
204     @Test
205     public void testPost() throws Exception {
206         doReturn(Futures.immediateCheckedFuture(null)).when(brokerFacade).commitConfigurationDataPost(
207                 any(SchemaContext.class), any(YangInstanceIdentifier.class), any(NormalizedNode.class),
208                 Mockito.anyString(), Mockito.anyString());
209
210         final String uriPath = null;
211         final String payload = loadData("/parts/ietf-interfaces_interfaces_absolute_path.json");
212
213         this.service.post(uriPath, payload);
214
215         final ArgumentCaptor<YangInstanceIdentifier> capturedPath = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
216         final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
217         verify(brokerFacade).commitConfigurationDataPost(notNull(SchemaContext.class), capturedPath.capture(),
218                 capturedNode.capture(), Mockito.anyString(), Mockito.anyString());
219
220         verifyPath(capturedPath.getValue(), INTERFACES_QNAME);
221
222         assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
223         final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
224         assertEquals("ContainerNode node type", INTERFACES_QNAME, actualNode.getNodeType());
225
226         final Optional<DataContainerChild<?, ?>> mapChild = actualNode.getChild(new NodeIdentifier(INTERFACE_QNAME));
227         assertEquals(INTERFACE_QNAME.toString() + " present", true, mapChild.isPresent());
228         assertTrue("Expected MapNode. Actual " + mapChild.get().getClass(), mapChild.get() instanceof MapNode);
229         final MapNode mapNode = (MapNode)mapChild.get();
230
231         final NodeIdentifierWithPredicates entryNodeID = new NodeIdentifierWithPredicates(
232                 INTERFACE_QNAME, NAME_QNAME, "eth0");
233         final Optional<MapEntryNode> entryChild = mapNode.getChild(entryNodeID);
234         assertEquals(entryNodeID.toString() + " present", true, entryChild.isPresent());
235         final MapEntryNode entryNode = entryChild.get();
236         verifyLeafNode(entryNode, NAME_QNAME, "eth0");
237         verifyLeafNode(entryNode, TYPE_QNAME, "ethernetCsmacd");
238         verifyLeafNode(entryNode, ENABLED_QNAME, Boolean.FALSE);
239         verifyLeafNode(entryNode, DESC_QNAME, "some interface");
240     }
241
242     @SuppressWarnings("rawtypes")
243     @Test
244     public void testPostBehindMountPoint() throws Exception {
245         final DOMMountPoint mockMountPoint = setupTestMountPoint();
246         doReturn(Futures.immediateCheckedFuture(null)).when(brokerFacade).commitConfigurationDataPost(
247                 notNull(DOMMountPoint.class), notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class),
248                 Mockito.anyString(), Mockito.anyString());
249
250         final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont";
251         final String payload = loadData("/full-versions/testCont1Data.json");
252
253         this.service.post(uriPath, payload);
254
255         final ArgumentCaptor<YangInstanceIdentifier> capturedPath = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
256         final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
257         verify(brokerFacade).commitConfigurationDataPost(same(mockMountPoint), capturedPath.capture(),
258                 capturedNode.capture(), Mockito.anyString(), Mockito.anyString());
259
260         verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
261
262         assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
263         final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
264         assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getNodeType());
265         verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
266         verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
267     }
268
269     @Test(expected = TransactionCommitFailedException.class)
270     public void testPostFailure() throws Throwable {
271         doReturn(Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("mock"))).when(brokerFacade)
272                 .commitConfigurationDataPost(any(SchemaContext.class), any(YangInstanceIdentifier.class),
273                         any(NormalizedNode.class), Mockito.anyString(), Mockito.anyString());
274
275         final String uriPath = null;
276         final String payload = loadData("/parts/ietf-interfaces_interfaces_absolute_path.json");
277
278         try {
279             this.service.post(uriPath, payload);
280         } catch (final OperationFailedException e) {
281             assertNotNull(e.getCause());
282             throw e.getCause();
283         }
284     }
285
286     @Test
287     public void testDelete() throws Exception {
288         doReturn(Futures.immediateCheckedFuture(null)).when(brokerFacade)
289                 .commitConfigurationDataDelete(notNull(YangInstanceIdentifier.class));
290
291         final String uriPath = "ietf-interfaces:interfaces/interface/eth0";
292
293         this.service.delete(uriPath);
294
295         final ArgumentCaptor<YangInstanceIdentifier> capturedPath = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
296         verify(brokerFacade).commitConfigurationDataDelete(capturedPath.capture());
297
298         verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
299                 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
300     }
301
302     @Test(expected=OperationFailedException.class)
303     public void testDeleteFailure() throws Exception {
304         final String invalidUriPath = "ietf-interfaces:interfaces/invalid";
305
306         this.service.delete(invalidUriPath);
307     }
308
309     @Test
310     public void testGetConfig() throws Exception {
311         testGet(LogicalDatastoreType.CONFIGURATION);
312     }
313
314     @Test
315     public void testGetOperational() throws Exception {
316         testGet(LogicalDatastoreType.OPERATIONAL);
317     }
318
319     @Test
320     public void testGetWithNoData() throws OperationFailedException {
321         doReturn(null).when(brokerFacade).readConfigurationData(notNull(YangInstanceIdentifier.class),
322                 Mockito.anyString());
323         final String uriPath = "ietf-interfaces:interfaces";
324         this.service.get(uriPath, LogicalDatastoreType.CONFIGURATION);
325     }
326
327     @Test(expected=OperationFailedException.class)
328     public void testGetFailure() throws Exception {
329         final String invalidUriPath = "/ietf-interfaces:interfaces/invalid";
330         this.service.get(invalidUriPath, LogicalDatastoreType.CONFIGURATION);
331     }
332
333     @SuppressWarnings("rawtypes")
334     @Test
335     public void testInvokeRpcWithInput() throws Exception {
336         final SchemaPath path = SchemaPath.create(true, MAKE_TOAST_QNAME);
337
338         final DOMRpcResult expResult = new DefaultDOMRpcResult((NormalizedNode<?, ?>)null);
339         doReturn(Futures.immediateCheckedFuture(expResult)).when(brokerFacade).invokeRpc(eq(path),
340                 any(NormalizedNode.class));
341
342         final String uriPath = "toaster:make-toast";
343         final String input = loadData("/full-versions/make-toast-rpc-input.json");
344
345         final Optional<String> output = this.service.invokeRpc(uriPath, Optional.of(input));
346
347         assertEquals("Output present", false, output.isPresent());
348
349         final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
350         verify(brokerFacade).invokeRpc(eq(path), capturedNode.capture());
351
352         assertTrue("Expected ContainerNode. Actual " + capturedNode.getValue().getClass(),
353                 capturedNode.getValue() instanceof ContainerNode);
354         final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
355         verifyLeafNode(actualNode, TOASTER_DONENESS_QNAME, Long.valueOf(10));
356         verifyLeafNode(actualNode, TOASTER_TYPE_QNAME, WHEAT_BREAD_QNAME);
357     }
358
359     @Test
360     public void testInvokeRpcWithNoInput() throws Exception {
361         final SchemaPath path = SchemaPath.create(true, CANCEL_TOAST_QNAME);
362
363         final DOMRpcResult expResult = new DefaultDOMRpcResult((NormalizedNode<?, ?>)null);
364         doReturn(Futures.immediateCheckedFuture(expResult)).when(brokerFacade).invokeRpc(any(SchemaPath.class),
365                 any(NormalizedNode.class));
366
367         final String uriPath = "toaster:cancel-toast";
368
369         final Optional<String> output = this.service.invokeRpc(uriPath, Optional.<String>absent());
370
371         assertEquals("Output present", false, output.isPresent());
372
373         verify(brokerFacade).invokeRpc(eq(path), isNull(NormalizedNode.class));
374     }
375
376     @Test
377     public void testInvokeRpcWithOutput() throws Exception {
378         final SchemaPath path = SchemaPath.create(true, TEST_OUTPUT_QNAME);
379
380         final NormalizedNode<?, ?> outputNode = ImmutableContainerNodeBuilder.create()
381                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TEST_OUTPUT_QNAME))
382                 .withChild(ImmutableNodes.leafNode(TEXT_OUT_QNAME, "foo")).build();
383         final DOMRpcResult expResult = new DefaultDOMRpcResult(outputNode);
384         doReturn(Futures.immediateCheckedFuture(expResult)).when(brokerFacade).invokeRpc(any(SchemaPath.class),
385                 any(NormalizedNode.class));
386
387         final String uriPath = "toaster:testOutput";
388
389         final Optional<String> output = this.service.invokeRpc(uriPath, Optional.<String>absent());
390
391         assertEquals("Output present", true, output.isPresent());
392         assertNotNull("Returned null response", output.get());
393         assertThat("Missing \"textOut\"", output.get(), containsString("\"textOut\":\"foo\""));
394
395         verify(brokerFacade).invokeRpc(eq(path), isNull(NormalizedNode.class));
396     }
397
398     @Test(expected=OperationFailedException.class)
399     public void testInvokeRpcFailure() throws Exception {
400         final DOMRpcException exception = new DOMRpcImplementationNotAvailableException("testExeption");
401         doReturn(Futures.immediateFailedCheckedFuture(exception)).when(brokerFacade).invokeRpc(any(SchemaPath.class),
402                 any(NormalizedNode.class));
403
404         final String uriPath = "toaster:cancel-toast";
405
406         this.service.invokeRpc(uriPath, Optional.<String>absent());
407     }
408
409     void testGet(final LogicalDatastoreType datastoreType) throws OperationFailedException {
410         final MapEntryNode entryNode = ImmutableNodes.mapEntryBuilder(INTERFACE_QNAME, NAME_QNAME, "eth0")
411                 .withChild(ImmutableNodes.leafNode(NAME_QNAME, "eth0"))
412                 .withChild(ImmutableNodes.leafNode(TYPE_QNAME, "ethernetCsmacd"))
413                 .withChild(ImmutableNodes.leafNode(ENABLED_QNAME, Boolean.TRUE))
414                 .withChild(ImmutableNodes.leafNode(DESC_QNAME, "eth interface"))
415                 .build();
416
417         if(datastoreType == LogicalDatastoreType.CONFIGURATION) {
418             doReturn(entryNode).when(brokerFacade).readConfigurationData(notNull(YangInstanceIdentifier.class),
419                     Mockito.anyString());
420         } else {
421             doReturn(entryNode).when(brokerFacade).readOperationalData(notNull(YangInstanceIdentifier.class));
422         }
423
424         final String uriPath = "/ietf-interfaces:interfaces/interface/eth0";
425
426         final Optional<String> optionalResp = this.service.get(uriPath, datastoreType);
427         assertEquals("Response present", true, optionalResp.isPresent());
428         final String jsonResp = optionalResp.get();
429
430         assertNotNull("Returned null response", jsonResp);
431         assertThat("Missing \"name\"", jsonResp, containsString("\"name\":\"eth0\""));
432         assertThat("Missing \"type\"", jsonResp, containsString("\"type\":\"ethernetCsmacd\""));
433         assertThat("Missing \"enabled\"", jsonResp, containsString("\"enabled\":true"));
434         assertThat("Missing \"description\"", jsonResp, containsString("\"description\":\"eth interface\""));
435
436         final ArgumentCaptor<YangInstanceIdentifier> capturedPath = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
437         if (datastoreType == LogicalDatastoreType.CONFIGURATION) {
438             verify(brokerFacade).readConfigurationData(capturedPath.capture(), Mockito.anyString());
439         } else {
440             verify(brokerFacade).readOperationalData(capturedPath.capture());
441         }
442
443         verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
444                 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
445     }
446
447     DOMMountPoint setupTestMountPoint() throws FileNotFoundException, ReactorException {
448         final SchemaContext schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
449         final DOMMountPoint mockMountPoint = mock(DOMMountPoint.class);
450         doReturn(schemaContextTestModule).when(mockMountPoint).getSchemaContext();
451
452         final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
453         doReturn(Optional.of(mockMountPoint)).when(mockMountService).getMountPoint(notNull(YangInstanceIdentifier.class));
454
455         ControllerContext.getInstance().setMountService(mockMountService);
456         return mockMountPoint;
457     }
458
459     void verifyLeafNode(final DataContainerNode<?> parent, final QName leafType, final Object leafValue) {
460         final Optional<DataContainerChild<?, ?>> leafChild = parent.getChild(new NodeIdentifier(leafType));
461         assertEquals(leafType.toString() + " present", true, leafChild.isPresent());
462         assertEquals(leafType.toString() + " value", leafValue, leafChild.get().getValue());
463     }
464
465     void verifyPath(final YangInstanceIdentifier path, final Object... expArgs) {
466         final List<PathArgument> pathArgs = path.getPathArguments();
467         assertEquals("Arg count for actual path " + path, expArgs.length, pathArgs.size());
468         int i = 0;
469         for(final PathArgument actual: pathArgs) {
470             QName expNodeType;
471             if(expArgs[i] instanceof Object[]) {
472                 final Object[] listEntry = (Object[]) expArgs[i];
473                 expNodeType = (QName) listEntry[0];
474
475                 assertTrue(actual instanceof NodeIdentifierWithPredicates);
476                 final Map<QName, Object> keyValues = ((NodeIdentifierWithPredicates)actual).getKeyValues();
477                 assertEquals(String.format("Path arg %d keyValues size", i + 1), 1, keyValues.size());
478                 final QName expKey = (QName) listEntry[1];
479                 assertEquals(String.format("Path arg %d keyValue for %s", i + 1, expKey), listEntry[2],
480                         keyValues.get(expKey));
481             } else {
482                 expNodeType = (QName) expArgs[i];
483             }
484
485             assertEquals(String.format("Path arg %d node type", i + 1), expNodeType, actual.getNodeType());
486             i++;
487         }
488
489     }
490 }