Added test for MouontPoints and URI
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / RestGetOperationTest.java
index d997a8afb19d60854f58bb2802ee4f5ad850da61..681444efd4b472561e4c4edd9275161c30b6e0aa 100644 (file)
@@ -4,7 +4,6 @@ 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;
 
@@ -20,16 +19,15 @@ 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.MountInstance;
 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;
@@ -50,6 +48,7 @@ public class RestGetOperationTest extends JerseyTest {
     private static RestconfImpl restconfImpl;
     private static SchemaContext schemaContextYangsIetf;
     private static SchemaContext schemaContextTestModule;
+    private static CompositeNode answerFromGet;
 
     @BeforeClass
     public static void init() throws FileNotFoundException {
@@ -61,27 +60,16 @@ public class RestGetOperationTest extends JerseyTest {
         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()); }
-         */
+        answerFromGet = prepareCompositeNodeWithIetfInterfacesInterfacesData();
     }
 
     @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());
-
+//         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,
@@ -90,184 +78,90 @@ public class RestGetOperationTest extends JerseyTest {
     }
 
     /**
-     * Tests {@link RestconfImpl#readData() readAllData()} method of
-     * RestconfImpl with url {@code "/datastore/ identifier}"}. Status codes 200
-     * is tested.
+     * Tests of status codes for "/datastore/{identifier}".
      */
     @Test
-    public void getDatastoreDataViaUrlTest200() throws FileNotFoundException, UnsupportedEncodingException {
+    public void getDatastoreStatusCodes() 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);
-    }
+        String uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
+        assertEquals(200, get(uri, MediaType.APPLICATION_XML));
 
-    /**
-     * 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);
-    }
+        uri = createUri("/datastore/", "wrong-module:interfaces/interface/eth0");
+        assertEquals(400, get(uri, MediaType.APPLICATION_XML));
 
-    /**
-     * 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);
+        // Test of request for not existing data. Returning status code 404
+        uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
+        when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(null);
+        assertEquals(404, get(uri, MediaType.APPLICATION_XML));
     }
 
     /**
-     * Tests {@link RestconfImpl#readOperationalData(String)
-     * readOperationalData(String)} method of RestconfImpl with url
-     * {@code "/operational/...identifier..."}. Status codes 400 is tested.
+     * Tests of status codes for "/operational/{identifier}".
      */
     @Test
-    public void getOperationalDataViaUrl400() throws UnsupportedEncodingException {
+    public void getOperationalStatusCodes() 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);
-    }
+        String uri = createUri("/operational/", "ietf-interfaces:interfaces/interface/eth0");
+        assertEquals(200, get(uri, MediaType.APPLICATION_XML));
 
-    /**
-     * 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);
+        uri = createUri("/operational/", "wrong-module:interfaces/interface/eth0");
+        assertEquals(400, get(uri, MediaType.APPLICATION_XML));
     }
 
     /**
-     * Tests {@link RestconfImpl#readOperationalData
-     * #readConfigurationData(String) readConfigurationData(String)} method of
-     * RestconfImpl with url {@code "/config/...identifier..."}. Status codes
-     * 400 is tested.
+     * Tests of status codes for "/config/{identifier}".
      */
     @Test
-    public void getConfigDataViaUrl400() throws UnsupportedEncodingException {
+    public void getConfigStatusCodes() 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");
+        String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
+        assertEquals(200, get(uri, MediaType.APPLICATION_XML));
 
-        when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(null);
-
-        Response response = target(uri).request(Draft01.MediaTypes.DATA + RestconfService.XML).get();
-        assertEquals(404, response.getStatus());
+        uri = createUri("/config/", "wrong-module:interfaces/interface/eth0");
+        assertEquals(400, get(uri, MediaType.APPLICATION_XML));
     }
 
     /**
      * MountPoint test. URI represents mount point.
      */
     @Test
-    public void getDataWithUrlMountPoint() throws UnsupportedEncodingException, FileNotFoundException,
-            URISyntaxException {
-        when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(
-                prepareCnDataForMountPointTest());
-
+    public void getDataWithUrlMountPoint() throws UnsupportedEncodingException, URISyntaxException {
+        when(
+                brokerFacade.readConfigurationDataBehindMountPoint(any(MountInstance.class),
+                        any(InstanceIdentifier.class))).thenReturn(prepareCnDataForMountPointTest());
+        MountInstance mountInstance = mock(MountInstance.class);
+        when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
         MountService mockMountService = mock(MountService.class);
-
-        when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(
-                new DummyMountInstanceImpl.Builder().setSchemaContext(schemaContextTestModule).build());
+        when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
 
         ControllerContext.getInstance().setMountService(mockMountService);
+        
+        String uri = createUri("/config/",
+                "ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont/cont1");
+        assertEquals(200, get(uri, MediaType.APPLICATION_XML));
 
-        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());
+        uri = createUri("/config/", "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont/cont1");
+        assertEquals(200, get(uri, MediaType.APPLICATION_XML));
     }
 
-    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());
-
-    }
+    @Test
+    public void getDataMountPointIntoHighestElement() throws UnsupportedEncodingException, URISyntaxException {
+        when(brokerFacade.readConfigurationDataBehindMountPoint(any(MountInstance.class),
+                        any(InstanceIdentifier.class))).thenReturn(prepareCnDataForMountPointTest());
+        MountInstance mountInstance = mock(MountInstance.class);
+        when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
+        MountService mockMountService = mock(MountService.class);
+        when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
 
-    private void getModulesDataTest(String mediaType) throws UnsupportedEncodingException {
-        String uri = createUri("/modules", "");
-        Response response = target(uri).request(mediaType).get();
+        ControllerContext.getInstance().setMountService(mockMountService);
 
-        assertEquals("Status is incorrect for media type " + mediaType + ".", 500, response.getStatus());
+        String uri = createUri("/config/",
+                "ietf-interfaces:interfaces/interface/0/yang-ext:mount/");
+        assertEquals(200, get(uri, MediaType.APPLICATION_XML));
     }
 
-    private void getDatastoreAllDataTest(String mediaType) throws UnsupportedEncodingException {
-        String uri = createUri("/datastore", "");
-        Response response = target(uri).request(mediaType).get();
-
-        assertEquals(500, response.getStatus());
+    private int get(String uri, String mediaType) {
+        return target(uri).request(mediaType).get().getStatus();
     }
 
     private CompositeNode prepareCnDataForMountPointTest() throws URISyntaxException {
@@ -277,7 +171,15 @@ public class RestGetOperationTest extends JerseyTest {
         return cont1.unwrap();
     }
 
-    private CompositeNode prepareCompositeNodeWithIetfInterfacesInterfacesData() {
+    private void mockReadOperationalDataMethod() {
+        when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(answerFromGet);
+    }
+
+    private void mockReadConfigurationDataMethod() {
+        when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(answerFromGet);
+    }
+
+    private static CompositeNode prepareCompositeNodeWithIetfInterfacesInterfacesData() {
         CompositeNode intface;
         try {
             intface = new CompositeNodeWrapper(new URI("interface"), "interface");
@@ -295,13 +197,4 @@ public class RestGetOperationTest extends JerseyTest {
         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);
-    }
 }