2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.restconf.impl.test;
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;
21 import java.io.FileNotFoundException;
22 import java.io.InputStream;
23 import java.io.UnsupportedEncodingException;
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;
34 import java.util.regex.Matcher;
35 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;
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.InstanceIdentifier;
66 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.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;
77 import com.google.common.collect.Lists;
78 import com.google.common.collect.Maps;
80 public class RestGetOperationTest extends JerseyTest {
82 static class NodeData {
84 Object data; // List for a CompositeNode, value Object for a SimpleNode
86 NodeData(final Object key, final Object data) {
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;
98 private static SchemaContext schemaContextModules;
99 private static SchemaContext schemaContextBehindMountPoint;
101 private static final String RESTCONF_NS = "urn:ietf:params:xml:ns:yang:ietf-restconf";
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();
115 schemaContextModules = TestUtils.loadSchemaContext("/modules");
116 schemaContextBehindMountPoint = TestUtils.loadSchemaContext("/modules/modules-behind-mount-point");
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;
135 * Tests of status codes for "/operational/{identifier}".
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));
143 uri = "/operational/wrong-module:interfaces/interface/eth0";
144 assertEquals(400, get(uri, MediaType.APPLICATION_XML));
148 * Tests of status codes for "/config/{identifier}".
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));
156 uri = "/config/wrong-module:interfaces/interface/eth0";
157 assertEquals(400, get(uri, MediaType.APPLICATION_XML));
161 * MountPoint test. URI represents mount point.
164 public void getDataWithUrlMountPoint() throws UnsupportedEncodingException, URISyntaxException {
166 brokerFacade.readConfigurationDataBehindMountPoint(any(MountInstance.class),
167 any(InstanceIdentifier.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(InstanceIdentifier.class))).thenReturn(mountInstance);
173 ControllerContext.getInstance().setMountService(mockMountService);
175 String uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont/cont1";
176 assertEquals(200, get(uri, MediaType.APPLICATION_XML));
178 uri = "/config/ietf-interfaces:interfaces/yang-ext:mount/test-module:cont/cont1";
179 assertEquals(200, get(uri, MediaType.APPLICATION_XML));
183 * MountPoint test. URI represents mount point.
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, InstanceIdentifier)} which is called in
189 * method {@link RestconfImpl#readConfigurationData}
192 * @throws ParseException
195 public void getDataWithSlashesBehindMountPoint() throws UnsupportedEncodingException, URISyntaxException,
197 InstanceIdentifier awaitedInstanceIdentifier = prepareInstanceIdentifierForList();
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(InstanceIdentifier.class))).thenReturn(mountInstance);
206 ControllerContext.getInstance().setMountService(mockMountService);
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));
212 private InstanceIdentifier prepareInstanceIdentifierForList() throws URISyntaxException, ParseException {
213 List<PathArgument> parameters = new ArrayList<>();
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");
221 parameters.add(new InstanceIdentifier.NodeIdentifier(qNameCont));
222 parameters.add(new InstanceIdentifier.NodeIdentifierWithPredicates(qNameList, qNameKeyList,
223 "GigabitEthernet0/0/0/0"));
224 return InstanceIdentifier.create(parameters);
228 public void getDataMountPointIntoHighestElement() throws UnsupportedEncodingException, URISyntaxException {
230 brokerFacade.readConfigurationDataBehindMountPoint(any(MountInstance.class),
231 any(InstanceIdentifier.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(InstanceIdentifier.class))).thenReturn(mountInstance);
237 ControllerContext.getInstance().setMountService(mockMountService);
239 String uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/";
240 assertEquals(200, get(uri, MediaType.APPLICATION_XML));
245 public void getModulesTest() throws UnsupportedEncodingException, FileNotFoundException {
246 ControllerContext.getInstance().setGlobalSchema(schemaContextModules);
248 String uri = "/modules";
250 Response response = target(uri).request("application/yang.api+json").get();
251 validateModulesResponseJson(response);
253 response = target(uri).request("application/yang.api+xml").get();
254 validateModulesResponseXml(response,schemaContextModules);
259 public void getStreamsTest() throws UnsupportedEncodingException, FileNotFoundException {
260 ControllerContext.getInstance().setGlobalSchema(schemaContextModules);
262 String uri = "/streams";
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"));
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();
274 assertEquals("streams", rootNode.getLocalName());
275 assertEquals(RESTCONF_NS, rootNode.getNamespaceURI());
280 public void getModuleTest() throws FileNotFoundException, UnsupportedEncodingException {
281 ControllerContext.getInstance().setGlobalSchema(schemaContextModules);
283 String uri = "/modules/module/module2/2014-01-02";
285 Response response = target(uri).request("application/yang.api+xml").get();
286 assertEquals(200, response.getStatus());
287 Document responseXml = response.readEntity(Document.class);
291 QName qname = assertedModuleXmlToModuleQName(responseXml.getDocumentElement());
292 assertNotNull(qname);
294 assertEquals("module2", qname.getLocalName());
295 assertEquals("module:2", qname.getNamespace().toString());
296 assertEquals("2014-01-02", qname.getFormattedRevision());
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)
303 String[] split = responseBody.split("\"module\"");
304 assertEquals("\"module\" element is returned more then once", 2, split.length);
310 public void getOperationsTest() throws FileNotFoundException, UnsupportedEncodingException {
311 ControllerContext.getInstance().setGlobalSchema(schemaContextModules);
313 String uri = "/operations";
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);
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());
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());
340 HashSet<QName> foundOperations = new HashSet<>();
342 NodeList operationsList = operationsElem.getChildNodes();
343 for(int i = 0;i < operationsList.getLength();i++) {
344 org.w3c.dom.Node operation = operationsList.item(i);
346 String namespace = operation.getNamespaceURI();
347 String name = operation.getLocalName();
348 QName opQName = QName.create(URI.create(namespace), null, name);
349 foundOperations.add(opQName);
352 for(RpcDefinition schemaOp : schemaContext.getOperations()) {
353 assertTrue(foundOperations.contains(schemaOp.getQName().withoutRevision()));
358 // /operations/pathToMountPoint/yang-ext:mount
360 public void getOperationsBehindMountPointTest() throws FileNotFoundException, UnsupportedEncodingException {
361 ControllerContext controllerContext = ControllerContext.getInstance();
362 controllerContext.setGlobalSchema(schemaContextModules);
364 MountInstance mountInstance = mock(MountInstance.class);
365 when(mountInstance.getSchemaContext()).thenReturn(schemaContextBehindMountPoint);
366 MountService mockMountService = mock(MountService.class);
367 when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
369 controllerContext.setMountService(mockMountService);
371 String uri = "/operations/ietf-interfaces:interfaces/interface/0/yang-ext:mount/";
373 Response response = target(uri).request("application/yang.api+xml").get();
374 assertEquals(200, response.getStatus());
376 Document responseDoc = response.readEntity(Document.class);
377 validateOperationsResponseXml(responseDoc, schemaContextBehindMountPoint);
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());
389 private Matcher validateOperationsResponseJson(final String searchIn, final String rpcName, final String moduleName) {
390 StringBuilder regex = new StringBuilder();
393 regex.append(".*\\{");
394 regex.append(".*\"");
396 // operations prefix optional
398 regex.append("ietf-restconf:");
400 // :operations prefix optional
402 regex.append("operations\"");
404 regex.append(".*\\{");
406 regex.append(".*\"" + moduleName);
408 regex.append(rpcName + "\"");
409 regex.append(".*\\[");
410 regex.append(".*null");
411 regex.append(".*\\]");
413 regex.append(".*\\}");
414 regex.append(".*\\}");
418 Pattern ptrn = Pattern.compile(regex.toString(), Pattern.DOTALL);
419 return ptrn.matcher(searchIn);
423 private Matcher validateOperationsResponseXml(final String searchIn, final String rpcName, final String namespace) {
424 StringBuilder regex = new StringBuilder();
428 regex.append(".*<operations");
429 regex.append(".*xmlns=\"urn:ietf:params:xml:ns:yang:ietf-restconf\"");
433 regex.append(".*" + rpcName);
434 regex.append(".*" + namespace);
438 regex.append(".*</operations.*");
443 Pattern ptrn = Pattern.compile(regex.toString(), Pattern.DOTALL);
444 return ptrn.matcher(searchIn);
447 // /restconf/modules/pathToMountPoint/yang-ext:mount
449 public void getModulesBehindMountPoint() throws FileNotFoundException, UnsupportedEncodingException {
450 ControllerContext controllerContext = ControllerContext.getInstance();
451 controllerContext.setGlobalSchema(schemaContextModules);
453 MountInstance mountInstance = mock(MountInstance.class);
454 when(mountInstance.getSchemaContext()).thenReturn(schemaContextBehindMountPoint);
455 MountService mockMountService = mock(MountService.class);
456 when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
458 controllerContext.setMountService(mockMountService);
460 String uri = "/modules/ietf-interfaces:interfaces/interface/0/yang-ext:mount/";
462 Response response = target(uri).request("application/yang.api+json").get();
463 assertEquals(200, response.getStatus());
464 String responseBody = response.readEntity(String.class);
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());
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());
475 response = target(uri).request("application/yang.api+xml").get();
476 assertEquals(200, response.getStatus());
477 validateModulesResponseXml(response, schemaContextBehindMountPoint);
481 // /restconf/modules/module/pathToMountPoint/yang-ext:mount/moduleName/revision
483 public void getModuleBehindMountPoint() throws FileNotFoundException, UnsupportedEncodingException {
484 ControllerContext controllerContext = ControllerContext.getInstance();
485 controllerContext.setGlobalSchema(schemaContextModules);
487 MountInstance mountInstance = mock(MountInstance.class);
488 when(mountInstance.getSchemaContext()).thenReturn(schemaContextBehindMountPoint);
489 MountService mockMountService = mock(MountService.class);
490 when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
492 controllerContext.setMountService(mockMountService);
494 String uri = "/modules/module/ietf-interfaces:interfaces/interface/0/yang-ext:mount/module1-behind-mount-point/2014-02-03";
496 Response response = target(uri).request("application/yang.api+json").get();
497 assertEquals(200, response.getStatus());
498 String responseBody = response.readEntity(String.class);
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);
507 response = target(uri).request("application/yang.api+xml").get();
508 assertEquals(200, response.getStatus());
509 Document responseXml = response.readEntity(Document.class);
511 QName module = assertedModuleXmlToModuleQName(responseXml.getDocumentElement());
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());
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");
525 assertTrue(moduleNodes.getLength() > 0);
527 HashSet<QName> foundModules = new HashSet<>();
529 for(int i=0;i < moduleNodes.getLength();i++) {
530 org.w3c.dom.Node module = moduleNodes.item(i);
532 QName name = assertedModuleXmlToModuleQName(module);
533 foundModules.add(name);
536 assertAllModules(foundModules,schemaContext);
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));
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;
555 NodeList childNodes = module.getChildNodes();
557 for(int i =0;i < childNodes.getLength(); i++) {
558 org.w3c.dom.Node child = childNodes.item(i);
559 assertEquals(RESTCONF_NS, child.getNamespaceURI());
561 switch(child.getLocalName()) {
563 assertNull("Name element appeared multiple times",name);
564 name = child.getTextContent().trim();
567 assertNull("Revision element appeared multiple times",revision);
568 revision = child.getTextContent().trim();
572 assertNull("Namespace element appeared multiple times",namespace);
573 namespace = child.getTextContent().trim();
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);
583 // TODO Auto-generated method stub
585 return QName.create(namespace,revision,name);
588 private void validateModulesResponseJson(final Response response) {
589 assertEquals(200, response.getStatus());
590 String responseBody = response.readEntity(String.class);
592 assertTrue("Module1 in json wasn't found", prepareJsonRegex("module1", "2014-01-01", "module:1", responseBody)
594 assertTrue("Module2 in json wasn't found", prepareJsonRegex("module2", "2014-01-02", "module:2", responseBody)
596 assertTrue("Module3 in json wasn't found", prepareJsonRegex("module3", "2014-01-03", "module:3", responseBody)
600 private Matcher prepareJsonRegex(final String module, final String revision, final String namespace,
601 final String searchIn) {
602 StringBuilder regex = new StringBuilder();
605 regex.append(".*\\{");
606 regex.append(".*\"name\"");
608 regex.append(".*\"" + module + "\",");
610 regex.append(".*\"revision\"");
612 regex.append(".*\"" + revision + "\",");
614 regex.append(".*\"namespace\"");
616 regex.append(".*\"" + namespace + "\"");
618 regex.append(".*\\}");
622 Pattern ptrn = Pattern.compile(regex.toString(), Pattern.DOTALL);
623 return ptrn.matcher(searchIn);
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);
635 private int get(final String uri, final String mediaType) {
636 return target(uri).request(mediaType).get().getStatus();
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();
646 private void mockReadOperationalDataMethod() {
647 when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(answerFromGet);
650 private void mockReadConfigurationDataMethod() {
651 when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(answerFromGet);
654 private static CompositeNode prepareCompositeNodeWithIetfInterfacesInterfacesData() {
655 CompositeNode intface;
657 intface = new CompositeNodeWrapper(new URI("interface"), "interface");
658 List<Node<?>> childs = new ArrayList<>();
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);
666 } catch (URISyntaxException e) {
673 * If includeWhiteChars URI parameter is set to false then no white characters can be included in returned output
675 * @throws UnsupportedEncodingException
678 public void getDataWithUriIncludeWhiteCharsParameterTest() throws UnsupportedEncodingException {
679 getDataWithUriIncludeWhiteCharsParameter("config");
680 getDataWithUriIncludeWhiteCharsParameter("operational");
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);
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
693 assertFalse(matcher.matches());
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());
705 public void getDataWithUriDepthParameterTest() throws UnsupportedEncodingException {
707 ControllerContext.getInstance().setGlobalSchema(schemaContextModules);
709 CompositeNode depth1Cont = toCompositeNode(toCompositeNodeData(
710 toNestedQName("depth1-cont"),
712 toNestedQName("depth2-cont1"),
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")),
720 toNestedQName("depth2-cont2"),
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")));
729 when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(depth1Cont);
731 // Test config with depth 1
733 Response response = target("/config/nested-module:depth1-cont").queryParam("depth", "1")
734 .request("application/xml").get();
736 verifyXMLResponse(response, expectEmptyContainer("depth1-cont"));
738 // Test config with depth 2
740 response = target("/config/nested-module:depth1-cont").queryParam("depth", "2").request("application/xml")
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) );
751 expectContainer("depth1-cont", expectEmptyContainer("depth2-cont1"),
752 expectEmptyContainer("depth2-cont2"), expectLeaf("depth2-leaf1", "depth2-leaf1-value")));
754 // Test config with depth 3
756 response = target("/config/nested-module:depth1-cont").queryParam("depth", "3").request("application/xml")
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")));
769 // Test config with depth 4
771 response = target("/config/nested-module:depth1-cont").queryParam("depth", "4").request("application/xml")
780 expectContainer("depth3-cont1", expectEmptyContainer("depth4-cont1"),
781 expectLeaf("depth4-leaf1", "depth4-leaf1-value")),
782 expectLeaf("depth3-leaf1", "depth3-leaf1-value")),
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")));
790 // Test config with depth 5
792 response = target("/config/nested-module:depth1-cont").queryParam("depth", "5").request("application/xml")
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")),
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")));
817 // Test config with depth unbounded
819 response = target("/config/nested-module:depth1-cont").queryParam("depth", "unbounded")
820 .request("application/xml").get();
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")),
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")));
846 CompositeNode depth2Cont1 = toCompositeNode(toCompositeNodeData(
847 toNestedQName("depth2-cont1"),
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")));
855 when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(depth2Cont1);
857 response = target("/operational/nested-module:depth1-cont/depth2-cont1").queryParam("depth", "3")
858 .request("application/xml").get();
864 expectContainer("depth3-cont1", expectEmptyContainer("depth4-cont1"),
865 expectLeaf("depth4-leaf1", "depth4-leaf1-value")),
866 expectLeaf("depth3-leaf1", "depth3-leaf1-value")));
870 public void getDataWithInvalidDepthParameterTest() {
872 ControllerContext.getInstance().setGlobalSchema(schemaContextModules);
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>>() {
879 public MultivaluedMap<String, String> answer(final InvocationOnMock invocation) {
884 getDataWithInvalidDepthParameterTest(mockInfo);
886 paramMap.putSingle("depth", "0");
887 getDataWithInvalidDepthParameterTest(mockInfo);
889 paramMap.putSingle("depth", "-1");
890 getDataWithInvalidDepthParameterTest(mockInfo);
893 private void getDataWithInvalidDepthParameterTest(final UriInfo uriInfo) {
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"));
903 private void verifyXMLResponse(final Response response, final NodeData nodeData) {
905 Document doc = TestUtils.loadDocumentFrom((InputStream) response.getEntity());
906 assertNotNull("Could not parse XML document", doc);
908 // System.out.println(TestUtils.getDocumentInPrintableForm( doc ));
910 verifyContainerElement(doc.getDocumentElement(), nodeData);
913 @SuppressWarnings("unchecked")
914 private void verifyContainerElement(final Element element, final NodeData nodeData) {
916 assertEquals("Element local name", nodeData.key, element.getNodeName());
918 NodeList childNodes = element.getChildNodes();
919 if (nodeData.data == null) { // empty container
920 assertTrue("Expected no child elements for \"" + element.getNodeName() + "\"", childNodes.getLength() == 0);
924 Map<String, NodeData> expChildMap = Maps.newHashMap();
925 for (NodeData expChild : (List<NodeData>) nodeData.data) {
926 expChildMap.put(expChild.key.toString(), expChild);
929 for (int i = 0; i < childNodes.getLength(); i++) {
930 org.w3c.dom.Node actualChild = childNodes.item(i);
931 if (!(actualChild instanceof Element)) {
935 Element actualElement = (Element) actualChild;
936 NodeData expChild = expChildMap.remove(actualElement.getNodeName());
938 "Unexpected child element for parent \"" + element.getNodeName() + "\": "
939 + actualElement.getNodeName(), expChild);
941 if (expChild.data == null || expChild.data instanceof List) {
942 verifyContainerElement(actualElement, expChild);
944 assertEquals("Text content for element: " + actualElement.getNodeName(), expChild.data,
945 actualElement.getTextContent());
949 if (!expChildMap.isEmpty()) {
950 fail("Missing elements for parent \"" + element.getNodeName() + "\": " + expChildMap.keySet());
954 private NodeData expectContainer(final String name, final NodeData... childData) {
955 return new NodeData(name, Lists.newArrayList(childData));
958 private NodeData expectEmptyContainer(final String name) {
959 return new NodeData(name, null);
962 private NodeData expectLeaf(final String name, final Object value) {
963 return new NodeData(name, value);
966 private QName toNestedQName(final String localName) {
967 return QName.create("urn:nested:module", "2014-06-3", localName);
970 @SuppressWarnings("unchecked")
971 private CompositeNode toCompositeNode(final NodeData nodeData) {
972 CompositeNodeBuilder<ImmutableCompositeNode> builder = ImmutableCompositeNode.builder();
973 builder.setQName((QName) nodeData.key);
975 for (NodeData child : (List<NodeData>) nodeData.data) {
976 if (child.data instanceof List) {
977 builder.add(toCompositeNode(child));
979 builder.addLeaf((QName) child.key, child.data);
983 return builder.toInstance();
986 private NodeData toCompositeNodeData(final QName key, final NodeData... childData) {
987 return new NodeData(key, Lists.newArrayList(childData));
990 private NodeData toSimpleNodeData(final QName key, final Object value) {
991 return new NodeData(key, value);