Restconf operation GET, PUT, POST tests refactoring 93/4193/4
authorJozef Gloncak <jgloncak@cisco.com>
Fri, 10 Jan 2014 09:05:24 +0000 (10:05 +0100)
committerJozef Gloncak <jgloncak@cisco.com>
Tue, 14 Jan 2014 13:48:50 +0000 (14:48 +0100)
Tests for GET, PUT, POST were refactored. For every operation there is one
class with name Rest<OPERATION_NAME>OperationTest.java and special class
RestOperationUtils for common subrutines.

Change-Id: If8c9b76b86d728b3e35a931b12caeed41eb75805
Signed-off-by: Jozef Gloncak <jgloncak@cisco.com>
19 files changed:
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonIncorrectTopLevelTest.java
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonNotExistingLeafTypeTest.java
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/xml/test/CnSnToXmlNotExistingLeafTypeTest.java
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/DummyMountInstanceImpl.java [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ReadConfAndOperDataTest.java [deleted file]
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestDeleteOperationTest.java [moved from opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/DeleteRestCallTest.java with 85% similarity]
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestGetOperationTest.java [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestOperationUtils.java [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestPostOperationTest.java [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestPutOperationTest.java [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/XmlProvidersTest.java [deleted file]
opendaylight/md-sal/sal-rest-connector/src/test/resources/full-versions/test-data2/data-rpc-input.json [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/resources/full-versions/test-data2/data-rpc-input.xml [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/resources/full-versions/test-data2/data2.xml [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/resources/full-versions/test-module/test-module [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/resources/parts/ietf-interfaces_interfaces.json [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/resources/parts/ietf-interfaces_interfaces_absolute_path.json [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/resources/parts/ietf-interfaces_interfaces_absolute_path.xml [new file with mode: 0644]

index a589f9fca3cd8950d1a7e32068e9ba490b899916..47222571b8d8d927b0a1886db6025c04e5513c02 100644 (file)
@@ -119,12 +119,6 @@ public class CnSnToJsonIncorrectTopLevelTest extends YangAndXmlAndDataSchemaLoad
             return null;
         }
 
-        public YangNode getParent() {
-            // TODO Auto-generated method stub
-            return null;
-        }
-
-        @Override
         public boolean isAddedByUses() {
             // TODO Auto-generated method stub
             return false;
index e2d7f0ce137322e1475ae50c8e3ad7ddd52a7d15..7fdd6cac47e432fb14810eaa4b9dbcb7ab457ada 100644 (file)
@@ -69,9 +69,7 @@ public class CnSnToJsonNotExistingLeafTypeTest extends YangAndXmlAndDataSchemaLo
         leafBuild.setConfiguration(true);
 
         contBuild.addChildNode(leafBuild);
-        // FIXME: build method does not accept any arguments
-        //return contBuild.build(null);
-        return null;
+        return contBuild.build();
     }
 
 }
index 4da354c2c00223d618e575cf8647c342ca827992..555f2b3e3c941e325bb1fbb2844ea3c0e1060d8f 100644 (file)
@@ -63,10 +63,7 @@ public class CnSnToXmlNotExistingLeafTypeTest {
         leafBuild.setConfiguration(true);
 
         contBuild.addChildNode(leafBuild);
-        // FIXME: build method does not accept any arguments
-        //return contBuild.build(null);
-        return null;
-
+        return contBuild.build();
     }
 
 }
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/DummyMountInstanceImpl.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/DummyMountInstanceImpl.java
new file mode 100644 (file)
index 0000000..181aa04
--- /dev/null
@@ -0,0 +1,78 @@
+package org.opendaylight.controller.sal.restconf.impl.test;
+
+import java.util.concurrent.Future;
+
+import org.opendaylight.controller.sal.core.api.data.DataChangeListener;
+import org.opendaylight.controller.sal.core.api.data.DataModificationTransaction;
+import org.opendaylight.controller.sal.core.api.mount.MountInstance;
+import org.opendaylight.controller.sal.core.api.notify.NotificationListener;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.concepts.Registration;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.data.api.CompositeNode;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
+class DummyMountInstanceImpl implements MountInstance {
+
+    SchemaContext schemaContext;
+
+    public static class Builder {
+        SchemaContext schemaContext;
+
+        public Builder setSchemaContext(SchemaContext schemaContext) {
+            this.schemaContext = schemaContext;
+            return this;
+        }
+
+        public MountInstance build() {
+            DummyMountInstanceImpl instance = new DummyMountInstanceImpl();
+            instance.schemaContext = schemaContext;
+            return instance;
+        }
+    }
+
+    @Override
+    public Registration<NotificationListener> addNotificationListener(QName notification, NotificationListener listener) {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public CompositeNode readConfigurationData(InstanceIdentifier path) {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public CompositeNode readOperationalData(InstanceIdentifier path) {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public DataModificationTransaction beginTransaction() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public ListenerRegistration<DataChangeListener> registerDataChangeListener(InstanceIdentifier path,
+            DataChangeListener listener) {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public SchemaContext getSchemaContext() {
+        return schemaContext;
+    }
+
+    @Override
+    public Future<RpcResult<CompositeNode>> rpc(QName type, CompositeNode input) {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+}
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ReadConfAndOperDataTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ReadConfAndOperDataTest.java
deleted file mode 100644 (file)
index f2eea9d..0000000
+++ /dev/null
@@ -1,122 +0,0 @@
-package org.opendaylight.controller.sal.restconf.impl.test;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.mockito.Matchers.any;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-import java.io.FileNotFoundException;
-import java.io.UnsupportedEncodingException;
-import java.net.URI;
-import java.net.URLEncoder;
-import java.util.List;
-import java.util.Set;
-import java.util.logging.Level;
-import java.util.logging.LogRecord;
-
-import javax.ws.rs.core.Application;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
-
-import org.glassfish.jersey.server.ResourceConfig;
-import org.glassfish.jersey.test.JerseyTest;
-import org.glassfish.jersey.test.TestProperties;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
-import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
-import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
-import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
-import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
-import org.opendaylight.yangtools.yang.data.api.CompositeNode;
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.model.api.Module;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-
-import com.google.common.base.Charsets;
-
-public class ReadConfAndOperDataTest extends JerseyTest {
-
-    private static ControllerContext controllerContext;
-    private static BrokerFacade brokerFacade;
-    private static RestconfImpl restconfImpl;
-    private static final MediaType MEDIA_TYPE_DRAFT02 = new MediaType("application", "yang.data+xml");
-
-    @BeforeClass
-    public static void init() throws FileNotFoundException {
-        Set<Module> allModules = TestUtils.loadModulesFrom("/full-versions/yangs");
-        assertNotNull(allModules);
-        SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules);
-        controllerContext = ControllerContext.getInstance();
-        controllerContext.setSchemas(schemaContext);
-        brokerFacade = mock(BrokerFacade.class);
-        restconfImpl = RestconfImpl.getInstance();
-        restconfImpl.setBroker(brokerFacade);
-        restconfImpl.setControllerContext(controllerContext);
-    }
-
-    @Before
-    public void logs() {
-        List<LogRecord> loggedRecords = getLoggedRecords();
-        for (LogRecord l : loggedRecords) {
-            System.out.println(l.getMessage());
-        }
-    }
-
-    @Test
-    public void testReadConfigurationData() throws UnsupportedEncodingException, FileNotFoundException {
-
-        String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
-
-        CompositeNode loadedCompositeNode = TestUtils.readInputToCnSn("/parts/ietf-interfaces_interfaces.xml", true,
-                XmlToCompositeNodeProvider.INSTANCE);
-        when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode);
-
-        Response response = target(uri).request(MEDIA_TYPE_DRAFT02).get();
-        assertEquals(200, response.getStatus());
-
-        uri = createUri("/config/", "ietf-interfaces:interfaces/interface/example");
-        when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(null);
-
-        response = target(uri).request(MEDIA_TYPE_DRAFT02).get();
-        assertEquals(404, response.getStatus());
-    }
-
-    @Test
-    public void testReadOperationalData() throws UnsupportedEncodingException, FileNotFoundException {
-        String uri = createUri("/operational/", "ietf-interfaces:interfaces/interface/eth0");
-
-        CompositeNode loadedCompositeNode = TestUtils.readInputToCnSn("/parts/ietf-interfaces_interfaces.xml", true,
-                XmlToCompositeNodeProvider.INSTANCE);
-
-        when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode);
-
-        Response response = target(uri).request(MEDIA_TYPE_DRAFT02).get();
-        assertEquals(200, response.getStatus());
-
-        uri = createUri("/config/", "ietf-interfaces:interfaces/interface/example");
-        when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(null);
-
-        response = target(uri).request(MEDIA_TYPE_DRAFT02).get();
-        assertEquals(404, response.getStatus());
-    }
-
-    private String createUri(String prefix, String encodedPart) throws UnsupportedEncodingException {
-        return URI.create(prefix + URLEncoder.encode(encodedPart, Charsets.US_ASCII.name()).toString()).toASCIIString();
-    }
-
-    @Override
-    protected Application configure() {
-        enable(TestProperties.LOG_TRAFFIC);
-        enable(TestProperties.DUMP_ENTITY);
-        enable(TestProperties.RECORD_LOG_LEVEL);
-        set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
-
-        ResourceConfig resourceConfig = new ResourceConfig();
-        resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
-                XmlToCompositeNodeProvider.INSTANCE);
-        return resourceConfig;
-    }
-}
@@ -4,19 +4,15 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
+import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.createUri;
 
 import java.io.FileNotFoundException;
-import java.io.InputStream;
 import java.io.UnsupportedEncodingException;
-import java.net.URI;
-import java.net.URLEncoder;
 import java.util.Set;
 import java.util.concurrent.Future;
 import java.util.logging.Level;
 
-import javax.ws.rs.client.Entity;
 import javax.ws.rs.core.Application;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
@@ -28,20 +24,16 @@ import org.junit.BeforeClass;
 import org.junit.Test;
 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
-import org.opendaylight.controller.sal.rest.impl.XmlMapper;
 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
 import org.opendaylight.yangtools.yang.common.RpcResult;
-import org.opendaylight.yangtools.yang.data.api.CompositeNode;
 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 
-import com.google.common.base.Charsets;
-
-public class DeleteRestCallTest extends JerseyTest {
+public class RestDeleteOperationTest extends JerseyTest {
 
     private static ControllerContext controllerContext;
     private static BrokerFacade brokerFacade;
@@ -61,6 +53,22 @@ public class DeleteRestCallTest extends JerseyTest {
         restconfImpl.setControllerContext(controllerContext);
     }
 
+    @Override
+    protected Application configure() {
+        /* enable/disable Jersey logs to console */
+        /*
+         * enable(TestProperties.LOG_TRAFFIC);
+         */
+        enable(TestProperties.DUMP_ENTITY);
+        enable(TestProperties.RECORD_LOG_LEVEL);
+        set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
+
+        ResourceConfig resourceConfig = new ResourceConfig();
+        resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
+                XmlToCompositeNodeProvider.INSTANCE);
+        return resourceConfig;
+    }
+
     @Test
     public void testDeleteConfigurationData() throws UnsupportedEncodingException, FileNotFoundException {
         String uri2 = createUri("/config/", "test-interface:interfaces");
@@ -82,20 +90,4 @@ public class DeleteRestCallTest extends JerseyTest {
         assertEquals(500, response.getStatus());
     }
 
-    private String createUri(String prefix, String encodedPart) throws UnsupportedEncodingException {
-        return URI.create(prefix + URLEncoder.encode(encodedPart, Charsets.US_ASCII.name()).toString()).toASCIIString();
-    }
-
-    @Override
-    protected Application configure() {
-        enable(TestProperties.LOG_TRAFFIC);
-        enable(TestProperties.DUMP_ENTITY);
-        enable(TestProperties.RECORD_LOG_LEVEL);
-        set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
-
-        ResourceConfig resourceConfig = new ResourceConfig();
-        resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
-                XmlToCompositeNodeProvider.INSTANCE);
-        return resourceConfig;
-    }
 }
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestGetOperationTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestGetOperationTest.java
new file mode 100644 (file)
index 0000000..d997a8a
--- /dev/null
@@ -0,0 +1,307 @@
+package org.opendaylight.controller.sal.restconf.impl.test;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.JSON;
+import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.XML;
+import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.createUri;
+
+import java.io.FileNotFoundException;
+import java.io.UnsupportedEncodingException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.logging.Level;
+
+import javax.ws.rs.core.Application;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import org.glassfish.jersey.server.ResourceConfig;
+import org.glassfish.jersey.test.JerseyTest;
+import org.glassfish.jersey.test.TestProperties;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.opendaylight.controller.sal.core.api.mount.MountService;
+import org.opendaylight.controller.sal.rest.api.Draft01;
+import org.opendaylight.controller.sal.rest.api.Draft02;
+import org.opendaylight.controller.sal.rest.api.RestconfService;
+import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
+import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
+import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
+import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
+import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
+import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
+import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
+import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
+import org.opendaylight.controller.sal.restconf.impl.SimpleNodeWrapper;
+import org.opendaylight.yangtools.yang.data.api.CompositeNode;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.Node;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
+public class RestGetOperationTest extends JerseyTest {
+
+    private static BrokerFacade brokerFacade;
+    private static RestconfImpl restconfImpl;
+    private static SchemaContext schemaContextYangsIetf;
+    private static SchemaContext schemaContextTestModule;
+
+    @BeforeClass
+    public static void init() throws FileNotFoundException {
+        schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
+        schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
+        ControllerContext controllerContext = ControllerContext.getInstance();
+        controllerContext.setSchemas(schemaContextYangsIetf);
+        brokerFacade = mock(BrokerFacade.class);
+        restconfImpl = RestconfImpl.getInstance();
+        restconfImpl.setBroker(brokerFacade);
+        restconfImpl.setControllerContext(controllerContext);
+    }
+
+    @Before
+    public void logs() {
+        /* enable/disable Jersey logs to console */
+        /*
+         * List<LogRecord> loggedRecords = getLoggedRecords(); for (LogRecord l
+         * : loggedRecords) { System.out.println(l.getMessage()); }
+         */
+    }
+
+    @Override
+    protected Application configure() {
+        /* enable/disable Jersey logs to console */
+        /*
+         * enable(TestProperties.LOG_TRAFFIC);
+         */
+        enable(TestProperties.DUMP_ENTITY);
+        enable(TestProperties.RECORD_LOG_LEVEL);
+        set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
+
+        ResourceConfig resourceConfig = new ResourceConfig();
+        resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
+                StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
+                JsonToCompositeNodeProvider.INSTANCE);
+        return resourceConfig;
+    }
+
+    /**
+     * Tests {@link RestconfImpl#readData() readAllData()} method of
+     * RestconfImpl with url {@code "/datastore/ identifier}"}. Status codes 200
+     * is tested.
+     */
+    @Test
+    public void getDatastoreDataViaUrlTest200() throws FileNotFoundException, UnsupportedEncodingException {
+        mockReadOperationalDataMethod();
+        getDataWithUrl("/datastore/", Draft01.MediaTypes.DATA + JSON, 200);
+        getDataWithUrl("/datastore/", Draft01.MediaTypes.DATA + XML, 200);
+        getDataWithUrl("/datastore/", MediaType.APPLICATION_JSON, 200);
+        getDataWithUrl("/datastore/", MediaType.APPLICATION_XML, 200);
+        getDataWithUrl("/datastore/", MediaType.TEXT_XML, 200);
+    }
+
+    /**
+     * Tests {@link RestconfImpl#readData() readAllData()} method of
+     * RestconfImpl with url {@code "/datastore/ identifier}"}. Status codes 400
+     * is tested.
+     */
+    @Test
+    public void getDatastoreDataViaUrlTest400() throws FileNotFoundException, UnsupportedEncodingException {
+        mockReadOperationalDataMethod();
+        getDataWithUrl("/datastore/", Draft01.MediaTypes.DATA + JSON, 400);
+        getDataWithUrl("/datastore/", Draft01.MediaTypes.DATA + XML, 400);
+        getDataWithUrl("/datastore/", MediaType.APPLICATION_JSON, 400);
+        getDataWithUrl("/datastore/", MediaType.APPLICATION_XML, 400);
+        getDataWithUrl("/datastore/", MediaType.TEXT_XML, 400);
+    }
+
+    /**
+     * Tests {@link RestconfImpl#readOperationalData(String)
+     * readOperationalData(String)} method of RestconfImpl with url
+     * {@code "/operational/...identifier..."}. Status codes 200 is tested.
+     */
+    @Test
+    public void getOperationalDataViaUrl200() throws UnsupportedEncodingException {
+        mockReadOperationalDataMethod();
+        getDataWithUrl("/operational/", Draft02.MediaTypes.DATA + JSON, 200);
+        getDataWithUrl("/operational/", Draft02.MediaTypes.DATA + XML, 200);
+        getDataWithUrl("/operational/", MediaType.APPLICATION_JSON, 200);
+        getDataWithUrl("/operational/", MediaType.APPLICATION_XML, 200);
+        getDataWithUrl("/operational/", MediaType.TEXT_XML, 200);
+    }
+
+    /**
+     * Tests {@link RestconfImpl#readOperationalData(String)
+     * readOperationalData(String)} method of RestconfImpl with url
+     * {@code "/operational/...identifier..."}. Status codes 400 is tested.
+     */
+    @Test
+    public void getOperationalDataViaUrl400() throws UnsupportedEncodingException {
+        mockReadOperationalDataMethod();
+        getDataWithUrl("/operational/", Draft02.MediaTypes.DATA + JSON, 400);
+        getDataWithUrl("/operational/", Draft02.MediaTypes.DATA + XML, 400);
+        getDataWithUrl("/operational/", MediaType.APPLICATION_JSON, 400);
+        getDataWithUrl("/operational/", MediaType.APPLICATION_XML, 400);
+        getDataWithUrl("/operational/", MediaType.TEXT_XML, 400);
+    }
+
+    /**
+     * Tests {@link RestconfImpl#readOperationalData
+     * #readConfigurationData(String) readConfigurationData(String)} method of
+     * RestconfImpl with url {@code "/config/...identifier..."}. Status codes
+     * 200 is tested.
+     */
+    @Test
+    public void getConfigDataViaUrl200() throws UnsupportedEncodingException {
+        mockReadConfigurationDataMethod();
+        getDataWithUrl("/config/", Draft02.MediaTypes.DATA + JSON, 200);
+        getDataWithUrl("/config/", Draft02.MediaTypes.DATA + XML, 200);
+        getDataWithUrl("/config/", MediaType.APPLICATION_JSON, 200);
+        getDataWithUrl("/config/", MediaType.APPLICATION_XML, 200);
+        getDataWithUrl("/config/", MediaType.TEXT_XML, 200);
+    }
+
+    /**
+     * Tests {@link RestconfImpl#readOperationalData
+     * #readConfigurationData(String) readConfigurationData(String)} method of
+     * RestconfImpl with url {@code "/config/...identifier..."}. Status codes
+     * 400 is tested.
+     */
+    @Test
+    public void getConfigDataViaUrl400() throws UnsupportedEncodingException {
+        mockReadConfigurationDataMethod();
+        getDataWithUrl("/config/", Draft02.MediaTypes.DATA + JSON, 400);
+        getDataWithUrl("/config/", Draft02.MediaTypes.DATA + XML, 400);
+        getDataWithUrl("/config/", MediaType.APPLICATION_JSON, 400);
+        getDataWithUrl("/config/", MediaType.APPLICATION_XML, 400);
+        getDataWithUrl("/config/", MediaType.TEXT_XML, 400);
+    }
+
+    /**
+     * Tests {@link RestconfImpl#readAllData() readAllData()} method of
+     * RestconfImpl with url {@code "/datastore"}. Currently the method isn't
+     * supported so it returns 500
+     */
+    @Test
+    public void getDatastoreDataAllTest500() throws UnsupportedEncodingException {
+        getDatastoreAllDataTest(Draft01.MediaTypes.DATASTORE + XML);
+        getDatastoreAllDataTest(Draft01.MediaTypes.DATASTORE + JSON);
+    }
+
+    /**
+     * 
+     * Tests {@link RestconfImpl#getModules getModules} method of RestconfImpl
+     * with uri {@code "/modules"}. Currently the method isn't supported so it
+     * returns 500
+     */
+    @Test
+    public void getModulesDataTest500() throws UnsupportedEncodingException {
+        getModulesDataTest(Draft01.MediaTypes.API + JSON);
+        getModulesDataTest(Draft01.MediaTypes.API + XML);
+        getModulesDataTest(Draft02.MediaTypes.API + JSON);
+        getModulesDataTest(Draft02.MediaTypes.API + XML);
+    }
+
+    /**
+     * Test of request for not existing data. Returning status code 404
+     */
+    @Test
+    public void getDataWithUrlNoExistingDataTest404() throws UnsupportedEncodingException, URISyntaxException {
+        String uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
+
+        when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(null);
+
+        Response response = target(uri).request(Draft01.MediaTypes.DATA + RestconfService.XML).get();
+        assertEquals(404, response.getStatus());
+    }
+
+    /**
+     * MountPoint test. URI represents mount point.
+     */
+    @Test
+    public void getDataWithUrlMountPoint() throws UnsupportedEncodingException, FileNotFoundException,
+            URISyntaxException {
+        when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(
+                prepareCnDataForMountPointTest());
+
+        MountService mockMountService = mock(MountService.class);
+
+        when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(
+                new DummyMountInstanceImpl.Builder().setSchemaContext(schemaContextTestModule).build());
+
+        ControllerContext.getInstance().setMountService(mockMountService);
+
+        String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/0/test-module:cont/cont1");
+        Response response = target(uri).request(Draft02.MediaTypes.DATA + XML).get();
+        assertEquals(200, response.getStatus());
+    }
+
+    private void getDataWithUrl(String mediaTypePrefix, String mediaType, int statusCode)
+            throws UnsupportedEncodingException {
+        String uri = null;
+        switch (statusCode) {
+        case 400:
+            uri = createUri(mediaTypePrefix, "wrong-module:interfaces/interface/eth0");
+            break;
+        case 200:
+            uri = createUri(mediaTypePrefix, "ietf-interfaces:interfaces/interface/eth0");
+            break;
+        }
+        Response response = target(uri).request(mediaType).get();
+        assertEquals("Status is incorrect for media type " + mediaType + ".", statusCode, response.getStatus());
+
+    }
+
+    private void getModulesDataTest(String mediaType) throws UnsupportedEncodingException {
+        String uri = createUri("/modules", "");
+        Response response = target(uri).request(mediaType).get();
+
+        assertEquals("Status is incorrect for media type " + mediaType + ".", 500, response.getStatus());
+    }
+
+    private void getDatastoreAllDataTest(String mediaType) throws UnsupportedEncodingException {
+        String uri = createUri("/datastore", "");
+        Response response = target(uri).request(mediaType).get();
+
+        assertEquals(500, response.getStatus());
+    }
+
+    private CompositeNode prepareCnDataForMountPointTest() throws URISyntaxException {
+        CompositeNodeWrapper cont1 = new CompositeNodeWrapper(new URI("test:module"), "cont1");
+        SimpleNodeWrapper lf11 = new SimpleNodeWrapper(new URI("test:module"), "lf11", "lf11 value");
+        cont1.addValue(lf11);
+        return cont1.unwrap();
+    }
+
+    private CompositeNode prepareCompositeNodeWithIetfInterfacesInterfacesData() {
+        CompositeNode intface;
+        try {
+            intface = new CompositeNodeWrapper(new URI("interface"), "interface");
+            List<Node<?>> childs = new ArrayList<>();
+
+            childs.add(new SimpleNodeWrapper(new URI("name"), "name", "eth0"));
+            childs.add(new SimpleNodeWrapper(new URI("type"), "type", "ethernetCsmacd"));
+            childs.add(new SimpleNodeWrapper(new URI("enabled"), "enabled", Boolean.FALSE));
+            childs.add(new SimpleNodeWrapper(new URI("description"), "description", "some interface"));
+            intface.setValue(childs);
+            return intface;
+        } catch (URISyntaxException e) {
+        }
+
+        return null;
+    }
+
+    private void mockReadOperationalDataMethod() {
+        CompositeNode loadedCompositeNode = prepareCompositeNodeWithIetfInterfacesInterfacesData();
+        when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode);
+    }
+
+    private void mockReadConfigurationDataMethod() {
+        CompositeNode loadedCompositeNode = prepareCompositeNodeWithIetfInterfacesInterfacesData();
+        when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode);
+    }
+}
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestOperationUtils.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestOperationUtils.java
new file mode 100644 (file)
index 0000000..1f0daac
--- /dev/null
@@ -0,0 +1,31 @@
+package org.opendaylight.controller.sal.restconf.impl.test;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URI;
+import java.net.URLEncoder;
+
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.core.MediaType;
+
+import com.google.common.base.Charsets;
+
+public class RestOperationUtils {
+
+    static final String JSON = "+json";
+    static final String XML = "+xml";
+
+    private RestOperationUtils() {
+    }
+
+    static Entity<String> entity(String data, MediaType mediaType) {
+        return Entity.entity(data, mediaType);
+    }
+
+    static Entity<String> entity(String data, String mediaType) {
+        return Entity.entity(data, mediaType);
+    }
+
+    static String createUri(String prefix, String encodedPart) throws UnsupportedEncodingException {
+        return URI.create(prefix + URLEncoder.encode(encodedPart, Charsets.US_ASCII.name()).toString()).toASCIIString();
+    }
+}
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestPostOperationTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestPostOperationTest.java
new file mode 100644 (file)
index 0000000..9bf6a0f
--- /dev/null
@@ -0,0 +1,384 @@
+package org.opendaylight.controller.sal.restconf.impl.test;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.JSON;
+import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.XML;
+import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.createUri;
+import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.entity;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.concurrent.Future;
+import java.util.logging.Level;
+
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.core.Application;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import org.glassfish.jersey.server.ResourceConfig;
+import org.glassfish.jersey.test.JerseyTest;
+import org.glassfish.jersey.test.TestProperties;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
+import org.opendaylight.controller.sal.core.api.mount.MountService;
+import org.opendaylight.controller.sal.rest.api.Draft01;
+import org.opendaylight.controller.sal.rest.api.Draft02;
+import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
+import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
+import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
+import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
+import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
+import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
+import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
+import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.data.api.CompositeNode;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
+public class RestPostOperationTest extends JerseyTest {
+
+    private static String xmlData;
+    private static String xmlDataAbsolutePath;
+    private static String jsonData;
+    private static String jsonDataAbsolutePath;
+    private static String xmlDataRpcInput;
+    private static CompositeNodeWrapper cnSnDataOutput;
+    private static String jsonDataRpcInput;
+    private static String xmlData2;
+
+    private static ControllerContext controllerContext;
+    private static BrokerFacade brokerFacade;
+    private static RestconfImpl restconfImpl;
+    private static SchemaContext schemaContextYangsIetf;
+    private static SchemaContext schemaContextTestModule;
+
+    @BeforeClass
+    public static void init() throws URISyntaxException, IOException {
+        schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
+        schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
+        controllerContext = ControllerContext.getInstance();
+        brokerFacade = mock(BrokerFacade.class);
+        restconfImpl = RestconfImpl.getInstance();
+        restconfImpl.setBroker(brokerFacade);
+        restconfImpl.setControllerContext(controllerContext);
+        loadData();
+    }
+
+    @Before
+    public void logs() throws IOException, URISyntaxException {
+        /* enable/disable Jersey logs to console */
+        /*
+         * List<LogRecord> loggedRecords = getLoggedRecords(); for (LogRecord l
+         * : loggedRecords) { System.out.println(l.getMessage()); }
+         */
+    }
+
+    @Override
+    protected Application configure() {
+        /* enable/disable Jersey logs to console */
+
+        /*
+         * enable(TestProperties.LOG_TRAFFIC);
+         */
+        enable(TestProperties.DUMP_ENTITY);
+        enable(TestProperties.RECORD_LOG_LEVEL);
+        set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
+
+        ResourceConfig resourceConfig = new ResourceConfig();
+        resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
+                StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
+                JsonToCompositeNodeProvider.INSTANCE);
+        return resourceConfig;
+    }
+
+    @Test
+    public void postOperationsDataViaUrl200() throws URISyntaxException, IOException {
+        controllerContext.setSchemas(schemaContextTestModule);
+        postOperationsDataViaUrl(Draft02.MediaTypes.DATA + JSON, cnSnDataOutput, jsonDataRpcInput, 200);
+        postOperationsDataViaUrl(Draft01.MediaTypes.DATA + JSON, cnSnDataOutput, jsonDataRpcInput, 200);
+        postOperationsDataViaUrl(MediaType.APPLICATION_JSON, cnSnDataOutput, jsonDataRpcInput, 200);
+
+        postOperationsDataViaUrl(Draft01.MediaTypes.DATA + XML, cnSnDataOutput, xmlDataRpcInput, 200);
+        postOperationsDataViaUrl(Draft02.MediaTypes.DATA + XML, cnSnDataOutput, xmlDataRpcInput, 200);
+        postOperationsDataViaUrl(MediaType.APPLICATION_XML, cnSnDataOutput, xmlDataRpcInput, 200);
+        postOperationsDataViaUrl(MediaType.TEXT_XML, cnSnDataOutput, xmlDataRpcInput, 200);
+    }
+
+    @Test
+    public void postOperationsDataViaUrl204() throws URISyntaxException, IOException {
+        controllerContext.setSchemas(schemaContextTestModule);
+        postOperationsDataViaUrl(Draft02.MediaTypes.DATA + JSON, cnSnDataOutput, jsonDataRpcInput, 204);
+        postOperationsDataViaUrl(Draft01.MediaTypes.DATA + JSON, cnSnDataOutput, jsonDataRpcInput, 204);
+        postOperationsDataViaUrl(MediaType.APPLICATION_JSON, cnSnDataOutput, jsonDataRpcInput, 204);
+
+        postOperationsDataViaUrl(Draft01.MediaTypes.DATA + XML, cnSnDataOutput, xmlDataRpcInput, 204);
+        postOperationsDataViaUrl(Draft02.MediaTypes.DATA + XML, cnSnDataOutput, xmlDataRpcInput, 204);
+        postOperationsDataViaUrl(MediaType.APPLICATION_XML, cnSnDataOutput, xmlDataRpcInput, 204);
+        postOperationsDataViaUrl(MediaType.TEXT_XML, cnSnDataOutput, xmlDataRpcInput, 204);
+    }
+
+    @Test
+    public void postOperationsDataViaUrl500() throws URISyntaxException, IOException {
+        controllerContext.setSchemas(schemaContextTestModule);
+        postOperationsDataViaUrl(Draft02.MediaTypes.DATA + JSON, cnSnDataOutput, jsonDataRpcInput, 500);
+        postOperationsDataViaUrl(Draft01.MediaTypes.DATA + JSON, cnSnDataOutput, jsonDataRpcInput, 500);
+        postOperationsDataViaUrl(MediaType.APPLICATION_JSON, cnSnDataOutput, jsonDataRpcInput, 500);
+
+        postOperationsDataViaUrl(Draft01.MediaTypes.DATA + XML, cnSnDataOutput, xmlDataRpcInput, 500);
+        postOperationsDataViaUrl(Draft02.MediaTypes.DATA + XML, cnSnDataOutput, xmlDataRpcInput, 500);
+        postOperationsDataViaUrl(MediaType.APPLICATION_XML, cnSnDataOutput, xmlDataRpcInput, 500);
+        postOperationsDataViaUrl(MediaType.TEXT_XML, cnSnDataOutput, xmlDataRpcInput, 500);
+    }
+
+    @Test
+    public void postOperationsDataViaUrl400() throws URISyntaxException, IOException {
+        controllerContext.setSchemas(schemaContextTestModule);
+        postOperationsDataViaUrl(Draft02.MediaTypes.DATA + JSON, cnSnDataOutput, jsonDataRpcInput, 400);
+        postOperationsDataViaUrl(Draft01.MediaTypes.DATA + JSON, cnSnDataOutput, jsonDataRpcInput, 400);
+        postOperationsDataViaUrl(MediaType.APPLICATION_JSON, cnSnDataOutput, jsonDataRpcInput, 400);
+
+        postOperationsDataViaUrl(Draft01.MediaTypes.DATA + XML, cnSnDataOutput, xmlDataRpcInput, 400);
+        postOperationsDataViaUrl(Draft02.MediaTypes.DATA + XML, cnSnDataOutput, xmlDataRpcInput, 400);
+        postOperationsDataViaUrl(MediaType.APPLICATION_XML, cnSnDataOutput, xmlDataRpcInput, 400);
+        postOperationsDataViaUrl(MediaType.TEXT_XML, cnSnDataOutput, xmlDataRpcInput, 400);
+    }
+
+    @Test
+    public void postOperationsDataViaUrl404() throws URISyntaxException, IOException {
+        controllerContext.setSchemas(schemaContextTestModule);
+        postOperationsDataViaUrl(Draft02.MediaTypes.DATA + JSON, cnSnDataOutput, jsonDataRpcInput, 404);
+        postOperationsDataViaUrl(Draft01.MediaTypes.DATA + JSON, cnSnDataOutput, jsonDataRpcInput, 404);
+        postOperationsDataViaUrl(MediaType.APPLICATION_JSON, cnSnDataOutput, jsonDataRpcInput, 404);
+
+        postOperationsDataViaUrl(Draft01.MediaTypes.DATA + XML, cnSnDataOutput, xmlDataRpcInput, 404);
+        postOperationsDataViaUrl(Draft02.MediaTypes.DATA + XML, cnSnDataOutput, xmlDataRpcInput, 404);
+        postOperationsDataViaUrl(MediaType.APPLICATION_XML, cnSnDataOutput, xmlDataRpcInput, 404);
+        postOperationsDataViaUrl(MediaType.TEXT_XML, cnSnDataOutput, xmlDataRpcInput, 404);
+    }
+
+    @Test
+    public void postConfigDataViaUrlConfigOnlyTest204() throws UnsupportedEncodingException, FileNotFoundException {
+        controllerContext.setSchemas(schemaContextYangsIetf);
+        mockCommitConfigurationDataPostMethod(TransactionStatus.COMMITED);
+        postDataViaUrlTest("/config", "", Draft02.MediaTypes.DATA + JSON, jsonDataAbsolutePath, 204);
+        postDataViaUrlTest("/config", "", Draft02.MediaTypes.DATA + XML, xmlDataAbsolutePath, 204);
+        postDataViaUrlTest("/config", "", MediaType.APPLICATION_JSON, jsonDataAbsolutePath, 204);
+        postDataViaUrlTest("/config", "", MediaType.APPLICATION_XML, xmlDataAbsolutePath, 204);
+        postDataViaUrlTest("/config", "", MediaType.TEXT_XML, xmlDataAbsolutePath, 204);
+    }
+
+    @Test
+    public void postConfigDataViaUrlConfigOnlyTest202() throws UnsupportedEncodingException, FileNotFoundException {
+        controllerContext.setSchemas(schemaContextYangsIetf);
+        mockCommitConfigurationDataPostMethod(null);
+        postDataViaUrlTest("/config", "", Draft02.MediaTypes.DATA + JSON, jsonDataAbsolutePath, 202);
+        postDataViaUrlTest("/config", "", Draft02.MediaTypes.DATA + XML, xmlDataAbsolutePath, 202);
+        postDataViaUrlTest("/config", "", MediaType.APPLICATION_JSON, jsonDataAbsolutePath, 202);
+        postDataViaUrlTest("/config", "", MediaType.APPLICATION_XML, xmlDataAbsolutePath, 202);
+        postDataViaUrlTest("/config", "", MediaType.TEXT_XML, xmlDataAbsolutePath, 202);
+    }
+
+    @Test
+    public void postConfigDataViaUrlConfigOnlyTest500() throws UnsupportedEncodingException, FileNotFoundException {
+        controllerContext.setSchemas(schemaContextYangsIetf);
+        mockCommitConfigurationDataPostMethod(TransactionStatus.FAILED);
+        postDataViaUrlTest("/config", "", Draft02.MediaTypes.DATA + JSON, jsonDataAbsolutePath, 500);
+        postDataViaUrlTest("/config", "", Draft02.MediaTypes.DATA + XML, xmlDataAbsolutePath, 500);
+        postDataViaUrlTest("/config", "", MediaType.APPLICATION_JSON, jsonDataAbsolutePath, 500);
+        postDataViaUrlTest("/config", "", MediaType.APPLICATION_XML, xmlDataAbsolutePath, 500);
+        postDataViaUrlTest("/config", "", MediaType.TEXT_XML, xmlDataAbsolutePath, 500);
+    }
+
+    @Test
+    public void postConfigDataViaUrlTest204() throws UnsupportedEncodingException {
+        controllerContext.setSchemas(schemaContextYangsIetf);
+        mockCommitConfigurationDataPostMethod(TransactionStatus.COMMITED);
+        String urlPath = "ietf-interfaces:interfaces";
+        postDataViaUrlTest("/config/", urlPath, Draft02.MediaTypes.DATA + JSON, jsonData, 204);
+        postDataViaUrlTest("/config/", urlPath, Draft02.MediaTypes.DATA + XML, xmlData, 204);
+        postDataViaUrlTest("/config/", urlPath, MediaType.APPLICATION_JSON, jsonData, 204);
+        postDataViaUrlTest("/config/", urlPath, MediaType.APPLICATION_XML, xmlData, 204);
+        postDataViaUrlTest("/config/", urlPath, MediaType.TEXT_XML, xmlData, 204);
+    }
+
+    @Test
+    public void postConfigDataViaUrlTest202() throws UnsupportedEncodingException {
+        controllerContext.setSchemas(schemaContextYangsIetf);
+        mockCommitConfigurationDataPostMethod(null);
+        String urlPath = "ietf-interfaces:interfaces";
+        postDataViaUrlTest("/config/", urlPath, Draft02.MediaTypes.DATA + JSON, jsonData, 202);
+        postDataViaUrlTest("/config/", urlPath, Draft02.MediaTypes.DATA + XML, xmlData, 202);
+        postDataViaUrlTest("/config/", urlPath, MediaType.APPLICATION_JSON, jsonData, 202);
+        postDataViaUrlTest("/config/", urlPath, MediaType.APPLICATION_XML, xmlData, 202);
+        postDataViaUrlTest("/config/", urlPath, MediaType.TEXT_XML, xmlData, 202);
+    }
+
+    @Test
+    public void postConfigDataViaUrlTest500() throws UnsupportedEncodingException {
+        controllerContext.setSchemas(schemaContextYangsIetf);
+        mockCommitConfigurationDataPostMethod(TransactionStatus.FAILED);
+        String urlPath = "ietf-interfaces:interfaces";
+        postDataViaUrlTest("/config/", urlPath, Draft02.MediaTypes.DATA + JSON, jsonData, 500);
+        postDataViaUrlTest("/config/", urlPath, Draft02.MediaTypes.DATA + XML, xmlData, 500);
+        postDataViaUrlTest("/config/", urlPath, MediaType.APPLICATION_JSON, jsonData, 500);
+        postDataViaUrlTest("/config/", urlPath, MediaType.APPLICATION_XML, xmlData, 500);
+        postDataViaUrlTest("/config/", urlPath, MediaType.TEXT_XML, xmlData, 500);
+    }
+
+    @Test
+    public void postDatastoreDataViaUrlTest204() throws UnsupportedEncodingException {
+        controllerContext.setSchemas(schemaContextYangsIetf);
+        mockCommitConfigurationDataPostMethod(TransactionStatus.COMMITED);
+        String urlPath = "ietf-interfaces:interfaces";
+        postDataViaUrlTest("/datastore/", urlPath, Draft01.MediaTypes.DATA + JSON, jsonData, 204);
+        postDataViaUrlTest("/datastore/", urlPath, Draft01.MediaTypes.DATA + XML, xmlData, 204);
+        postDataViaUrlTest("/datastore/", urlPath, MediaType.APPLICATION_JSON, jsonData, 204);
+        postDataViaUrlTest("/datastore/", urlPath, MediaType.APPLICATION_XML, xmlData, 204);
+        postDataViaUrlTest("/datastore/", urlPath, MediaType.TEXT_XML, xmlData, 204);
+    }
+
+    @Test
+    public void postDatastoreDataViaUrlTest202() throws UnsupportedEncodingException {
+        controllerContext.setSchemas(schemaContextYangsIetf);
+        mockCommitConfigurationDataPostMethod(null);
+        String urlPath = "ietf-interfaces:interfaces";
+        postDataViaUrlTest("/datastore/", urlPath, Draft01.MediaTypes.DATA + JSON, jsonData, 202);
+        postDataViaUrlTest("/datastore/", urlPath, Draft01.MediaTypes.DATA + XML, xmlData, 202);
+        postDataViaUrlTest("/datastore/", urlPath, MediaType.APPLICATION_JSON, jsonData, 202);
+        postDataViaUrlTest("/datastore/", urlPath, MediaType.APPLICATION_XML, xmlData, 202);
+        postDataViaUrlTest("/datastore/", urlPath, MediaType.TEXT_XML, xmlData, 202);
+    }
+
+    @Test
+    public void postDatastoreDataViaUrlTest500() throws UnsupportedEncodingException {
+        controllerContext.setSchemas(schemaContextYangsIetf);
+        mockCommitConfigurationDataPostMethod(TransactionStatus.FAILED);
+        String urlPath = "ietf-interfaces:interfaces";
+        postDataViaUrlTest("/datastore/", urlPath, Draft01.MediaTypes.DATA + JSON, jsonData, 500);
+        postDataViaUrlTest("/datastore/", urlPath, Draft01.MediaTypes.DATA + XML, xmlData, 500);
+        postDataViaUrlTest("/datastore/", urlPath, MediaType.APPLICATION_JSON, jsonData, 500);
+        postDataViaUrlTest("/datastore/", urlPath, MediaType.APPLICATION_XML, xmlData, 500);
+        postDataViaUrlTest("/datastore/", urlPath, MediaType.TEXT_XML, xmlData, 500);
+    }
+
+    @Test
+    public void postDataViaUrlMountPoint() throws UnsupportedEncodingException {
+        controllerContext.setSchemas(schemaContextYangsIetf);
+        mockCommitConfigurationDataPostMethod(TransactionStatus.COMMITED);
+
+        MountService mockMountService = mock(MountService.class);
+        SchemaContext otherSchemaContext = schemaContextTestModule;
+        when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(
+                new DummyMountInstanceImpl.Builder().setSchemaContext(otherSchemaContext).build());
+
+        ControllerContext.getInstance().setMountService(mockMountService);
+
+        String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/0/test-module:cont/cont1");
+        Response response = target(uri).request(Draft02.MediaTypes.DATA + XML).post(
+                entity(xmlData2, Draft02.MediaTypes.DATA + XML));
+        // 204 code is returned when COMMITED transaction status is put as input
+        // to mock method
+        assertEquals(204, response.getStatus());
+    }
+
+    private void postDataViaUrlTest(String urlPrefix, String urlPath, String mediaType, String data, int responseStatus)
+            throws UnsupportedEncodingException {
+        String url = createUri(urlPrefix, urlPath);
+        Response response = target(url).request(mediaType).post(entity(data, mediaType));
+        assertEquals(responseStatus, response.getStatus());
+    }
+
+    private void mockCommitConfigurationDataPostMethod(TransactionStatus statusName) {
+        RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(statusName)
+                .build();
+        Future<RpcResult<TransactionStatus>> dummyFuture = null;
+        if (statusName != null) {
+            dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
+        } else {
+            dummyFuture = DummyFuture.builder().build();
+        }
+
+        when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
+                .thenReturn(dummyFuture);
+    }
+
+    private static CompositeNodeWrapper prepareCnSnRpcOutput() throws URISyntaxException {
+        CompositeNodeWrapper cnSnDataOutput = new CompositeNodeWrapper(new URI("test:module"), "output");
+        CompositeNodeWrapper cont = new CompositeNodeWrapper(new URI("test:module"), "cont-output");
+        cnSnDataOutput.addValue(cont);
+        cnSnDataOutput.unwrap();
+        return cnSnDataOutput;
+    }
+
+    private void mockInvokeRpc(CompositeNode compositeNode, boolean sucessful) {
+        RpcResult<CompositeNode> rpcResult = new DummyRpcResult.Builder<CompositeNode>().result(compositeNode)
+                .isSuccessful(sucessful).build();
+        when(brokerFacade.invokeRpc(any(QName.class), any(CompositeNode.class))).thenReturn(rpcResult);
+    }
+
+    private void postOperationsDataViaUrl(String mediaType, CompositeNode cnSnDataOut, String dataIn, int statusCode)
+            throws FileNotFoundException, UnsupportedEncodingException {
+        String url = createUri("/operations/", "test-module:rpc-test");
+        Response response = null;
+        switch (statusCode) {
+        case 200:
+            mockInvokeRpc(cnSnDataOut, true);
+            break;
+        case 204:
+            mockInvokeRpc(null, true);
+            break;
+        case 500:
+            mockInvokeRpc(null, false);
+            break;
+        case 400:
+            response = target(url).request(mediaType).post(Entity.entity("{}", mediaType));
+            break;
+        case 404:
+            url = createUri("/operations/", "test-module:rpc-wrongtest");
+            break;
+        }
+        response = response == null ? target(url).request(mediaType).post(Entity.entity(dataIn, mediaType)) : response;
+        assertEquals(statusCode, response.getStatus());
+    }
+
+    private static void loadData() throws IOException, URISyntaxException {
+
+        InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
+        xmlData = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
+
+        xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces_absolute_path.xml");
+        xmlDataAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
+
+        String jsonPath = RestconfImplTest.class.getResource("/parts/ietf-interfaces_interfaces.json").getPath();
+        jsonData = TestUtils.loadTextFile(jsonPath);
+
+        String jsonFullPath = RestconfImplTest.class
+                .getResource("/parts/ietf-interfaces_interfaces_absolute_path.json").getPath();
+        jsonDataAbsolutePath = TestUtils.loadTextFile(jsonFullPath);
+
+        String xmlPathRpcInput = RestconfImplTest.class.getResource("/full-versions/test-data2/data-rpc-input.xml")
+                .getPath();
+        xmlDataRpcInput = TestUtils.loadTextFile(xmlPathRpcInput);
+        cnSnDataOutput = prepareCnSnRpcOutput();
+
+        String jsonPathToRpcInput = RestconfImplTest.class.getResource("/full-versions/test-data2/data-rpc-input.json")
+                .getPath();
+        jsonDataRpcInput = TestUtils.loadTextFile(jsonPathToRpcInput);
+
+        String data2Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data2.xml").getPath();
+        xmlData2 = TestUtils.loadTextFile(data2Input);
+
+    }
+}
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestPutOperationTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestPutOperationTest.java
new file mode 100644 (file)
index 0000000..2bef9ba
--- /dev/null
@@ -0,0 +1,210 @@
+package org.opendaylight.controller.sal.restconf.impl.test;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.JSON;
+import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.XML;
+import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.createUri;
+import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.entity;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+import java.net.URISyntaxException;
+import java.util.concurrent.Future;
+import java.util.logging.Level;
+
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.core.Application;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import org.glassfish.jersey.server.ResourceConfig;
+import org.glassfish.jersey.test.JerseyTest;
+import org.glassfish.jersey.test.TestProperties;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
+import org.opendaylight.controller.sal.core.api.mount.MountService;
+import org.opendaylight.controller.sal.rest.api.Draft01;
+import org.opendaylight.controller.sal.rest.api.Draft02;
+import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
+import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
+import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
+import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
+import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
+import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
+import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
+import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.data.api.CompositeNode;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
+public class RestPutOperationTest extends JerseyTest {
+
+    private static String xmlData;
+    private static String jsonData;
+
+    private static BrokerFacade brokerFacade;
+    private static RestconfImpl restconfImpl;
+    private static SchemaContext schemaContextYangsIetf;
+    private static SchemaContext schemaContextTestModule;
+
+    @BeforeClass
+    public static void init() throws IOException {
+        schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
+        schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
+        ControllerContext controllerContext = ControllerContext.getInstance();
+        controllerContext.setSchemas(schemaContextYangsIetf);
+        brokerFacade = mock(BrokerFacade.class);
+        restconfImpl = RestconfImpl.getInstance();
+        restconfImpl.setBroker(brokerFacade);
+        restconfImpl.setControllerContext(controllerContext);
+        loadData();
+    }
+
+    @Before
+    public void logs() throws IOException {
+        /* enable/disable Jersey logs to console */
+        /*
+         * List<LogRecord> loggedRecords = getLoggedRecords(); for (LogRecord l
+         * : loggedRecords) { System.out.println(l.getMessage()); }
+         */
+    }
+
+    @Override
+    protected Application configure() {
+        /* enable/disable Jersey logs to console */
+        /*
+         * enable(TestProperties.LOG_TRAFFIC);
+         */
+        enable(TestProperties.DUMP_ENTITY);
+        enable(TestProperties.RECORD_LOG_LEVEL);
+        set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
+
+        ResourceConfig resourceConfig = new ResourceConfig();
+        resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
+                StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
+                JsonToCompositeNodeProvider.INSTANCE);
+        return resourceConfig;
+    }
+
+    /**
+     * Test method
+     * {@link RestconfImpl#updateConfigurationData(String, CompositeNode)} of
+     * RestconfImpl for "/config/...identifier..." URL. Return status code is
+     * 200.
+     * 
+     */
+    @Test
+    public void putConfigDataViaUrlTest200() throws UnsupportedEncodingException {
+        mockCommitConfigurationDataPutMethod(TransactionStatus.COMMITED);
+        putDataViaUrlTest("/config/", Draft02.MediaTypes.DATA + JSON, jsonData, 200);
+        putDataViaUrlTest("/config/", Draft02.MediaTypes.DATA + XML, xmlData, 200);
+        putDataViaUrlTest("/config/", MediaType.APPLICATION_JSON, jsonData, 200);
+        putDataViaUrlTest("/config/", MediaType.APPLICATION_XML, xmlData, 200);
+        putDataViaUrlTest("/config/", MediaType.TEXT_XML, xmlData, 200);
+
+    }
+
+    /**
+     * Test method
+     * {@link RestconfImpl#updateConfigurationData(String, CompositeNode)} of
+     * RestconfImpl for "/config/...identifier..." URL. Return status code is
+     * 500.
+     * 
+     */
+    @Test
+    public void putConfigDataViaUrlTest500() throws UnsupportedEncodingException {
+        mockCommitConfigurationDataPutMethod(TransactionStatus.FAILED);
+        putDataViaUrlTest("/config/", Draft02.MediaTypes.DATA + JSON, jsonData, 500);
+        putDataViaUrlTest("/config/", Draft02.MediaTypes.DATA + XML, xmlData, 500);
+        putDataViaUrlTest("/config/", MediaType.APPLICATION_JSON, jsonData, 500);
+        putDataViaUrlTest("/config/", MediaType.APPLICATION_XML, xmlData, 500);
+        putDataViaUrlTest("/config/", MediaType.TEXT_XML, xmlData, 500);
+
+    }
+
+    /**
+     * Test method
+     * {@link RestconfImpl#updateConfigurationData(String, CompositeNode)} of
+     * RestconfImpl for "/datastore/...identifier..." URL. Return status code is
+     * 200.
+     * 
+     */
+    @Test
+    public void putDatastoreDataViaUrlTest200() throws UnsupportedEncodingException {
+        mockCommitConfigurationDataPutMethod(TransactionStatus.COMMITED);
+        putDataViaUrlTest("/datastore/", Draft01.MediaTypes.DATA + JSON, jsonData, 200);
+        putDataViaUrlTest("/datastore/", Draft01.MediaTypes.DATA + XML, xmlData, 200);
+        putDataViaUrlTest("/datastore/", MediaType.APPLICATION_JSON, jsonData, 200);
+        putDataViaUrlTest("/datastore/", MediaType.APPLICATION_XML, xmlData, 200);
+        putDataViaUrlTest("/datastore/", MediaType.TEXT_XML, xmlData, 200);
+    }
+
+    /**
+     * Test method
+     * {@link RestconfImpl#updateConfigurationData(String, CompositeNode)} of
+     * RestconfImpl for "/datastore/...identifier..." URL. Return status code is
+     * 500.
+     * 
+     */
+    @Test
+    public void putDatastoreDataViaUrlTest500() throws UnsupportedEncodingException {
+        mockCommitConfigurationDataPutMethod(TransactionStatus.FAILED);
+        putDataViaUrlTest("/datastore/", Draft01.MediaTypes.DATA + JSON, jsonData, 500);
+        putDataViaUrlTest("/datastore/", Draft01.MediaTypes.DATA + XML, xmlData, 500);
+        putDataViaUrlTest("/datastore/", MediaType.APPLICATION_JSON, jsonData, 500);
+        putDataViaUrlTest("/datastore/", MediaType.APPLICATION_XML, xmlData, 500);
+        putDataViaUrlTest("/datastore/", MediaType.TEXT_XML, xmlData, 500);
+    }
+
+    @Test
+    public void testRpcResultCommitedToStatusCodesWithMountPoint() throws UnsupportedEncodingException,
+            FileNotFoundException, URISyntaxException {
+
+        mockCommitConfigurationDataPutMethod(TransactionStatus.COMMITED);
+
+        InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/full-versions/test-data2/data2.xml");
+        String xml = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
+        Entity<String> entity = Entity.entity(xml, Draft02.MediaTypes.DATA + XML);
+
+        MountService mockMountService = mock(MountService.class);
+        when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(
+                new DummyMountInstanceImpl.Builder().setSchemaContext(schemaContextTestModule).build());
+
+        ControllerContext.getInstance().setMountService(mockMountService);
+
+        String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/0/test-module:cont");
+        Response response = target(uri).request(Draft02.MediaTypes.DATA + XML).put(entity);
+        assertEquals(200, response.getStatus());
+    }
+
+    private void putDataViaUrlTest(String uriPrefix, String mediaType, String data, int responseStatus)
+            throws UnsupportedEncodingException {
+        String uri = createUri(uriPrefix, "ietf-interfaces:interfaces/interface/eth0");
+        Response response = target(uri).request(mediaType).put(entity(data, mediaType));
+        assertEquals(responseStatus, response.getStatus());
+    }
+
+    private static void loadData() throws IOException {
+        InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
+        xmlData = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
+
+        String jsonPath = RestconfImplTest.class.getResource("/parts/ietf-interfaces_interfaces.json").getPath();
+        jsonData = TestUtils.loadTextFile(jsonPath);
+    }
+
+    private void mockCommitConfigurationDataPutMethod(TransactionStatus statusName) {
+        RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(statusName)
+                .build();
+        Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
+        when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class)))
+                .thenReturn(dummyFuture);
+    }
+
+}
index 3427fbde38f1a7682a838e5fc80c01ecc8df6752..5ef66c3b257da4af2811a9e40b7c5d1908b4f474 100644 (file)
@@ -6,9 +6,11 @@ import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
+import java.io.BufferedReader;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileNotFoundException;
+import java.io.FileReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStreamWriter;
@@ -276,4 +278,18 @@ public final class TestUtils {
 
         return byteArrayOS.toString();
     }
+
+    public static String loadTextFile(String filePath) throws IOException {
+        FileReader fileReader = new FileReader(filePath);
+        BufferedReader bufReader = new BufferedReader(fileReader);
+
+        String line = null;
+        StringBuilder result = new StringBuilder();
+        while ((line = bufReader.readLine()) != null) {
+            result.append(line);
+        }
+        bufReader.close();
+        return result.toString();
+
+    }
 }
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/XmlProvidersTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/XmlProvidersTest.java
deleted file mode 100644 (file)
index 7cce34f..0000000
+++ /dev/null
@@ -1,231 +0,0 @@
-package org.opendaylight.controller.sal.restconf.impl.test;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.mockito.Matchers.any;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-import java.io.FileNotFoundException;
-import java.io.InputStream;
-import java.io.UnsupportedEncodingException;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.net.URLEncoder;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Set;
-import java.util.concurrent.Future;
-import java.util.logging.Level;
-import java.util.logging.LogRecord;
-
-import javax.ws.rs.client.Entity;
-import javax.ws.rs.core.Application;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
-
-import org.glassfish.jersey.server.ResourceConfig;
-import org.glassfish.jersey.test.JerseyTest;
-import org.glassfish.jersey.test.TestProperties;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
-import org.opendaylight.controller.sal.rest.api.Draft01;
-import org.opendaylight.controller.sal.rest.api.RestconfService;
-import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
-import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
-import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
-import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
-import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
-import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
-import org.opendaylight.controller.sal.restconf.impl.SimpleNodeWrapper;
-import org.opendaylight.yangtools.yang.common.RpcResult;
-import org.opendaylight.yangtools.yang.data.api.CompositeNode;
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.api.Node;
-import org.opendaylight.yangtools.yang.model.api.Module;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-
-import com.google.common.base.Charsets;
-
-public class XmlProvidersTest extends JerseyTest {
-
-    private static ControllerContext controllerContext;
-    private static BrokerFacade brokerFacade;
-    private static RestconfImpl restconfImpl;
-    private static final MediaType MEDIA_TYPE = new MediaType("application", "vnd.yang.data+xml");
-    private static final MediaType MEDIA_TYPE_DRAFT02 = new MediaType("application", "yang.data+xml");
-
-    @BeforeClass
-    public static void init() throws FileNotFoundException {
-        Set<Module> allModules = TestUtils.loadModulesFrom("/full-versions/yangs");
-        assertNotNull(allModules);
-        SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules);
-        controllerContext = ControllerContext.getInstance();
-        controllerContext.setSchemas(schemaContext);
-        brokerFacade = mock(BrokerFacade.class);
-        restconfImpl = RestconfImpl.getInstance();
-        restconfImpl.setBroker(brokerFacade);
-        restconfImpl.setControllerContext(controllerContext);
-    }
-
-    @Before
-    public void logs() {
-        List<LogRecord> loggedRecords = getLoggedRecords();
-        for (LogRecord l : loggedRecords) {
-            System.out.println(l.getMessage());
-        }
-    }
-
-    @Test
-    public void testStructuredDataToXmlProvider() throws FileNotFoundException, UnsupportedEncodingException {
-        String uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
-
-        CompositeNode loadedCompositeNode = prepareCompositeNodeWithIetfInterfacesInterfacesData();
-        when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode);
-
-        Response response = target(uri).request(MEDIA_TYPE).get();
-        assertEquals(200, response.getStatus());
-    }
-
-    private CompositeNode prepareCompositeNodeWithIetfInterfacesInterfacesData() {
-        CompositeNode intface;
-        try {
-            intface = new CompositeNodeWrapper(new URI("interface"), "interface");
-            List<Node<?>> childs = new ArrayList<>();
-
-            childs.add(new SimpleNodeWrapper(new URI("name"), "name", "eth0"));
-            childs.add(new SimpleNodeWrapper(new URI("type"), "type", "ethernetCsmacd"));
-            childs.add(new SimpleNodeWrapper(new URI("enabled"), "enabled", Boolean.FALSE));
-            childs.add(new SimpleNodeWrapper(new URI("description"), "description", "some interface"));
-            intface.setValue(childs);
-            return intface;
-        } catch (URISyntaxException e) {
-        }
-
-        return null;
-    }
-
-    @Test
-    public void testBadFormatXmlToCompositeNodeProvider() throws UnsupportedEncodingException, URISyntaxException {
-        String uri = createUri("/operations/", "ietf-interfaces:interfaces/interface/eth0");
-
-        Response response = target(uri).request(Draft01.MediaTypes.DATA + RestconfService.XML).post(
-                Entity.entity("<SimpleNode/>", MEDIA_TYPE));
-        assertEquals(400, response.getStatus());
-
-        response = target(uri).request(Draft01.MediaTypes.DATA + RestconfService.XML).post(
-                Entity.entity("<SimpleNode>", MEDIA_TYPE));
-        assertEquals(400, response.getStatus());
-    }
-
-    @Test
-    public void testXmlToCompositeNode404NotFound() throws UnsupportedEncodingException, URISyntaxException {
-        String uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
-
-        when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(null);
-
-        Response response = target(uri).request(Draft01.MediaTypes.DATA + RestconfService.XML).get();
-        assertEquals(404, response.getStatus());
-    }
-
-    @Test
-    public void testXmlToCompositeNode400() throws UnsupportedEncodingException, URISyntaxException {
-        String uri = createUri("/datastore/", "simple-nodes:user/name");
-
-        when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(null);
-
-        Response response = target(uri).request(Draft01.MediaTypes.DATA + RestconfService.XML).get();
-        assertEquals(400, response.getStatus());
-    }
-
-    @Test
-    public void testRpcResultCommitedToStatusCodes() throws UnsupportedEncodingException {
-        InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
-        String xml = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
-        Entity<String> entity = Entity.entity(xml, MEDIA_TYPE_DRAFT02);
-        RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
-                TransactionStatus.COMMITED).build();
-        Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
-        when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class)))
-                .thenReturn(dummyFuture);
-        when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
-                .thenReturn(dummyFuture);
-
-        String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
-        Response response = target(uri).request(MEDIA_TYPE_DRAFT02).put(entity);
-        assertEquals(200, response.getStatus());
-
-        uri = createUri("/config/", "ietf-interfaces:interfaces");
-        response = target(uri).request(MEDIA_TYPE_DRAFT02).post(entity);
-        assertEquals(204, response.getStatus());
-
-        uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
-        response = target(uri).request(MEDIA_TYPE_DRAFT02).put(entity);
-        assertEquals(200, response.getStatus());
-
-        uri = createUri("/config/", "ietf-interfaces:interfaces");
-        response = target(uri).request(MEDIA_TYPE_DRAFT02).post(entity);
-        assertEquals(204, response.getStatus());
-
-        uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
-        entity = Entity.entity(xml, MEDIA_TYPE);
-        response = target(uri).request(MEDIA_TYPE).put(entity);
-        assertEquals(200, response.getStatus());
-
-        uri = createUri("/datastore/", "ietf-interfaces:interfaces");
-        entity = Entity.entity(xml, MEDIA_TYPE);
-        response = target(uri).request(MEDIA_TYPE).post(entity);
-        assertEquals(204, response.getStatus());
-    }
-
-    @Test
-    public void testRpcResultOtherToStatusCodes() throws UnsupportedEncodingException {
-        InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
-        String xml = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
-        Entity<String> entity = Entity.entity(xml, MEDIA_TYPE_DRAFT02);
-        RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
-                TransactionStatus.FAILED).build();
-        Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
-        when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class)))
-                .thenReturn(dummyFuture);
-
-        String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
-        Response response = target(uri).request(MEDIA_TYPE_DRAFT02).put(entity);
-        assertEquals(500, response.getStatus());
-        response = target(uri).request(MEDIA_TYPE_DRAFT02).post(entity);
-        assertEquals(500, response.getStatus());
-
-        uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
-        response = target(uri).request(MEDIA_TYPE_DRAFT02).put(entity);
-        assertEquals(500, response.getStatus());
-        response = target(uri).request(MEDIA_TYPE_DRAFT02).post(entity);
-        assertEquals(500, response.getStatus());
-
-        uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
-        entity = Entity.entity(xml, MEDIA_TYPE);
-        response = target(uri).request().put(entity);
-        assertEquals(500, response.getStatus());
-        response = target(uri).request().accept(MEDIA_TYPE).post(entity);
-        assertEquals(500, response.getStatus());
-    }
-
-    private String createUri(String prefix, String encodedPart) throws UnsupportedEncodingException {
-        return URI.create(prefix + URLEncoder.encode(encodedPart, Charsets.US_ASCII.name()).toString()).toASCIIString();
-    }
-
-    @Override
-    protected Application configure() {
-        enable(TestProperties.LOG_TRAFFIC);
-        enable(TestProperties.DUMP_ENTITY);
-        enable(TestProperties.RECORD_LOG_LEVEL);
-        set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
-
-        ResourceConfig resourceConfig = new ResourceConfig();
-        resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
-                XmlToCompositeNodeProvider.INSTANCE);
-        return resourceConfig;
-    }
-
-}
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/full-versions/test-data2/data-rpc-input.json b/opendaylight/md-sal/sal-rest-connector/src/test/resources/full-versions/test-data2/data-rpc-input.json
new file mode 100644 (file)
index 0000000..0eae37a
--- /dev/null
@@ -0,0 +1,10 @@
+{
+    "test-module:input":{
+        "cont":{
+            "cont1":{
+                "lf11":"lf1 data",
+                "lf12":"lf2 data"
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/full-versions/test-data2/data-rpc-input.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/full-versions/test-data2/data-rpc-input.xml
new file mode 100644 (file)
index 0000000..5954c09
--- /dev/null
@@ -0,0 +1,8 @@
+<input xmlns="test:module">
+       <cont>
+           <cont1>
+               <lf11>lf1 data</lf11>
+               <lf12>lf2 data</lf12>
+           </cont1>
+       </cont>
+</input>
\ No newline at end of file
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/full-versions/test-data2/data2.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/full-versions/test-data2/data2.xml
new file mode 100644 (file)
index 0000000..8fbc0a9
--- /dev/null
@@ -0,0 +1,6 @@
+<cont xmlns="test:module">
+    <cont1>
+           <lf11>lf1 data</lf11>
+           <lf12>lf2 data</lf12>
+    </cont1>
+</cont>
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/full-versions/test-module/test-module b/opendaylight/md-sal/sal-rest-connector/src/test/resources/full-versions/test-module/test-module
new file mode 100644 (file)
index 0000000..ad0716b
--- /dev/null
@@ -0,0 +1,43 @@
+module test-module {
+  namespace "test:module";
+  prefix tstmod;
+
+  revision 2014-01-09 {
+  }
+  
+  container cont {
+    container cont1 {
+        leaf lf11 {
+            type string;
+        }
+        leaf lf12 {
+            type string;
+        }
+    }
+  } 
+  
+  
+  rpc rpc-test {
+    input {
+      container cont {
+        container cont1 {
+            leaf lf11 {
+                type string;
+            }
+            leaf lf12 {
+                type string;
+            }
+        }
+      }    
+    }
+    output {
+        container cont-output {
+        }
+    } 
+  
+  }
+  
+  
+
+}
\ No newline at end of file
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/parts/ietf-interfaces_interfaces.json b/opendaylight/md-sal/sal-rest-connector/src/test/resources/parts/ietf-interfaces_interfaces.json
new file mode 100644 (file)
index 0000000..0b39dc7
--- /dev/null
@@ -0,0 +1,10 @@
+{
+    "interface":[
+        {
+            "name":"eth0",
+            "type":"ethernetCsmacd",
+            "enabled":false,
+            "description": "some interface"
+        }
+    ]
+}
\ No newline at end of file
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/parts/ietf-interfaces_interfaces_absolute_path.json b/opendaylight/md-sal/sal-rest-connector/src/test/resources/parts/ietf-interfaces_interfaces_absolute_path.json
new file mode 100644 (file)
index 0000000..7de7fac
--- /dev/null
@@ -0,0 +1,12 @@
+{
+    "ietf-interfaces:interfaces":{
+        "interface":[
+            {
+                "name":"eth0",
+                "type":"ethernetCsmacd",
+                "enabled":false,
+                "description": "some interface"
+            }
+        ]
+    }
+}
\ No newline at end of file
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/parts/ietf-interfaces_interfaces_absolute_path.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/parts/ietf-interfaces_interfaces_absolute_path.xml
new file mode 100644 (file)
index 0000000..7e3aa97
--- /dev/null
@@ -0,0 +1,8 @@
+<interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces" >
+       <interface>
+         <name>eth0</name>
+         <type>ethernetCsmacd</type>
+         <enabled>false</enabled>
+         <description>some interface</description>
+       </interface>
+</interfaces>
\ No newline at end of file