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