From 62d30cbf84ff68195c1b29298512a333ff05c25e Mon Sep 17 00:00:00 2001 From: Menaka Jayawardena Date: Wed, 11 Oct 2017 17:08:38 +0530 Subject: [PATCH] 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 @@ + +