NETCONF-521: Replace literal '\n' by newline
[netconf.git] / netconf / mdsal-netconf-connector / src / test / java / org / opendaylight / netconf / mdsal / connector / ops / NetconfMDSalMappingTest.java
index 1084907fe16665fa9afeb28ac470e108415f6ae1..ad35b0dfa55d5498815cfdf59b074a565308769d 100644 (file)
@@ -17,12 +17,8 @@ import static org.mockito.Mockito.doAnswer;
 
 import com.google.common.io.ByteSource;
 import com.google.common.util.concurrent.Futures;
-import java.io.InputStream;
 import java.io.StringWriter;
-import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.EnumMap;
-import java.util.List;
 import java.util.concurrent.ExecutorService;
 import javax.xml.transform.OutputKeys;
 import javax.xml.transform.Transformer;
@@ -32,9 +28,11 @@ import javax.xml.transform.stream.StreamResult;
 import org.custommonkey.xmlunit.DetailedDiff;
 import org.custommonkey.xmlunit.Diff;
 import org.custommonkey.xmlunit.XMLUnit;
+import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mock;
+import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
 import org.opendaylight.controller.cluster.databroker.ConcurrentDOMDataBroker;
 import org.opendaylight.controller.config.util.xml.DocumentedException;
@@ -69,12 +67,14 @@ import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
+import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 
 public class NetconfMDSalMappingTest {
     private static final Logger LOG = LoggerFactory.getLogger(NetconfMDSalMappingTest.class);
 
+    private static final String TARGET_KEY = "target";
     private static final String RPC_REPLY_ELEMENT = "rpc-reply";
     private static final String DATA_ELEMENT = "data";
     private static final String FILTER_NODE = "filter";
@@ -129,7 +129,8 @@ public class NetconfMDSalMappingTest {
         XMLUnit.setIgnoreWhitespace(true);
         XMLUnit.setIgnoreAttributeOrder(true);
 
-        this.schemaContext = YangParserTestUtils.parseYangStreams(getYangSchemas());
+        this.schemaContext = YangParserTestUtils.parseYangResources(NetconfMDSalMappingTest.class,
+            "/META-INF/yang/config@2013-04-05.yang", "/yang/mdsal-netconf-mapping-test.yang");
         schemaContext.getModules();
         final SchemaService schemaService = createSchemaService();
 
@@ -141,7 +142,7 @@ public class NetconfMDSalMappingTest {
         datastores.put(LogicalDatastoreType.OPERATIONAL, operStore);
 
         final ExecutorService listenableFutureExecutor = SpecialExecutors.newBlockingBoundedCachedThreadPool(
-                16, 16, "CommitFutures");
+                16, 16, "CommitFutures", NetconfMDSalMappingTest.class);
 
         final ConcurrentDOMDataBroker cdb = new ConcurrentDOMDataBroker(datastores, listenableFutureExecutor);
         this.transactionProvider = new TransactionProvider(cdb, SESSION_ID_FOR_REPORTING);
@@ -150,7 +151,7 @@ public class NetconfMDSalMappingTest {
             final SourceIdentifier sId = (SourceIdentifier) invocationOnMock.getArguments()[0];
             final YangTextSchemaSource yangTextSchemaSource =
                     YangTextSchemaSource.delegateForByteSource(sId, ByteSource.wrap("module test".getBytes()));
-            return Futures.immediateCheckedFuture(yangTextSchemaSource);
+            return Futures.immediateFuture(yangTextSchemaSource);
 
         }).when(sourceProvider).getSource(any(SourceIdentifier.class));
 
@@ -326,6 +327,32 @@ public class NetconfMDSalMappingTest {
 
     }
 
+    @Test
+    public void testAugmentedContainerReplace() throws Exception {
+        verifyResponse(edit("messages/mapping/editConfigs/editConfig_empty_modules_create.xml"),
+                RPC_REPLY_OK);
+        verifyResponse(commit(), RPC_REPLY_OK);
+
+        verifyResponse(edit("messages/mapping/editConfigs/editConfig_augmented_container_replace.xml"),
+                RPC_REPLY_OK);
+        verifyResponse(commit(), RPC_REPLY_OK);
+
+        deleteDatastore();
+    }
+
+    @Test
+    public void testLeafFromAugmentReplace() throws Exception {
+        verifyResponse(edit("messages/mapping/editConfigs/editConfig_empty_modules_create.xml"),
+                RPC_REPLY_OK);
+        verifyResponse(commit(), RPC_REPLY_OK);
+
+        verifyResponse(edit("messages/mapping/editConfigs/editConfig_leaf_from_augment_replace.xml"),
+                RPC_REPLY_OK);
+        verifyResponse(commit(), RPC_REPLY_OK);
+
+        deleteDatastore();
+    }
+
     @Test
     public void testLock() throws Exception {
         verifyResponse(lockCandidate(), RPC_REPLY_OK);
@@ -512,6 +539,61 @@ public class NetconfMDSalMappingTest {
         deleteDatastore();
     }
 
+    @Test
+    public void testEditConfigGetElementByTagName() throws Exception {
+        EditConfig editConfig = new EditConfig("test_edit-config", Mockito.mock(CurrentSchemaContext.class),
+                Mockito.mock(TransactionProvider.class));
+
+        String stringWithoutPrefix =
+                "<rpc xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"0\">\n"
+                        + "  <edit-config>\n"
+                        + "    <target>\n"
+                        + "      <candidate/>\n"
+                        + "    </target>\n"
+                        + "  </edit-config>\n"
+                        + "</rpc>";
+        XmlElement xe = getXmlElement(stringWithoutPrefix);
+        NodeList nodeList = EditConfig.getElementsByTagName(xe, TARGET_KEY);
+        Assert.assertEquals(1, nodeList.getLength());
+
+        String stringWithPrefix =
+                "<nc:rpc xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"0\">\n"
+                        + "  <nc:edit-config>\n"
+                        + "    <nc:target>\n"
+                        + "      <nc:candidate/>\n"
+                        + "    </nc:target>\n"
+                        + "  </nc:edit-config>\n"
+                        + "</nc:rpc>";
+
+        xe = getXmlElement(stringWithPrefix);
+        nodeList = EditConfig.getElementsByTagName(xe, TARGET_KEY);
+        Assert.assertEquals(1, nodeList.getLength());
+
+        String stringWithoutTarget =
+                "<nc:rpc xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"0\">\n"
+                        + "  <nc:edit-config>\n"
+                        + "    <nc:target>\n"
+                        + "    </nc:target>\n"
+                        + "  </nc:edit-config>\n"
+                        + "</nc:rpc>";
+        xe = getXmlElement(stringWithoutTarget);
+
+        try {
+            nodeList = EditConfig.getElementsByTagName(xe, TARGET_KEY);
+            XmlElement.fromDomElement((Element) nodeList.item(0)).getOnlyChildElement();
+            Assert.fail("Not specified target, we should fail");
+        } catch (DocumentedException documentedException) {
+            // Ignore
+        }
+
+    }
+
+    private static XmlElement getXmlElement(final String elementAsString) throws Exception {
+        Document document = XmlUtil.readXmlToDocument(elementAsString);
+        Element element = document.getDocumentElement();
+        return XmlElement.fromDomElement(element);
+    }
+
     @Test
     public void testReplaceMapEntry() throws Exception {
         verifyResponse(edit("messages/mapping/editConfigs/edit-config-replace-map-entry.xml"), RPC_REPLY_OK);
@@ -739,19 +821,6 @@ public class NetconfMDSalMappingTest {
         return response;
     }
 
-    private List<InputStream> getYangSchemas() {
-        final List<String> schemaPaths = Arrays.asList("/META-INF/yang/config.yang",
-                "/yang/mdsal-netconf-mapping-test.yang");
-        final List<InputStream> schemas = new ArrayList<>();
-
-        for (final String schemaPath : schemaPaths) {
-            final InputStream resourceAsStream = getClass().getResourceAsStream(schemaPath);
-            schemas.add(resourceAsStream);
-        }
-
-        return schemas;
-    }
-
     private SchemaService createSchemaService() {
         return new SchemaService() {
 
@@ -792,4 +861,4 @@ public class NetconfMDSalMappingTest {
             }
         };
     }
-}
\ No newline at end of file
+}