Fix preparing of action request
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / schema / mapping / NetconfMessageTransformerTest.java
index 5214d8f77be0cd869f674e0d95a076244d9c1abd..02d8ddc3b40b0ba0dbfcbd2d4ba8b7325751da77 100644 (file)
@@ -32,10 +32,11 @@ import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTr
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
@@ -48,10 +49,10 @@ import org.custommonkey.xmlunit.XMLUnit;
 import org.hamcrest.CoreMatchers;
 import org.junit.Before;
 import org.junit.Test;
-import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
 import org.opendaylight.mdsal.dom.api.DOMActionResult;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
+import org.opendaylight.mdsal.dom.api.DOMRpcResult;
 import org.opendaylight.netconf.api.NetconfMessage;
 import org.opendaylight.netconf.api.xml.XmlUtil;
 import org.opendaylight.netconf.sal.connect.netconf.schema.NetconfRemoteSchemaYangSourceProvider;
@@ -213,7 +214,7 @@ public class NetconfMessageTransformerTest {
                 NetconfRemoteSchemaYangSourceProvider
                         .createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
 
-        final Map<QName, Object> keys = Maps.newHashMap();
+        final Map<QName, Object> keys = new HashMap<>();
         for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> value : values) {
             keys.put(value.getNodeType(), value.getValue());
         }
@@ -293,7 +294,7 @@ public class NetconfMessageTransformerTest {
                 NetconfRemoteSchemaYangSourceProvider
                         .createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
 
-        final Map<QName, Object> keys = Maps.newHashMap();
+        final Map<QName, Object> keys = new HashMap<>();
         for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> value : values) {
             keys.put(value.getNodeType(), value.getValue());
         }
@@ -308,7 +309,7 @@ public class NetconfMessageTransformerTest {
                 .nodeWithKey(Schema.QNAME, keys).build();
         final DataContainerChild<?, ?> editConfigStructure =
                 createEditConfigStructure(BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS.getSchemaContext(), id,
-                    com.google.common.base.Optional.absent(), com.google.common.base.Optional.fromNullable(schemaNode));
+                    Optional.empty(), Optional.ofNullable(schemaNode));
 
         final DataContainerChild<?, ?> target = NetconfBaseOps.getTargetNode(NETCONF_CANDIDATE_QNAME);
 
@@ -399,7 +400,8 @@ public class NetconfMessageTransformerTest {
         QName reset = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "reset");
         QName start = QName.create(reset, "start");
         QName open = QName.create(start, "open");
-        Set<QName> qnames = new HashSet<>(Arrays.asList(reset, start, open));
+        QName enable = QName.create(open, "enable");
+        Set<QName> qnames = new HashSet<>(Arrays.asList(reset, start, open, enable));
         Set<ActionDefinition> actions = actionNetconfMessageTransformer.getActions();
         assertTrue(!actions.isEmpty());
         for (ActionDefinition actionDefinition : actions) {
@@ -414,9 +416,9 @@ public class NetconfMessageTransformerTest {
         QName qname = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "server");
         QName nameQname = QName.create(qname, "name");
         QName actionResetQName = QName.create(qname, "reset");
-
-        Set<PathArgument> nodeIdentifiers =
-                Collections.singleton(new NodeIdentifierWithPredicates(qname, nameQname, "test"));
+        List<PathArgument> nodeIdentifiers = new ArrayList<>();
+        nodeIdentifiers.add(new NodeIdentifier(qname));
+        nodeIdentifiers.add(new NodeIdentifierWithPredicates(qname, nameQname, "test"));
         DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
 
         ContainerNode data = initInputAction(QName.create(qname, "reset-at"), "now");
@@ -443,7 +445,7 @@ public class NetconfMessageTransformerTest {
         QName qname = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "device");
         QName actionStartQName = QName.create(qname, "start");
 
-        Set<PathArgument> nodeIdentifiers = Collections.singleton(NodeIdentifier.create(qname));
+        List<PathArgument> nodeIdentifiers = Collections.singletonList(NodeIdentifier.create(qname));
         DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
 
         NormalizedNode<?, ?> payload = initInputAction(QName.create(qname, "start-at"), "now");
@@ -464,7 +466,7 @@ public class NetconfMessageTransformerTest {
         QName boxInQName = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "box-in");
         QName actionOpenQName = QName.create(boxOutQName, "open");
 
-        Set<PathArgument> nodeIdentifiers = new HashSet<>();
+        List<PathArgument> nodeIdentifiers = new ArrayList<>();
         nodeIdentifiers.add(NodeIdentifier.create(boxOutQName));
         nodeIdentifiers.add(NodeIdentifier.create(boxInQName));
 
@@ -486,6 +488,42 @@ public class NetconfMessageTransformerTest {
         checkNode(action, null, actionOpenQName.getLocalName(), null);
     }
 
+    @Test
+    public void toActionRequestListInContainerTest() {
+        QName qnameDevice = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "device");
+        QName qnameInterface = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "interface");
+        QName nameQname = QName.create(qnameInterface, "name");
+        QName actionEnableQName = QName.create(qnameInterface, "enable");
+
+        List<PathArgument> nodeIdentifiers = new ArrayList<>();
+        nodeIdentifiers.add(NodeIdentifier.create(qnameDevice));
+        nodeIdentifiers.add(NodeIdentifier.create(qnameInterface));
+        nodeIdentifiers.add(new NodeIdentifierWithPredicates(qnameInterface, nameQname, "test"));
+
+        DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
+
+        NormalizedNode<?, ?> payload = initEmptyInputAction(qnameInterface);
+        NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
+                SchemaPath.create(true, actionEnableQName), domDataTreeIdentifier, payload);
+
+        Node childAction = checkBasePartOfActionRequest(actionRequest);
+
+        Node childDevice = childAction.getFirstChild();
+        checkNode(childDevice, "device", "device", qnameDevice.getNamespace().toString());
+
+        Node childInterface = childDevice.getFirstChild();
+        checkNode(childInterface, "interface", "interface", qnameInterface.getNamespace().toString());
+
+        Node childName = childInterface.getFirstChild();
+        checkNode(childName, "name", "name", nameQname.getNamespace().toString());
+
+        Node childTest = childName.getFirstChild();
+        assertEquals(childTest.getNodeValue(), "test");
+
+        Node action = childInterface.getLastChild();
+        checkNode(action, null, actionEnableQName.getLocalName(), null);
+    }
+
     @SuppressWarnings({ "rawtypes", "unchecked" })
     @Test
     public void toActionResultTest() throws Exception {
@@ -532,7 +570,7 @@ public class NetconfMessageTransformerTest {
         return childAction;
     }
 
-    private static DOMDataTreeIdentifier prepareDataTreeId(final Set<PathArgument> nodeIdentifiers) {
+    private static DOMDataTreeIdentifier prepareDataTreeId(final List<PathArgument> nodeIdentifiers) {
         YangInstanceIdentifier yangInstanceIdentifier =
                 YangInstanceIdentifier.builder().append(nodeIdentifiers).build();
         DOMDataTreeIdentifier domDataTreeIdentifier =
@@ -550,6 +588,11 @@ public class NetconfMessageTransformerTest {
         return data;
     }
 
+    private static ContainerNode initEmptyInputAction(final QName qname) {
+        return ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(
+                QName.create(qname, "input"))).build();
+    }
+
     private static void checkNode(final Node childServer, final String expectedLocalName, final String expectedNodeName,
             final String expectedNamespace) {
         assertNotNull(childServer);