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