+ final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
+ when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
+
+ ControllerContext.getInstance().setMountService(mockMountService);
+
+ final String uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont/lst1/GigabitEthernet0%2F0%2F0%2F0";
+ assertEquals(200, get(uri, MediaType.APPLICATION_XML));
+ }
+
+ private YangInstanceIdentifier prepareInstanceIdentifierForList() throws URISyntaxException, ParseException {
+ final List<PathArgument> parameters = new ArrayList<>();
+
+ final Date revision = new SimpleDateFormat("yyyy-MM-dd").parse("2014-01-09");
+ final URI uri = new URI("test:module");
+ final QName qNameCont = QName.create(uri, revision, "cont");
+ final QName qNameList = QName.create(uri, revision, "lst1");
+ final QName qNameKeyList = QName.create(uri, revision, "lf11");
+
+ parameters.add(new YangInstanceIdentifier.NodeIdentifier(qNameCont));
+ parameters.add(new YangInstanceIdentifier.NodeIdentifier(qNameList));
+ parameters.add(new YangInstanceIdentifier.NodeIdentifierWithPredicates(qNameList, qNameKeyList,
+ "GigabitEthernet0/0/0/0"));
+ return YangInstanceIdentifier.create(parameters);
+ }
+
+ @Test
+ public void getDataMountPointIntoHighestElement() throws UnsupportedEncodingException, URISyntaxException,
+ ParseException {
+ when(brokerFacade.readConfigurationData(any(DOMMountPoint.class), any(YangInstanceIdentifier.class))).thenReturn(
+ prepareCnDataForMountPointTest(true));
+ final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
+ when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
+ final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
+ when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
+
+ ControllerContext.getInstance().setMountService(mockMountService);
+
+ final String uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont";
+ assertEquals(200, get(uri, MediaType.APPLICATION_XML));
+ }
+
+ // /modules
+ @Test
+ public void getModulesTest() throws UnsupportedEncodingException, FileNotFoundException {
+ final ControllerContext controllerContext = ControllerContext.getInstance();
+ controllerContext.setGlobalSchema(schemaContextModules);
+ restconfImpl.setControllerContext(controllerContext);
+
+ final String uri = "/modules";
+
+ Response response = target(uri).request("application/yang.api+json").get();
+ validateModulesResponseJson(response);
+
+ response = target(uri).request("application/yang.api+xml").get();
+ validateModulesResponseXml(response,schemaContextModules);
+ }
+
+ // /streams/
+ @Test
+ @Ignore // FIXME : find why it is fail by in gerrit build
+ public void getStreamsTest() throws UnsupportedEncodingException, FileNotFoundException {
+ setControllerContext(schemaContextModules);
+
+ final String uri = "/streams";
+
+ Response response = target(uri).request("application/yang.api+json").get();
+ final String responseBody = response.readEntity(String.class);
+ assertEquals(200, response.getStatus());
+ assertNotNull(responseBody);
+ assertTrue(responseBody.contains("streams"));
+
+ response = target(uri).request("application/yang.api+xml").get();
+ assertEquals(200, response.getStatus());
+ final Document responseXmlBody = response.readEntity(Document.class);
+ assertNotNull(responseXmlBody);
+ final Element rootNode = responseXmlBody.getDocumentElement();
+
+ assertEquals("streams", rootNode.getLocalName());
+ assertEquals(RESTCONF_NS, rootNode.getNamespaceURI());
+ }
+
+ // /modules/module
+ @Test
+ public void getModuleTest() throws FileNotFoundException, UnsupportedEncodingException {
+ setControllerContext(schemaContextModules);
+
+ final String uri = "/modules/module/module2/2014-01-02";
+
+ Response response = target(uri).request("application/yang.api+xml").get();
+ assertEquals(200, response.getStatus());
+ final Document responseXml = response.readEntity(Document.class);
+
+ final QName qname = assertedModuleXmlToModuleQName(responseXml.getDocumentElement());
+ assertNotNull(qname);
+
+ assertEquals("module2", qname.getLocalName());
+ assertEquals("module:2", qname.getNamespace().toString());
+ assertEquals("2014-01-02", qname.getFormattedRevision());
+
+ response = target(uri).request("application/yang.api+json").get();
+ assertEquals(200, response.getStatus());
+ final String responseBody = response.readEntity(String.class);
+ assertTrue("Module2 in json wasn't found", prepareJsonRegex("module2", "2014-01-02", "module:2", responseBody)
+ .find());
+ final String[] split = responseBody.split("\"module\"");
+ assertEquals("\"module\" element is returned more then once", 2, split.length);
+
+ }
+
+ // /operations
+ @Test
+ public void getOperationsTest() throws FileNotFoundException, UnsupportedEncodingException {
+ setControllerContext(schemaContextModules);
+
+ final String uri = "/operations";
+
+ Response response = target(uri).request("application/yang.api+xml").get();
+ assertEquals(200, response.getStatus());
+ final Document responseDoc = response.readEntity(Document.class);
+ validateOperationsResponseXml(responseDoc, schemaContextModules);
+
+ response = target(uri).request("application/yang.api+json").get();
+ assertEquals(200, response.getStatus());
+ final String responseBody = response.readEntity(String.class);
+ assertTrue("Json response for /operations dummy-rpc1-module1 is incorrect",
+ validateOperationsResponseJson(responseBody, "dummy-rpc1-module1", "module1").find());
+ assertTrue("Json response for /operations dummy-rpc2-module1 is incorrect",
+ validateOperationsResponseJson(responseBody, "dummy-rpc2-module1", "module1").find());
+ assertTrue("Json response for /operations dummy-rpc1-module2 is incorrect",
+ validateOperationsResponseJson(responseBody, "dummy-rpc1-module2", "module2").find());
+ assertTrue("Json response for /operations dummy-rpc2-module2 is incorrect",
+ validateOperationsResponseJson(responseBody, "dummy-rpc2-module2", "module2").find());
+
+ }
+
+ private void validateOperationsResponseXml(final Document responseDoc, final SchemaContext schemaContext) {
+ final Element operationsElem = responseDoc.getDocumentElement();
+ assertEquals(RESTCONF_NS, operationsElem.getNamespaceURI());
+ assertEquals("operations", operationsElem.getLocalName());
+
+
+ final HashSet<QName> foundOperations = new HashSet<>();
+
+ final NodeList operationsList = operationsElem.getChildNodes();
+ for(int i = 0;i < operationsList.getLength();i++) {
+ final org.w3c.dom.Node operation = operationsList.item(i);
+
+ final String namespace = operation.getNamespaceURI();
+ final String name = operation.getLocalName();
+ final QName opQName = QName.create(URI.create(namespace), null, name);
+ foundOperations.add(opQName);
+ }
+
+ for(final RpcDefinition schemaOp : schemaContext.getOperations()) {
+ assertTrue(foundOperations.contains(schemaOp.getQName().withoutRevision()));
+ }
+
+ }
+
+ // /operations/pathToMountPoint/yang-ext:mount
+ @Test
+ public void getOperationsBehindMountPointTest() throws FileNotFoundException, UnsupportedEncodingException {
+ setControllerContext(schemaContextModules);
+
+ final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
+ when(mountInstance.getSchemaContext()).thenReturn(schemaContextBehindMountPoint);
+ final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
+ when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
+
+ ControllerContext.getInstance().setMountService(mockMountService);
+
+ final String uri = "/operations/ietf-interfaces:interfaces/interface/0/yang-ext:mount/";
+
+ Response response = target(uri).request("application/yang.api+xml").get();
+ assertEquals(200, response.getStatus());
+
+ final Document responseDoc = response.readEntity(Document.class);
+ validateOperationsResponseXml(responseDoc, schemaContextBehindMountPoint);
+
+ response = target(uri).request("application/yang.api+json").get();
+ assertEquals(200, response.getStatus());
+ final String responseBody = response.readEntity(String.class);
+ assertTrue("Json response for /operations/mount_point rpc-behind-module1 is incorrect",
+ validateOperationsResponseJson(responseBody, "rpc-behind-module1", "module1-behind-mount-point").find());
+ assertTrue("Json response for /operations/mount_point rpc-behind-module2 is incorrect",
+ validateOperationsResponseJson(responseBody, "rpc-behind-module2", "module2-behind-mount-point").find());
+
+ }
+
+ private Matcher validateOperationsResponseJson(final String searchIn, final String rpcName, final String moduleName) {
+ final StringBuilder regex = new StringBuilder();
+ regex.append("^");
+
+ regex.append(".*\\{");
+ regex.append(".*\"");
+
+ // operations prefix optional
+ regex.append("(");
+ regex.append("ietf-restconf:");
+ regex.append("|)");
+ // :operations prefix optional
+
+ regex.append("operations\"");
+ regex.append(".*:");
+ regex.append(".*\\{");
+
+ regex.append(".*\"" + moduleName);
+ regex.append(":");
+ regex.append(rpcName + "\"");
+ regex.append(".*\\[");
+ regex.append(".*null");
+ regex.append(".*\\]");
+
+ regex.append(".*\\}");
+ regex.append(".*\\}");
+
+ regex.append(".*");
+ regex.append("$");
+ final Pattern ptrn = Pattern.compile(regex.toString(), Pattern.DOTALL);
+ return ptrn.matcher(searchIn);
+
+ }
+
+ private Matcher validateOperationsResponseXml(final String searchIn, final String rpcName, final String namespace) {
+ final StringBuilder regex = new StringBuilder();
+
+ regex.append("^");
+
+ regex.append(".*<operations");
+ regex.append(".*xmlns=\"urn:ietf:params:xml:ns:yang:ietf-restconf\"");
+ regex.append(".*>");
+
+ regex.append(".*<");
+ regex.append(".*" + rpcName);
+ regex.append(".*" + namespace);
+ regex.append(".*/");
+ regex.append(".*>");
+
+ regex.append(".*</operations.*");
+ regex.append(".*>");
+
+ regex.append(".*");
+ regex.append("$");
+ final Pattern ptrn = Pattern.compile(regex.toString(), Pattern.DOTALL);
+ return ptrn.matcher(searchIn);
+ }
+
+ // /restconf/modules/pathToMountPoint/yang-ext:mount
+ @Test
+ public void getModulesBehindMountPoint() throws FileNotFoundException, UnsupportedEncodingException {
+ setControllerContext(schemaContextModules);
+
+ final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
+ when(mountInstance.getSchemaContext()).thenReturn(schemaContextBehindMountPoint);
+ final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
+ when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
+
+ ControllerContext.getInstance().setMountService(mockMountService);
+
+ final String uri = "/modules/ietf-interfaces:interfaces/interface/0/yang-ext:mount/";
+
+ Response response = target(uri).request("application/yang.api+json").get();
+ assertEquals(200, response.getStatus());
+ final String responseBody = response.readEntity(String.class);
+
+ assertTrue(
+ "module1-behind-mount-point in json wasn't found",
+ prepareJsonRegex("module1-behind-mount-point", "2014-02-03", "module:1:behind:mount:point",
+ responseBody).find());
+ assertTrue(
+ "module2-behind-mount-point in json wasn't found",
+ prepareJsonRegex("module2-behind-mount-point", "2014-02-04", "module:2:behind:mount:point",
+ responseBody).find());
+
+ response = target(uri).request("application/yang.api+xml").get();
+ assertEquals(200, response.getStatus());
+ validateModulesResponseXml(response, schemaContextBehindMountPoint);
+
+ }
+
+ // /restconf/modules/module/pathToMountPoint/yang-ext:mount/moduleName/revision
+ @Test
+ public void getModuleBehindMountPoint() throws FileNotFoundException, UnsupportedEncodingException {
+ setControllerContext(schemaContextModules);
+
+ final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
+ when(mountInstance.getSchemaContext()).thenReturn(schemaContextBehindMountPoint);
+ final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
+ when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));