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