+ 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));
+
+ ControllerContext.getInstance().setMountService(mockMountService);
+
+ final String uri = "/modules/module/ietf-interfaces:interfaces/interface/0/yang-ext:mount/module1-behind-mount-point/2014-02-03";
+
+ 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());
+ final String[] split = responseBody.split("\"module\"");
+ assertEquals("\"module\" element is returned more then once", 2, split.length);
+
+ response = target(uri).request("application/yang.api+xml").get();
+ assertEquals(200, response.getStatus());
+ final Document responseXml = response.readEntity(Document.class);
+
+ final QName module = assertedModuleXmlToModuleQName(responseXml.getDocumentElement());
+
+ assertEquals("module1-behind-mount-point", module.getLocalName());
+ assertEquals("2014-02-03", module.getFormattedRevision());
+ assertEquals("module:1:behind:mount:point", module.getNamespace().toString());
+
+
+ }
+
+ private void validateModulesResponseXml(final Response response, final SchemaContext schemaContext) {
+ assertEquals(200, response.getStatus());
+ final Document responseBody = response.readEntity(Document.class);
+ final NodeList moduleNodes = responseBody.getDocumentElement().getElementsByTagNameNS(RESTCONF_NS, "module");
+
+ assertTrue(moduleNodes.getLength() > 0);
+
+ final HashSet<QName> foundModules = new HashSet<>();
+
+ for(int i=0;i < moduleNodes.getLength();i++) {
+ final org.w3c.dom.Node module = moduleNodes.item(i);
+
+ final QName name = assertedModuleXmlToModuleQName(module);
+ foundModules.add(name);
+ }
+
+ assertAllModules(foundModules,schemaContext);
+ }
+
+ private void assertAllModules(final Set<QName> foundModules, final SchemaContext schemaContext) {
+ for(final Module module : schemaContext.getModules()) {
+ final QName current = QName.create(module.getQNameModule(),module.getName());
+ assertTrue("Module not found in response.",foundModules.contains(current));
+ }
+
+ }
+
+ private QName assertedModuleXmlToModuleQName(final org.w3c.dom.Node module) {
+ assertEquals("module", module.getLocalName());
+ assertEquals(RESTCONF_NS, module.getNamespaceURI());
+ String revision = null;
+ String namespace = null;
+ String name = null;
+
+
+ final NodeList childNodes = module.getChildNodes();
+
+ for(int i =0;i < childNodes.getLength(); i++) {
+ final org.w3c.dom.Node child = childNodes.item(i);
+ assertEquals(RESTCONF_NS, child.getNamespaceURI());
+
+ switch(child.getLocalName()) {
+ case "name":
+ assertNull("Name element appeared multiple times",name);
+ name = child.getTextContent().trim();
+ break;
+ case "revision":
+ assertNull("Revision element appeared multiple times",revision);
+ revision = child.getTextContent().trim();
+ break;
+
+ case "namespace":
+ assertNull("Namespace element appeared multiple times",namespace);
+ namespace = child.getTextContent().trim();
+ break;
+ }
+ }
+
+ assertNotNull("Revision was not part of xml",revision);
+ assertNotNull("Module namespace was not part of xml",namespace);
+ assertNotNull("Module identiffier was not part of xml",name);
+
+
+ // TODO Auto-generated method stub
+
+ return QName.create(namespace,revision,name);
+ }
+
+ private void validateModulesResponseJson(final Response response) {
+ assertEquals(200, response.getStatus());
+ final String responseBody = response.readEntity(String.class);
+
+ assertTrue("Module1 in json wasn't found", prepareJsonRegex("module1", "2014-01-01", "module:1", responseBody)
+ .find());
+ assertTrue("Module2 in json wasn't found", prepareJsonRegex("module2", "2014-01-02", "module:2", responseBody)
+ .find());
+ assertTrue("Module3 in json wasn't found", prepareJsonRegex("module3", "2014-01-03", "module:3", responseBody)
+ .find());
+ }
+
+ private Matcher prepareJsonRegex(final String module, final String revision, final String namespace,
+ final String searchIn) {
+ final StringBuilder regex = new StringBuilder();
+ regex.append("^");
+
+ regex.append(".*\\{");
+ regex.append(".*\"name\"");
+ regex.append(".*:");
+ regex.append(".*\"" + module + "\",");
+
+ regex.append(".*\"revision\"");
+ regex.append(".*:");
+ regex.append(".*\"" + revision + "\",");
+
+ regex.append(".*\"namespace\"");
+ regex.append(".*:");
+ regex.append(".*\"" + namespace + "\"");
+
+ regex.append(".*\\}");
+
+ regex.append(".*");
+ regex.append("$");
+ final Pattern ptrn = Pattern.compile(regex.toString(), Pattern.DOTALL);
+ return ptrn.matcher(searchIn);
+
+ }
+
+
+ private void prepareMockForModulesTest(final ControllerContext mockedControllerContext)
+ throws FileNotFoundException {
+ final SchemaContext schemaContext = TestUtils.loadSchemaContext("/modules");
+ mockedControllerContext.setGlobalSchema(schemaContext);
+ // when(mockedControllerContext.getGlobalSchema()).thenReturn(schemaContext);
+ }
+
+ private int get(final String uri, final String mediaType) {