From a31767d1148ca13f0707f51d4336a549e8a8b9e9 Mon Sep 17 00:00:00 2001 From: prabathabey Date: Sun, 18 Jan 2015 12:46:14 +0530 Subject: [PATCH] Adding test cases for device repository handling and device management config initialization --- .../core/config/DeviceManagementConfig.java | 2 +- .../config/DeviceManagementRepository.java | 2 +- .../config/datasource/DataSourceConfig.java | 4 +- .../datasource/JNDILookupDefinition.java | 4 +- .../dao/util/DeviceManagementDAOUtil.java | 5 +- .../mgt/core/DeviceManagementConfigTests.java | 95 +++++++++++++++ .../core/DeviceManagementRepositoryTests.java | 35 ++++++ .../mgt/core/TestDeviceManagerService.java | 90 ++++++++++++++ .../carbon/device/mgt/core/TestUtils.java | 54 +++++++++ .../mgt/core/dao/DeviceMgtDAOTestSuite.java | 110 +++++++++--------- .../malformed-cdm-config-no-ds-config.xml | 26 +++++ .../malformed-cdm-config-no-jndi.config.xml | 26 +++++ .../malformed-cdm-config-no-mgt-repo.xml | 26 +++++ .../schema/DeviceManagementConfigSchema.xsd | 31 +++++ .../src/test/resources/testng.xml | 3 +- 15 files changed, 449 insertions(+), 64 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementConfigTests.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepositoryTests.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManagerService.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestUtils.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/malformed-cdm-config-no-ds-config.xml create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/malformed-cdm-config-no-jndi.config.xml create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/malformed-cdm-config-no-mgt-repo.xml create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/schema/DeviceManagementConfigSchema.xsd diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfig.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfig.java index 8735df0fe5..678f20d8f0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfig.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfig.java @@ -28,7 +28,7 @@ public final class DeviceManagementConfig { private DeviceManagementRepository deviceMgtRepository; - @XmlElement(name = "ManagementRepository", nillable = false) + @XmlElement(name = "ManagementRepository", required = true) public DeviceManagementRepository getDeviceMgtRepository() { return deviceMgtRepository; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementRepository.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementRepository.java index b84ef0388f..a6bd91a519 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementRepository.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementRepository.java @@ -29,7 +29,7 @@ public class DeviceManagementRepository { private DataSourceConfig dataSourceConfig; - @XmlElement(name = "DataSourceConfiguration", nillable = false) + @XmlElement(name = "DataSourceConfiguration", required = false) public DataSourceConfig getDataSourceConfig() { return dataSourceConfig; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/datasource/DataSourceConfig.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/datasource/DataSourceConfig.java index f8b07312a1..bc7f98e925 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/datasource/DataSourceConfig.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/datasource/DataSourceConfig.java @@ -21,14 +21,14 @@ import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; /** - * Class for holding data source configuration in cdm-config.xml at parsing with JAXB + * Class for holding data source configuration in malformed-cdm-config-no-mgt-repo.xml at parsing with JAXB */ @XmlRootElement(name = "DataSourceConfiguration") public class DataSourceConfig { private JNDILookupDefinition jndiLookupDefintion; - @XmlElement(name = "JndiLookupDefinition", nillable = true) + @XmlElement(name = "JndiLookupDefinition", required = true) public JNDILookupDefinition getJndiLookupDefintion() { return jndiLookupDefintion; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/datasource/JNDILookupDefinition.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/datasource/JNDILookupDefinition.java index 2d0004f52c..bc85ffb143 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/datasource/JNDILookupDefinition.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/datasource/JNDILookupDefinition.java @@ -27,7 +27,7 @@ public class JNDILookupDefinition { private String jndiName; private List jndiProperties; - @XmlElement(name = "Name", nillable = false) + @XmlElement(name = "Name", required = false) public String getJndiName() { return jndiName; } @@ -36,7 +36,7 @@ public class JNDILookupDefinition { this.jndiName = jndiName; } - @XmlElementWrapper(name = "Environment", nillable = false) + @XmlElementWrapper(name = "Environment", required = false) @XmlElement(name = "Property", nillable = false) public List getJndiProperties() { return jndiProperties; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java index 20fa5e2354..04f03a765a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java @@ -63,7 +63,8 @@ public final class DeviceManagementDAOUtil { } /** - * Get id of the current tenant + * Get id of the current tenant. + * * @return tenant id * @throws DeviceManagementDAOException if an error is observed when getting tenant id */ @@ -94,7 +95,7 @@ public final class DeviceManagementDAOUtil { return (DataSource) InitialContext.doLookup(dataSourceName); } final InitialContext context = new InitialContext(jndiProperties); - return (DataSource) context.doLookup(dataSourceName); + return (DataSource) context.lookup(dataSourceName); } catch (Exception e) { throw new RuntimeException("Error in looking up data source: " + e.getMessage(), e); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementConfigTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementConfigTests.java new file mode 100644 index 0000000000..ee4625c8fc --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementConfigTests.java @@ -0,0 +1,95 @@ +/** + * Copyright (c) 2012, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wso2.carbon.device.mgt.core; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; +import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig; +import org.xml.sax.SAXException; + +import javax.xml.XMLConstants; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; +import javax.xml.validation.Schema; +import javax.xml.validation.SchemaFactory; +import java.io.File; + +public class DeviceManagementConfigTests { + + private static final Log log = LogFactory.getLog(DeviceManagementConfigTests.class); + private static final String MALFORMED_TEST_CONFIG_LOCATION_NO_MGT_REPOSITORY = + "./src/test/resources/config/malformed-cdm-config-no-mgt-repo.xml"; + private static final String MALFORMED_TEST_CONFIG_LOCATION_NO_DS_CONFIG = + "./src/test/resources/config/malformed-cdm-config-no-ds-config.xml"; + private static final String MALFORMED_TEST_CONFIG_LOCATION_NO_JNDI_CONFIG = + "./src/test/resources/config/malformed-cdm-config-no-jndi-config.xml"; + private static final String TEST_CONFIG_SCHEMA_LOCATION = + "./src/test/resources/config/schema/DeviceManagementConfigSchema.xsd"; + + private Schema schema; + + @BeforeClass + private void initSchema() { + File deviceManagementSchemaConfig = new File(DeviceManagementConfigTests.TEST_CONFIG_SCHEMA_LOCATION); + SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); + try { + schema = sf.newSchema(deviceManagementSchemaConfig); + } catch (SAXException e) { + Assert.fail("Invalid schema found", e); + } + } + + @Test() + public void testMandateManagementRepositoryElement() { + File malformedConfig = + new File(DeviceManagementConfigTests.MALFORMED_TEST_CONFIG_LOCATION_NO_MGT_REPOSITORY); + this.validateConfig(malformedConfig); + } + + @Test + public void testMandateDataSourceConfigurationElement() { + File malformedConfig = new File(DeviceManagementConfigTests.MALFORMED_TEST_CONFIG_LOCATION_NO_DS_CONFIG); + this.validateConfig(malformedConfig); + } + + @Test + public void testMandateJndiLookupDefinitionElement() { + File malformedConfig = new File(DeviceManagementConfigTests.MALFORMED_TEST_CONFIG_LOCATION_NO_JNDI_CONFIG); + this.validateConfig(malformedConfig); + } + + private void validateConfig(File malformedConfig) { + try { + JAXBContext ctx = JAXBContext.newInstance(DeviceManagementConfig.class); + Unmarshaller um = ctx.createUnmarshaller(); + um.setSchema(this.getSchema()); + um.unmarshal(malformedConfig); + Assert.assertTrue(false); + } catch (JAXBException e) { + e.printStackTrace(); + Assert.assertTrue(true); + } + } + + private Schema getSchema() { + return schema; + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepositoryTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepositoryTests.java new file mode 100644 index 0000000000..a023665e40 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepositoryTests.java @@ -0,0 +1,35 @@ +/** + * Copyright (c) 2012, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wso2.carbon.device.mgt.core; + +import org.testng.Assert; +import org.testng.annotations.Test; +import org.wso2.carbon.device.mgt.common.spi.DeviceManagerService; + +public class DeviceManagementRepositoryTests { + + @Test + public void testAddDeviceManagementService() { + DeviceManagerService sourceProvider = new TestDeviceManagerService(); + DeviceManagementRepository repository = new DeviceManagementRepository(); + repository.addDeviceManagementProvider(sourceProvider); + + DeviceManagerService targetProvider = + repository.getDeviceManagementProvider(TestDeviceManagerService.DEVICE_TYPE_TEST); + Assert.assertEquals(targetProvider.getProviderType(), sourceProvider.getProviderType()); + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManagerService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManagerService.java new file mode 100644 index 0000000000..8d217b5eb4 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManagerService.java @@ -0,0 +1,90 @@ +/** + * Copyright (c) 2012, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wso2.carbon.device.mgt.core; + +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.OperationManager; +import org.wso2.carbon.device.mgt.common.spi.DeviceManagerService; + +import java.util.List; + +public class TestDeviceManagerService implements DeviceManagerService { + + public static final String DEVICE_TYPE_TEST = "Test"; + + @Override + public String getProviderType() { + return TestDeviceManagerService.DEVICE_TYPE_TEST; + } + + @Override + public boolean enrollDevice(Device device) throws DeviceManagementException { + return false; + } + + @Override + public boolean modifyEnrollment(Device device) throws DeviceManagementException { + return false; + } + + @Override + public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { + return false; + } + + @Override + public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException { + return false; + } + + @Override + public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException { + return false; + } + + @Override + public boolean setActive(DeviceIdentifier deviceId, boolean status) throws DeviceManagementException { + return false; + } + + @Override + public List getAllDevices() throws DeviceManagementException { + return null; + } + + @Override + public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException { + return null; + } + + @Override + public boolean updateDeviceInfo(Device device) throws DeviceManagementException { + return false; + } + + @Override + public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType) throws DeviceManagementException { + return false; + } + + @Override + public OperationManager getOperationManager() throws DeviceManagementException { + return null; + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestUtils.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestUtils.java new file mode 100644 index 0000000000..a12560cc33 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestUtils.java @@ -0,0 +1,54 @@ +/** + * Copyright (c) 2012, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wso2.carbon.device.mgt.core; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +public class TestUtils { + + private static final Log log = LogFactory.getLog(TestUtils.class); + + public static void cleanupResources(Connection conn, Statement stmt, ResultSet rs) { + if (rs != null) { + try { + rs.close(); + } catch (SQLException e) { + log.warn("Error occurred while closing result set", e); + } + } + if (stmt != null) { + try { + stmt.close(); + } catch (SQLException e) { + log.warn("Error occurred while closing prepared statement", e); + } + } + if (conn != null) { + try { + conn.close(); + } catch (SQLException e) { + log.warn("Error occurred while closing database connection", e); + } + } + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DeviceMgtDAOTestSuite.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DeviceMgtDAOTestSuite.java index 98bb0d4372..93d29a7757 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DeviceMgtDAOTestSuite.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DeviceMgtDAOTestSuite.java @@ -25,6 +25,7 @@ import org.testng.annotations.Parameters; import org.testng.annotations.Test; import org.w3c.dom.Document; import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.core.TestUtils; import org.wso2.carbon.device.mgt.core.common.DBTypes; import org.wso2.carbon.device.mgt.core.common.TestDBConfiguration; import org.wso2.carbon.device.mgt.core.common.TestDBConfigurations; @@ -40,42 +41,33 @@ import javax.xml.bind.Unmarshaller; import java.io.File; import java.sql.*; import java.util.Date; -import java.util.Iterator; public class DeviceMgtDAOTestSuite { - private TestDBConfiguration testDBConfiguration; - private DeviceType devType = new DeviceType(); - private Connection conn = null; - private Statement stmt = null; - @BeforeClass @Parameters("dbType") public void setUpDB(String dbTypeStr) throws Exception { - DBTypes dbType = DBTypes.valueOf(dbTypeStr); - testDBConfiguration = getTestDBConfiguration(dbType); + TestDBConfiguration dbConfig = getTestDBConfiguration(dbType); switch (dbType) { - case H2: - createH2DB(testDBConfiguration); - BasicDataSource testDataSource = new BasicDataSource(); - testDataSource.setDriverClassName(testDBConfiguration.getDriverClass()); - testDataSource.setUrl(testDBConfiguration.getConnectionUrl()); - testDataSource.setUsername(testDBConfiguration.getUserName()); - testDataSource.setPassword(testDBConfiguration.getPwd()); - DeviceManagementDAOFactory.init(testDataSource); - default: + case H2: + createH2DB(dbConfig); + BasicDataSource testDataSource = new BasicDataSource(); + testDataSource.setDriverClassName(dbConfig.getDriverClass()); + testDataSource.setUrl(dbConfig.getConnectionUrl()); + testDataSource.setUsername(dbConfig.getUserName()); + testDataSource.setPassword(dbConfig.getPwd()); + DeviceManagementDAOFactory.init(testDataSource); + default: } } private TestDBConfiguration getTestDBConfiguration(DBTypes dbType) throws DeviceManagementDAOException, DeviceManagementException { - File deviceMgtConfig = new File("src/test/resources/testdbconfig.xml"); - Document doc = null; - testDBConfiguration = null; - TestDBConfigurations testDBConfigurations = null; + Document doc; + TestDBConfigurations dbConfigs; doc = DeviceManagerUtil.convertToDocument(deviceMgtConfig); JAXBContext testDBContext = null; @@ -83,92 +75,100 @@ public class DeviceMgtDAOTestSuite { try { testDBContext = JAXBContext.newInstance(TestDBConfigurations.class); Unmarshaller unmarshaller = testDBContext.createUnmarshaller(); - testDBConfigurations = (TestDBConfigurations) unmarshaller.unmarshal(doc); + dbConfigs = (TestDBConfigurations) unmarshaller.unmarshal(doc); } catch (JAXBException e) { throw new DeviceManagementDAOException("Error parsing test db configurations", e); } - Iterator itrDBConfigs = testDBConfigurations.getDbTypesList().iterator(); - while (itrDBConfigs.hasNext()) { - testDBConfiguration = itrDBConfigs.next(); - if (testDBConfiguration.getDbType().equals(dbType.toString())) { - break; + for (TestDBConfiguration config : dbConfigs.getDbTypesList()) { + if (config.getDbType().equals(dbType.toString())) { + return config; } } - - return testDBConfiguration; + return null; } private void createH2DB(TestDBConfiguration testDBConf) throws Exception { - - Class.forName(testDBConf.getDriverClass()); - conn = DriverManager.getConnection(testDBConf.getConnectionUrl()); - stmt = conn.createStatement(); - stmt.executeUpdate("RUNSCRIPT FROM './src/test/resources/sql/CreateH2TestDB.sql'"); - stmt.close(); - conn.close(); - + Connection conn = null; + Statement stmt = null; + try { + Class.forName(testDBConf.getDriverClass()); + conn = DriverManager.getConnection(testDBConf.getConnectionUrl()); + stmt = conn.createStatement(); + stmt.executeUpdate("RUNSCRIPT FROM './src/test/resources/sql/CreateH2TestDB.sql'"); + } finally { + TestUtils.cleanupResources(conn, stmt, null); + } } @Test public void addDeviceTypeTest() throws DeviceManagementDAOException, DeviceManagementException { - DeviceTypeDAO deviceTypeMgtDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); - devType.setName("IOS"); + DeviceType deviceType = new DeviceType(); + deviceType.setName("IOS"); - deviceTypeMgtDAO.addDeviceType(devType); + deviceTypeMgtDAO.addDeviceType(deviceType); Long deviceTypeId = null; + Connection conn = null; + Statement stmt = null; try { conn = DeviceManagementDAOFactory.getDataSource().getConnection(); stmt = conn.createStatement(); - ResultSet resultSet = stmt.executeQuery("SELECT ID,NAME from DM_DEVICE_TYPE DType where DType.NAME='IOS'"); + ResultSet resultSet = + stmt.executeQuery("SELECT ID,NAME from DM_DEVICE_TYPE DType where DType.NAME='IOS'"); while (resultSet.next()) { deviceTypeId = resultSet.getLong(1); } - conn.close(); } catch (SQLException sqlEx) { throw new DeviceManagementDAOException("error in fetch device type by name IOS", sqlEx); + } finally { + TestUtils.cleanupResources(conn, stmt, null); } Assert.assertNotNull(deviceTypeId, "Device Type Id is null"); - devType.setId(deviceTypeId); + deviceType.setId(deviceTypeId); } - @Test(dependsOnMethods = { "addDeviceTypeTest" }) + @Test(dependsOnMethods = {"addDeviceTypeTest"}) public void addDeviceTest() throws DeviceManagementDAOException, DeviceManagementException { - DeviceDAO deviceMgtDAO = DeviceManagementDAOFactory.getDeviceDAO(); Device device = new Device(); - device.setDateOfEnrollment(new Date().getTime()); device.setDateOfLastUpdate(new Date().getTime()); device.setDescription("test description"); device.setStatus(Status.ACTIVE); device.setDeviceIdentificationId("111"); - device.setDeviceType(devType.getId().intValue()); + + DeviceType deviceType = new DeviceType(); + deviceType.setId(Long.parseLong("1")); + + device.setDeviceType(deviceType.getId().intValue()); device.setOwnerShip(OwnerShip.BYOD.toString()); device.setOwnerId("111"); device.setTenantId(-1234); deviceMgtDAO.addDevice(device); Long deviceId = null; + Connection conn = null; + PreparedStatement stmt = null; + ResultSet rs = null; try { conn = DeviceManagementDAOFactory.getDataSource().getConnection(); - stmt = conn.createStatement(); - ResultSet resultSet = stmt - .executeQuery("SELECT ID from DM_DEVICE DEVICE where DEVICE.DEVICE_IDENTIFICATION='111'"); + stmt = conn.prepareStatement("SELECT ID from DM_DEVICE DEVICE where DEVICE.DEVICE_IDENTIFICATION='111'"); + rs = stmt.executeQuery(); - while (resultSet.next()) { - deviceId = resultSet.getLong(1); + while (rs.next()) { + deviceId = rs.getLong(1); } - conn.close(); - } catch (SQLException sqlEx) { - throw new DeviceManagementDAOException("error in fetch device by device identification id", sqlEx); + } catch (SQLException e) { + throw new DeviceManagementDAOException("error in fetch device by device identification id", e); + } finally { + TestUtils.cleanupResources(conn, stmt, rs); } Assert.assertNotNull(deviceId, "Device Id is null"); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/malformed-cdm-config-no-ds-config.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/malformed-cdm-config-no-ds-config.xml new file mode 100644 index 0000000000..a2a20ccd46 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/malformed-cdm-config-no-ds-config.xml @@ -0,0 +1,26 @@ + + + + + + + + jdbc/DEVICE_MGT_DS + + + + + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/malformed-cdm-config-no-jndi.config.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/malformed-cdm-config-no-jndi.config.xml new file mode 100644 index 0000000000..5a4e934dc5 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/malformed-cdm-config-no-jndi.config.xml @@ -0,0 +1,26 @@ + + + + + + + + jdbc/DEVICE_MGT_DS + + + + + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/malformed-cdm-config-no-mgt-repo.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/malformed-cdm-config-no-mgt-repo.xml new file mode 100644 index 0000000000..6f2b60631c --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/malformed-cdm-config-no-mgt-repo.xml @@ -0,0 +1,26 @@ + + + + + + + + jdbc/DEVICE_MGT_DS + + + + + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/schema/DeviceManagementConfigSchema.xsd b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/schema/DeviceManagementConfigSchema.xsd new file mode 100644 index 0000000000..85cac09f39 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/schema/DeviceManagementConfigSchema.xsd @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml index 1bf0f08f81..66ecc2ad1e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml @@ -21,8 +21,9 @@ - + + \ No newline at end of file