From 4f47b63bfd4072b3814f7a852035b7bd313a4e17 Mon Sep 17 00:00:00 2001 From: Pasindu Date: Fri, 29 Sep 2017 12:04:03 +0530 Subject: [PATCH] Adding more test cases to GeoLocationProviderService --- .../org.wso2.carbon.device.mgt.core/pom.xml | 3 +- .../core/common/BaseDeviceManagementTest.java | 2 +- .../mgt/core/common/TestDataHolder.java | 15 ++ .../GeoLocationProviderServiceTest.java | 132 ++++++++++-------- 4 files changed, 88 insertions(+), 64 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml index f5f07e1b32..aff01d97ad 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml @@ -357,7 +357,8 @@ javassist - javassist + javassist + org.powermock powermock-api-mockito 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 e5c21cdcd8..51789b1fef 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 @@ -80,7 +80,7 @@ public abstract class BaseDeviceManagementTest { NotificationManagementDAOFactory.init(dataSource); } - private void initServices() throws DeviceManagementException, RegistryException { + protected void initServices() throws DeviceManagementException, RegistryException { DeviceConfigurationManager.getInstance().initConfig(); DeviceManagementProviderService deviceMgtService = new DeviceManagementProviderServiceImpl(); DeviceManagementServiceComponent.notifyStartupListeners(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDataHolder.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDataHolder.java index a23fdbc638..5a0be001ec 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDataHolder.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDataHolder.java @@ -98,6 +98,21 @@ public class TestDataHolder { return devices; } + public static Device generateDummyDeviceData(DeviceIdentifier deviceIdentifier) { + Device device = new Device(); + EnrolmentInfo enrolmentInfo = new EnrolmentInfo(); + enrolmentInfo.setDateOfEnrolment(new Date().getTime()); + enrolmentInfo.setDateOfLastUpdate(new Date().getTime()); + enrolmentInfo.setOwner(OWNER); + enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.BYOD); + enrolmentInfo.setStatus(EnrolmentInfo.Status.CREATED); + device.setEnrolmentInfo(enrolmentInfo); + device.setDescription("Test Description"); + device.setDeviceIdentifier(deviceIdentifier.getId()); + device.setType(deviceIdentifier.getType()); + return device; + } + public static DeviceType generateDeviceTypeData(String devTypeName) { DeviceType deviceType = new DeviceType(); deviceType.setName(devTypeName); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceTest.java index 1c184d3e51..50398432e7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceTest.java @@ -1,10 +1,24 @@ +/* + * 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.geo.service; -import org.junit.runner.RunWith; import org.mockito.Mockito; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.powermock.core.classloader.annotations.PrepareForTest; import org.testng.Assert; import org.testng.annotations.BeforeClass; @@ -15,33 +29,18 @@ import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.geo.service.Alert; import org.wso2.carbon.device.mgt.common.geo.service.GeoFence; import org.wso2.carbon.device.mgt.core.TestDeviceManagementService; -import org.wso2.carbon.device.mgt.core.authorization.DeviceAccessAuthorizationServiceImpl; 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.service.DeviceManagementProviderService; -import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; -import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderServiceImpl; import org.wso2.carbon.event.processor.stub.EventProcessorAdminServiceStub; import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException; -import org.wso2.carbon.registry.core.config.RegistryContext; -import org.wso2.carbon.registry.core.exceptions.RegistryException; -import org.wso2.carbon.registry.core.internal.RegistryDataHolder; -import org.wso2.carbon.registry.core.jdbc.realm.InMemoryRealmService; -import org.wso2.carbon.registry.core.service.RegistryService; -import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; -import java.io.InputStream; import java.rmi.RemoteException; import java.util.List; -import static org.powermock.api.mockito.PowerMockito.*; - -@PrepareForTest(GeoLocationProviderServiceImpl.class) public class GeoLocationProviderServiceTest { - private static final Log log = LogFactory.getLog(GeoLocationProviderServiceTest.class); private static final String DEVICE_TYPE = "GL_TEST_TYPE"; private static final String DEVICE_ID = "GL-TEST-DEVICE-ID-1"; private static final String SAMPLE_GEO_JSON = "12121"; @@ -59,54 +58,52 @@ public class GeoLocationProviderServiceTest { @BeforeClass public void init() throws Exception { initMocks(); - enrollTestDevice(); + enrollDev(); } - @Test + @Test (description = "Create a sample geo exit-alert with relevant details.") public void createGeoExitAlert() throws Exception { Boolean result = geoLocationProviderServiceImpl. - createGeoAlert(getTestAlert(), getDeviceIdentifier(), DeviceManagementConstants.GeoServices.ALERT_TYPE_EXIT); + createGeoAlert(getExitAlert(), getDeviceIdentifier(), DeviceManagementConstants.GeoServices.ALERT_TYPE_EXIT); Assert.assertEquals(result, Boolean.TRUE); - verifyPrivate(geoLocationProviderServiceImpl).invoke("getEventProcessorAdminServiceStub"); } - @Test + @Test (description = "Create a sample geo within-alert with relevant details.") public void createGeoWithinAlert() throws Exception { Boolean result = geoLocationProviderServiceImpl. - createGeoAlert(getTestAlert(), getDeviceIdentifier(), DeviceManagementConstants.GeoServices.ALERT_TYPE_WITHIN); + createGeoAlert(getWithinAlert(), getDeviceIdentifier(), DeviceManagementConstants.GeoServices.ALERT_TYPE_WITHIN); Assert.assertEquals(result, Boolean.TRUE); } - @Test + @Test (description = "Create a sample geo proximity-alert with relevant details.") public void createGeoProximityAlert() throws Exception { Boolean result = geoLocationProviderServiceImpl. createGeoAlert(getProximityAlert(), getDeviceIdentifier(), DeviceManagementConstants.GeoServices.ALERT_TYPE_PROXIMITY); Assert.assertEquals(result, Boolean.TRUE); } - @Test + @Test (description = "Create a sample geo speed-alert with relevant details.") public void createGeoSpeedAlert() throws Exception { Boolean result = geoLocationProviderServiceImpl. createGeoAlert(getSpeedAlert(), getDeviceIdentifier(), DeviceManagementConstants.GeoServices.ALERT_TYPE_SPEED); Assert.assertEquals(result, Boolean.TRUE); } - @Test + @Test (description = "Create a sample geo stationary-alert with relevant details.") public void createGeoStationaryAlert() throws Exception { Boolean result = geoLocationProviderServiceImpl. createGeoAlert(getStationaryAlert(), getDeviceIdentifier(), DeviceManagementConstants.GeoServices.ALERT_TYPE_STATIONARY); Assert.assertEquals(result, Boolean.TRUE); } - @Test + @Test (description = "Create a sample geo traffic-alert with relevant details.") public void createGeoTrafficAlert() throws Exception { Boolean result = geoLocationProviderServiceImpl. createGeoAlert(getTrafficAlert(), getDeviceIdentifier(), DeviceManagementConstants.GeoServices.ALERT_TYPE_TRAFFIC); Assert.assertEquals(result, Boolean.TRUE); } - //Test methods to retrieve saved Data. - @Test(dependsOnMethods = "createGeoExitAlert") + @Test(dependsOnMethods = "createGeoExitAlert", description = "retrieve saved geo exit-alert.") public void getGeoExitAlerts() throws Exception { List geoFences; geoFences = geoLocationProviderServiceImpl.getExitAlerts(getDeviceIdentifier()); @@ -117,7 +114,7 @@ public class GeoLocationProviderServiceTest { Assert.assertEquals(geoFenceNode.getQueryName(), SAMPLE_QUERY_NAME); } - @Test(dependsOnMethods = "createGeoWithinAlert") + @Test(dependsOnMethods = "createGeoWithinAlert", description = "retrieve saved geo within-alert.") public void getGeoWithinAlerts() throws Exception { List geoFences; geoFences = geoLocationProviderServiceImpl.getWithinAlerts(getDeviceIdentifier()); @@ -127,7 +124,7 @@ public class GeoLocationProviderServiceTest { Assert.assertEquals(geoFenceNode.getQueryName(), SAMPLE_QUERY_NAME); } - @Test(dependsOnMethods = "createGeoSpeedAlert") + @Test(dependsOnMethods = "createGeoSpeedAlert", description = "retrieve saved geo speed-alert.") public void getGeoSpeedAlerts() throws Exception { String result; result = geoLocationProviderServiceImpl.getSpeedAlerts(getDeviceIdentifier()); @@ -136,7 +133,7 @@ public class GeoLocationProviderServiceTest { SAMPLE_SPEED_ALERT_VALUE + "}"); } - @Test(dependsOnMethods = "createGeoTrafficAlert") + @Test(dependsOnMethods = "createGeoTrafficAlert" , description = "retrieve saved geo exit-alert.") public void getGeoTrafficAlerts() throws Exception { List geoFences; geoFences = geoLocationProviderServiceImpl.getTrafficAlerts(getDeviceIdentifier()); @@ -147,7 +144,7 @@ public class GeoLocationProviderServiceTest { "}"); } - @Test(dependsOnMethods = "createGeoStationaryAlert") + @Test(dependsOnMethods = "createGeoStationaryAlert", description = "retrieve saved geo stationary-alert.") public void getGeoStationaryAlerts() throws Exception { List geoFences; geoFences = geoLocationProviderServiceImpl.getStationaryAlerts(getDeviceIdentifier()); @@ -161,8 +158,10 @@ public class GeoLocationProviderServiceTest { private void initMocks() throws JWTClientException, RemoteException { mockEventProcessorAdminServiceStub = Mockito.mock(EventProcessorAdminServiceStub.class); geoLocationProviderServiceImpl = Mockito.mock(GeoLocationProviderServiceImpl.class, Mockito.CALLS_REAL_METHODS); - doReturn(mockEventProcessorAdminServiceStub).when(geoLocationProviderServiceImpl).getEventProcessorAdminServiceStub(); - doReturn("success").when(mockEventProcessorAdminServiceStub).validateExecutionPlan(Mockito.anyString()); + Mockito.doReturn(mockEventProcessorAdminServiceStub). + when(geoLocationProviderServiceImpl).getEventProcessorAdminServiceStub(); + Mockito.doReturn("success"). + when(mockEventProcessorAdminServiceStub).validateExecutionPlan(Mockito.anyString()); } private DeviceIdentifier getDeviceIdentifier() { @@ -171,31 +170,8 @@ public class GeoLocationProviderServiceTest { deviceIdentifier.setType("TEST"); return deviceIdentifier; } - private RegistryService getRegistryService() throws RegistryException { - RealmService realmService = new InMemoryRealmService(); - RegistryDataHolder.getInstance().setRealmService(realmService); - DeviceManagementDataHolder.getInstance().setRealmService(realmService); - InputStream is = this.getClass().getClassLoader().getResourceAsStream("carbon-home/repository/conf/registry.xml"); - RegistryContext context = RegistryContext.getBaseInstance(is, realmService); - context.setSetup(true); - return context.getEmbeddedRegistryService(); - } - - private void enrollTestDevice() throws Exception { - Device device = TestDataHolder.generateDummyDeviceData(DEVICE_ID); - DeviceManagementProviderService deviceMgtService = new DeviceManagementProviderServiceImpl(); - DeviceManagementServiceComponent.notifyStartupListeners(); - DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(deviceMgtService); - DeviceManagementDataHolder.getInstance().setRegistryService(getRegistryService()); - DeviceManagementDataHolder.getInstance().setDeviceAccessAuthorizationService(new DeviceAccessAuthorizationServiceImpl()); - DeviceManagementDataHolder.getInstance().setGroupManagementProviderService(new GroupManagementProviderServiceImpl()); - DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService(null); - deviceMgtService.registerDeviceType(new TestDeviceManagementService(DEVICE_TYPE, - MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)); - deviceMgtService.enrollDevice(device); - } - private Alert getTestAlert() { + private Alert getWithinAlert() { Alert alert = new Alert(); alert.setDeviceId(DEVICE_ID); alert.setCepAction("CEP_ACTION"); @@ -208,6 +184,20 @@ public class GeoLocationProviderServiceTest { return alert; } + private Alert getExitAlert() { + Alert alert = new Alert(); + alert.setDeviceId(DEVICE_ID); + alert.setQueryName(SAMPLE_QUERY_NAME); + alert.setCustomName(SAMPLE_AREA_NAME); + alert.setStationeryTime(SAMPLE_STATIONARY_TIME); + alert.setFluctuationRadius(SAMPLE_FLUCTUATION_RADIUS); + alert.setParseData("{\n" + + " \" " + DeviceManagementConstants.GeoServices.GEO_FENCE_GEO_JSON + "\": \"" + SAMPLE_GEO_JSON + "\"\n" + + "}"); + alert.setExecutionPlan("EXECUTION_PLAN"); + return alert; + } + private Alert getProximityAlert() { Alert alert = new Alert(); alert.setDeviceId(DEVICE_ID); @@ -220,7 +210,8 @@ public class GeoLocationProviderServiceTest { } private Alert getSpeedAlert() { - Alert alert = getTestAlert(); + Alert alert = new Alert(); + alert.setDeviceId(DEVICE_ID); alert.setParseData("{\n" + " \"" + DeviceManagementConstants.GeoServices.GEO_FENCE_GEO_JSON + "\": \"" + SAMPLE_GEO_JSON + "\",\n" + " \"" + DeviceManagementConstants.GeoServices.SPEED_ALERT_VALUE + "\": \"" + SAMPLE_SPEED_ALERT_VALUE + "\"\n" + @@ -252,4 +243,21 @@ public class GeoLocationProviderServiceTest { alert.setQueryName(SAMPLE_QUERY_NAME); return alert; } + + private void enrollDev() throws Exception { + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(DEVICE_ID, DEVICE_TYPE); + Device device = TestDataHolder.generateDummyDeviceData(deviceIdentifier); + DeviceManagementProviderService deviceMgtService = DeviceManagementDataHolder.getInstance(). + getDeviceManagementProvider(); + deviceMgtService.registerDeviceType(new TestDeviceManagementService(DEVICE_TYPE, + MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)); + deviceMgtService.enrollDevice(device); + + Device returnedDevice = deviceMgtService.getDevice(deviceIdentifier); + + if (!returnedDevice.getDeviceIdentifier().equals(deviceIdentifier.getId())) { + throw new Exception("Incorrect device with ID - " + device.getDeviceIdentifier() + " returned!"); + } + } + }