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 index d1bf071f25..68678131f3 100644 --- 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 @@ -21,14 +21,11 @@ import org.testng.Assert; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.common.DeviceManager; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; import org.wso2.carbon.device.mgt.core.common.TestDataHolder; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.task.impl.DeviceTaskManagerServiceImpl; -import org.wso2.carbon.ntask.core.service.TaskService; -import org.wso2.carbon.ntask.core.service.impl.TaskServiceImpl; public class DeviceManagementRepositoryTests extends BaseDeviceManagementTest{ @@ -39,6 +36,7 @@ public class DeviceManagementRepositoryTests extends BaseDeviceManagementTest{ public void init() throws Exception { this.repository = new DeviceManagementPluginRepository(); DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService(new DeviceTaskManagerServiceImpl()); + DeviceManagementDataHolder.getInstance().setTaskService(new TestTaskServiceImpl()); } @Test diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestTaskServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestTaskServiceImpl.java new file mode 100644 index 0000000000..7210942ca5 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestTaskServiceImpl.java @@ -0,0 +1,123 @@ +/* +* 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; + +import org.wso2.carbon.ntask.common.TaskException; +import org.wso2.carbon.ntask.core.TaskInfo; +import org.wso2.carbon.ntask.core.TaskManager; +import org.wso2.carbon.ntask.core.service.TaskService; + +import java.util.List; +import java.util.Set; + +public class TestTaskServiceImpl implements TaskService { + @Override + public TaskManager getTaskManager(String s) throws TaskException { + return new TaskManager() { + @Override + public void initStartupTasks() throws TaskException { + + } + + @Override + public void scheduleTask(String s) throws TaskException { + + } + + @Override + public void rescheduleTask(String s) throws TaskException { + + } + + @Override + public boolean deleteTask(String s) throws TaskException { + return false; + } + + @Override + public void pauseTask(String s) throws TaskException { + + } + + @Override + public void resumeTask(String s) throws TaskException { + + } + + @Override + public void registerTask(TaskInfo taskInfo) throws TaskException { + + } + + @Override + public TaskState getTaskState(String s) throws TaskException { + return null; + } + + @Override + public TaskInfo getTask(String s) throws TaskException { + return null; + } + + @Override + public List getAllTasks() throws TaskException { + return null; + } + + @Override + public boolean isTaskScheduled(String s) throws TaskException { + return false; + } + }; + } + + @Override + public List getAllTenantTaskManagersForType(String s) throws TaskException { + return null; + } + + @Override + public void registerTaskType(String s) throws TaskException { + + } + + @Override + public Set getRegisteredTaskTypes() { + return null; + } + + @Override + public void serverInitialized() { + + } + + @Override + public boolean isServerInit() { + return false; + } + + @Override + public TaskServiceConfiguration getServerConfiguration() { + return null; + } + + @Override + public void runAfterRegistrationActions() throws TaskException { + + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/GroupPersistTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/GroupPersistTests.java index 1115faeab1..cc57e51b85 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/GroupPersistTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/GroupPersistTests.java @@ -25,6 +25,7 @@ import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; +import org.wso2.carbon.device.mgt.common.IllegalTransactionStateException; import org.wso2.carbon.device.mgt.common.TransactionManagementException; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; @@ -54,20 +55,20 @@ public class GroupPersistTests extends BaseDeviceManagementTest { GroupManagementDAOFactory.beginTransaction(); groupId = groupDAO.addGroup(deviceGroup, TestDataHolder.SUPER_TENANT_ID); GroupManagementDAOFactory.commitTransaction(); + GroupManagementDAOFactory.closeConnection(); log.debug("Group added to database. ID: " + groupId); } catch (GroupManagementDAOException e) { GroupManagementDAOFactory.rollbackTransaction(); + GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while adding device type '" + deviceGroup.getName() + "'."; log.error(msg, e); Assert.fail(msg, e); } catch (TransactionManagementException e) { + GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while initiating transaction."; log.error(msg, e); Assert.fail(msg, e); - } finally { - GroupManagementDAOFactory.closeConnection(); } - DeviceGroup group = getGroupById(groupId); Assert.assertNotNull(group, "Group is null"); log.debug("Group name: " + group.getName()); @@ -81,19 +82,20 @@ public class GroupPersistTests extends BaseDeviceManagementTest { request.setGroupName(null); request.setOwner(null); List groups = groupDAO.getGroups(request, TestDataHolder.SUPER_TENANT_ID); + GroupManagementDAOFactory.closeConnection(); Assert.assertNotEquals(groups.size(), 0, "No groups found"); Assert.assertNotNull(groups.get(0), "Group is null"); log.debug("No of Groups found: " + groups.size()); } catch (GroupManagementDAOException e) { + GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while find group by name."; log.error(msg, e); Assert.fail(msg, e); } catch (SQLException e) { + GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while opening a connection to the data source."; log.error(msg, e); Assert.fail(msg, e); - } finally { - GroupManagementDAOFactory.closeConnection(); } } @@ -109,18 +111,19 @@ public class GroupPersistTests extends BaseDeviceManagementTest { } GroupManagementDAOFactory.commitTransaction(); List roles = groupDAO.getRoles(groupId, TestDataHolder.SUPER_TENANT_ID); + GroupManagementDAOFactory.closeConnection(); Assert.assertEquals(roles, addedRoles, "Added roles are not equal to returned roles."); log.debug("Group shared with roles."); } catch (GroupManagementDAOException e) { + GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while find group by name."; log.error(msg, e); Assert.fail(msg, e); } catch (TransactionManagementException e) { + GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while opening a connection to the data source."; log.error(msg, e); Assert.fail(msg, e); - } finally { - GroupManagementDAOFactory.closeConnection(); } } @@ -131,19 +134,20 @@ public class GroupPersistTests extends BaseDeviceManagementTest { List roles = groupDAO.getRoles(groupId, TestDataHolder.SUPER_TENANT_ID); roles.remove(0); List deviceGroups = groupDAO.getGroups(roles.toArray(new String[roles.size()]), TestDataHolder.SUPER_TENANT_ID); + GroupManagementDAOFactory.closeConnection(); Assert.assertEquals(deviceGroups.size(), 1, "Unexpected number of device groups found with role."); Assert.assertEquals(deviceGroups.get(0).getGroupId(), groupId, "Unexpected groupId found with role."); log.debug("Group found for given roles."); } catch (GroupManagementDAOException e) { + GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while getting groups shared with roles."; log.error(msg, e); Assert.fail(msg, e); } catch (SQLException e) { + GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while opening a connection to the data source."; log.error(msg, e); Assert.fail(msg, e); - } finally { - GroupManagementDAOFactory.closeConnection(); } } @@ -157,18 +161,19 @@ public class GroupPersistTests extends BaseDeviceManagementTest { } GroupManagementDAOFactory.commitTransaction(); List roles = groupDAO.getRoles(groupId, TestDataHolder.SUPER_TENANT_ID); + GroupManagementDAOFactory.closeConnection(); Assert.assertNotEquals(roles, rolesToRemove, "Roles not removed."); log.debug("Group unshared with given roles."); } catch (GroupManagementDAOException e) { + GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while find group by name."; log.error(msg, e); Assert.fail(msg, e); } catch (TransactionManagementException e) { + GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while opening a connection to the data source."; log.error(msg, e); Assert.fail(msg, e); - } finally { - GroupManagementDAOFactory.closeConnection(); } } @@ -181,36 +186,38 @@ public class GroupPersistTests extends BaseDeviceManagementTest { GroupManagementDAOFactory.beginTransaction(); groupDAO.addDevice(deviceGroup.getGroupId(), initialTestDevice.getId(), TestDataHolder.SUPER_TENANT_ID); GroupManagementDAOFactory.commitTransaction(); + GroupManagementDAOFactory.closeConnection(); log.debug("Device added to group."); } catch (GroupManagementDAOException e) { GroupManagementDAOFactory.rollbackTransaction(); + GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while adding device '" + initialTestDevice.getName() + "'."; log.error(msg, e); Assert.fail(msg, e); } catch (TransactionManagementException e) { + GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while initiating transaction."; log.error(msg, e); Assert.fail(msg, e); - } finally { - GroupManagementDAOFactory.closeConnection(); } try { GroupManagementDAOFactory.openConnection(); List groupedDevices = groupDAO.getDevices(deviceGroup.getGroupId(), 0, 10, TestDataHolder.SUPER_TENANT_ID); + GroupManagementDAOFactory.closeConnection(); Assert.assertNotEquals(groupedDevices.size(), 0, "No device found"); Assert.assertNotNull(groupedDevices.get(0), "Device is null"); Assert.assertEquals(groupedDevices.get(0).getId(), initialTestDevice.getId(), "Device ids not matched"); } catch (GroupManagementDAOException e) { + GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while retrieving group details."; log.error(msg, e); Assert.fail(msg, e); } catch (SQLException e) { + GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while opening a connection to the data source."; log.error(msg, e); Assert.fail(msg, e); - } finally { - GroupManagementDAOFactory.closeConnection(); } } @@ -223,18 +230,19 @@ public class GroupPersistTests extends BaseDeviceManagementTest { GroupManagementDAOFactory.beginTransaction(); groupDAO.removeDevice(deviceGroup.getGroupId(), initialTestDevice.getId(), TestDataHolder.SUPER_TENANT_ID); GroupManagementDAOFactory.commitTransaction(); + GroupManagementDAOFactory.closeConnection(); log.debug("Device added to group."); } catch (GroupManagementDAOException e) { GroupManagementDAOFactory.rollbackTransaction(); + GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while adding device '" + initialTestDevice.getDeviceIdentifier() + "'."; log.error(msg, e); Assert.fail(msg, e); } catch (TransactionManagementException e) { + GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while initiating transaction."; log.error(msg, e); Assert.fail(msg, e); - } finally { - GroupManagementDAOFactory.closeConnection(); } } @@ -250,18 +258,19 @@ public class GroupPersistTests extends BaseDeviceManagementTest { GroupManagementDAOFactory.beginTransaction(); groupDAO.updateGroup(group, groupId, TestDataHolder.SUPER_TENANT_ID); GroupManagementDAOFactory.commitTransaction(); + GroupManagementDAOFactory.closeConnection(); log.debug("Group updated"); } catch (GroupManagementDAOException e) { GroupManagementDAOFactory.rollbackTransaction(); + GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while updating group details."; log.error(msg, e); Assert.fail(msg, e); } catch (TransactionManagementException e) { + GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while initiating transaction."; log.error(msg, e); Assert.fail(msg, e); - } finally { - GroupManagementDAOFactory.closeConnection(); } group = getGroupById(groupId); @@ -278,18 +287,19 @@ public class GroupPersistTests extends BaseDeviceManagementTest { GroupManagementDAOFactory.beginTransaction(); groupDAO.deleteGroup(group.getGroupId(), TestDataHolder.SUPER_TENANT_ID); GroupManagementDAOFactory.commitTransaction(); + GroupManagementDAOFactory.closeConnection(); log.debug("Group deleted"); } catch (GroupManagementDAOException e) { GroupManagementDAOFactory.rollbackTransaction(); + GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while updating group details."; log.error(msg, e); Assert.fail(msg, e); } catch (TransactionManagementException e) { + GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while initiating transaction."; log.error(msg, e); Assert.fail(msg, e); - } finally { - GroupManagementDAOFactory.closeConnection(); } group = getGroupById(groupId); Assert.assertNull(group, "Group is not deleted"); @@ -298,17 +308,19 @@ public class GroupPersistTests extends BaseDeviceManagementTest { private DeviceGroup getGroupById(int groupId) { try { GroupManagementDAOFactory.openConnection(); - return groupDAO.getGroup(groupId, TestDataHolder.SUPER_TENANT_ID); + DeviceGroup deviceGroup = groupDAO.getGroup(groupId, TestDataHolder.SUPER_TENANT_ID); + GroupManagementDAOFactory.closeConnection(); + return deviceGroup; } catch (GroupManagementDAOException e) { + GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while retrieving group details."; log.error(msg, e); Assert.fail(msg, e); } catch (SQLException e) { + GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while opening a connection to the data source."; log.error(msg, e); Assert.fail(msg, e); - } finally { - GroupManagementDAOFactory.closeConnection(); } return null; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementTests.java index f205725565..81cc2c4738 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementTests.java @@ -64,7 +64,7 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; -public class OperationManagementTests extends BaseDeviceManagementTest { +public class OperationManagementTests{ private static final Log log = LogFactory.getLog(OperationManagementTests.class); private static final String DEVICE_TYPE = "OP_TEST_TYPE"; @@ -82,10 +82,8 @@ public class OperationManagementTests extends BaseDeviceManagementTest { private OperationManager operationMgtService; @BeforeClass - @Override public void init() throws Exception { DeviceConfigurationManager.getInstance().initConfig(); - initDataSource(); log.info("Initializing"); for (int i = 0; i < NO_OF_DEVICES; i++) { deviceIds.add(new DeviceIdentifier(DEVICE_ID_PREFIX + i, DEVICE_TYPE)); @@ -97,6 +95,7 @@ public class OperationManagementTests extends BaseDeviceManagementTest { DeviceManagementDataHolder.getInstance().setRegistryService(getRegistryService()); DeviceManagementDataHolder.getInstance().setDeviceAccessAuthorizationService(new DeviceAccessAuthorizationServiceImpl()); DeviceManagementDataHolder.getInstance().setGroupManagementProviderService(new GroupManagementProviderServiceImpl()); + DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService(null); deviceMgtService.registerDeviceType(new TestDeviceManagementService(DEVICE_TYPE, MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)); for (Device device : devices) { 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 d181c28dd0..a48ce1d3a7 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 @@ -25,7 +25,7 @@ import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.FeatureManager; import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; -public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTest { +public class DeviceManagementProviderServiceTest { private static final Log log = LogFactory.getLog(DeviceManagementProviderServiceTest.class); private DeviceManagementProviderService providerService; @@ -34,9 +34,7 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes @BeforeClass - @Override public void init() throws Exception { - this.initDataSource(); this.providerService = new DeviceManagementProviderServiceImpl(); } @@ -82,8 +80,4 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes } } - @AfterClass - public void cleanResources() { - } - } 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 7e7ae9512d..e562ed1740 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 @@ -40,6 +40,7 @@ +