From 258108df640cfa6c4181bad949d5a096adc6997d Mon Sep 17 00:00:00 2001 From: sinthuja Date: Thu, 12 Oct 2017 14:27:34 +0530 Subject: [PATCH 1/4] Adding no database table exists test cases. --- .../org.wso2.carbon.apimgt.handlers/pom.xml | 3 +- .../core/common/BaseDeviceManagementTest.java | 15 +- .../OperationManagementNoDBSchemaTests.java | 194 ++++++++++++++++++ .../operation/OperationManagementTests.java | 1 + .../no-table-data-source-config.xml | 33 +++ .../src/test/resources/testng.xml | 6 +- 6 files changed, 238 insertions(+), 14 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementNoDBSchemaTests.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/datasource/no-table-data-source-config.xml diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/pom.xml b/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/pom.xml index bf1e7018e6..a15c7e86b0 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/pom.xml +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/pom.xml @@ -17,7 +17,8 @@ ~ under the License. --> - + apimgt-extensions org.wso2.carbon.devicemgt 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 466d92becf..57ac66a1ea 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 @@ -21,10 +21,7 @@ package org.wso2.carbon.device.mgt.core.common; import org.apache.axis2.AxisFault; import org.apache.axis2.context.ConfigurationContext; import org.apache.axis2.context.ConfigurationContextFactory; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.apache.tomcat.jdbc.pool.PoolProperties; -import org.testng.Assert; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeSuite; import org.w3c.dom.Document; @@ -59,14 +56,11 @@ import javax.xml.bind.Unmarshaller; import java.io.File; import java.io.InputStream; import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.SQLException; import java.sql.Statement; public abstract class BaseDeviceManagementTest { private DataSource dataSource; - private static final Log log = LogFactory.getLog(BaseDeviceManagementTest.class); @BeforeSuite public void setupDataSource() throws Exception { @@ -77,7 +71,8 @@ public abstract class BaseDeviceManagementTest { } protected void initDataSource() throws Exception { - this.dataSource = this.getDataSource(this.readDataSourceConfig()); + this.dataSource = this.getDataSource(this. + readDataSourceConfig("src/test/resources/config/datasource/data-source-config.xml")); DeviceManagementDAOFactory.init(dataSource); GroupManagementDAOFactory.init(dataSource); OperationManagementDAOFactory.init(dataSource); @@ -116,7 +111,7 @@ public abstract class BaseDeviceManagementTest { @BeforeClass public abstract void init() throws Exception; - private DataSource getDataSource(DataSourceConfig config) { + protected DataSource getDataSource(DataSourceConfig config) { PoolProperties properties = new PoolProperties(); properties.setUrl(config.getUrl()); properties.setDriverClassName(config.getDriverClassName()); @@ -151,9 +146,9 @@ public abstract class BaseDeviceManagementTest { PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID); } - private DataSourceConfig readDataSourceConfig() throws DeviceManagementException { + protected DataSourceConfig readDataSourceConfig(String configLocation) throws DeviceManagementException { try { - File file = new File("src/test/resources/config/datasource/data-source-config.xml"); + File file = new File(configLocation); Document doc = DeviceManagerUtil.convertToDocument(file); JAXBContext testDBContext = JAXBContext.newInstance(DataSourceConfig.class); Unmarshaller unmarshaller = testDBContext.createUnmarshaller(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementNoDBSchemaTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementNoDBSchemaTests.java new file mode 100644 index 0000000000..f6d9aa5985 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementNoDBSchemaTests.java @@ -0,0 +1,194 @@ +/* +* 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.operation; + +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.InvalidDeviceException; +import org.wso2.carbon.device.mgt.common.PaginationRequest; +import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; +import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; +import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; +import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; +import org.wso2.carbon.device.mgt.core.TestDeviceManagementService; +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.operation.mgt.CommandOperation; +import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; +import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; +import org.wso2.carbon.utils.multitenancy.MultitenantConstants; + +import java.util.ArrayList; +import java.util.List; +import javax.sql.DataSource; + +/** + * This is the testcase which covers the methods from {@link OperationManager} + */ +public class OperationManagementNoDBSchemaTests extends BaseDeviceManagementTest { + + private static final String DEVICE_TYPE = "NEG_OP_TEST_TYPE"; + private static final String DEVICE_ID_PREFIX = "NEG_OP-TEST-DEVICE-ID-"; + private static final String COMMAND_OPERATON_CODE = "COMMAND-TEST"; + private static final int NO_OF_DEVICES = 5; + private static final String ADMIN_USER = "admin"; + + private List deviceIds = new ArrayList<>(); + private OperationManager operationMgtService; + + @BeforeClass + public void init() throws Exception { + DataSource datasource = this.getDataSource(this. + readDataSourceConfig("src/test/resources/config/datasource/no-table-data-source-config.xml")); + OperationManagementDAOFactory.init(datasource); + for (int i = 0; i < NO_OF_DEVICES; i++) { + deviceIds.add(new DeviceIdentifier(DEVICE_ID_PREFIX + i, DEVICE_TYPE)); + } + List devices = TestDataHolder.generateDummyDeviceData(this.deviceIds); + DeviceManagementProviderService deviceMgtService = DeviceManagementDataHolder.getInstance(). + getDeviceManagementProvider(); + deviceMgtService.registerDeviceType(new TestDeviceManagementService(DEVICE_TYPE, + MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)); + for (Device device : devices) { + deviceMgtService.enrollDevice(device); + } + List returnedDevices = deviceMgtService.getAllDevices(DEVICE_TYPE); + for (Device device : returnedDevices) { + if (!device.getDeviceIdentifier().startsWith(DEVICE_ID_PREFIX)) { + throw new Exception("Incorrect device with ID - " + device.getDeviceIdentifier() + " returned!"); + } + } + NotificationStrategy notificationStrategy = new TestNotificationStrategy(); + this.operationMgtService = new OperationManagerImpl(DEVICE_TYPE, notificationStrategy); + } + + @Test(description = "add operation", expectedExceptions = OperationManagementException.class) + public void addCommandOperation() throws DeviceManagementException, OperationManagementException, + InvalidDeviceException { + this.operationMgtService.addOperation( + OperationManagementTests.getOperation(new CommandOperation(), Operation.Type.COMMAND, COMMAND_OPERATON_CODE), + this.deviceIds); + } + + @Test(description = "Get operations", expectedExceptions = OperationManagementException.class) + public void getOperations() throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + for (DeviceIdentifier deviceIdentifier : deviceIds) { + this.operationMgtService.getOperations(deviceIdentifier); + } + } + + + @Test(description = "Get Pending operations", expectedExceptions = OperationManagementException.class) + public void getPendingOperations() throws DeviceManagementException, OperationManagementException, + InvalidDeviceException { + for (DeviceIdentifier deviceIdentifier : deviceIds) { + this.operationMgtService.getPendingOperations(deviceIdentifier); + } + } + + @Test(description = "Get paginated request", expectedExceptions = OperationManagementException.class) + public void getPaginatedRequestAsAdmin() throws OperationManagementException { + PrivilegedCarbonContext.startTenantFlow(); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID, true); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(ADMIN_USER); + PaginationRequest request = new PaginationRequest(1, 2); + request.setDeviceType(DEVICE_TYPE); + request.setOwner(ADMIN_USER); + for (DeviceIdentifier deviceIdentifier : deviceIds) { + this.operationMgtService.getOperations(deviceIdentifier, request); + } + PrivilegedCarbonContext.endTenantFlow(); + } + + @Test(description = "Update operation", expectedExceptions = OperationManagementException.class) + public void updateOperation() throws OperationManagementException { + DeviceIdentifier deviceIdentifier = this.deviceIds.get(0); + Operation operation = OperationManagementTests.getOperation(new CommandOperation(), Operation.Type.COMMAND, + COMMAND_OPERATON_CODE); + operation.setStatus(Operation.Status.COMPLETED); + operation.setOperationResponse("The operation is successfully completed"); + this.operationMgtService.updateOperation(deviceIdentifier, operation); + } + + @Test(description = "Get next pending operation", expectedExceptions = OperationManagementException.class) + public void getNextPendingOperation() throws OperationManagementException { + DeviceIdentifier deviceIdentifier = this.deviceIds.get(0); + this.operationMgtService.getNextPendingOperation(deviceIdentifier); + } + + + @Test(description = "get operation by device and operation id", + expectedExceptions = OperationManagementException.class) + public void getOperationByDeviceAndOperationId() throws OperationManagementException { + DeviceIdentifier deviceIdentifier = this.deviceIds.get(0); + this.operationMgtService.getOperationByDeviceAndOperationId(deviceIdentifier, 1); + } + + @Test(description = "Get operation by device and status", + expectedExceptions = OperationManagementException.class) + public void getOperationsByDeviceAndStatus() throws OperationManagementException, DeviceManagementException { + DeviceIdentifier deviceIdentifier = this.deviceIds.get(0); + this.operationMgtService.getOperationsByDeviceAndStatus(deviceIdentifier, + Operation.Status.PENDING); + } + + @Test(description = "Get operation by operation id", expectedExceptions = OperationManagementException.class) + public void getOperation() throws OperationManagementException, DeviceManagementException { + this.operationMgtService.getOperation(1); + } + + @Test(description = "Get operation activity", expectedExceptions = OperationManagementException.class) + public void getOperationActivity() throws OperationManagementException { + this.operationMgtService.getOperationByActivityId + (DeviceManagementConstants.OperationAttributes.ACTIVITY + "1"); + } + + @Test(description = "Get operation by activity id and device", + expectedExceptions = OperationManagementException.class) + public void getOperationByActivityIdAndDevice() throws OperationManagementException { + this.operationMgtService.getOperationByActivityIdAndDevice( + DeviceManagementConstants.OperationAttributes.ACTIVITY + "1", this.deviceIds.get(0)); + } + + @Test(description = "Get activities updated after some time", + expectedExceptions = OperationManagementException.class) + public void getOperationUpdatedAfterWithLimitAndOffset() throws OperationManagementException { + this.operationMgtService.getActivitiesUpdatedAfter(System.currentTimeMillis() / 1000, 10, 0); + } + + @Test(description = "Get activity count updated after", + expectedExceptions = OperationManagementException.class) + public void getActivityCountUpdatedAfter() throws OperationManagementException { + this.operationMgtService.getActivityCountUpdatedAfter(System.currentTimeMillis() / 1000); + } + + @AfterClass + public void resetDatabase() throws DeviceManagementException { + OperationManagementDAOFactory.init(this.getDataSource(this. + readDataSourceConfig("src/test/resources/config/datasource/data-source-config.xml"))); + } +} 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 6ee4c3af37..ac4965bc6e 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 @@ -45,6 +45,7 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.ConfigOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl; import org.wso2.carbon.device.mgt.core.operation.mgt.PolicyOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/datasource/no-table-data-source-config.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/datasource/no-table-data-source-config.xml new file mode 100644 index 0000000000..7781e61ea2 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/datasource/no-table-data-source-config.xml @@ -0,0 +1,33 @@ + + + + + jdbc:h2:mem:nodb-test-db;DB_CLOSE_ON_EXIT=FALSE;MVCC=true + org.h2.Driver + wso2carbon + wso2carbon + + + + + + + + + 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 8785463a18..8ea8320887 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 @@ -1,4 +1,3 @@ - + + + jdbc:h2:mem:cdm-test-db;DB_CLOSE_ON_EXIT=FALSE;MVCC=true + org.wso2.carbon.device.mgt.core.mock.MockJDBCDriver + wso2carbon + wso2carbon + + + + + + + + + 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 8ea8320887..8093861d4c 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 @@ -41,6 +41,7 @@ + From 8021f0acaa4e8d9ff01f4a238ce0a6a61de880ec Mon Sep 17 00:00:00 2001 From: sinthuja Date: Thu, 12 Oct 2017 16:36:07 +0530 Subject: [PATCH 3/4] Fixing code formatting issues. --- .../carbon/device/mgt/core/mock/MockConnection.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) 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 00b15f6a3a..f7a3e1bdf4 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 @@ -141,12 +141,14 @@ public class MockConnection implements Connection { } @Override - public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { + public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) + throws SQLException { return null; } @Override - public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { + public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) + throws SQLException { return null; } @@ -196,12 +198,14 @@ public class MockConnection implements Connection { } @Override - public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { + public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, + int resultSetHoldability) throws SQLException { return null; } @Override - public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { + public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, + int resultSetHoldability) throws SQLException { return null; } From bfe3701b49708775e1845a592fc73d4c4c34bd45 Mon Sep 17 00:00:00 2001 From: sinthuja Date: Thu, 12 Oct 2017 16:38:27 +0530 Subject: [PATCH 4/4] Adding missing class comments. --- .../org/wso2/carbon/device/mgt/core/mock/MockDataSource.java | 4 ++++ .../device/mgt/core/operation/OperationManagementTests.java | 1 - 2 files changed, 4 insertions(+), 1 deletion(-) 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 1e997f55c4..e9231bbe42 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 @@ -24,6 +24,10 @@ import java.sql.SQLFeatureNotSupportedException; 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; 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 ac4965bc6e..6ee4c3af37 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 @@ -45,7 +45,6 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.ConfigOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl; import org.wso2.carbon.device.mgt.core.operation.mgt.PolicyOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation; -import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.utils.multitenancy.MultitenantConstants;