Adding mock database impl.

revert-70aa11f8
sinthuja 7 years ago
parent a6e85b856a
commit b9131fa141

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

@ -254,9 +254,16 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
try {
DeviceManagementDAOFactory.beginTransaction();
DeviceType type = deviceTypeDAO.getDeviceType(device.getType(), tenantId);
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() +

@ -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.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) {
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() {

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

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

@ -36,14 +36,37 @@ 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<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
public boolean next() throws SQLException {
if (!this.iterated && this.hasData) {
this.iterated = true;
return true;
} else {
return false;
}
}
@Override
public void close() throws SQLException {
@ -57,13 +80,30 @@ 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 {
Object item = getItem(this.booleanList, this.booleanCounter);
if (item != null) {
return (Boolean) item;
} else {
return false;
}
}
@Override
public byte getByte(int columnIndex) throws SQLException {
@ -77,8 +117,13 @@ public class MockResultSet implements ResultSet {
@Override
public int getInt(int columnIndex) throws SQLException {
Object item = getItem(this.integerList, this.integerCounter);
if (item != null) {
return (Integer) item;
} else {
return 0;
}
}
@Override
public long getLong(int columnIndex) throws SQLException {
@ -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,13 +192,23 @@ 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 {
Object item = getItem(this.booleanList, this.booleanCounter);
if (item != null) {
return (Boolean) item;
} else {
return false;
}
}
@Override
public byte getByte(String columnLabel) throws SQLException {
@ -157,8 +222,13 @@ public class MockResultSet implements ResultSet {
@Override
public int getInt(String columnLabel) throws SQLException {
Object item = getItem(this.integerList, this.integerCounter);
if (item != null) {
return (Integer) item;
} else {
return 0;
}
}
@Override
public long getLong(String columnLabel) throws SQLException {
@ -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;
}
}

@ -39,17 +39,31 @@ 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 test cases.
*/
public class MockStatement implements PreparedStatement {
private List<MockResultSet> resultSets = new ArrayList<>();
private int resultSetCounter;
@Override
public ResultSet executeQuery(String sql) throws SQLException {
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
public int executeUpdate(String sql) throws SQLException {
@ -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);
}
}

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

@ -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,14 +105,18 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes
@Test
public void testGetAvailableDeviceTypes() throws DeviceManagementException {
List<DeviceType> deviceTypes = deviceMgtService.getDeviceTypes();
if (!isMock()) {
Assert.assertTrue(deviceTypes.size() > 0);
}
}
@Test
public void testGetAvailableDeviceType() throws DeviceManagementException {
DeviceType deviceType = deviceMgtService.getDeviceType(DEVICE_TYPE);
if (!isMock()) {
Assert.assertTrue(deviceType.getName().equalsIgnoreCase(DEVICE_TYPE));
}
}
@Test
public void addLicense() throws DeviceManagementException {
@ -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));
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,8 +503,10 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes
@Test
public void testGetAvaliableDeviceTypes() throws DeviceManagementException {
List<String> deviceTypes = deviceMgtService.getAvailableDeviceTypes();
if (!isMock()) {
Assert.assertTrue(!deviceTypes.isEmpty());
}
}
@Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"})
public void testGetAllDevices() throws DeviceManagementException {

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

Loading…
Cancel
Save