From 485a0d3721f25f73e3603dbc4fe0ceb87151f645 Mon Sep 17 00:00:00 2001 From: Menaka Jayawardena Date: Wed, 11 Oct 2017 16:53:25 +0530 Subject: [PATCH 01/11] Check for device info and persist when enrolling a device. --- .../service/DeviceManagementProviderServiceImpl.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 552c10d12e..5d28eb34af 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -66,8 +66,11 @@ import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO; import org.wso2.carbon.device.mgt.core.dao.EnrollmentDAO; +import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceDetailsMgtException; +import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManager; import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsDAO; import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsMgtDAOException; +import org.wso2.carbon.device.mgt.core.device.details.mgt.impl.DeviceInformationManagerImpl; import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceTypeServiceIdentifier; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; @@ -272,6 +275,15 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.closeConnection(); } + if (device.getDeviceInfo() != null) { + DeviceInformationManager deviceInformationManager = new DeviceInformationManagerImpl(); + try { + deviceInformationManager.addDeviceInfo(deviceIdentifier, device.getDeviceInfo()); + } catch (DeviceDetailsMgtException e) { + e.printStackTrace(); + } + } + if (log.isDebugEnabled()) { log.debug("An enrolment is successfully created with the id '" + enrolmentId + "' associated with " + "the device identified by key '" + device.getDeviceIdentifier() + "', which belongs to " + From 26022daf03df6b3badd09a974b33468821926df7 Mon Sep 17 00:00:00 2001 From: Menaka Jayawardena Date: Wed, 11 Oct 2017 16:54:21 +0530 Subject: [PATCH 02/11] Added checks for other data types. --- .../core/search/mgt/impl/ProcessorImpl.java | 2 +- .../search/mgt/impl/QueryBuilderImpl.java | 64 +++++++++++++------ .../mgt/core/search/mgt/impl/Utils.java | 42 +++++++++--- 3 files changed, 77 insertions(+), 31 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/ProcessorImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/ProcessorImpl.java index cf7a1eb069..f96e2e4048 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/ProcessorImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/ProcessorImpl.java @@ -251,7 +251,7 @@ public class ProcessorImpl implements Processor { if (log.isDebugEnabled()) { log.debug("Query : " + queryHolder.getQuery()); } - Connection conn; + Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; List devices = new ArrayList<>(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/QueryBuilderImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/QueryBuilderImpl.java index d5d0b1700a..65b6af46fa 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/QueryBuilderImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/QueryBuilderImpl.java @@ -23,7 +23,11 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.search.Condition; -import org.wso2.carbon.device.mgt.core.search.mgt.*; +import org.wso2.carbon.device.mgt.core.search.mgt.Constants; +import org.wso2.carbon.device.mgt.core.search.mgt.InvalidOperatorException; +import org.wso2.carbon.device.mgt.core.search.mgt.QueryBuilder; +import org.wso2.carbon.device.mgt.core.search.mgt.QueryHolder; +import org.wso2.carbon.device.mgt.core.search.mgt.ValueType; import java.util.ArrayList; import java.util.HashMap; @@ -93,8 +97,8 @@ public class QueryBuilderImpl implements QueryBuilder { intArr[0] = 1; //int x = 1; String query = this.getGenericQueryPart(valueTypeArray) + - this.processAND(andColumns, valueTypeArray, intArr) + - this.processOR(orColumns, valueTypeArray, intArr); + this.processAND(andColumns, valueTypeArray, intArr) + + this.processOR(orColumns, valueTypeArray, intArr); List queryHolders = new ArrayList<>(); QueryHolder queryHolder = new QueryHolder(); queryHolder.setQuery(query); @@ -135,20 +139,13 @@ public class QueryBuilderImpl implements QueryBuilder { + " LIKE ? "; ValueType type = new ValueType(); type.setColumnType(ValueType.columnType.STRING); - type.setStringValue("%"+con.getValue()+"%"); + type.setStringValue("%" + con.getValue() + "%"); valueType[x] = type; x++; } else { querySuffix = querySuffix + " AND DD." + Utils.getDeviceDetailsColumnNames().get(con.getKey()) + con .getOperator() + " ? "; - ValueType type = new ValueType(); - if (Utils.checkColumnType(con.getKey())) { - type.setColumnType(ValueType.columnType.STRING); - type.setStringValue(con.getValue()); - } else { - type.setColumnType(ValueType.columnType.INTEGER); - type.setIntValue(Integer.parseInt(con.getValue())); - } + ValueType type = this.getValueType(con); valueType[x] = type; x++; } @@ -182,21 +179,15 @@ public class QueryBuilderImpl implements QueryBuilder { + " LIKE ? "; ValueType type = new ValueType(); type.setColumnType(ValueType.columnType.STRING); - type.setStringValue("%"+con.getValue()+"%"); + type.setStringValue("%" + con.getValue() + "%"); valueType[x] = type; x++; } else { querySuffix = querySuffix + " OR DD." + Utils.getDeviceDetailsColumnNames().get(con.getKey()) + con .getOperator() + " ? "; - ValueType type = new ValueType(); - if (Utils.checkColumnType(con.getKey())) { - type.setColumnType(ValueType.columnType.STRING); - type.setStringValue(con.getValue()); - } else { - type.setColumnType(ValueType.columnType.INTEGER); - type.setIntValue(Integer.parseInt(con.getValue())); - } + ValueType type = this.getValueType(con); + valueType[x] = type; x++; } @@ -386,4 +377,35 @@ public class QueryBuilderImpl implements QueryBuilder { throw new InvalidOperatorException("Error occurred while building the sql", e); } } + + /** + * Returns a Value type based on the Condition data. + * + * @param con : The condition that passed. + * @re + */ + private ValueType getValueType(Condition con) { + ValueType type = new ValueType(); + String colValue = Utils.checkColumnType(con.getKey()); + + switch (colValue) { + case "String": + type.setColumnType(ValueType.columnType.STRING); + type.setStringValue(con.getValue()); + break; + case "Double": + type.setColumnType(ValueType.columnType.DOUBLE); + type.setDoubleValue(Double.parseDouble(con.getValue())); + break; + case "Integer": + type.setColumnType(ValueType.columnType.INTEGER); + type.setIntValue(Integer.parseInt(con.getValue())); + break; + case "Long": + type.setColumnType(ValueType.columnType.STRING); + type.setLongValue(Long.parseLong(con.getValue())); + } + + return type; + } } \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/Utils.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/Utils.java index 9e0d044e1e..fb5251acf4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/Utils.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/Utils.java @@ -75,32 +75,56 @@ public class Utils { } - public static boolean checkColumnType(String column) { + public static String checkColumnType(String column) { - boolean bool = false; + String type; switch (column) { case "deviceModel": - bool = true; + type = "String"; break; case "vendor": - bool = true; + type = "String"; break; case "osVersion": - bool = true; + type = "String"; break; case "connectionType": - bool = true; + type = "String"; break; case "ssid": - bool = true; + type = "String"; + break; + case "imei": + type = "String"; + break; + case "imsi": + type = "String"; + break; + case "batteryLevel": + type = "Double"; + break; + case "internalTotalMemory": + type = "Double"; + break; + case "internalAvailableMemory": + type = "Double"; + break; + case "externalAvailableMemory": + type = "Double"; + break; + case "externalTotalMemory": + type = "Double"; + break; + case "cpuUsage": + type = "Double"; break; default: - bool = false; + type = "String"; break; } - return bool; + return type; } public static Map getDeviceDetailsColumnNames() { From 62d30cbf84ff68195c1b29298512a333ff05c25e Mon Sep 17 00:00:00 2001 From: Menaka Jayawardena Date: Wed, 11 Oct 2017 17:08:38 +0530 Subject: [PATCH 03/11] Added search Management Test cases. --- .../device/mgt/core/search/SearchDevice.java | 183 ----------- .../search/SearchManagementServiceTest.java | 293 ++++++++++++++++++ .../device/mgt/core/search/util/Utils.java | 17 +- .../src/test/resources/testng.xml | 2 + 4 files changed, 304 insertions(+), 191 deletions(-) delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchDevice.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchManagementServiceTest.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchDevice.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchDevice.java deleted file mode 100644 index d624bfa4e6..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchDevice.java +++ /dev/null @@ -1,183 +0,0 @@ -/* - * Copyright (c) 2016, 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.search; - -import com.google.gson.Gson; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -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.search.Condition; -import org.wso2.carbon.device.mgt.common.search.SearchContext; -import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; -import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; -import org.wso2.carbon.device.mgt.core.search.mgt.SearchManagerService; -import org.wso2.carbon.device.mgt.core.search.mgt.impl.SearchManagerServiceImpl; -import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; -import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; - -import java.util.ArrayList; -import java.util.List; - -public class SearchDevice extends BaseDeviceManagementTest { - - private static final Log log = LogFactory.getLog(SearchDevice.class); - - @BeforeClass - @Override - public void init() throws Exception { - DeviceManagementProviderService deviceManagementProviderService = new DeviceManagementProviderServiceImpl(); - DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(deviceManagementProviderService); - } - - @Test - public void searchDeviceDetails() throws Exception { - - SearchContext context = new SearchContext(); - List conditions = new ArrayList<>(); - - - Condition cond = new Condition(); - cond.setKey("BATTERY_VOLTAGE"); - cond.setOperator("="); - cond.setValue("40"); - cond.setState(Condition.State.AND); - conditions.add(cond); - -// Condition cond2 = new Condition(); -// cond2.setKey("CPU_USAGE"); -// cond2.setOperator(">"); -// cond2.setValue("40"); -// cond2.setState(Condition.State.OR); -// conditions.add(cond2); -// -// Condition cond3 = new Condition(); -// cond3.setKey("LOCATION"); -// cond3.setOperator("="); -// cond3.setValue("Colombo"); -// cond3.setState(Condition.State.AND); -// conditions.add(cond3); - - context.setConditions(conditions); - - SearchManagerService service = new SearchManagerServiceImpl(); - List devices = service.search(context); - - Gson gson = new Gson(); - String bbbb = gson.toJson(devices); - log.info(bbbb); - - - for (Device device : devices) { - log.debug(device.getDescription()); - log.debug(device.getDeviceIdentifier()); - } - - } - - @Test - public void doValidLocationSearch() throws Exception { - - SearchContext context = new SearchContext(); - List conditions = new ArrayList<>(); - - Condition cond = new Condition(); - cond.setKey("LOCATION"); - cond.setOperator("="); - cond.setValue("Karan"); - cond.setState(Condition.State.AND); - conditions.add(cond); - - context.setConditions(conditions); - - SearchManagerService service = new SearchManagerServiceImpl(); - List devices = service.search(context); - - Gson gson = new Gson(); - String bbbb = gson.toJson(devices); - log.info("Valid Search " + bbbb); - - - for (Device device : devices) { - log.debug(device.getDescription()); - log.debug(device.getDeviceIdentifier()); - } - } - - @Test - public void doInvalidLocationSearch() throws Exception { - - SearchContext context = new SearchContext(); - List conditions = new ArrayList<>(); - - Condition cond = new Condition(); - cond.setKey("LOCATION"); - cond.setOperator("="); - cond.setValue("Colombo"); - cond.setState(Condition.State.AND); - conditions.add(cond); - - context.setConditions(conditions); - - SearchManagerService service = new SearchManagerServiceImpl(); - List devices = service.search(context); - - Gson gson = new Gson(); - String bbbb = gson.toJson(devices); - log.info("Invalid Search " + bbbb); - - - for (Device device : devices) { - log.debug(device.getDescription()); - log.debug(device.getDeviceIdentifier()); - } - } - - @Test - public void doStringSearch() throws Exception { - - SearchContext context = new SearchContext(); - List conditions = new ArrayList<>(); - - Condition cond = new Condition(); - cond.setKey("deviceModel"); - cond.setOperator("="); - cond.setValue("SM-T520"); - cond.setState(Condition.State.AND); - conditions.add(cond); - - context.setConditions(conditions); - - SearchManagerService service = new SearchManagerServiceImpl(); - List devices = service.search(context); - - Gson gson = new Gson(); - String bbbb = gson.toJson(devices); - log.info("Invalid Search " + bbbb); - - - for (Device device : devices) { - log.debug(device.getDescription()); - log.debug(device.getDeviceIdentifier()); - } - } -} - diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchManagementServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchManagementServiceTest.java new file mode 100644 index 0000000000..aa179b967a --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchManagementServiceTest.java @@ -0,0 +1,293 @@ +/* + * Copyright (c) 2016, 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.search; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.testng.Assert; +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.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.search.Condition; +import org.wso2.carbon.device.mgt.common.search.SearchContext; +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.internal.DeviceManagementServiceComponent; +import org.wso2.carbon.device.mgt.core.search.mgt.SearchManagerService; +import org.wso2.carbon.device.mgt.core.search.mgt.SearchMgtException; +import org.wso2.carbon.device.mgt.core.search.mgt.impl.SearchManagerServiceImpl; +import org.wso2.carbon.device.mgt.core.search.util.Utils; +import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; +import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; +import org.wso2.carbon.utils.multitenancy.MultitenantConstants; + +import java.util.ArrayList; +import java.util.Calendar; +import java.util.List; + +public class SearchManagementServiceTest extends BaseDeviceManagementTest { + + private static final Log log = LogFactory.getLog(SearchManagementServiceTest.class); + private static List deviceIdentifiers = new ArrayList<>(); + private static String DEVICE_ID_PREFIX = "SEARCH-DEVICE-ID-"; + private static String DEVICE_TYPE = "SEARCH_TYPE"; + + @BeforeClass + public void init() throws Exception { + DeviceManagementDataHolder.getInstance().getDeviceAccessAuthorizationService(); + + for (int i = 0; i < 5; i++) { + deviceIdentifiers.add(new DeviceIdentifier(DEVICE_ID_PREFIX + i, DEVICE_TYPE)); + } + DeviceManagementProviderService deviceMgtService = new DeviceManagementProviderServiceImpl(); + DeviceManagementServiceComponent.notifyStartupListeners(); + DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(deviceMgtService); + deviceMgtService.registerDeviceType(new TestDeviceManagementService(DEVICE_TYPE, + MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)); + + List devices = TestDataHolder.generateDummyDeviceData(deviceIdentifiers); + for (Device device : devices) { + device.setDeviceInfo(Utils.getDeviceInfo()); + deviceMgtService.enrollDevice(device); + } + List returnedDevices = deviceMgtService.getAllDevices(DEVICE_TYPE, true); + for (Device device : returnedDevices) { + if (!device.getDeviceIdentifier().startsWith(DEVICE_ID_PREFIX)) { + throw new Exception("Incorrect device with ID - " + device.getDeviceIdentifier() + " returned!"); + } + } + } + + @Test(description = "Search for device details.") + public void searchDeviceDetails() throws Exception { + SearchContext context = new SearchContext(); + List conditions = new ArrayList<>(); + + Condition cond = new Condition(); + cond.setKey("batteryVoltage"); + cond.setOperator("="); + cond.setValue("40"); + cond.setState(Condition.State.OR); + conditions.add(cond); + + context.setConditions(conditions); + + SearchManagerService service = new SearchManagerServiceImpl(); + List devices = service.search(context); + Assert.assertTrue(devices != null); + } + + @Test(description = "Search devices by location") + public void doValidLocationSearch() throws Exception { + SearchContext context = new SearchContext(); + List conditions = new ArrayList<>(); + + Condition cond = new Condition(); + cond.setKey("LOCATION"); + cond.setOperator("="); + cond.setValue("Karandeniya"); + cond.setState(Condition.State.AND); + conditions.add(cond); + + context.setConditions(conditions); + + SearchManagerService service = new SearchManagerServiceImpl(); + List devices = service.search(context); + Assert.assertTrue(devices != null); + } + + @Test(description = "Search devices by location.") + public void doInvalidLocationSearch() throws Exception { + SearchContext context = new SearchContext(); + List conditions = new ArrayList<>(); + + Condition cond = new Condition(); + cond.setKey("LOCATION"); + cond.setOperator("="); + cond.setValue("Colombo"); + cond.setState(Condition.State.AND); + conditions.add(cond); + + context.setConditions(conditions); + + SearchManagerService service = new SearchManagerServiceImpl(); + List devices = service.search(context); + Assert.assertTrue(devices.size() == 0); + } + + @Test + public void testStringSearch() throws Exception { + SearchContext context = new SearchContext(); + List conditions = new ArrayList<>(); + + Condition cond = new Condition(); + cond.setKey("deviceModel"); + cond.setOperator("="); + cond.setValue("SM-T520"); + cond.setState(Condition.State.AND); + conditions.add(cond); + + context.setConditions(conditions); + + SearchManagerService service = new SearchManagerServiceImpl(); + List devices = service.search(context); + + Assert.assertTrue(devices != null); + } + + @Test(expectedExceptions = {SearchMgtException.class}) + public void testInvalidOperator() throws SearchMgtException { + SearchContext context = new SearchContext(); + List conditions = new ArrayList<>(); + + Condition cond = new Condition(); + cond.setKey("deviceModel"); + cond.setOperator("=/"); + cond.setValue("SM-T520"); + cond.setState(Condition.State.OR); + conditions.add(cond); + + context.setConditions(conditions); + + SearchManagerService service = new SearchManagerServiceImpl(); + List devices = service.search(context); + + Assert.assertTrue(devices != null); + } + + @Test(description = "Test for search updated devices in given time.") + public void testGetUpdatedDevices() throws SearchMgtException { + SearchManagerService service = new SearchManagerServiceImpl(); + List updatedDevices = service.getUpdated(Calendar.getInstance().getTimeInMillis()); + Assert.assertEquals(updatedDevices.size(), 0); + } + + @Test(expectedExceptions = {SearchMgtException.class}, description = "Test for invalid number") + public void testInvalidNumber() throws SearchMgtException { + SearchContext context = new SearchContext(); + List conditions = new ArrayList<>(); + + Condition cond = new Condition(); + cond.setKey("batteryLevel"); + cond.setOperator("="); + cond.setValue("bbb"); + cond.setState(Condition.State.OR); + conditions.add(cond); + + context.setConditions(conditions); + + SearchManagerService service = new SearchManagerServiceImpl(); + service.search(context); + } + + @Test(description = "Test multiple search conditions") + public void testMultipleConditions() throws SearchMgtException { + SearchContext context = new SearchContext(); + List conditions = new ArrayList<>(); + + Condition cond = new Condition(); + cond.setKey("batteryLevel"); + cond.setOperator("%"); + cond.setValue("40"); + cond.setState(Condition.State.OR); + conditions.add(cond); + + Condition cond2 = new Condition(); + cond2.setKey("availableTotalMemory"); + cond2.setOperator("="); + cond2.setValue("40.0"); + cond2.setState(Condition.State.OR); + conditions.add(cond2); + + Condition cond3 = new Condition(); + cond3.setKey("LOCATION"); + cond3.setOperator("="); + cond3.setValue("Karandeniya"); + cond3.setState(Condition.State.OR); + conditions.add(cond3); + + Condition cond4 = new Condition(); + cond4.setKey("deviceModel"); + cond4.setOperator("%"); + cond4.setValue("SM-T520"); + cond4.setState(Condition.State.AND); + conditions.add(cond4); + + Condition cond5 = new Condition(); + cond5.setKey("vendor"); + cond5.setOperator("%"); + cond5.setValue("Samsung"); + cond5.setState(Condition.State.AND); + conditions.add(cond5); + + Condition cond6 = new Condition(); + cond6.setKey("osVersion"); + cond6.setOperator("%"); + cond6.setValue("Marshmellow"); + cond6.setState(Condition.State.OR); + conditions.add(cond6); + + context.setConditions(conditions); + + SearchManagerService service = new SearchManagerServiceImpl(); + List devices = service.search(context); + Assert.assertTrue(devices != null); + } + + @Test(description = "Test with wildcard operator") + public void testWithWildcardOperator() throws SearchMgtException { + SearchContext context = new SearchContext(); + List conditions = new ArrayList<>(); + + Condition cond = new Condition(); + cond.setKey("batteryLevel"); + cond.setOperator("="); + cond.setValue("40"); + cond.setState(Condition.State.AND); + conditions.add(cond); + + Condition condition2 = new Condition(); + condition2.setKey("LOCATION"); + condition2.setOperator("%"); + condition2.setValue("Karandeniya"); + condition2.setState(Condition.State.OR); + + Condition condition3 = new Condition(); + condition3.setKey("internalAvailableMemory"); + condition3.setOperator("!="); + condition3.setValue("23.2"); + condition2.setState(Condition.State.AND); + + Condition condition4 = new Condition(); + condition4.setKey("connectionType"); + condition4.setOperator("%"); + condition4.setValue("DIALOG"); + condition4.setState(Condition.State.AND); + + conditions.add(condition2); + + context.setConditions(conditions); + + SearchManagerService service = new SearchManagerServiceImpl(); + List devices = service.search(context); + Assert.assertTrue(devices != null); + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/Utils.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/Utils.java index 58c6f621ac..c46844dee4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/Utils.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/Utils.java @@ -15,30 +15,33 @@ * specific language governing permissions and limitations * under the License. */ - - package org.wso2.carbon.device.mgt.core.search.util; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation; +import org.wso2.carbon.device.mgt.common.search.Condition; +import org.wso2.carbon.device.mgt.common.search.SearchContext; import org.wso2.carbon.device.mgt.core.common.TestDataHolder; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; +import static org.wso2.carbon.device.mgt.common.search.Condition.State.AND; +import static org.wso2.carbon.device.mgt.common.search.Condition.State.OR; + public class Utils { public static DeviceInfo getDeviceInfo() { - DeviceInfo deviceInfo = new DeviceInfo(); deviceInfo.setIMSI("e6f236ac82537a8e"); deviceInfo.setSsid("FAFDA"); - deviceInfo.setAvailableRAMMemory(1.24); - deviceInfo.setBatteryLevel(27.3); + deviceInfo.setBatteryLevel(40.0); deviceInfo.setConnectionType("GSM"); deviceInfo.setCpuUsage(82.34); deviceInfo.setDeviceModel("SM-T520"); @@ -56,7 +59,7 @@ public class Utils { deviceInfo.setSsid("SSSSSS"); deviceInfo.setTotalRAMMemory(4.00); deviceInfo.setVendor("SAMSUNG"); - + deviceInfo.setLocation(getSampleDeviceLocation()); Map propertyMap = new HashMap<>(); @@ -75,8 +78,6 @@ public class Utils { public static DeviceLocation getSampleDeviceLocation(){ - - DeviceLocation deviceLocation = new DeviceLocation(); deviceLocation.setDeviceIdentifier(Utils.getDeviceIdentifier()); deviceLocation.setLatitude(76.2422); 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 7830b7fbe3..01752512bb 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 @@ -50,6 +50,8 @@ + + From 6c0d0f89a41bf7443371f1b6f3863565e97a6d0f Mon Sep 17 00:00:00 2001 From: Menaka Jayawardena Date: Wed, 11 Oct 2017 18:54:47 +0530 Subject: [PATCH 04/11] Refactoring --- .../service/DeviceManagementProviderServiceImpl.java | 5 ++++- .../mgt/core/search/SearchManagementServiceTest.java | 10 +++++++--- .../src/test/resources/testng.xml | 2 -- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 5d28eb34af..5244a74201 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -280,7 +280,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv try { deviceInformationManager.addDeviceInfo(deviceIdentifier, device.getDeviceInfo()); } catch (DeviceDetailsMgtException e) { - e.printStackTrace(); + String msg = "Error occurred while adding device info for the device " + + device.getDeviceIdentifier(); + log.error(msg, e); + throw new DeviceManagementException(msg, e); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchManagementServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchManagementServiceTest.java index aa179b967a..242df3cecb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchManagementServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchManagementServiceTest.java @@ -43,12 +43,16 @@ import java.util.ArrayList; import java.util.Calendar; import java.util.List; + +/** + * This class contains unit tests for the class SearchManagerService + * */ public class SearchManagementServiceTest extends BaseDeviceManagementTest { private static final Log log = LogFactory.getLog(SearchManagementServiceTest.class); private static List deviceIdentifiers = new ArrayList<>(); - private static String DEVICE_ID_PREFIX = "SEARCH-DEVICE-ID-"; - private static String DEVICE_TYPE = "SEARCH_TYPE"; + private static final String DEVICE_ID_PREFIX = "SEARCH-DEVICE-ID-"; + private static final String DEVICE_TYPE = "SEARCH_TYPE"; @BeforeClass public void init() throws Exception { @@ -133,7 +137,7 @@ public class SearchManagementServiceTest extends BaseDeviceManagementTest { Assert.assertTrue(devices.size() == 0); } - @Test + @Test(description = "Search devices by string parameter.") public void testStringSearch() throws Exception { SearchContext context = new SearchContext(); List conditions = new ArrayList<>(); 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 01752512bb..e3c165ca99 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 @@ -32,7 +32,6 @@ - @@ -50,7 +49,6 @@ - From f35bab013310a0f51dbe944ef159a20f38ccb54d Mon Sep 17 00:00:00 2001 From: Menaka Jayawardena Date: Thu, 12 Oct 2017 11:30:16 +0530 Subject: [PATCH 05/11] Changed the type of exception. --- .../device/mgt/core/search/SearchManagementServiceTest.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchManagementServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchManagementServiceTest.java index 242df3cecb..46eac34837 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchManagementServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchManagementServiceTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * 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 @@ -43,7 +43,6 @@ import java.util.ArrayList; import java.util.Calendar; import java.util.List; - /** * This class contains unit tests for the class SearchManagerService * */ @@ -184,7 +183,7 @@ public class SearchManagementServiceTest extends BaseDeviceManagementTest { Assert.assertEquals(updatedDevices.size(), 0); } - @Test(expectedExceptions = {SearchMgtException.class}, description = "Test for invalid number") + @Test(expectedExceptions = {NumberFormatException.class}, description = "Test for invalid number") public void testInvalidNumber() throws SearchMgtException { SearchContext context = new SearchContext(); List conditions = new ArrayList<>(); From c850962783a147194961cd6eaad3c99bfdfdcbd2 Mon Sep 17 00:00:00 2001 From: Menaka Jayawardena Date: Thu, 12 Oct 2017 11:45:36 +0530 Subject: [PATCH 06/11] Made tests more specific. --- .../search/SearchManagementServiceTest.java | 60 +++++++++++++------ 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchManagementServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchManagementServiceTest.java index 46eac34837..307441a812 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchManagementServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchManagementServiceTest.java @@ -156,6 +156,26 @@ public class SearchManagementServiceTest extends BaseDeviceManagementTest { Assert.assertTrue(devices != null); } + @Test(description = "Search devices by Double parameter.") + public void testDoubleSearch() throws Exception { + SearchContext context = new SearchContext(); + List conditions = new ArrayList<>(); + + Condition cond = new Condition(); + cond.setKey("internalAvailableMemory"); + cond.setOperator("="); + cond.setValue("3.56"); + cond.setState(Condition.State.AND); + conditions.add(cond); + + context.setConditions(conditions); + + SearchManagerService service = new SearchManagerServiceImpl(); + List devices = service.search(context); + + Assert.assertTrue(devices != null); + } + @Test(expectedExceptions = {SearchMgtException.class}) public void testInvalidOperator() throws SearchMgtException { SearchContext context = new SearchContext(); @@ -183,8 +203,8 @@ public class SearchManagementServiceTest extends BaseDeviceManagementTest { Assert.assertEquals(updatedDevices.size(), 0); } - @Test(expectedExceptions = {NumberFormatException.class}, description = "Test for invalid number") - public void testInvalidNumber() throws SearchMgtException { + @Test(description = "Test for invalid number") + public void testInvalidNumber() { SearchContext context = new SearchContext(); List conditions = new ArrayList<>(); @@ -198,7 +218,12 @@ public class SearchManagementServiceTest extends BaseDeviceManagementTest { context.setConditions(conditions); SearchManagerService service = new SearchManagerServiceImpl(); - service.search(context); + try { + service.search(context); + } catch (SearchMgtException e) { + String expectedException = e.getCause().getClass().getName(); + Assert.assertTrue(expectedException.contains("InvalidOperatorException")); + } } @Test(description = "Test multiple search conditions") @@ -229,21 +254,21 @@ public class SearchManagementServiceTest extends BaseDeviceManagementTest { Condition cond4 = new Condition(); cond4.setKey("deviceModel"); - cond4.setOperator("%"); + cond4.setOperator("="); cond4.setValue("SM-T520"); cond4.setState(Condition.State.AND); conditions.add(cond4); Condition cond5 = new Condition(); cond5.setKey("vendor"); - cond5.setOperator("%"); + cond5.setOperator("="); cond5.setValue("Samsung"); cond5.setState(Condition.State.AND); conditions.add(cond5); Condition cond6 = new Condition(); cond6.setKey("osVersion"); - cond6.setOperator("%"); + cond6.setOperator("="); cond6.setValue("Marshmellow"); cond6.setState(Condition.State.OR); conditions.add(cond6); @@ -260,32 +285,33 @@ public class SearchManagementServiceTest extends BaseDeviceManagementTest { SearchContext context = new SearchContext(); List conditions = new ArrayList<>(); - Condition cond = new Condition(); - cond.setKey("batteryLevel"); - cond.setOperator("="); - cond.setValue("40"); - cond.setState(Condition.State.AND); - conditions.add(cond); + Condition condition = new Condition(); + condition.setKey("batteryLevel"); + condition.setOperator("="); + condition.setValue("40"); + condition.setState(Condition.State.AND); + conditions.add(condition); Condition condition2 = new Condition(); condition2.setKey("LOCATION"); condition2.setOperator("%"); condition2.setValue("Karandeniya"); condition2.setState(Condition.State.OR); + conditions.add(condition2); Condition condition3 = new Condition(); - condition3.setKey("internalAvailableMemory"); - condition3.setOperator("!="); + condition3.setKey("internalTotalMemory"); + condition3.setOperator("%"); condition3.setValue("23.2"); - condition2.setState(Condition.State.AND); + condition3.setState(Condition.State.OR); + conditions.add(condition3); Condition condition4 = new Condition(); condition4.setKey("connectionType"); condition4.setOperator("%"); condition4.setValue("DIALOG"); condition4.setState(Condition.State.AND); - - conditions.add(condition2); + conditions.add(condition4); context.setConditions(conditions); From fd0e08a063a1fc96a844739e23fa4bedb5ca6247 Mon Sep 17 00:00:00 2001 From: Menaka Jayawardena Date: Thu, 12 Oct 2017 11:51:03 +0530 Subject: [PATCH 07/11] Added test cased for Search Management Utils. --- .../mgt/core/search/SearchMgtUtilTest.java | 115 ++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchMgtUtilTest.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchMgtUtilTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchMgtUtilTest.java new file mode 100644 index 0000000000..df99f82bff --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchMgtUtilTest.java @@ -0,0 +1,115 @@ +/* + * 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.search; + +import org.junit.Assert; +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.DeviceIdentifier; +import org.wso2.carbon.device.mgt.core.common.TestDataHolder; +import org.wso2.carbon.device.mgt.core.search.mgt.impl.Utils; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * This class holds the Unit test cases to test org.wso2.carbon.device.mgt.core.search.mgt.impl.Util + * */ +public class SearchMgtUtilTest { + + private static List deviceIdentifiers = new ArrayList<>(); + private static final String DEVICE_ID_PREFIX = "SEARCH-DEVICE-ID-"; + private static final String DEVICE_TYPE = "SEARCH_TYPE"; + private static final String DEVICE_IDS = "0,0,0,0,0"; + private static final Integer[] DEVICE_IDS_INT = {0,0,0,0,0}; + private List devices; + + @BeforeClass + public void init() throws Exception { + for (int i = 0; i < 5; i++) { + deviceIdentifiers.add(new DeviceIdentifier(DEVICE_ID_PREFIX + i, DEVICE_TYPE)); + } + devices = TestDataHolder.generateDummyDeviceData(deviceIdentifiers); + } + + @Test(description = "Test for converting given devices list to string") + public void testConvertDeviceListToString() { + String ids = Utils.getDeviceIdsAsString(devices); + Assert.assertEquals(ids, DEVICE_IDS); + } + + @Test(description = "Test for get all the device ids as an array") + public void testGetArrayOfDeviceIds() { + Integer[] deviceIds = Utils.getArrayOfDeviceIds(devices); + Assert.assertArrayEquals(deviceIds, DEVICE_IDS_INT); + } + + @Test(description = "Test to convert given String to a List") + public void testConvertStringToList() { + List stringList = Utils.convertStringToList("some string"); + List expected = this.getStringList(); + + Assert.assertEquals(stringList, expected); + } + + @Test(description = "Test to check what type of data the specified column can hold") + public void testColumnTypes() { + Map colTypes = this.buildColumnMap(); + + for (String key : colTypes.keySet()) { + String result = Utils.checkColumnType(key); + Assert.assertEquals(result, colTypes.get(key)); + } + } + + /** + * Generates a map of columns and particular data type. + * @return HashMap of column name and data type. + * */ + private Map buildColumnMap() { + Map columnTypes = new HashMap<>(); + + columnTypes.put("deviceModel", "String"); + columnTypes.put("vendor", "String"); + columnTypes.put("osVersion", "String"); + columnTypes.put("connectionType", "String"); + columnTypes.put("ssid", "String"); + columnTypes.put("imei", "String"); + columnTypes.put("imsi", "String"); + columnTypes.put("batteryLevel", "Double"); + columnTypes.put("externalAvailableMemory", "Double"); + columnTypes.put("externalTotalMemory", "Double"); + columnTypes.put("internalAvailableMemory", "Double"); + columnTypes.put("cpuUsage", "Double"); + columnTypes.put("someProperty", "String"); + return columnTypes; + } + + /** + * Generates a list of Strings. + * @return List + * */ + private List getStringList() { + List strings = new ArrayList<>(); + strings.add("some string"); + return strings; + } +} From 4d3645549201d54c79025f0e554f5debb1e373a4 Mon Sep 17 00:00:00 2001 From: Menaka Jayawardena Date: Thu, 12 Oct 2017 11:51:36 +0530 Subject: [PATCH 08/11] Added test cases for ProcessorImpl. --- .../mgt/core/search/ProcessorImplTest.java | 173 ++++++++++++++++++ .../core/search/util/ChangeEnumValues.java | 132 +++++++++++++ 2 files changed, 305 insertions(+) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/ProcessorImplTest.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/ChangeEnumValues.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/ProcessorImplTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/ProcessorImplTest.java new file mode 100644 index 0000000000..32d0d365e3 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/ProcessorImplTest.java @@ -0,0 +1,173 @@ +/* + * 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.search; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.powermock.api.mockito.PowerMockito; +import org.testng.Assert; +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.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService; +import org.wso2.carbon.device.mgt.common.search.Condition; +import org.wso2.carbon.device.mgt.common.search.SearchContext; +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.dao.DeviceManagementDAOFactory; +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.search.mgt.SearchMgtException; +import org.wso2.carbon.device.mgt.core.search.mgt.impl.ProcessorImpl; +import org.wso2.carbon.device.mgt.core.search.util.ChangeEnumValues; +import org.wso2.carbon.device.mgt.core.search.util.Utils; +import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; +import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; +import org.wso2.carbon.utils.multitenancy.MultitenantConstants; + +import javax.sql.DataSource; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * This class holds unit test cases for org.wso2.carbon.device.mgt.core.search.mgt.impl.ProcessorImpl + * */ +public class ProcessorImplTest extends BaseDeviceManagementTest{ + + private DeviceAccessAuthorizationService deviceAccessAuthorizationService; + private static final Log log = LogFactory.getLog(SearchManagementServiceTest.class); + private static List deviceIdentifiers = new ArrayList<>(); + private static final String DEVICE_ID_PREFIX = "SEARCH-DEVICE-ID-"; + private static final String DEVICE_TYPE = "SEARCH_TYPE"; + + @BeforeClass + public void init() throws Exception { + deviceAccessAuthorizationService = DeviceManagementDataHolder.getInstance() + .getDeviceAccessAuthorizationService(); + + for (int i = 0; i < 5; i++) { + deviceIdentifiers.add(new DeviceIdentifier(DEVICE_ID_PREFIX + i, DEVICE_TYPE)); + } + DeviceManagementProviderService deviceMgtService = new DeviceManagementProviderServiceImpl(); + DeviceManagementServiceComponent.notifyStartupListeners(); + DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(deviceMgtService); + deviceMgtService.registerDeviceType(new TestDeviceManagementService(DEVICE_TYPE, + MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)); + + List devices = TestDataHolder.generateDummyDeviceData(deviceIdentifiers); + for (Device device : devices) { + device.setDeviceInfo(Utils.getDeviceInfo()); + deviceMgtService.enrollDevice(device); + } + List returnedDevices = deviceMgtService.getAllDevices(DEVICE_TYPE, true); + for (Device device : returnedDevices) { + if (!device.getDeviceIdentifier().startsWith(DEVICE_ID_PREFIX)) { + throw new Exception("Incorrect device with ID - " + device.getDeviceIdentifier() + " returned!"); + } + } + } + + @Test(description = "Test the Search Processor") + public void testWithNoDeviceAccessAuthorization() throws Exception { + SearchContext context = new SearchContext(); + List conditions = new ArrayList<>(); + + Condition cond = new Condition(); + cond.setKey("batteryLevel"); + cond.setOperator("="); + cond.setValue("40"); + cond.setState(Condition.State.AND); + conditions.add(cond); + + context.setConditions(conditions); + + ProcessorImpl processor = new ProcessorImpl(); + Field deviceAccessAuthorizationServiceField = ProcessorImpl.class.getDeclaredField + ("deviceAccessAuthorizationService"); + deviceAccessAuthorizationServiceField.setAccessible(true); + deviceAccessAuthorizationServiceField.set(processor, null); + + List searchedDevices = processor.execute(context); + + Assert.assertEquals(0, searchedDevices.size()); + } + + + @Test(description = "Test for invalid state") + public void testInvalidState() { + SearchContext context = new SearchContext(); + List conditions = new ArrayList<>(); + + ChangeEnumValues.addEnum(Condition.State.class, "BLA"); + + Condition.State state = Condition.State.valueOf("BLA"); + + Condition cond = new Condition(); + cond.setKey("batteryLevel"); + cond.setOperator("="); + cond.setValue("40"); + cond.setState(Condition.State.AND); + conditions.add(cond); + + Condition cond2 = new Condition(); + cond2.setKey("LOCATION"); + cond2.setOperator("="); + cond2.setValue("Karandeniya"); + cond2.setState(Condition.State.AND); + conditions.add(cond2); + + Condition cond3 = new Condition(); + cond3.setKey("batteryLevel"); + cond3.setOperator("="); + cond3.setValue("23.0"); + cond3.setState(state); + conditions.add(cond3); + + context.setConditions(conditions); + + ProcessorImpl processor = new ProcessorImpl(); + try { + processor.execute(context); + } catch (SearchMgtException e) { + Boolean isInvalidOperation = e.getCause().getClass().getCanonicalName().contains + ("InvalidOperatorException"); + Assert.assertTrue(isInvalidOperation); + } + } + + @Test(description = "Test when Device Access Authorization is null", expectedExceptions = {IllegalStateException + .class}, dependsOnMethods = {"testWithNoDeviceAccessAuthorization", "testInvalidState"}) + public void testProcessorInitializationError() throws ClassNotFoundException, NoSuchMethodException, + NoSuchFieldException, + IllegalAccessException, SearchMgtException { + DeviceManagementDataHolder deviceManagementDataHolder = DeviceManagementDataHolder.getInstance(); + Field field = DeviceManagementDataHolder.class.getDeclaredField("deviceAccessAuthorizationService"); + field.setAccessible(true); + field.set(deviceManagementDataHolder, null); + + ProcessorImpl processor = new ProcessorImpl(); + processor.execute(null); + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/ChangeEnumValues.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/ChangeEnumValues.java new file mode 100644 index 0000000000..1aaa317861 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/ChangeEnumValues.java @@ -0,0 +1,132 @@ +/* + * 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.search.util; + +import org.wso2.carbon.device.mgt.common.search.Condition; +import sun.reflect.ConstructorAccessor; +import sun.reflect.FieldAccessor; +import sun.reflect.ReflectionFactory; + +import java.lang.reflect.AccessibleObject; +import java.lang.reflect.Array; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * Changes the Enum values of a given class. + */ +public class ChangeEnumValues { + + private static ReflectionFactory reflectionFactory = ReflectionFactory.getReflectionFactory(); + + public ChangeEnumValues() {} + + private static void setFailSafeFieldValue(Field field, Object target, Object value) throws NoSuchFieldException, + IllegalAccessException { + + field.setAccessible(true); + + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + int modifiers = modifiersField.getInt(field); + + modifiers &= ~Modifier.FINAL; + modifiersField.setInt(field, modifiers); + + FieldAccessor fa = reflectionFactory.newFieldAccessor(field, false); + fa.set(target, value); + } + + private static void blankField(Class enumClass, String fieldName) throws NoSuchFieldException, + IllegalAccessException { + for (Field field : Class.class.getDeclaredFields()) { + if (field.getName().contains(fieldName)) { + AccessibleObject.setAccessible(new Field[]{field}, true); + setFailSafeFieldValue(field, enumClass, null); + break; + } + } + } + + private static void cleanEnumCache(Class enumClass) throws NoSuchFieldException, IllegalAccessException { + blankField(enumClass, "enumConstantDirectory"); + blankField(enumClass, "enumConstants"); + } + + private static ConstructorAccessor getConstructorAccessor(Class enumClass, Class[] additionalParameterTypes) + throws NoSuchMethodException { + Class[] parameterTypes = new Class[additionalParameterTypes.length + 2]; + parameterTypes[0] = String.class; + parameterTypes[1] = int.class; + System.arraycopy(additionalParameterTypes, 0, parameterTypes, 2, additionalParameterTypes.length); + return reflectionFactory.newConstructorAccessor(enumClass.getDeclaredConstructor(parameterTypes)); + } + + private static Object makeEnum(Class enumClass, String value, int ordinal, Class[] additionalTypes, + Object[] additionalValues) throws Exception { + Object[] parms = new Object[additionalValues.length + 2]; + parms[0] = value; + parms[1] = ordinal; + System.arraycopy(additionalValues, 0, parms, 2, additionalValues.length); + return enumClass.cast(getConstructorAccessor(enumClass, additionalTypes).newInstance(parms)); + } + + /** + * Add an enum instance to the enum class given as argument + * + * @param the type of the enum + * @param enumType the class of the enum to be modified + * @param enumName the name of the new enum instance to be added to the class. + */ + public static > void addEnum(Class enumType, String enumName) { + + if (!Enum.class.isAssignableFrom(enumType)) { + throw new RuntimeException("class " + enumType + " is not an instance of Enum"); + } + + Field valuesField = null; + Field[] fields = Condition.State.class.getDeclaredFields(); + for (Field field : fields) { + if (field.getName().contains("$VALUES")) { + valuesField = field; + break; + } + } + AccessibleObject.setAccessible(new Field[]{valuesField}, true); + + try { + T[] previousValues = (T[]) valuesField.get(enumType); + List values = new ArrayList(Arrays.asList(previousValues)); + + T newValue = (T) makeEnum(enumType, enumName, values.size(), new Class[]{}, new Object[]{}); + + values.add(newValue); + + setFailSafeFieldValue(valuesField, null, values.toArray((T[]) Array.newInstance(enumType, 0))); + + cleanEnumCache(enumType); + + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException(e.getMessage(), e); + } + } +} \ No newline at end of file From 0578307a795ab0809429a33ffb7b40b84afb7fd1 Mon Sep 17 00:00:00 2001 From: Menaka Jayawardena Date: Thu, 12 Oct 2017 11:54:35 +0530 Subject: [PATCH 09/11] Modified testng.xml --- .../src/test/resources/testng.xml | 2 ++ 1 file changed, 2 insertions(+) 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 e3c165ca99..8785463a18 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 @@ -50,6 +50,8 @@ + + From aee8995ff6dd36922a37ca4bf2c2f197534bf24a Mon Sep 17 00:00:00 2001 From: Menaka Jayawardena Date: Thu, 12 Oct 2017 11:57:32 +0530 Subject: [PATCH 10/11] Code formatting. --- .../carbon/device/mgt/core/search/util/ChangeEnumValues.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/ChangeEnumValues.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/ChangeEnumValues.java index 1aaa317861..3fa378a005 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/ChangeEnumValues.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/ChangeEnumValues.java @@ -129,4 +129,4 @@ public class ChangeEnumValues { throw new RuntimeException(e.getMessage(), e); } } -} \ No newline at end of file +} From ae9c1a2c8ba10fb2c60654d41657bf1093715b4a Mon Sep 17 00:00:00 2001 From: Menaka Jayawardena Date: Thu, 12 Oct 2017 12:21:05 +0530 Subject: [PATCH 11/11] Removed print stacktrace and changed the check for exceptions. --- .../carbon/device/mgt/core/search/ProcessorImplTest.java | 9 +++++---- .../mgt/core/search/SearchManagementServiceTest.java | 8 +++++--- .../device/mgt/core/search/util/ChangeEnumValues.java | 1 - 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/ProcessorImplTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/ProcessorImplTest.java index 32d0d365e3..4e5f26a548 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/ProcessorImplTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/ProcessorImplTest.java @@ -34,6 +34,7 @@ import org.wso2.carbon.device.mgt.core.common.TestDataHolder; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; 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.search.mgt.InvalidOperatorException; import org.wso2.carbon.device.mgt.core.search.mgt.SearchMgtException; import org.wso2.carbon.device.mgt.core.search.mgt.impl.ProcessorImpl; import org.wso2.carbon.device.mgt.core.search.util.ChangeEnumValues; @@ -116,7 +117,7 @@ public class ProcessorImplTest extends BaseDeviceManagementTest{ @Test(description = "Test for invalid state") - public void testInvalidState() { + public void testInvalidState() throws SearchMgtException { SearchContext context = new SearchContext(); List conditions = new ArrayList<>(); @@ -151,9 +152,9 @@ public class ProcessorImplTest extends BaseDeviceManagementTest{ try { processor.execute(context); } catch (SearchMgtException e) { - Boolean isInvalidOperation = e.getCause().getClass().getCanonicalName().contains - ("InvalidOperatorException"); - Assert.assertTrue(isInvalidOperation); + if (!(e.getCause() instanceof InvalidOperatorException)) { + throw e; + } } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchManagementServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchManagementServiceTest.java index 307441a812..9e13782945 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchManagementServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchManagementServiceTest.java @@ -31,6 +31,7 @@ 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.internal.DeviceManagementServiceComponent; +import org.wso2.carbon.device.mgt.core.search.mgt.InvalidOperatorException; import org.wso2.carbon.device.mgt.core.search.mgt.SearchManagerService; import org.wso2.carbon.device.mgt.core.search.mgt.SearchMgtException; import org.wso2.carbon.device.mgt.core.search.mgt.impl.SearchManagerServiceImpl; @@ -204,7 +205,7 @@ public class SearchManagementServiceTest extends BaseDeviceManagementTest { } @Test(description = "Test for invalid number") - public void testInvalidNumber() { + public void testInvalidNumber() throws SearchMgtException { SearchContext context = new SearchContext(); List conditions = new ArrayList<>(); @@ -221,8 +222,9 @@ public class SearchManagementServiceTest extends BaseDeviceManagementTest { try { service.search(context); } catch (SearchMgtException e) { - String expectedException = e.getCause().getClass().getName(); - Assert.assertTrue(expectedException.contains("InvalidOperatorException")); + if (!(e.getCause() instanceof InvalidOperatorException)) { + throw e; + } } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/ChangeEnumValues.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/ChangeEnumValues.java index 3fa378a005..2d0163e7c3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/ChangeEnumValues.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/ChangeEnumValues.java @@ -125,7 +125,6 @@ public class ChangeEnumValues { cleanEnumCache(enumType); } catch (Exception e) { - e.printStackTrace(); throw new RuntimeException(e.getMessage(), e); } }