Adding more test cases to GeoLocationProviderService

revert-70aa11f8
Pasindu 7 years ago
parent b5f6212392
commit 4f47b63bfd

@ -357,7 +357,8 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>javassist</groupId> <groupId>javassist</groupId>
<artifactId>javassist</artifactId></dependency> <artifactId>javassist</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.powermock</groupId> <groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId> <artifactId>powermock-api-mockito</artifactId>

@ -80,7 +80,7 @@ public abstract class BaseDeviceManagementTest {
NotificationManagementDAOFactory.init(dataSource); NotificationManagementDAOFactory.init(dataSource);
} }
private void initServices() throws DeviceManagementException, RegistryException { protected void initServices() throws DeviceManagementException, RegistryException {
DeviceConfigurationManager.getInstance().initConfig(); DeviceConfigurationManager.getInstance().initConfig();
DeviceManagementProviderService deviceMgtService = new DeviceManagementProviderServiceImpl(); DeviceManagementProviderService deviceMgtService = new DeviceManagementProviderServiceImpl();
DeviceManagementServiceComponent.notifyStartupListeners(); DeviceManagementServiceComponent.notifyStartupListeners();

@ -98,6 +98,21 @@ public class TestDataHolder {
return devices; 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) { public static DeviceType generateDeviceTypeData(String devTypeName) {
DeviceType deviceType = new DeviceType(); DeviceType deviceType = new DeviceType();
deviceType.setName(devTypeName); deviceType.setName(devTypeName);

@ -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; package org.wso2.carbon.device.mgt.core.geo.service;
import org.junit.runner.RunWith;
import org.mockito.Mockito; 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.Assert;
import org.testng.annotations.BeforeClass; 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.Alert;
import org.wso2.carbon.device.mgt.common.geo.service.GeoFence; 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.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.common.TestDataHolder;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; 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.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.event.processor.stub.EventProcessorAdminServiceStub;
import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException; 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 org.wso2.carbon.utils.multitenancy.MultitenantConstants;
import java.io.InputStream;
import java.rmi.RemoteException; import java.rmi.RemoteException;
import java.util.List; import java.util.List;
import static org.powermock.api.mockito.PowerMockito.*;
@PrepareForTest(GeoLocationProviderServiceImpl.class)
public class GeoLocationProviderServiceTest { 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_TYPE = "GL_TEST_TYPE";
private static final String DEVICE_ID = "GL-TEST-DEVICE-ID-1"; private static final String DEVICE_ID = "GL-TEST-DEVICE-ID-1";
private static final String SAMPLE_GEO_JSON = "12121"; private static final String SAMPLE_GEO_JSON = "12121";
@ -59,54 +58,52 @@ public class GeoLocationProviderServiceTest {
@BeforeClass @BeforeClass
public void init() throws Exception { public void init() throws Exception {
initMocks(); initMocks();
enrollTestDevice(); enrollDev();
} }
@Test @Test (description = "Create a sample geo exit-alert with relevant details.")
public void createGeoExitAlert() throws Exception { public void createGeoExitAlert() throws Exception {
Boolean result = geoLocationProviderServiceImpl. Boolean result = geoLocationProviderServiceImpl.
createGeoAlert(getTestAlert(), getDeviceIdentifier(), DeviceManagementConstants.GeoServices.ALERT_TYPE_EXIT); createGeoAlert(getExitAlert(), getDeviceIdentifier(), DeviceManagementConstants.GeoServices.ALERT_TYPE_EXIT);
Assert.assertEquals(result, Boolean.TRUE); 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 { public void createGeoWithinAlert() throws Exception {
Boolean result = geoLocationProviderServiceImpl. Boolean result = geoLocationProviderServiceImpl.
createGeoAlert(getTestAlert(), getDeviceIdentifier(), DeviceManagementConstants.GeoServices.ALERT_TYPE_WITHIN); createGeoAlert(getWithinAlert(), getDeviceIdentifier(), DeviceManagementConstants.GeoServices.ALERT_TYPE_WITHIN);
Assert.assertEquals(result, Boolean.TRUE); Assert.assertEquals(result, Boolean.TRUE);
} }
@Test @Test (description = "Create a sample geo proximity-alert with relevant details.")
public void createGeoProximityAlert() throws Exception { public void createGeoProximityAlert() throws Exception {
Boolean result = geoLocationProviderServiceImpl. Boolean result = geoLocationProviderServiceImpl.
createGeoAlert(getProximityAlert(), getDeviceIdentifier(), DeviceManagementConstants.GeoServices.ALERT_TYPE_PROXIMITY); createGeoAlert(getProximityAlert(), getDeviceIdentifier(), DeviceManagementConstants.GeoServices.ALERT_TYPE_PROXIMITY);
Assert.assertEquals(result, Boolean.TRUE); Assert.assertEquals(result, Boolean.TRUE);
} }
@Test @Test (description = "Create a sample geo speed-alert with relevant details.")
public void createGeoSpeedAlert() throws Exception { public void createGeoSpeedAlert() throws Exception {
Boolean result = geoLocationProviderServiceImpl. Boolean result = geoLocationProviderServiceImpl.
createGeoAlert(getSpeedAlert(), getDeviceIdentifier(), DeviceManagementConstants.GeoServices.ALERT_TYPE_SPEED); createGeoAlert(getSpeedAlert(), getDeviceIdentifier(), DeviceManagementConstants.GeoServices.ALERT_TYPE_SPEED);
Assert.assertEquals(result, Boolean.TRUE); Assert.assertEquals(result, Boolean.TRUE);
} }
@Test @Test (description = "Create a sample geo stationary-alert with relevant details.")
public void createGeoStationaryAlert() throws Exception { public void createGeoStationaryAlert() throws Exception {
Boolean result = geoLocationProviderServiceImpl. Boolean result = geoLocationProviderServiceImpl.
createGeoAlert(getStationaryAlert(), getDeviceIdentifier(), DeviceManagementConstants.GeoServices.ALERT_TYPE_STATIONARY); createGeoAlert(getStationaryAlert(), getDeviceIdentifier(), DeviceManagementConstants.GeoServices.ALERT_TYPE_STATIONARY);
Assert.assertEquals(result, Boolean.TRUE); Assert.assertEquals(result, Boolean.TRUE);
} }
@Test @Test (description = "Create a sample geo traffic-alert with relevant details.")
public void createGeoTrafficAlert() throws Exception { public void createGeoTrafficAlert() throws Exception {
Boolean result = geoLocationProviderServiceImpl. Boolean result = geoLocationProviderServiceImpl.
createGeoAlert(getTrafficAlert(), getDeviceIdentifier(), DeviceManagementConstants.GeoServices.ALERT_TYPE_TRAFFIC); createGeoAlert(getTrafficAlert(), getDeviceIdentifier(), DeviceManagementConstants.GeoServices.ALERT_TYPE_TRAFFIC);
Assert.assertEquals(result, Boolean.TRUE); Assert.assertEquals(result, Boolean.TRUE);
} }
//Test methods to retrieve saved Data. @Test(dependsOnMethods = "createGeoExitAlert", description = "retrieve saved geo exit-alert.")
@Test(dependsOnMethods = "createGeoExitAlert")
public void getGeoExitAlerts() throws Exception { public void getGeoExitAlerts() throws Exception {
List<GeoFence> geoFences; List<GeoFence> geoFences;
geoFences = geoLocationProviderServiceImpl.getExitAlerts(getDeviceIdentifier()); geoFences = geoLocationProviderServiceImpl.getExitAlerts(getDeviceIdentifier());
@ -117,7 +114,7 @@ public class GeoLocationProviderServiceTest {
Assert.assertEquals(geoFenceNode.getQueryName(), SAMPLE_QUERY_NAME); Assert.assertEquals(geoFenceNode.getQueryName(), SAMPLE_QUERY_NAME);
} }
@Test(dependsOnMethods = "createGeoWithinAlert") @Test(dependsOnMethods = "createGeoWithinAlert", description = "retrieve saved geo within-alert.")
public void getGeoWithinAlerts() throws Exception { public void getGeoWithinAlerts() throws Exception {
List<GeoFence> geoFences; List<GeoFence> geoFences;
geoFences = geoLocationProviderServiceImpl.getWithinAlerts(getDeviceIdentifier()); geoFences = geoLocationProviderServiceImpl.getWithinAlerts(getDeviceIdentifier());
@ -127,7 +124,7 @@ public class GeoLocationProviderServiceTest {
Assert.assertEquals(geoFenceNode.getQueryName(), SAMPLE_QUERY_NAME); Assert.assertEquals(geoFenceNode.getQueryName(), SAMPLE_QUERY_NAME);
} }
@Test(dependsOnMethods = "createGeoSpeedAlert") @Test(dependsOnMethods = "createGeoSpeedAlert", description = "retrieve saved geo speed-alert.")
public void getGeoSpeedAlerts() throws Exception { public void getGeoSpeedAlerts() throws Exception {
String result; String result;
result = geoLocationProviderServiceImpl.getSpeedAlerts(getDeviceIdentifier()); result = geoLocationProviderServiceImpl.getSpeedAlerts(getDeviceIdentifier());
@ -136,7 +133,7 @@ public class GeoLocationProviderServiceTest {
SAMPLE_SPEED_ALERT_VALUE + "}"); SAMPLE_SPEED_ALERT_VALUE + "}");
} }
@Test(dependsOnMethods = "createGeoTrafficAlert") @Test(dependsOnMethods = "createGeoTrafficAlert" , description = "retrieve saved geo exit-alert.")
public void getGeoTrafficAlerts() throws Exception { public void getGeoTrafficAlerts() throws Exception {
List<GeoFence> geoFences; List<GeoFence> geoFences;
geoFences = geoLocationProviderServiceImpl.getTrafficAlerts(getDeviceIdentifier()); 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 { public void getGeoStationaryAlerts() throws Exception {
List<GeoFence> geoFences; List<GeoFence> geoFences;
geoFences = geoLocationProviderServiceImpl.getStationaryAlerts(getDeviceIdentifier()); geoFences = geoLocationProviderServiceImpl.getStationaryAlerts(getDeviceIdentifier());
@ -161,8 +158,10 @@ public class GeoLocationProviderServiceTest {
private void initMocks() throws JWTClientException, RemoteException { private void initMocks() throws JWTClientException, RemoteException {
mockEventProcessorAdminServiceStub = Mockito.mock(EventProcessorAdminServiceStub.class); mockEventProcessorAdminServiceStub = Mockito.mock(EventProcessorAdminServiceStub.class);
geoLocationProviderServiceImpl = Mockito.mock(GeoLocationProviderServiceImpl.class, Mockito.CALLS_REAL_METHODS); geoLocationProviderServiceImpl = Mockito.mock(GeoLocationProviderServiceImpl.class, Mockito.CALLS_REAL_METHODS);
doReturn(mockEventProcessorAdminServiceStub).when(geoLocationProviderServiceImpl).getEventProcessorAdminServiceStub(); Mockito.doReturn(mockEventProcessorAdminServiceStub).
doReturn("success").when(mockEventProcessorAdminServiceStub).validateExecutionPlan(Mockito.anyString()); when(geoLocationProviderServiceImpl).getEventProcessorAdminServiceStub();
Mockito.doReturn("success").
when(mockEventProcessorAdminServiceStub).validateExecutionPlan(Mockito.anyString());
} }
private DeviceIdentifier getDeviceIdentifier() { private DeviceIdentifier getDeviceIdentifier() {
@ -171,31 +170,8 @@ public class GeoLocationProviderServiceTest {
deviceIdentifier.setType("TEST"); deviceIdentifier.setType("TEST");
return deviceIdentifier; 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 alert = new Alert();
alert.setDeviceId(DEVICE_ID); alert.setDeviceId(DEVICE_ID);
alert.setCepAction("CEP_ACTION"); alert.setCepAction("CEP_ACTION");
@ -208,6 +184,20 @@ public class GeoLocationProviderServiceTest {
return alert; 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() { private Alert getProximityAlert() {
Alert alert = new Alert(); Alert alert = new Alert();
alert.setDeviceId(DEVICE_ID); alert.setDeviceId(DEVICE_ID);
@ -220,7 +210,8 @@ public class GeoLocationProviderServiceTest {
} }
private Alert getSpeedAlert() { private Alert getSpeedAlert() {
Alert alert = getTestAlert(); Alert alert = new Alert();
alert.setDeviceId(DEVICE_ID);
alert.setParseData("{\n" + alert.setParseData("{\n" +
" \"" + DeviceManagementConstants.GeoServices.GEO_FENCE_GEO_JSON + "\": \"" + SAMPLE_GEO_JSON + "\",\n" + " \"" + DeviceManagementConstants.GeoServices.GEO_FENCE_GEO_JSON + "\": \"" + SAMPLE_GEO_JSON + "\",\n" +
" \"" + DeviceManagementConstants.GeoServices.SPEED_ALERT_VALUE + "\": \"" + SAMPLE_SPEED_ALERT_VALUE + "\"\n" + " \"" + DeviceManagementConstants.GeoServices.SPEED_ALERT_VALUE + "\": \"" + SAMPLE_SPEED_ALERT_VALUE + "\"\n" +
@ -252,4 +243,21 @@ public class GeoLocationProviderServiceTest {
alert.setQueryName(SAMPLE_QUERY_NAME); alert.setQueryName(SAMPLE_QUERY_NAME);
return alert; 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!");
}
}
} }

Loading…
Cancel
Save