BUG 2412 - restconf @POST invokeRpc with nopayload method
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / RestGetOperationTest.java
1 /*
2  * Copyright (c) 2014 Cisco 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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
15 import static org.junit.Assert.fail;
16 import static org.mockito.Matchers.any;
17 import static org.mockito.Matchers.eq;
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.when;
20 import com.google.common.base.Optional;
21 import com.google.common.collect.Lists;
22 import com.google.common.collect.Maps;
23 import java.io.FileNotFoundException;
24 import java.io.UnsupportedEncodingException;
25 import java.net.URI;
26 import java.net.URISyntaxException;
27 import java.text.ParseException;
28 import java.text.SimpleDateFormat;
29 import java.util.ArrayList;
30 import java.util.Date;
31 import java.util.HashSet;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Set;
35 import java.util.regex.Matcher;
36 import java.util.regex.Pattern;
37 import javax.ws.rs.core.Application;
38 import javax.ws.rs.core.MediaType;
39 import javax.ws.rs.core.MultivaluedHashMap;
40 import javax.ws.rs.core.MultivaluedMap;
41 import javax.ws.rs.core.Response;
42 import javax.ws.rs.core.UriInfo;
43 import org.glassfish.jersey.server.ResourceConfig;
44 import org.glassfish.jersey.test.JerseyTest;
45 import org.junit.BeforeClass;
46 import org.junit.Ignore;
47 import org.junit.Test;
48 import org.mockito.invocation.InvocationOnMock;
49 import org.mockito.stubbing.Answer;
50 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
51 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
52 import org.opendaylight.controller.sal.rest.impl.JsonNormalizedNodeBodyReader;
53 import org.opendaylight.controller.sal.rest.impl.NormalizedNodeJsonBodyWriter;
54 import org.opendaylight.controller.sal.rest.impl.NormalizedNodeXmlBodyWriter;
55 import org.opendaylight.controller.sal.rest.impl.RestconfApplication;
56 import org.opendaylight.controller.sal.rest.impl.RestconfDocumentedExceptionMapper;
57 import org.opendaylight.controller.sal.rest.impl.XmlNormalizedNodeBodyReader;
58 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
59 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
60 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
61 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
62 import org.opendaylight.yangtools.yang.common.QName;
63 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
64 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
65 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
66 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
67 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
68 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
69 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
70 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
71 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
72 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
73 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
74 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
75 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
76 import org.opendaylight.yangtools.yang.model.api.Module;
77 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
78 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
79 import org.w3c.dom.Document;
80 import org.w3c.dom.Element;
81 import org.w3c.dom.NodeList;
82
83 public class RestGetOperationTest extends JerseyTest {
84
85     static class NodeData {
86         Object key;
87         Object data; // List for a CompositeNode, value Object for a SimpleNode
88
89         NodeData(final Object key, final Object data) {
90             this.key = key;
91             this.data = data;
92         }
93     }
94
95     private static BrokerFacade brokerFacade;
96     private static RestconfImpl restconfImpl;
97     private static SchemaContext schemaContextYangsIetf;
98     private static SchemaContext schemaContextTestModule;
99     private static NormalizedNode answerFromGet;
100
101     private static SchemaContext schemaContextModules;
102     private static SchemaContext schemaContextBehindMountPoint;
103
104     private static final String RESTCONF_NS = "urn:ietf:params:xml:ns:yang:ietf-restconf";
105
106     @BeforeClass
107     public static void init() throws FileNotFoundException, ParseException {
108         schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
109         schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
110         brokerFacade = mock(BrokerFacade.class);
111         restconfImpl = RestconfImpl.getInstance();
112         restconfImpl.setBroker(brokerFacade);
113         answerFromGet = TestUtils.prepareNormalizedNodeWithIetfInterfacesInterfacesData();
114
115         schemaContextModules = TestUtils.loadSchemaContext("/modules");
116         schemaContextBehindMountPoint = TestUtils.loadSchemaContext("/modules/modules-behind-mount-point");
117     }
118
119     @Override
120     protected Application configure() {
121         /* enable/disable Jersey logs to console */
122         // enable(TestProperties.LOG_TRAFFIC);
123         // enable(TestProperties.DUMP_ENTITY);
124         // enable(TestProperties.RECORD_LOG_LEVEL);
125         // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
126         ResourceConfig resourceConfig = new ResourceConfig();
127         resourceConfig = resourceConfig.registerInstances(restconfImpl, new NormalizedNodeJsonBodyWriter(),
128                 new NormalizedNodeXmlBodyWriter(), new XmlNormalizedNodeBodyReader(), new JsonNormalizedNodeBodyReader());
129         resourceConfig.registerClasses(RestconfDocumentedExceptionMapper.class);
130         resourceConfig.registerClasses(new RestconfApplication().getClasses());
131         return resourceConfig;
132     }
133
134     private void setControllerContext(final SchemaContext schemaContext) {
135         final ControllerContext controllerContext = ControllerContext.getInstance();
136         controllerContext.setSchemas(schemaContext);
137         restconfImpl.setControllerContext(controllerContext);
138     }
139
140     /**
141      * Tests of status codes for "/operational/{identifier}".
142      */
143     @Test
144     public void getOperationalStatusCodes() throws UnsupportedEncodingException {
145         setControllerContext(schemaContextYangsIetf);
146         mockReadOperationalDataMethod();
147         String uri = "/operational/ietf-interfaces:interfaces/interface/eth0";
148         assertEquals(200, get(uri, MediaType.APPLICATION_XML));
149
150         uri = "/operational/wrong-module:interfaces/interface/eth0";
151         assertEquals(400, get(uri, MediaType.APPLICATION_XML));
152     }
153
154     /**
155      * Tests of status codes for "/config/{identifier}".
156      */
157     @Test
158     public void getConfigStatusCodes() throws UnsupportedEncodingException {
159         setControllerContext(schemaContextYangsIetf);
160         mockReadConfigurationDataMethod();
161         String uri = "/config/ietf-interfaces:interfaces/interface/eth0";
162         assertEquals(200, get(uri, MediaType.APPLICATION_XML));
163
164         uri = "/config/wrong-module:interfaces/interface/eth0";
165         assertEquals(400, get(uri, MediaType.APPLICATION_XML));
166     }
167
168     /**
169      * MountPoint test. URI represents mount point.
170      */
171     @Test
172     public void getDataWithUrlMountPoint() throws UnsupportedEncodingException, URISyntaxException, ParseException {
173         when(brokerFacade.readConfigurationData(any(DOMMountPoint.class), any(YangInstanceIdentifier.class))).thenReturn(
174                 prepareCnDataForMountPointTest(false));
175         final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
176         when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
177         final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
178         when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
179
180         ControllerContext.getInstance().setMountService(mockMountService);
181
182         String uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont/cont1";
183         assertEquals(200, get(uri, MediaType.APPLICATION_XML));
184
185         uri = "/config/ietf-interfaces:interfaces/yang-ext:mount/test-module:cont/cont1";
186         assertEquals(200, get(uri, MediaType.APPLICATION_XML));
187     }
188
189     /**
190      * MountPoint test. URI represents mount point.
191      *
192      * Slashes in URI behind mount point. lst1 element with key GigabitEthernet0%2F0%2F0%2F0 (GigabitEthernet0/0/0/0) is
193      * requested via GET HTTP operation. It is tested whether %2F character is replaced with simple / in
194      * InstanceIdentifier parameter in method
195      * {@link BrokerFacade#readConfigurationDataBehindMountPoint(MountInstance, YangInstanceIdentifier)} which is called in
196      * method {@link RestconfImpl#readConfigurationData}
197      *
198      * @throws ParseException
199      */
200     @Test
201     public void getDataWithSlashesBehindMountPoint() throws UnsupportedEncodingException, URISyntaxException,
202             ParseException {
203         final YangInstanceIdentifier awaitedInstanceIdentifier = prepareInstanceIdentifierForList();
204         when(brokerFacade.readConfigurationData(any(DOMMountPoint.class), eq(awaitedInstanceIdentifier))).thenReturn(
205                 prepareCnDataForSlashesBehindMountPointTest());
206         final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
207         when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
208         final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
209         when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
210
211         ControllerContext.getInstance().setMountService(mockMountService);
212
213         final String uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont/lst1/GigabitEthernet0%2F0%2F0%2F0";
214         assertEquals(200, get(uri, MediaType.APPLICATION_XML));
215     }
216
217     private YangInstanceIdentifier prepareInstanceIdentifierForList() throws URISyntaxException, ParseException {
218         final List<PathArgument> parameters = new ArrayList<>();
219
220         final Date revision = new SimpleDateFormat("yyyy-MM-dd").parse("2014-01-09");
221         final URI uri = new URI("test:module");
222         final QName qNameCont = QName.create(uri, revision, "cont");
223         final QName qNameList = QName.create(uri, revision, "lst1");
224         final QName qNameKeyList = QName.create(uri, revision, "lf11");
225
226         parameters.add(new YangInstanceIdentifier.NodeIdentifier(qNameCont));
227         parameters.add(new YangInstanceIdentifier.NodeIdentifier(qNameList));
228         parameters.add(new YangInstanceIdentifier.NodeIdentifierWithPredicates(qNameList, qNameKeyList,
229                 "GigabitEthernet0/0/0/0"));
230         return YangInstanceIdentifier.create(parameters);
231     }
232
233     @Test
234     public void getDataMountPointIntoHighestElement() throws UnsupportedEncodingException, URISyntaxException,
235             ParseException {
236         when(brokerFacade.readConfigurationData(any(DOMMountPoint.class), any(YangInstanceIdentifier.class))).thenReturn(
237                 prepareCnDataForMountPointTest(true));
238         final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
239         when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
240         final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
241         when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
242
243         ControllerContext.getInstance().setMountService(mockMountService);
244
245         final String uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont";
246         assertEquals(200, get(uri, MediaType.APPLICATION_XML));
247     }
248
249     // /modules
250     @Test
251     public void getModulesTest() throws UnsupportedEncodingException, FileNotFoundException {
252         final ControllerContext controllerContext = ControllerContext.getInstance();
253         controllerContext.setGlobalSchema(schemaContextModules);
254         restconfImpl.setControllerContext(controllerContext);
255
256         final String uri = "/modules";
257
258         Response response = target(uri).request("application/yang.api+json").get();
259         validateModulesResponseJson(response);
260
261         response = target(uri).request("application/yang.api+xml").get();
262         validateModulesResponseXml(response,schemaContextModules);
263     }
264
265     // /streams/
266     @Test
267     @Ignore // FIXME : find why it is fail by in gerrit build
268     public void getStreamsTest() throws UnsupportedEncodingException, FileNotFoundException {
269         setControllerContext(schemaContextModules);
270
271         final String uri = "/streams";
272
273         Response response = target(uri).request("application/yang.api+json").get();
274         final String responseBody = response.readEntity(String.class);
275         assertEquals(200, response.getStatus());
276         assertNotNull(responseBody);
277         assertTrue(responseBody.contains("streams"));
278
279         response = target(uri).request("application/yang.api+xml").get();
280         assertEquals(200, response.getStatus());
281         final Document responseXmlBody = response.readEntity(Document.class);
282         assertNotNull(responseXmlBody);
283         final Element rootNode = responseXmlBody.getDocumentElement();
284
285         assertEquals("streams", rootNode.getLocalName());
286         assertEquals(RESTCONF_NS, rootNode.getNamespaceURI());
287     }
288
289     // /modules/module
290     @Test
291     public void getModuleTest() throws FileNotFoundException, UnsupportedEncodingException {
292         setControllerContext(schemaContextModules);
293
294         final String uri = "/modules/module/module2/2014-01-02";
295
296         Response response = target(uri).request("application/yang.api+xml").get();
297         assertEquals(200, response.getStatus());
298         final Document responseXml = response.readEntity(Document.class);
299
300         final QName qname = assertedModuleXmlToModuleQName(responseXml.getDocumentElement());
301         assertNotNull(qname);
302
303         assertEquals("module2", qname.getLocalName());
304         assertEquals("module:2", qname.getNamespace().toString());
305         assertEquals("2014-01-02", qname.getFormattedRevision());
306
307         response = target(uri).request("application/yang.api+json").get();
308         assertEquals(200, response.getStatus());
309         final String responseBody = response.readEntity(String.class);
310         assertTrue("Module2 in json wasn't found", prepareJsonRegex("module2", "2014-01-02", "module:2", responseBody)
311                 .find());
312         final String[] split = responseBody.split("\"module\"");
313         assertEquals("\"module\" element is returned more then once", 2, split.length);
314
315     }
316
317     // /operations
318     @Test
319     @Ignore // FIXME restconf-netconf yang schema has to be updated for operations container
320     public void getOperationsTest() throws FileNotFoundException, UnsupportedEncodingException {
321         setControllerContext(schemaContextModules);
322
323         final String uri = "/operations";
324
325         Response response = target(uri).request("application/yang.api+xml").get();
326         assertEquals(200, response.getStatus());
327         final Document responseDoc = response.readEntity(Document.class);
328         validateOperationsResponseXml(responseDoc, schemaContextModules);
329
330         response = target(uri).request("application/yang.api+json").get();
331         assertEquals(200, response.getStatus());
332         final String responseBody = response.readEntity(String.class);
333         assertTrue("Json response for /operations dummy-rpc1-module1 is incorrect",
334                 validateOperationsResponseJson(responseBody, "dummy-rpc1-module1", "module1").find());
335         assertTrue("Json response for /operations dummy-rpc2-module1 is incorrect",
336                 validateOperationsResponseJson(responseBody, "dummy-rpc2-module1", "module1").find());
337         assertTrue("Json response for /operations dummy-rpc1-module2 is incorrect",
338                 validateOperationsResponseJson(responseBody, "dummy-rpc1-module2", "module2").find());
339         assertTrue("Json response for /operations dummy-rpc2-module2 is incorrect",
340                 validateOperationsResponseJson(responseBody, "dummy-rpc2-module2", "module2").find());
341
342     }
343
344     private void validateOperationsResponseXml(final Document responseDoc, final SchemaContext schemaContext) {
345         final Element operationsElem = responseDoc.getDocumentElement();
346         assertEquals(RESTCONF_NS, operationsElem.getNamespaceURI());
347         assertEquals("operations", operationsElem.getLocalName());
348
349
350         final HashSet<QName> foundOperations = new HashSet<>();
351
352         final NodeList operationsList = operationsElem.getChildNodes();
353         for(int i = 0;i < operationsList.getLength();i++) {
354             final org.w3c.dom.Node operation = operationsList.item(i);
355
356             final String namespace = operation.getNamespaceURI();
357             final String name = operation.getLocalName();
358             final QName opQName = QName.create(URI.create(namespace), null, name);
359             foundOperations.add(opQName);
360         }
361
362         for(final RpcDefinition schemaOp : schemaContext.getOperations()) {
363             assertTrue(foundOperations.contains(schemaOp.getQName().withoutRevision()));
364         }
365
366     }
367
368     // /operations/pathToMountPoint/yang-ext:mount
369     @Test
370     @Ignore // FIXME fix the way to provide operations overview functionality asap
371     public void getOperationsBehindMountPointTest() throws FileNotFoundException, UnsupportedEncodingException {
372         setControllerContext(schemaContextModules);
373
374         final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
375         when(mountInstance.getSchemaContext()).thenReturn(schemaContextBehindMountPoint);
376         final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
377         when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
378
379         ControllerContext.getInstance().setMountService(mockMountService);
380
381         final String uri = "/operations/ietf-interfaces:interfaces/interface/0/yang-ext:mount/";
382
383         Response response = target(uri).request("application/yang.api+xml").get();
384         assertEquals(200, response.getStatus());
385
386         final Document responseDoc = response.readEntity(Document.class);
387         validateOperationsResponseXml(responseDoc, schemaContextBehindMountPoint);
388
389         response = target(uri).request("application/yang.api+json").get();
390         assertEquals(200, response.getStatus());
391         final String responseBody = response.readEntity(String.class);
392         assertTrue("Json response for /operations/mount_point rpc-behind-module1 is incorrect",
393                 validateOperationsResponseJson(responseBody, "rpc-behind-module1", "module1-behind-mount-point").find());
394         assertTrue("Json response for /operations/mount_point rpc-behind-module2 is incorrect",
395                 validateOperationsResponseJson(responseBody, "rpc-behind-module2", "module2-behind-mount-point").find());
396
397     }
398
399     private Matcher validateOperationsResponseJson(final String searchIn, final String rpcName, final String moduleName) {
400         final StringBuilder regex = new StringBuilder();
401         regex.append("^");
402
403         regex.append(".*\\{");
404         regex.append(".*\"");
405
406         // operations prefix optional
407         regex.append("(");
408         regex.append("ietf-restconf:");
409         regex.append("|)");
410         // :operations prefix optional
411
412         regex.append("operations\"");
413         regex.append(".*:");
414         regex.append(".*\\{");
415
416         regex.append(".*\"" + moduleName);
417         regex.append(":");
418         regex.append(rpcName + "\"");
419         regex.append(".*\\[");
420         regex.append(".*null");
421         regex.append(".*\\]");
422
423         regex.append(".*\\}");
424         regex.append(".*\\}");
425
426         regex.append(".*");
427         regex.append("$");
428         final Pattern ptrn = Pattern.compile(regex.toString(), Pattern.DOTALL);
429         return ptrn.matcher(searchIn);
430
431     }
432
433     private Matcher validateOperationsResponseXml(final String searchIn, final String rpcName, final String namespace) {
434         final StringBuilder regex = new StringBuilder();
435
436         regex.append("^");
437
438         regex.append(".*<operations");
439         regex.append(".*xmlns=\"urn:ietf:params:xml:ns:yang:ietf-restconf\"");
440         regex.append(".*>");
441
442         regex.append(".*<");
443         regex.append(".*" + rpcName);
444         regex.append(".*" + namespace);
445         regex.append(".*/");
446         regex.append(".*>");
447
448         regex.append(".*</operations.*");
449         regex.append(".*>");
450
451         regex.append(".*");
452         regex.append("$");
453         final Pattern ptrn = Pattern.compile(regex.toString(), Pattern.DOTALL);
454         return ptrn.matcher(searchIn);
455     }
456
457     // /restconf/modules/pathToMountPoint/yang-ext:mount
458     @Test
459     public void getModulesBehindMountPoint() throws FileNotFoundException, UnsupportedEncodingException {
460         setControllerContext(schemaContextModules);
461
462         final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
463         when(mountInstance.getSchemaContext()).thenReturn(schemaContextBehindMountPoint);
464         final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
465         when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
466
467         ControllerContext.getInstance().setMountService(mockMountService);
468
469         final String uri = "/modules/ietf-interfaces:interfaces/interface/0/yang-ext:mount/";
470
471         Response response = target(uri).request("application/yang.api+json").get();
472         assertEquals(200, response.getStatus());
473         final String responseBody = response.readEntity(String.class);
474
475         assertTrue(
476                 "module1-behind-mount-point in json wasn't found",
477                 prepareJsonRegex("module1-behind-mount-point", "2014-02-03", "module:1:behind:mount:point",
478                         responseBody).find());
479         assertTrue(
480                 "module2-behind-mount-point in json wasn't found",
481                 prepareJsonRegex("module2-behind-mount-point", "2014-02-04", "module:2:behind:mount:point",
482                         responseBody).find());
483
484         response = target(uri).request("application/yang.api+xml").get();
485         assertEquals(200, response.getStatus());
486         validateModulesResponseXml(response, schemaContextBehindMountPoint);
487
488     }
489
490     // /restconf/modules/module/pathToMountPoint/yang-ext:mount/moduleName/revision
491     @Test
492     public void getModuleBehindMountPoint() throws FileNotFoundException, UnsupportedEncodingException {
493         setControllerContext(schemaContextModules);
494
495         final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
496         when(mountInstance.getSchemaContext()).thenReturn(schemaContextBehindMountPoint);
497         final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
498         when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
499
500         ControllerContext.getInstance().setMountService(mockMountService);
501
502         final String uri = "/modules/module/ietf-interfaces:interfaces/interface/0/yang-ext:mount/module1-behind-mount-point/2014-02-03";
503
504         Response response = target(uri).request("application/yang.api+json").get();
505         assertEquals(200, response.getStatus());
506         final String responseBody = response.readEntity(String.class);
507
508         assertTrue(
509                 "module1-behind-mount-point in json wasn't found",
510                 prepareJsonRegex("module1-behind-mount-point", "2014-02-03", "module:1:behind:mount:point",
511                         responseBody).find());
512         final String[] split = responseBody.split("\"module\"");
513         assertEquals("\"module\" element is returned more then once", 2, split.length);
514
515         response = target(uri).request("application/yang.api+xml").get();
516         assertEquals(200, response.getStatus());
517         final Document responseXml = response.readEntity(Document.class);
518
519         final QName module = assertedModuleXmlToModuleQName(responseXml.getDocumentElement());
520
521         assertEquals("module1-behind-mount-point", module.getLocalName());
522         assertEquals("2014-02-03", module.getFormattedRevision());
523         assertEquals("module:1:behind:mount:point", module.getNamespace().toString());
524
525
526     }
527
528     private void validateModulesResponseXml(final Response response, final SchemaContext schemaContext) {
529         assertEquals(200, response.getStatus());
530         final Document responseBody = response.readEntity(Document.class);
531         final NodeList moduleNodes = responseBody.getDocumentElement().getElementsByTagNameNS(RESTCONF_NS, "module");
532
533         assertTrue(moduleNodes.getLength() > 0);
534
535         final HashSet<QName> foundModules = new HashSet<>();
536
537         for(int i=0;i < moduleNodes.getLength();i++) {
538             final org.w3c.dom.Node module = moduleNodes.item(i);
539
540             final QName name = assertedModuleXmlToModuleQName(module);
541             foundModules.add(name);
542         }
543
544         assertAllModules(foundModules,schemaContext);
545     }
546
547     private void assertAllModules(final Set<QName> foundModules, final SchemaContext schemaContext) {
548         for(final Module module : schemaContext.getModules()) {
549             final QName current = QName.create(module.getQNameModule(),module.getName());
550             assertTrue("Module not found in response.",foundModules.contains(current));
551         }
552
553     }
554
555     private QName assertedModuleXmlToModuleQName(final org.w3c.dom.Node module) {
556         assertEquals("module", module.getLocalName());
557         assertEquals(RESTCONF_NS, module.getNamespaceURI());
558         String revision = null;
559         String namespace = null;
560         String name = null;
561
562
563         final NodeList childNodes = module.getChildNodes();
564
565         for(int i =0;i < childNodes.getLength(); i++) {
566             final org.w3c.dom.Node child = childNodes.item(i);
567             assertEquals(RESTCONF_NS, child.getNamespaceURI());
568
569             switch(child.getLocalName()) {
570                 case "name":
571                     assertNull("Name element appeared multiple times",name);
572                     name = child.getTextContent().trim();
573                     break;
574                 case "revision":
575                     assertNull("Revision element appeared multiple times",revision);
576                     revision = child.getTextContent().trim();
577                     break;
578
579                 case "namespace":
580                     assertNull("Namespace element appeared multiple times",namespace);
581                     namespace = child.getTextContent().trim();
582                     break;
583             }
584         }
585
586         assertNotNull("Revision was not part of xml",revision);
587         assertNotNull("Module namespace was not part of xml",namespace);
588         assertNotNull("Module identiffier was not part of xml",name);
589
590
591         // TODO Auto-generated method stub
592
593         return QName.create(namespace,revision,name);
594     }
595
596     private void validateModulesResponseJson(final Response response) {
597         assertEquals(200, response.getStatus());
598         final String responseBody = response.readEntity(String.class);
599
600         assertTrue("Module1 in json wasn't found", prepareJsonRegex("module1", "2014-01-01", "module:1", responseBody)
601                 .find());
602         assertTrue("Module2 in json wasn't found", prepareJsonRegex("module2", "2014-01-02", "module:2", responseBody)
603                 .find());
604         assertTrue("Module3 in json wasn't found", prepareJsonRegex("module3", "2014-01-03", "module:3", responseBody)
605                 .find());
606     }
607
608     private Matcher prepareJsonRegex(final String module, final String revision, final String namespace,
609             final String searchIn) {
610         final StringBuilder regex = new StringBuilder();
611         regex.append("^");
612
613         regex.append(".*\\{");
614         regex.append(".*\"name\"");
615         regex.append(".*:");
616         regex.append(".*\"" + module + "\",");
617
618         regex.append(".*\"revision\"");
619         regex.append(".*:");
620         regex.append(".*\"" + revision + "\",");
621
622         regex.append(".*\"namespace\"");
623         regex.append(".*:");
624         regex.append(".*\"" + namespace + "\"");
625
626         regex.append(".*\\}");
627
628         regex.append(".*");
629         regex.append("$");
630         final Pattern ptrn = Pattern.compile(regex.toString(), Pattern.DOTALL);
631         return ptrn.matcher(searchIn);
632
633     }
634
635
636     private void prepareMockForModulesTest(final ControllerContext mockedControllerContext)
637             throws FileNotFoundException {
638         final SchemaContext schemaContext = TestUtils.loadSchemaContext("/modules");
639         mockedControllerContext.setGlobalSchema(schemaContext);
640         // when(mockedControllerContext.getGlobalSchema()).thenReturn(schemaContext);
641     }
642
643     private int get(final String uri, final String mediaType) {
644         return target(uri).request(mediaType).get().getStatus();
645     }
646
647     /**
648     container cont {
649         container cont1 {
650             leaf lf11 {
651                 type string;
652             }
653     */
654     private NormalizedNode prepareCnDataForMountPointTest(final boolean wrapToCont) throws URISyntaxException, ParseException {
655         final String testModuleDate = "2014-01-09";
656         final ContainerNode contChild = Builders
657                 .containerBuilder()
658                 .withNodeIdentifier(TestUtils.getNodeIdentifier("cont1", "test:module", testModuleDate))
659                 .withChild(
660                         Builders.leafBuilder()
661                                 .withNodeIdentifier(TestUtils.getNodeIdentifier("lf11", "test:module", testModuleDate))
662                                 .withValue("lf11 value").build()).build();
663
664         if (wrapToCont) {
665             return Builders.containerBuilder()
666                     .withNodeIdentifier(TestUtils.getNodeIdentifier("cont", "test:module", testModuleDate))
667                     .withChild(contChild).build();
668         }
669         return contChild;
670
671     }
672
673     private void mockReadOperationalDataMethod() {
674         when(brokerFacade.readOperationalData(any(YangInstanceIdentifier.class))).thenReturn(answerFromGet);
675     }
676
677     private void mockReadConfigurationDataMethod() {
678         when(brokerFacade.readConfigurationData(any(YangInstanceIdentifier.class))).thenReturn(answerFromGet);
679     }
680
681     private NormalizedNode prepareCnDataForSlashesBehindMountPointTest() throws ParseException {
682         return ImmutableMapEntryNodeBuilder
683                 .create()
684                 .withNodeIdentifier(
685                         TestUtils.getNodeIdentifierPredicate("lst1", "test:module", "2014-01-09", "lf11",
686                                 "GigabitEthernet0/0/0/0"))
687                 .withChild(
688                         ImmutableLeafNodeBuilder.create()
689                                 .withNodeIdentifier(TestUtils.getNodeIdentifier("lf11", "test:module", "2014-01-09"))
690                                 .withValue("GigabitEthernet0/0/0/0").build()).build();
691
692     }
693
694     /**
695      * If includeWhiteChars URI parameter is set to false then no white characters can be included in returned output
696      *
697      * @throws UnsupportedEncodingException
698      */
699     @Test
700     public void getDataWithUriIncludeWhiteCharsParameterTest() throws UnsupportedEncodingException {
701         getDataWithUriIncludeWhiteCharsParameter("config");
702         getDataWithUriIncludeWhiteCharsParameter("operational");
703     }
704
705     private void getDataWithUriIncludeWhiteCharsParameter(final String target) throws UnsupportedEncodingException {
706         mockReadConfigurationDataMethod();
707         mockReadOperationalDataMethod();
708         final String uri = "/" + target + "/ietf-interfaces:interfaces/interface/eth0";
709         Response response = target(uri).queryParam("prettyPrint", "false").request("application/xml").get();
710         final String xmlData = response.readEntity(String.class);
711
712         Pattern pattern = Pattern.compile(".*(>\\s+|\\s+<).*", Pattern.DOTALL);
713         Matcher matcher = pattern.matcher(xmlData);
714         // XML element can't surrounded with white character (e.g ">    " or
715         // "    <")
716         assertFalse(matcher.matches());
717
718         response = target(uri).queryParam("prettyPrint", "false").request("application/json").get();
719         final String jsonData = response.readEntity(String.class);
720         pattern = Pattern.compile(".*(\\}\\s+|\\s+\\{|\\]\\s+|\\s+\\[|\\s+:|:\\s+).*", Pattern.DOTALL);
721         matcher = pattern.matcher(jsonData);
722         // JSON element can't surrounded with white character (e.g "} ", " {",
723         // "] ", " [", " :" or ": ")
724         assertFalse(matcher.matches());
725     }
726
727     @Test
728     @Ignore
729     public void getDataWithUriDepthParameterTest() throws UnsupportedEncodingException {
730         setControllerContext(schemaContextModules);
731
732         final CompositeNode depth1Cont = toCompositeNode(toCompositeNodeData(
733                 toNestedQName("depth1-cont"),
734                 toCompositeNodeData(
735                         toNestedQName("depth2-cont1"),
736                         toCompositeNodeData(
737                                 toNestedQName("depth3-cont1"),
738                                 toCompositeNodeData(toNestedQName("depth4-cont1"),
739                                         toSimpleNodeData(toNestedQName("depth5-leaf1"), "depth5-leaf1-value")),
740                                 toSimpleNodeData(toNestedQName("depth4-leaf1"), "depth4-leaf1-value")),
741                         toSimpleNodeData(toNestedQName("depth3-leaf1"), "depth3-leaf1-value")),
742                 toCompositeNodeData(
743                         toNestedQName("depth2-cont2"),
744                         toCompositeNodeData(
745                                 toNestedQName("depth3-cont2"),
746                                 toCompositeNodeData(toNestedQName("depth4-cont2"),
747                                         toSimpleNodeData(toNestedQName("depth5-leaf2"), "depth5-leaf2-value")),
748                                 toSimpleNodeData(toNestedQName("depth4-leaf2"), "depth4-leaf2-value")),
749                         toSimpleNodeData(toNestedQName("depth3-leaf2"), "depth3-leaf2-value")),
750                 toSimpleNodeData(toNestedQName("depth2-leaf1"), "depth2-leaf1-value")));
751
752         final Module module = TestUtils.findModule(schemaContextModules.getModules(), "nested-module");
753         assertNotNull(module);
754
755         final DataSchemaNode dataSchemaNode = TestUtils.resolveDataSchemaNode("depth1-cont", module);
756         assertNotNull(dataSchemaNode);
757
758         when(brokerFacade.readConfigurationData(any(YangInstanceIdentifier.class))).thenReturn(
759                 TestUtils.compositeNodeToDatastoreNormalizedNode(depth1Cont, dataSchemaNode));
760
761         // Test config with depth 1
762
763         Response response = target("/config/nested-module:depth1-cont").queryParam("depth", "1")
764                 .request("application/xml").get();
765
766         verifyXMLResponse(response, expectEmptyContainer("depth1-cont"));
767
768         // Test config with depth 2
769
770         response = target("/config/nested-module:depth1-cont").queryParam("depth", "2").request("application/xml")
771                 .get();
772
773         // String
774         // xml="<depth1-cont><depth2-cont1/><depth2-cont2/><depth2-leaf1>depth2-leaf1-value</depth2-leaf1></depth1-cont>";
775         // Response mr=mock(Response.class);
776         // when(mr.getEntity()).thenReturn( new
777         // java.io.StringBufferInputStream(xml) );
778
779         verifyXMLResponse(
780                 response,
781                 expectContainer("depth1-cont", expectEmptyContainer("depth2-cont1"),
782                         expectEmptyContainer("depth2-cont2"), expectLeaf("depth2-leaf1", "depth2-leaf1-value")));
783
784         // Test config with depth 3
785
786         response = target("/config/nested-module:depth1-cont").queryParam("depth", "3").request("application/xml")
787                 .get();
788
789         verifyXMLResponse(
790                 response,
791                 expectContainer(
792                         "depth1-cont",
793                         expectContainer("depth2-cont1", expectEmptyContainer("depth3-cont1"),
794                                 expectLeaf("depth3-leaf1", "depth3-leaf1-value")),
795                         expectContainer("depth2-cont2", expectEmptyContainer("depth3-cont2"),
796                                 expectLeaf("depth3-leaf2", "depth3-leaf2-value")),
797                         expectLeaf("depth2-leaf1", "depth2-leaf1-value")));
798
799         // Test config with depth 4
800
801         response = target("/config/nested-module:depth1-cont").queryParam("depth", "4").request("application/xml")
802                 .get();
803
804         verifyXMLResponse(
805                 response,
806                 expectContainer(
807                         "depth1-cont",
808                         expectContainer(
809                                 "depth2-cont1",
810                                 expectContainer("depth3-cont1", expectEmptyContainer("depth4-cont1"),
811                                         expectLeaf("depth4-leaf1", "depth4-leaf1-value")),
812                                 expectLeaf("depth3-leaf1", "depth3-leaf1-value")),
813                         expectContainer(
814                                 "depth2-cont2",
815                                 expectContainer("depth3-cont2", expectEmptyContainer("depth4-cont2"),
816                                         expectLeaf("depth4-leaf2", "depth4-leaf2-value")),
817                                 expectLeaf("depth3-leaf2", "depth3-leaf2-value")),
818                         expectLeaf("depth2-leaf1", "depth2-leaf1-value")));
819
820         // Test config with depth 5
821
822         response = target("/config/nested-module:depth1-cont").queryParam("depth", "5").request("application/xml")
823                 .get();
824
825         verifyXMLResponse(
826                 response,
827                 expectContainer(
828                         "depth1-cont",
829                         expectContainer(
830                                 "depth2-cont1",
831                                 expectContainer(
832                                         "depth3-cont1",
833                                         expectContainer("depth4-cont1",
834                                                 expectLeaf("depth5-leaf1", "depth5-leaf1-value")),
835                                         expectLeaf("depth4-leaf1", "depth4-leaf1-value")),
836                                 expectLeaf("depth3-leaf1", "depth3-leaf1-value")),
837                         expectContainer(
838                                 "depth2-cont2",
839                                 expectContainer(
840                                         "depth3-cont2",
841                                         expectContainer("depth4-cont2",
842                                                 expectLeaf("depth5-leaf2", "depth5-leaf2-value")),
843                                         expectLeaf("depth4-leaf2", "depth4-leaf2-value")),
844                                 expectLeaf("depth3-leaf2", "depth3-leaf2-value")),
845                         expectLeaf("depth2-leaf1", "depth2-leaf1-value")));
846
847         // Test config with depth unbounded
848
849         response = target("/config/nested-module:depth1-cont").queryParam("depth", "unbounded")
850                 .request("application/xml").get();
851
852         verifyXMLResponse(
853                 response,
854                 expectContainer(
855                         "depth1-cont",
856                         expectContainer(
857                                 "depth2-cont1",
858                                 expectContainer(
859                                         "depth3-cont1",
860                                         expectContainer("depth4-cont1",
861                                                 expectLeaf("depth5-leaf1", "depth5-leaf1-value")),
862                                         expectLeaf("depth4-leaf1", "depth4-leaf1-value")),
863                                 expectLeaf("depth3-leaf1", "depth3-leaf1-value")),
864                         expectContainer(
865                                 "depth2-cont2",
866                                 expectContainer(
867                                         "depth3-cont2",
868                                         expectContainer("depth4-cont2",
869                                                 expectLeaf("depth5-leaf2", "depth5-leaf2-value")),
870                                         expectLeaf("depth4-leaf2", "depth4-leaf2-value")),
871                                 expectLeaf("depth3-leaf2", "depth3-leaf2-value")),
872                         expectLeaf("depth2-leaf1", "depth2-leaf1-value")));
873
874         // Test operational
875
876         final CompositeNode depth2Cont1 = toCompositeNode(toCompositeNodeData(
877                 toNestedQName("depth2-cont1"),
878                 toCompositeNodeData(
879                         toNestedQName("depth3-cont1"),
880                         toCompositeNodeData(toNestedQName("depth4-cont1"),
881                                 toSimpleNodeData(toNestedQName("depth5-leaf1"), "depth5-leaf1-value")),
882                         toSimpleNodeData(toNestedQName("depth4-leaf1"), "depth4-leaf1-value")),
883                 toSimpleNodeData(toNestedQName("depth3-leaf1"), "depth3-leaf1-value")));
884
885         assertTrue(dataSchemaNode instanceof DataNodeContainer);
886         DataSchemaNode depth2cont1Schema = null;
887         for (final DataSchemaNode childNode : ((DataNodeContainer) dataSchemaNode).getChildNodes()) {
888             if (childNode.getQName().getLocalName().equals("depth2-cont1")) {
889                 depth2cont1Schema = childNode;
890                 break;
891             }
892         }
893         assertNotNull(depth2Cont1);
894
895         when(brokerFacade.readOperationalData(any(YangInstanceIdentifier.class))).thenReturn(
896                 TestUtils.compositeNodeToDatastoreNormalizedNode(depth2Cont1, depth2cont1Schema));
897
898         response = target("/operational/nested-module:depth1-cont/depth2-cont1").queryParam("depth", "3")
899                 .request("application/xml").get();
900
901         verifyXMLResponse(
902                 response,
903                 expectContainer(
904                         "depth2-cont1",
905                         expectContainer("depth3-cont1", expectEmptyContainer("depth4-cont1"),
906                                 expectLeaf("depth4-leaf1", "depth4-leaf1-value")),
907                         expectLeaf("depth3-leaf1", "depth3-leaf1-value")));
908     }
909
910     /**
911      * Tests behavior when invalid value of depth URI parameter
912      */
913     @Test
914     @Ignore
915     public void getDataWithInvalidDepthParameterTest() {
916         setControllerContext(schemaContextModules);
917
918         final MultivaluedMap<String, String> paramMap = new MultivaluedHashMap<>();
919         paramMap.putSingle("depth", "1o");
920         final UriInfo mockInfo = mock(UriInfo.class);
921         when(mockInfo.getQueryParameters(false)).thenAnswer(new Answer<MultivaluedMap<String, String>>() {
922             @Override
923             public MultivaluedMap<String, String> answer(final InvocationOnMock invocation) {
924                 return paramMap;
925             }
926         });
927
928         getDataWithInvalidDepthParameterTest(mockInfo);
929
930         paramMap.putSingle("depth", "0");
931         getDataWithInvalidDepthParameterTest(mockInfo);
932
933         paramMap.putSingle("depth", "-1");
934         getDataWithInvalidDepthParameterTest(mockInfo);
935     }
936
937     private void getDataWithInvalidDepthParameterTest(final UriInfo uriInfo) {
938         try {
939             final QName qNameDepth1Cont = QName.create("urn:nested:module", "2014-06-3", "depth1-cont");
940             final YangInstanceIdentifier ii = YangInstanceIdentifier.builder().node(qNameDepth1Cont).build();
941             final NormalizedNode value = (Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(qNameDepth1Cont)).build());
942             when(brokerFacade.readConfigurationData(eq(ii))).thenReturn(value);
943             restconfImpl.readConfigurationData("nested-module:depth1-cont", uriInfo);
944             fail("Expected RestconfDocumentedException");
945         } catch (final RestconfDocumentedException e) {
946             assertTrue("Unexpected error message: " + e.getErrors().get(0).getErrorMessage(), e.getErrors().get(0)
947                     .getErrorMessage().contains("depth"));
948         }
949     }
950
951     private void verifyXMLResponse(final Response response, final NodeData nodeData) {
952         final Document doc = response.readEntity(Document.class);
953 //        Document doc = TestUtils.loadDocumentFrom((InputStream) response.getEntity());
954 //        System.out.println();
955         assertNotNull("Could not parse XML document", doc);
956
957         // System.out.println(TestUtils.getDocumentInPrintableForm( doc ));
958
959         verifyContainerElement(doc.getDocumentElement(), nodeData);
960     }
961
962     @SuppressWarnings("unchecked")
963     private void verifyContainerElement(final Element element, final NodeData nodeData) {
964
965         assertEquals("Element local name", nodeData.key, element.getLocalName());
966
967         final NodeList childNodes = element.getChildNodes();
968         if (nodeData.data == null) { // empty container
969             assertTrue("Expected no child elements for \"" + element.getLocalName() + "\"", childNodes.getLength() == 0);
970             return;
971         }
972
973         final Map<String, NodeData> expChildMap = Maps.newHashMap();
974         for (final NodeData expChild : (List<NodeData>) nodeData.data) {
975             expChildMap.put(expChild.key.toString(), expChild);
976         }
977
978         for (int i = 0; i < childNodes.getLength(); i++) {
979             final org.w3c.dom.Node actualChild = childNodes.item(i);
980             if (!(actualChild instanceof Element)) {
981                 continue;
982             }
983
984             final Element actualElement = (Element) actualChild;
985             final NodeData expChild = expChildMap.remove(actualElement.getLocalName());
986             assertNotNull(
987                     "Unexpected child element for parent \"" + element.getLocalName() + "\": "
988                             + actualElement.getLocalName(), expChild);
989
990             if (expChild.data == null || expChild.data instanceof List) {
991                 verifyContainerElement(actualElement, expChild);
992             } else {
993                 assertEquals("Text content for element: " + actualElement.getLocalName(), expChild.data,
994                         actualElement.getTextContent());
995             }
996         }
997
998         if (!expChildMap.isEmpty()) {
999             fail("Missing elements for parent \"" + element.getLocalName() + "\": " + expChildMap.keySet());
1000         }
1001     }
1002
1003     private NodeData expectContainer(final String name, final NodeData... childData) {
1004         return new NodeData(name, Lists.newArrayList(childData));
1005     }
1006
1007     private NodeData expectEmptyContainer(final String name) {
1008         return new NodeData(name, null);
1009     }
1010
1011     private NodeData expectLeaf(final String name, final Object value) {
1012         return new NodeData(name, value);
1013     }
1014
1015     private QName toNestedQName(final String localName) {
1016         return QName.create("urn:nested:module", "2014-06-3", localName);
1017     }
1018
1019     @SuppressWarnings("unchecked")
1020     private CompositeNode toCompositeNode(final NodeData nodeData) {
1021         final CompositeNodeBuilder<ImmutableCompositeNode> builder = ImmutableCompositeNode.builder();
1022         builder.setQName((QName) nodeData.key);
1023
1024         for (final NodeData child : (List<NodeData>) nodeData.data) {
1025             if (child.data instanceof List) {
1026                 builder.add(toCompositeNode(child));
1027             } else {
1028                 builder.addLeaf((QName) child.key, child.data);
1029             }
1030         }
1031
1032         return builder.toInstance();
1033     }
1034
1035     private NodeData toCompositeNodeData(final QName key, final NodeData... childData) {
1036         return new NodeData(key, Lists.newArrayList(childData));
1037     }
1038
1039     private NodeData toSimpleNodeData(final QName key, final Object value) {
1040         return new NodeData(key, value);
1041     }
1042
1043 }