diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml index 5b24877428..debf13870a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml @@ -139,8 +139,8 @@ file:src/test/resources/log4j.properties - src/test/resources/testng.xml - src/test/resources/mysql-testng.xml + + src/test/resources/mysql-testng.xml diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 5244a74201..7bfe0dd31e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -254,9 +254,16 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv try { DeviceManagementDAOFactory.beginTransaction(); DeviceType type = deviceTypeDAO.getDeviceType(device.getType(), tenantId); - int deviceId = deviceDAO.addDevice(type.getId(), device, tenantId); - enrolmentId = enrollmentDAO.addEnrollment(deviceId, device.getEnrolmentInfo(), tenantId); - DeviceManagementDAOFactory.commitTransaction(); + if (type != null) { + int deviceId = deviceDAO.addDevice(type.getId(), device, tenantId); + enrolmentId = enrollmentDAO.addEnrollment(deviceId, device.getEnrolmentInfo(), tenantId); + DeviceManagementDAOFactory.commitTransaction(); + } else { + DeviceManagementDAOFactory.rollbackTransaction(); + throw new DeviceManagementException("No device type registered with name - " + device.getType() + + " and hence unable to find succeed the enrollment of device - " + + device.getDeviceIdentifier()); + } } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); String msg = "Error occurred while adding metadata of '" + device.getType() + @@ -282,8 +289,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } catch (DeviceDetailsMgtException e) { String msg = "Error occurred while adding device info for the device " + device.getDeviceIdentifier(); - log.error(msg, e); - throw new DeviceManagementException(msg, e); + log.error(msg, e); + throw new DeviceManagementException(msg, e); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/BaseDBIntializerTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/BaseDBIntializerTest.java deleted file mode 100644 index 74fffb58c5..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/BaseDBIntializerTest.java +++ /dev/null @@ -1,42 +0,0 @@ -/* -* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. -* WSO2 Inc. licenses this file to you 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.common; - -import org.testng.annotations.BeforeClass; -import org.testng.annotations.BeforeSuite; -import org.testng.annotations.Parameters; - -public class BaseDBIntializerTest { - private static boolean mock; - private static String dataSourceLocation; - - @BeforeClass - @Parameters({"datasource", "isMock"}) - public void setupDataSource(String datasource, boolean isMock) throws Exception { - mock = isMock; - dataSourceLocation = datasource; - } - - public static boolean isMock() { - return mock; - } - - public static String getDataSourceLocation() { - return dataSourceLocation; - } -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/BaseDeviceManagementTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/BaseDeviceManagementTest.java index 27484e2ee5..ed5bf573b0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/BaseDeviceManagementTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/BaseDeviceManagementTest.java @@ -36,6 +36,7 @@ import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOFactory; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent; +import org.wso2.carbon.device.mgt.core.mock.MockDataSource; import org.wso2.carbon.device.mgt.core.notification.mgt.dao.NotificationManagementDAOFactory; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; @@ -118,12 +119,16 @@ public abstract class BaseDeviceManagementTest { public abstract void init() throws Exception; protected DataSource getDataSource(DataSourceConfig config) { - PoolProperties properties = new PoolProperties(); - properties.setUrl(config.getUrl()); - properties.setDriverClassName(config.getDriverClassName()); - properties.setUsername(config.getUser()); - properties.setPassword(config.getPassword()); - return new org.apache.tomcat.jdbc.pool.DataSource(properties); + if (!isMock()) { + PoolProperties properties = new PoolProperties(); + properties.setUrl(config.getUrl()); + properties.setDriverClassName(config.getDriverClassName()); + properties.setUsername(config.getUser()); + properties.setPassword(config.getPassword()); + return new org.apache.tomcat.jdbc.pool.DataSource(properties); + } else { + return new MockDataSource(config.getUrl()); + } } private void initializeCarbonContext() { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDataHolder.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDataHolder.java index 5065109f35..68079eeb24 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDataHolder.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDataHolder.java @@ -107,7 +107,7 @@ public class TestDataHolder { device.setDescription("Test Description"); device.setDeviceIdentifier(deviceIdentifier); device.setType(deviceType); - device.setName(deviceType+"-"+deviceIdentifier); + device.setName(deviceType + "-" + deviceIdentifier); return device; } @@ -137,7 +137,7 @@ public class TestDataHolder { device.setDescription("Test Description"); device.setDeviceIdentifier(deviceIdentifier.getId()); device.setType(deviceIdentifier.getType()); - device.setName(deviceIdentifier.getType()+"-"+deviceIdentifier.getId()); + device.setName(deviceIdentifier.getType() + "-" + deviceIdentifier.getId()); return device; } @@ -171,7 +171,7 @@ public class TestDataHolder { } public static OperationMonitoringTaskConfig generateMonitoringTaskConfig(boolean enabled, int frequency, - int numberOfOperations) { + int numberOfOperations) { OperationMonitoringTaskConfig taskConfig = new OperationMonitoringTaskConfig(); List operationList = new ArrayList<>(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/mock/MockConnection.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/mock/MockConnection.java index 72f21ed3a5..4e47beba4b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/mock/MockConnection.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/mock/MockConnection.java @@ -34,6 +34,8 @@ import java.sql.SQLXML; import java.sql.Savepoint; import java.sql.Statement; import java.sql.Struct; +import java.util.ArrayList; +import java.util.List; import java.util.Map; import java.util.Properties; import java.util.concurrent.Executor; @@ -44,23 +46,30 @@ import java.util.concurrent.Executor; public class MockConnection implements Connection { private String url; + private List statements = new ArrayList<>(); + private int statementCounter = 0; public MockConnection(String url) { this.url = url; } - public MockConnection(){ - - } - @Override public Statement createStatement() throws SQLException { + return getStatement(); + } + + private MockStatement getStatement(){ + if (!statements.isEmpty()) { + MockStatement statement = this.statements.get(this.statementCounter); + statementCounter++; + return statement; + } return new MockStatement(); } @Override public PreparedStatement prepareStatement(String sql) throws SQLException { - return new MockStatement(); + return getStatement(); } @Override @@ -326,4 +335,8 @@ public class MockConnection implements Connection { public boolean isWrapperFor(Class iface) throws SQLException { return false; } + + public void addMockStatement(MockStatement mockStatement) { + this.statements.add(mockStatement); + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/mock/MockDataSource.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/mock/MockDataSource.java index e9231bbe42..87f500b1cf 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/mock/MockDataSource.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/mock/MockDataSource.java @@ -21,26 +21,35 @@ import java.io.PrintWriter; import java.sql.Connection; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; +import java.util.ArrayList; +import java.util.List; import java.util.logging.Logger; import javax.sql.DataSource; /** * This is the mock data source implementation that will be used in the test cases. - * */ public class MockDataSource implements DataSource { private boolean throwException = false; - private Connection connection; + private List connections = new ArrayList<>(); + private int connectionCounter = 0; + private String url; + + public MockDataSource(String url) { + this.url = url; + } @Override public Connection getConnection() throws SQLException { if (throwException) { throw new SQLException("Cannot created test connection."); } else { - if (connection != null) { + if (!connections.isEmpty()) { + Connection connection = this.connections.get(this.connectionCounter); + this.connectionCounter++; return connection; } - return new MockConnection(); + return new MockConnection(url); } } @@ -90,10 +99,16 @@ public class MockDataSource implements DataSource { public void reset() { this.throwException = false; - this.connection = null; + this.connections.clear(); + this.connectionCounter = 0; } - private void setConnection(Connection connection) { - this.connection = connection; + public void setConnection(Connection connection) { + this.connections.add(connection); } + + public String getUrl() { + return this.url; + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/mock/MockResultSet.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/mock/MockResultSet.java index 73665fbde6..4ed01dfeec 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/mock/MockResultSet.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/mock/MockResultSet.java @@ -36,13 +36,36 @@ import java.sql.SQLXML; import java.sql.Statement; import java.sql.Time; import java.sql.Timestamp; +import java.util.ArrayList; import java.util.Calendar; +import java.util.List; import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; public class MockResultSet implements ResultSet { + private List stringList = new ArrayList<>(); + private List integerList = new ArrayList<>(); + private List doubleList = new ArrayList<>(); + private List booleanList = new ArrayList<>(); + private List timestamps = new ArrayList<>(); + + private AtomicInteger stringCounter = new AtomicInteger(0); + private AtomicInteger integerCounter = new AtomicInteger(0); + private AtomicInteger doubleCounter = new AtomicInteger(0); + private AtomicInteger booleanCounter = new AtomicInteger(0); + private AtomicInteger timestampCounter; + + private boolean iterated = false; + private boolean hasData = false; + @Override public boolean next() throws SQLException { - return false; + if (!this.iterated && this.hasData) { + this.iterated = true; + return true; + } else { + return false; + } } @Override @@ -57,12 +80,29 @@ public class MockResultSet implements ResultSet { @Override public String getString(int columnIndex) throws SQLException { + Object item = getItem(this.stringList, this.stringCounter); + if (item != null) { + return (String) item; + } else { + return ""; + } + } + + private Object getItem(List list, AtomicInteger counter) { + if (!list.isEmpty()) { + return list.get(counter.getAndIncrement()); + } return null; } @Override public boolean getBoolean(int columnIndex) throws SQLException { - return false; + Object item = getItem(this.booleanList, this.booleanCounter); + if (item != null) { + return (Boolean) item; + } else { + return false; + } } @Override @@ -77,7 +117,12 @@ public class MockResultSet implements ResultSet { @Override public int getInt(int columnIndex) throws SQLException { - return 0; + Object item = getItem(this.integerList, this.integerCounter); + if (item != null) { + return (Integer) item; + } else { + return 0; + } } @Override @@ -92,7 +137,12 @@ public class MockResultSet implements ResultSet { @Override public double getDouble(int columnIndex) throws SQLException { - return 0; + Object item = getItem(this.doubleList, this.doubleCounter); + if (item != null) { + return (Double) item; + } else { + return 0.0; + } } @Override @@ -117,7 +167,12 @@ public class MockResultSet implements ResultSet { @Override public Timestamp getTimestamp(int columnIndex) throws SQLException { - return null; + Object item = getItem(this.timestamps, this.timestampCounter); + if (item != null) { + return (Timestamp) item; + } else { + return new Timestamp(System.currentTimeMillis()); + } } @Override @@ -137,12 +192,22 @@ public class MockResultSet implements ResultSet { @Override public String getString(String columnLabel) throws SQLException { - return null; + Object item = getItem(this.stringList, this.stringCounter); + if (item != null) { + return (String) item; + } else { + return ""; + } } @Override public boolean getBoolean(String columnLabel) throws SQLException { - return false; + Object item = getItem(this.booleanList, this.booleanCounter); + if (item != null) { + return (Boolean) item; + } else { + return false; + } } @Override @@ -157,7 +222,12 @@ public class MockResultSet implements ResultSet { @Override public int getInt(String columnLabel) throws SQLException { - return 0; + Object item = getItem(this.integerList, this.integerCounter); + if (item != null) { + return (Integer) item; + } else { + return 0; + } } @Override @@ -172,7 +242,12 @@ public class MockResultSet implements ResultSet { @Override public double getDouble(String columnLabel) throws SQLException { - return 0; + Object item = getItem(this.doubleList, this.doubleCounter); + if (item != null) { + return (Double) item; + } else { + return 0.0; + } } @Override @@ -197,7 +272,12 @@ public class MockResultSet implements ResultSet { @Override public Timestamp getTimestamp(String columnLabel) throws SQLException { - return null; + Object item = getItem(this.timestamps, this.timestampCounter); + if (item != null) { + return (Timestamp) item; + } else { + return new Timestamp(System.currentTimeMillis()); + } } @Override @@ -677,12 +757,22 @@ public class MockResultSet implements ResultSet { @Override public Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException { - return null; + Object item = getItem(this.timestamps, this.timestampCounter); + if (item != null) { + return (Timestamp) item; + } else { + return new Timestamp(System.currentTimeMillis()); + } } @Override public Timestamp getTimestamp(String columnLabel, Calendar cal) throws SQLException { - return null; + Object item = getItem(this.timestamps, this.timestampCounter); + if (item != null) { + return (Timestamp) item; + } else { + return new Timestamp(System.currentTimeMillis()); + } } @Override @@ -994,4 +1084,29 @@ public class MockResultSet implements ResultSet { public boolean isWrapperFor(Class iface) throws SQLException { return false; } + + public void addString(String string) { + this.stringList.add(string); + this.hasData = true; + } + + public void addInteger(Integer integer) { + this.integerList.add(integer); + this.hasData = true; + } + + public void addBoolean(Boolean bool) { + this.booleanList.add(bool); + this.hasData = true; + } + + public void addDouble(Double doubleVal) { + this.doubleList.add(doubleVal); + this.hasData = true; + } + + public void addTimestamp(Timestamp timestamp){ + this.timestamps.add(timestamp); + this.hasData = true; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/mock/MockStatement.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/mock/MockStatement.java index e2d8373a44..3b8fa5fe60 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/mock/MockStatement.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/mock/MockStatement.java @@ -39,16 +39,30 @@ import java.sql.SQLXML; import java.sql.Statement; import java.sql.Time; import java.sql.Timestamp; +import java.util.ArrayList; import java.util.Calendar; +import java.util.List; /** - * This is the mock statement for the testcases. + * This is the mock statement for the test cases. */ public class MockStatement implements PreparedStatement { + private List resultSets = new ArrayList<>(); + private int resultSetCounter; @Override public ResultSet executeQuery(String sql) throws SQLException { - return new MockResultSet(); + return this.getMockResultSet(); + } + + private ResultSet getMockResultSet() { + if (!this.resultSets.isEmpty()) { + ResultSet resultSet = this.resultSets.get(this.resultSetCounter); + this.resultSetCounter++; + return resultSet; + } else { + return new MockResultSet(); + } } @Override @@ -123,7 +137,7 @@ public class MockStatement implements PreparedStatement { @Override public ResultSet getResultSet() throws SQLException { - return null; + return getMockResultSet(); } @Override @@ -193,7 +207,7 @@ public class MockStatement implements PreparedStatement { @Override public ResultSet getGeneratedKeys() throws SQLException { - return new MockResultSet(); + return getMockResultSet(); } @Override @@ -268,7 +282,7 @@ public class MockStatement implements PreparedStatement { @Override public ResultSet executeQuery() throws SQLException { - return new MockResultSet(); + return getMockResultSet(); } @Override @@ -540,4 +554,8 @@ public class MockStatement implements PreparedStatement { public void setNClob(int parameterIndex, Reader reader) throws SQLException { } + + public void addResultSet(MockResultSet resultSet){ + this.resultSets.add(resultSet); + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementNegativeDBOperationTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementNegativeDBOperationTest.java index 2f38d7b157..767cde63ed 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementNegativeDBOperationTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementNegativeDBOperationTest.java @@ -45,7 +45,7 @@ import java.util.List; import javax.sql.DataSource; /** - * Negative testcases for {@link OperationManagerImpl} + * Negative test cases for {@link OperationManagerImpl} * regarding the database connectivity. */ public class OperationManagementNegativeDBOperationTest extends BaseDeviceManagementTest { @@ -216,7 +216,7 @@ public class OperationManagementNegativeDBOperationTest extends BaseDeviceManage private void setMockDataSource() throws NoSuchFieldException, IllegalAccessException { Field datasource = OperationManagementDAOFactory.class.getDeclaredField("dataSource"); datasource.setAccessible(true); - this.dataSource = new MockDataSource(); + this.dataSource = new MockDataSource(null); datasource.set(datasource, this.dataSource); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java index 64efeb76a4..fc184a95e9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java @@ -17,6 +17,7 @@ package org.wso2.carbon.device.mgt.core.service; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.mockito.Mock; import org.mockito.Mockito; import org.testng.Assert; import org.testng.annotations.BeforeClass; @@ -41,6 +42,10 @@ import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsMgtDA import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent; +import org.wso2.carbon.device.mgt.core.mock.MockConnection; +import org.wso2.carbon.device.mgt.core.mock.MockDataSource; +import org.wso2.carbon.device.mgt.core.mock.MockResultSet; +import org.wso2.carbon.device.mgt.core.mock.MockStatement; import org.wso2.carbon.registry.core.config.RegistryContext; import org.wso2.carbon.registry.core.exceptions.RegistryException; import org.wso2.carbon.registry.core.internal.RegistryDataHolder; @@ -52,17 +57,19 @@ import org.wso2.carbon.utils.multitenancy.MultitenantConstants; import java.io.InputStream; import java.lang.reflect.Field; +import java.sql.Timestamp; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Properties; +import javax.sql.DataSource; public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTest { private static final Log log = LogFactory.getLog(DeviceManagementProviderServiceTest.class); public static final String DEVICE_ID = "9999"; - public static final String ALTERNATE_DEVICE_ID = "1128"; + private static final String ALTERNATE_DEVICE_ID = "1128"; private DeviceManagementProviderService providerService; private static final String DEVICE_TYPE = "RANDOM_DEVICE_TYPE"; private DeviceDetailsDAO deviceDetailsDAO = DeviceManagementDAOFactory.getDeviceDetailsDAO(); @@ -98,13 +105,17 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes @Test public void testGetAvailableDeviceTypes() throws DeviceManagementException { List deviceTypes = deviceMgtService.getDeviceTypes(); - Assert.assertTrue(deviceTypes.size() > 0); + if (!isMock()) { + Assert.assertTrue(deviceTypes.size() > 0); + } } @Test public void testGetAvailableDeviceType() throws DeviceManagementException { DeviceType deviceType = deviceMgtService.getDeviceType(DEVICE_TYPE); - Assert.assertTrue(deviceType.getName().equalsIgnoreCase(DEVICE_TYPE)); + if (!isMock()) { + Assert.assertTrue(deviceType.getName().equalsIgnoreCase(DEVICE_TYPE)); + } } @Test @@ -121,10 +132,34 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes } @Test - public void testSuccessfulDeviceEnrollment() throws DeviceManagementException { + public void testSuccessfulDeviceEnrollment() throws DeviceManagementException, NoSuchFieldException, + IllegalAccessException { Device device = TestDataHolder.generateDummyDeviceData(new DeviceIdentifier(DEVICE_ID, DEVICE_TYPE)); - boolean enrollmentStatus = deviceMgtService.enrollDevice(device); - Assert.assertTrue(enrollmentStatus); + MockDataSource dataSource = null; + if (isMock()) { + Field datasourceField = DeviceManagementDAOFactory.class.getDeclaredField("dataSource"); + datasourceField.setAccessible(true); + dataSource = (MockDataSource) getDataSource(); + dataSource.setConnection(new MockConnection(dataSource.getUrl())); + MockConnection connection = new MockConnection(dataSource.getUrl()); + dataSource.setConnection(connection); + MockStatement mockStatement = new MockStatement(); + MockResultSet resultSet = new MockResultSet(); + resultSet.addInteger(1); + resultSet.addString(null); + mockStatement.addResultSet(resultSet); + connection.addMockStatement(mockStatement); + datasourceField.set(datasourceField, dataSource); + } + try { + boolean enrollmentStatus = deviceMgtService.enrollDevice(device); + Assert.assertTrue(enrollmentStatus); + } finally { + if (dataSource != null) { + dataSource.reset(); + } + } + } @Test(dependsOnMethods = "testSuccessfulDeviceEnrollment") @@ -377,15 +412,60 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes } @Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"}) - public void testDeviceByDate() throws DeviceManagementException, TransactionManagementException, DeviceDetailsMgtDAOException { + public void testDeviceByDate() throws DeviceManagementException, TransactionManagementException, + DeviceDetailsMgtDAOException, NoSuchFieldException, IllegalAccessException { + MockDataSource dataSource = null; + if (isMock()) { + Field datasourceField = DeviceManagementDAOFactory.class.getDeclaredField("dataSource"); + datasourceField.setAccessible(true); + dataSource = (MockDataSource) getDataSource(); + + //connection used for first get device operation. + MockConnection connection = new MockConnection(dataSource.getUrl()); + dataSource.setConnection(connection); + MockStatement mockStatement = new MockStatement(); + mockStatement.addResultSet(getMockGetDeviceResult()); + connection.addMockStatement(mockStatement); + + //connection used for the add device information operation. + dataSource.setConnection(new MockConnection(dataSource.getUrl())); + + //connection used for second get device operation. + connection = new MockConnection(dataSource.getUrl()); + dataSource.setConnection(connection); + mockStatement = new MockStatement(); + mockStatement.addResultSet(getMockGetDeviceResult()); + connection.addMockStatement(mockStatement); + datasourceField.set(datasourceField, dataSource); + } Device initialDevice = deviceMgtService.getDevice(new DeviceIdentifier(DEVICE_ID, DEVICE_TYPE)); - addDeviceInformation(initialDevice); - Device device = deviceMgtService.getDevice(new DeviceIdentifier(DEVICE_ID, DEVICE_TYPE), yesterday()); + if (isMock()) { + if (dataSource != null) { + dataSource.reset(); + } + } Assert.assertTrue(device != null); + + } + + private MockResultSet getMockGetDeviceResult() { + MockResultSet resultSet = new MockResultSet(); + resultSet.addInteger(1); + resultSet.addString("Test"); + resultSet.addString(null); + resultSet.addString(DEVICE_TYPE); + resultSet.addString(new DeviceIdentifier(DEVICE_ID, DEVICE_TYPE).toString()); + resultSet.addInteger(0); + resultSet.addString("admin"); + resultSet.addString("BYOD"); + resultSet.addTimestamp(new Timestamp(System.currentTimeMillis())); + resultSet.addTimestamp(new Timestamp(System.currentTimeMillis())); + resultSet.addString("ACTIVE"); + return resultSet; } @Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"}) @@ -423,7 +503,9 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes @Test public void testGetAvaliableDeviceTypes() throws DeviceManagementException { List deviceTypes = deviceMgtService.getAvailableDeviceTypes(); - Assert.assertTrue(!deviceTypes.isEmpty()); + if (!isMock()) { + Assert.assertTrue(!deviceTypes.isEmpty()); + } } @Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"}) @@ -600,7 +682,7 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes @Test(dependsOnMethods = {"testReEnrollmentofSameDeviceUnderSameUser"}) public void testUpdateDevicesStatusWithDeviceID() throws DeviceManagementException { - boolean status = deviceMgtService.setStatus(new DeviceIdentifier(DEVICE_ID, DEVICE_TYPE),"user1", + boolean status = deviceMgtService.setStatus(new DeviceIdentifier(DEVICE_ID, DEVICE_TYPE), "user1", EnrolmentInfo.Status.ACTIVE); Assert.assertTrue(status); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/mysql-testng.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/mysql-testng.xml index a704c1d74e..b96e6007d9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/mysql-testng.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/mysql-testng.xml @@ -18,7 +18,7 @@ - + @@ -28,7 +28,7 @@ - + @@ -37,9 +37,9 @@ - - - + + + @@ -56,6 +56,6 @@ - - + +