Adding mock database impl.

4.x.x
sinthuja 7 years ago
parent a6e85b856a
commit b9131fa141

@ -139,8 +139,8 @@
<log4j.configuration>file:src/test/resources/log4j.properties</log4j.configuration> <log4j.configuration>file:src/test/resources/log4j.properties</log4j.configuration>
</systemPropertyVariables> </systemPropertyVariables>
<suiteXmlFiles> <suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile> <!--<file>src/test/resources/testng.xml</file>-->
<suiteXmlFile>src/test/resources/mysql-testng.xml</suiteXmlFile> <file>src/test/resources/mysql-testng.xml</file>
</suiteXmlFiles> </suiteXmlFiles>
</configuration> </configuration>
</plugin> </plugin>

@ -254,9 +254,16 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
try { try {
DeviceManagementDAOFactory.beginTransaction(); DeviceManagementDAOFactory.beginTransaction();
DeviceType type = deviceTypeDAO.getDeviceType(device.getType(), tenantId); DeviceType type = deviceTypeDAO.getDeviceType(device.getType(), tenantId);
int deviceId = deviceDAO.addDevice(type.getId(), device, tenantId); if (type != null) {
enrolmentId = enrollmentDAO.addEnrollment(deviceId, device.getEnrolmentInfo(), tenantId); int deviceId = deviceDAO.addDevice(type.getId(), device, tenantId);
DeviceManagementDAOFactory.commitTransaction(); 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) { } catch (DeviceManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction(); DeviceManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while adding metadata of '" + device.getType() + String msg = "Error occurred while adding metadata of '" + device.getType() +
@ -282,8 +289,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} catch (DeviceDetailsMgtException e) { } catch (DeviceDetailsMgtException e) {
String msg = "Error occurred while adding device info for the device " + String msg = "Error occurred while adding device info for the device " +
device.getDeviceIdentifier(); device.getDeviceIdentifier();
log.error(msg, e); log.error(msg, e);
throw new DeviceManagementException(msg, e); throw new DeviceManagementException(msg, e);
} }
} }

@ -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;
}
}

@ -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.dao.GroupManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; 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.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.notification.mgt.dao.NotificationManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
@ -118,12 +119,16 @@ public abstract class BaseDeviceManagementTest {
public abstract void init() throws Exception; public abstract void init() throws Exception;
protected DataSource getDataSource(DataSourceConfig config) { protected DataSource getDataSource(DataSourceConfig config) {
PoolProperties properties = new PoolProperties(); if (!isMock()) {
properties.setUrl(config.getUrl()); PoolProperties properties = new PoolProperties();
properties.setDriverClassName(config.getDriverClassName()); properties.setUrl(config.getUrl());
properties.setUsername(config.getUser()); properties.setDriverClassName(config.getDriverClassName());
properties.setPassword(config.getPassword()); properties.setUsername(config.getUser());
return new org.apache.tomcat.jdbc.pool.DataSource(properties); properties.setPassword(config.getPassword());
return new org.apache.tomcat.jdbc.pool.DataSource(properties);
} else {
return new MockDataSource(config.getUrl());
}
} }
private void initializeCarbonContext() { private void initializeCarbonContext() {

@ -107,7 +107,7 @@ public class TestDataHolder {
device.setDescription("Test Description"); device.setDescription("Test Description");
device.setDeviceIdentifier(deviceIdentifier); device.setDeviceIdentifier(deviceIdentifier);
device.setType(deviceType); device.setType(deviceType);
device.setName(deviceType+"-"+deviceIdentifier); device.setName(deviceType + "-" + deviceIdentifier);
return device; return device;
} }
@ -137,7 +137,7 @@ public class TestDataHolder {
device.setDescription("Test Description"); device.setDescription("Test Description");
device.setDeviceIdentifier(deviceIdentifier.getId()); device.setDeviceIdentifier(deviceIdentifier.getId());
device.setType(deviceIdentifier.getType()); device.setType(deviceIdentifier.getType());
device.setName(deviceIdentifier.getType()+"-"+deviceIdentifier.getId()); device.setName(deviceIdentifier.getType() + "-" + deviceIdentifier.getId());
return device; return device;
} }
@ -171,7 +171,7 @@ public class TestDataHolder {
} }
public static OperationMonitoringTaskConfig generateMonitoringTaskConfig(boolean enabled, int frequency, public static OperationMonitoringTaskConfig generateMonitoringTaskConfig(boolean enabled, int frequency,
int numberOfOperations) { int numberOfOperations) {
OperationMonitoringTaskConfig taskConfig = new OperationMonitoringTaskConfig(); OperationMonitoringTaskConfig taskConfig = new OperationMonitoringTaskConfig();
List<MonitoringOperation> operationList = new ArrayList<>(); List<MonitoringOperation> operationList = new ArrayList<>();

@ -34,6 +34,8 @@ import java.sql.SQLXML;
import java.sql.Savepoint; import java.sql.Savepoint;
import java.sql.Statement; import java.sql.Statement;
import java.sql.Struct; import java.sql.Struct;
import java.util.ArrayList;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
@ -44,23 +46,30 @@ import java.util.concurrent.Executor;
public class MockConnection implements Connection { public class MockConnection implements Connection {
private String url; private String url;
private List<MockStatement> statements = new ArrayList<>();
private int statementCounter = 0;
public MockConnection(String url) { public MockConnection(String url) {
this.url = url; this.url = url;
} }
public MockConnection(){
}
@Override @Override
public Statement createStatement() throws SQLException { 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(); return new MockStatement();
} }
@Override @Override
public PreparedStatement prepareStatement(String sql) throws SQLException { public PreparedStatement prepareStatement(String sql) throws SQLException {
return new MockStatement(); return getStatement();
} }
@Override @Override
@ -326,4 +335,8 @@ public class MockConnection implements Connection {
public boolean isWrapperFor(Class<?> iface) throws SQLException { public boolean isWrapperFor(Class<?> iface) throws SQLException {
return false; return false;
} }
public void addMockStatement(MockStatement mockStatement) {
this.statements.add(mockStatement);
}
} }

@ -21,26 +21,35 @@ import java.io.PrintWriter;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException; import java.sql.SQLFeatureNotSupportedException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.sql.DataSource; import javax.sql.DataSource;
/** /**
* This is the mock data source implementation that will be used in the test cases. * This is the mock data source implementation that will be used in the test cases.
*
*/ */
public class MockDataSource implements DataSource { public class MockDataSource implements DataSource {
private boolean throwException = false; private boolean throwException = false;
private Connection connection; private List<Connection> connections = new ArrayList<>();
private int connectionCounter = 0;
private String url;
public MockDataSource(String url) {
this.url = url;
}
@Override @Override
public Connection getConnection() throws SQLException { public Connection getConnection() throws SQLException {
if (throwException) { if (throwException) {
throw new SQLException("Cannot created test connection."); throw new SQLException("Cannot created test connection.");
} else { } else {
if (connection != null) { if (!connections.isEmpty()) {
Connection connection = this.connections.get(this.connectionCounter);
this.connectionCounter++;
return connection; return connection;
} }
return new MockConnection(); return new MockConnection(url);
} }
} }
@ -90,10 +99,16 @@ public class MockDataSource implements DataSource {
public void reset() { public void reset() {
this.throwException = false; this.throwException = false;
this.connection = null; this.connections.clear();
this.connectionCounter = 0;
} }
private void setConnection(Connection connection) { public void setConnection(Connection connection) {
this.connection = connection; this.connections.add(connection);
} }
public String getUrl() {
return this.url;
}
} }

@ -36,13 +36,36 @@ import java.sql.SQLXML;
import java.sql.Statement; import java.sql.Statement;
import java.sql.Time; import java.sql.Time;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
public class MockResultSet implements ResultSet { public class MockResultSet implements ResultSet {
private List<String> stringList = new ArrayList<>();
private List<Integer> integerList = new ArrayList<>();
private List<Double> doubleList = new ArrayList<>();
private List<Boolean> booleanList = new ArrayList<>();
private List<Timestamp> 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 @Override
public boolean next() throws SQLException { public boolean next() throws SQLException {
return false; if (!this.iterated && this.hasData) {
this.iterated = true;
return true;
} else {
return false;
}
} }
@Override @Override
@ -57,12 +80,29 @@ public class MockResultSet implements ResultSet {
@Override @Override
public String getString(int columnIndex) throws SQLException { 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; return null;
} }
@Override @Override
public boolean getBoolean(int columnIndex) throws SQLException { 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 @Override
@ -77,7 +117,12 @@ public class MockResultSet implements ResultSet {
@Override @Override
public int getInt(int columnIndex) throws SQLException { 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 @Override
@ -92,7 +137,12 @@ public class MockResultSet implements ResultSet {
@Override @Override
public double getDouble(int columnIndex) throws SQLException { 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 @Override
@ -117,7 +167,12 @@ public class MockResultSet implements ResultSet {
@Override @Override
public Timestamp getTimestamp(int columnIndex) throws SQLException { 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 @Override
@ -137,12 +192,22 @@ public class MockResultSet implements ResultSet {
@Override @Override
public String getString(String columnLabel) throws SQLException { 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 @Override
public boolean getBoolean(String columnLabel) throws SQLException { 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 @Override
@ -157,7 +222,12 @@ public class MockResultSet implements ResultSet {
@Override @Override
public int getInt(String columnLabel) throws SQLException { 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 @Override
@ -172,7 +242,12 @@ public class MockResultSet implements ResultSet {
@Override @Override
public double getDouble(String columnLabel) throws SQLException { 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 @Override
@ -197,7 +272,12 @@ public class MockResultSet implements ResultSet {
@Override @Override
public Timestamp getTimestamp(String columnLabel) throws SQLException { 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 @Override
@ -677,12 +757,22 @@ public class MockResultSet implements ResultSet {
@Override @Override
public Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException { 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 @Override
public Timestamp getTimestamp(String columnLabel, Calendar cal) throws SQLException { 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 @Override
@ -994,4 +1084,29 @@ public class MockResultSet implements ResultSet {
public boolean isWrapperFor(Class<?> iface) throws SQLException { public boolean isWrapperFor(Class<?> iface) throws SQLException {
return false; 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;
}
} }

@ -39,16 +39,30 @@ import java.sql.SQLXML;
import java.sql.Statement; import java.sql.Statement;
import java.sql.Time; import java.sql.Time;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar; 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 { public class MockStatement implements PreparedStatement {
private List<MockResultSet> resultSets = new ArrayList<>();
private int resultSetCounter;
@Override @Override
public ResultSet executeQuery(String sql) throws SQLException { 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 @Override
@ -123,7 +137,7 @@ public class MockStatement implements PreparedStatement {
@Override @Override
public ResultSet getResultSet() throws SQLException { public ResultSet getResultSet() throws SQLException {
return null; return getMockResultSet();
} }
@Override @Override
@ -193,7 +207,7 @@ public class MockStatement implements PreparedStatement {
@Override @Override
public ResultSet getGeneratedKeys() throws SQLException { public ResultSet getGeneratedKeys() throws SQLException {
return new MockResultSet(); return getMockResultSet();
} }
@Override @Override
@ -268,7 +282,7 @@ public class MockStatement implements PreparedStatement {
@Override @Override
public ResultSet executeQuery() throws SQLException { public ResultSet executeQuery() throws SQLException {
return new MockResultSet(); return getMockResultSet();
} }
@Override @Override
@ -540,4 +554,8 @@ public class MockStatement implements PreparedStatement {
public void setNClob(int parameterIndex, Reader reader) throws SQLException { public void setNClob(int parameterIndex, Reader reader) throws SQLException {
} }
public void addResultSet(MockResultSet resultSet){
this.resultSets.add(resultSet);
}
} }

@ -45,7 +45,7 @@ import java.util.List;
import javax.sql.DataSource; import javax.sql.DataSource;
/** /**
* Negative testcases for {@link OperationManagerImpl} * Negative test cases for {@link OperationManagerImpl}
* regarding the database connectivity. * regarding the database connectivity.
*/ */
public class OperationManagementNegativeDBOperationTest extends BaseDeviceManagementTest { public class OperationManagementNegativeDBOperationTest extends BaseDeviceManagementTest {
@ -216,7 +216,7 @@ public class OperationManagementNegativeDBOperationTest extends BaseDeviceManage
private void setMockDataSource() throws NoSuchFieldException, IllegalAccessException { private void setMockDataSource() throws NoSuchFieldException, IllegalAccessException {
Field datasource = OperationManagementDAOFactory.class.getDeclaredField("dataSource"); Field datasource = OperationManagementDAOFactory.class.getDeclaredField("dataSource");
datasource.setAccessible(true); datasource.setAccessible(true);
this.dataSource = new MockDataSource(); this.dataSource = new MockDataSource(null);
datasource.set(datasource, this.dataSource); datasource.set(datasource, this.dataSource);
} }

@ -17,6 +17,7 @@ package org.wso2.carbon.device.mgt.core.service;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.mockito.Mock;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.BeforeClass; 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.dto.DeviceType;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; 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.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.config.RegistryContext;
import org.wso2.carbon.registry.core.exceptions.RegistryException; import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.core.internal.RegistryDataHolder; 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.io.InputStream;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.sql.Timestamp;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import javax.sql.DataSource;
public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTest { public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTest {
private static final Log log = LogFactory.getLog(DeviceManagementProviderServiceTest.class); private static final Log log = LogFactory.getLog(DeviceManagementProviderServiceTest.class);
public static final String DEVICE_ID = "9999"; 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 DeviceManagementProviderService providerService;
private static final String DEVICE_TYPE = "RANDOM_DEVICE_TYPE"; private static final String DEVICE_TYPE = "RANDOM_DEVICE_TYPE";
private DeviceDetailsDAO deviceDetailsDAO = DeviceManagementDAOFactory.getDeviceDetailsDAO(); private DeviceDetailsDAO deviceDetailsDAO = DeviceManagementDAOFactory.getDeviceDetailsDAO();
@ -98,13 +105,17 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes
@Test @Test
public void testGetAvailableDeviceTypes() throws DeviceManagementException { public void testGetAvailableDeviceTypes() throws DeviceManagementException {
List<DeviceType> deviceTypes = deviceMgtService.getDeviceTypes(); List<DeviceType> deviceTypes = deviceMgtService.getDeviceTypes();
Assert.assertTrue(deviceTypes.size() > 0); if (!isMock()) {
Assert.assertTrue(deviceTypes.size() > 0);
}
} }
@Test @Test
public void testGetAvailableDeviceType() throws DeviceManagementException { public void testGetAvailableDeviceType() throws DeviceManagementException {
DeviceType deviceType = deviceMgtService.getDeviceType(DEVICE_TYPE); DeviceType deviceType = deviceMgtService.getDeviceType(DEVICE_TYPE);
Assert.assertTrue(deviceType.getName().equalsIgnoreCase(DEVICE_TYPE)); if (!isMock()) {
Assert.assertTrue(deviceType.getName().equalsIgnoreCase(DEVICE_TYPE));
}
} }
@Test @Test
@ -121,10 +132,34 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes
} }
@Test @Test
public void testSuccessfulDeviceEnrollment() throws DeviceManagementException { public void testSuccessfulDeviceEnrollment() throws DeviceManagementException, NoSuchFieldException,
IllegalAccessException {
Device device = TestDataHolder.generateDummyDeviceData(new DeviceIdentifier(DEVICE_ID, DEVICE_TYPE)); Device device = TestDataHolder.generateDummyDeviceData(new DeviceIdentifier(DEVICE_ID, DEVICE_TYPE));
boolean enrollmentStatus = deviceMgtService.enrollDevice(device); MockDataSource dataSource = null;
Assert.assertTrue(enrollmentStatus); 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") @Test(dependsOnMethods = "testSuccessfulDeviceEnrollment")
@ -377,15 +412,60 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes
} }
@Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"}) @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 initialDevice = deviceMgtService.getDevice(new DeviceIdentifier(DEVICE_ID,
DEVICE_TYPE)); DEVICE_TYPE));
addDeviceInformation(initialDevice); addDeviceInformation(initialDevice);
Device device = deviceMgtService.getDevice(new DeviceIdentifier(DEVICE_ID, Device device = deviceMgtService.getDevice(new DeviceIdentifier(DEVICE_ID,
DEVICE_TYPE), yesterday()); DEVICE_TYPE), yesterday());
if (isMock()) {
if (dataSource != null) {
dataSource.reset();
}
}
Assert.assertTrue(device != null); 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"}) @Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"})
@ -423,7 +503,9 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes
@Test @Test
public void testGetAvaliableDeviceTypes() throws DeviceManagementException { public void testGetAvaliableDeviceTypes() throws DeviceManagementException {
List<String> deviceTypes = deviceMgtService.getAvailableDeviceTypes(); List<String> deviceTypes = deviceMgtService.getAvailableDeviceTypes();
Assert.assertTrue(!deviceTypes.isEmpty()); if (!isMock()) {
Assert.assertTrue(!deviceTypes.isEmpty());
}
} }
@Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"}) @Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"})
@ -600,7 +682,7 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes
@Test(dependsOnMethods = {"testReEnrollmentofSameDeviceUnderSameUser"}) @Test(dependsOnMethods = {"testReEnrollmentofSameDeviceUnderSameUser"})
public void testUpdateDevicesStatusWithDeviceID() throws DeviceManagementException { 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); EnrolmentInfo.Status.ACTIVE);
Assert.assertTrue(status); Assert.assertTrue(status);
} }

@ -18,7 +18,7 @@
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="DeviceManagementCore"> <suite name="MySQLDeviceManagementCore">
<parameter name="useDefaultListeners" value="false"/> <parameter name="useDefaultListeners" value="false"/>
<parameter name="datasource" value="src/test/resources/config/datasource/mysql/data-source-config"/> <parameter name="datasource" value="src/test/resources/config/datasource/mysql/data-source-config"/>
<parameter name="isMock" value="true"/> <parameter name="isMock" value="true"/>
@ -28,7 +28,7 @@
<classes> <classes>
<class name="org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest"/> <class name="org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest"/>
<class name="org.wso2.carbon.device.mgt.core.dao.DevicePersistTests"/> <class name="org.wso2.carbon.device.mgt.core.dao.DevicePersistTests"/>
<!--<class name="org.wso2.carbon.device.mgt.core.DeviceManagementRepositoryTests"/>--> <class name="org.wso2.carbon.device.mgt.core.DeviceManagementRepositoryTests"/>
<class name="org.wso2.carbon.device.mgt.core.DeviceManagementConfigTests"/> <class name="org.wso2.carbon.device.mgt.core.DeviceManagementConfigTests"/>
<class name="org.wso2.carbon.device.mgt.core.app.mgt.AppManagementConfigurationManagerTest"/> <class name="org.wso2.carbon.device.mgt.core.app.mgt.AppManagementConfigurationManagerTest"/>
<!--<class name="org.wso2.carbon.device.mgt.core.dao.ApplicationPersistenceTests"/>--> <!--<class name="org.wso2.carbon.device.mgt.core.dao.ApplicationPersistenceTests"/>-->
@ -37,9 +37,9 @@
</classes> </classes>
</test> </test>
<!--<test name="Service Unit Tests" preserve-order="true">--> <test name="Service Unit Tests" preserve-order="true">
<!--<classes>--> <classes>
<!--<class name="org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceTest"/>--> <class name="org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceTest"/>
<!--<class name="org.wso2.carbon.device.mgt.core.app.mgt.ApplicationManagementProviderServiceTest"/>--> <!--<class name="org.wso2.carbon.device.mgt.core.app.mgt.ApplicationManagementProviderServiceTest"/>-->
<!--<class name="org.wso2.carbon.device.mgt.core.service.GroupManagementProviderServiceTest"/>--> <!--<class name="org.wso2.carbon.device.mgt.core.service.GroupManagementProviderServiceTest"/>-->
<!--<class name="org.wso2.carbon.device.mgt.core.operation.OperationManagementTests"/>--> <!--<class name="org.wso2.carbon.device.mgt.core.operation.OperationManagementTests"/>-->
@ -56,6 +56,6 @@
<!--<class name="org.wso2.carbon.device.mgt.core.search.SearchManagementServiceTest"/>--> <!--<class name="org.wso2.carbon.device.mgt.core.search.SearchManagementServiceTest"/>-->
<!--<class name="org.wso2.carbon.device.mgt.core.search.ProcessorImplTest"/>--> <!--<class name="org.wso2.carbon.device.mgt.core.search.ProcessorImplTest"/>-->
<!--<class name="org.wso2.carbon.device.mgt.core.search.SearchMgtUtilTest"/>--> <!--<class name="org.wso2.carbon.device.mgt.core.search.SearchMgtUtilTest"/>-->
<!--</classes>--> </classes>
<!--</test>--> </test>
</suite> </suite>

Loading…
Cancel
Save