diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java index a87a8ba9848..848d043b45b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java @@ -178,6 +178,10 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService { String error = "Error occurred while creating the geo alert for " + deviceType + " with id: " + deviceId; log.error(error, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build(); + } catch (AlertAlreadyExist e) { + String error = "A geo alert with this name already exists."; + log.error(error,e); + return Response.status(Response.Status.BAD_REQUEST).entity(error).build(); } } @@ -211,6 +215,10 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService { String error = "Error occurred while creating the geo alert for " + deviceType + " with id: " + deviceId; log.error(error, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build(); + } catch (AlertAlreadyExist e) { + String error = "A geo alert with this name already exists."; + log.error(error,e); + return Response.status(Response.Status.BAD_REQUEST).entity(error).build(); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/AlertAlreadyExist.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/AlertAlreadyExist.java new file mode 100644 index 00000000000..4e6fd641b70 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/AlertAlreadyExist.java @@ -0,0 +1,20 @@ +package org.wso2.carbon.device.mgt.common.geo.service; + +public class AlertAlreadyExist extends Exception { + + private static final long serialVersionUID = 4709355511911265093L; + private String errorMessage; + + public String getErrorMessage() { + return errorMessage; + } + + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } + + public AlertAlreadyExist(String msg) { + super(msg); + setErrorMessage(msg); + } +} \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/GeoLocationProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/GeoLocationProviderService.java index d8375ab583f..2d287fa862e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/GeoLocationProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/GeoLocationProviderService.java @@ -33,10 +33,10 @@ public interface GeoLocationProviderService { List getExitAlerts(DeviceIdentifier identifier) throws GeoLocationBasedServiceException; boolean createGeoAlert(Alert alert, DeviceIdentifier identifier, String alertType) - throws GeoLocationBasedServiceException; + throws GeoLocationBasedServiceException, AlertAlreadyExist; boolean updateGeoAlert(Alert alert, DeviceIdentifier identifier, String alertType) - throws GeoLocationBasedServiceException; + throws GeoLocationBasedServiceException, AlertAlreadyExist; boolean removeGeoAlert(String alertType, DeviceIdentifier identifier, String queryName) throws GeoLocationBasedServiceException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java index 91102990091..da821ad78b0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java @@ -41,6 +41,7 @@ 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.GeoLocationProviderService; import org.wso2.carbon.device.mgt.common.geo.service.GeoLocationBasedServiceException; +import org.wso2.carbon.device.mgt.common.geo.service.AlertAlreadyExist; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.event.processor.stub.EventProcessorAdminServiceStub; import org.wso2.carbon.identity.jwt.client.extension.JWTClient; @@ -207,18 +208,18 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic @Override public boolean createGeoAlert(Alert alert, DeviceIdentifier identifier, String alertType) - throws GeoLocationBasedServiceException { + throws GeoLocationBasedServiceException, AlertAlreadyExist { return saveGeoAlert(alert, identifier, alertType, false); } @Override public boolean updateGeoAlert(Alert alert, DeviceIdentifier identifier, String alertType) - throws GeoLocationBasedServiceException { + throws GeoLocationBasedServiceException, AlertAlreadyExist { return saveGeoAlert(alert, identifier, alertType, true); } public boolean saveGeoAlert(Alert alert, DeviceIdentifier identifier, String alertType, boolean isUpdate) - throws GeoLocationBasedServiceException { + throws GeoLocationBasedServiceException, AlertAlreadyExist { Type type = new TypeToken>() { }.getType(); @@ -260,24 +261,30 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic "Unrecognized execution plan type: " + alertType + " while creating geo alert"); } - //persist alert in registry - updateRegistry(getRegistryPath(alertType, identifier, alert.getQueryName()), identifier, content, - options); - //deploy alert into event processor EventProcessorAdminServiceStub eventprocessorStub = null; String action = (isUpdate ? "updating" : "creating"); try { + String existingPlanName = null; + String executionPlanName = getExecutionPlanName(alertType, alert.getQueryName(), + identifier.getId()); eventprocessorStub = getEventProcessorAdminServiceStub(); String parsedTemplate = parseTemplate(alertType, parseMap); String validationResponse = eventprocessorStub.validateExecutionPlan(parsedTemplate); if (validationResponse.equals("success")) { if (isUpdate) { - String executionPlanName = getExecutionPlanName(alertType, alert.getQueryName(), - identifier.getId()); eventprocessorStub.editActiveExecutionPlan(parsedTemplate, executionPlanName); } else { - eventprocessorStub.deployExecutionPlan(parsedTemplate); + try { + existingPlanName = eventprocessorStub.getActiveExecutionPlan(executionPlanName); + if (existingPlanName.contains(executionPlanName)) { + throw new AlertAlreadyExist("Execution plan with this name already exists"); + } + } catch (AxisFault axisFault) { + updateRegistry(getRegistryPath(alertType, identifier, alert.getQueryName()), identifier, content, + options); + eventprocessorStub.deployExecutionPlan(parsedTemplate); + } } } else { if (validationResponse.startsWith( 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 26aad9a9085..e8333a0c66d 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 @@ -19,6 +19,7 @@ package org.wso2.carbon.device.mgt.core.geo.service; +import org.apache.axis2.AxisFault; import org.mockito.Mockito; import org.testng.Assert; @@ -31,6 +32,7 @@ import org.wso2.carbon.device.mgt.common.DeviceManagementException; 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.GeoLocationBasedServiceException; +import org.wso2.carbon.device.mgt.common.geo.service.AlertAlreadyExist; import org.wso2.carbon.device.mgt.core.TestDeviceManagementService; import org.wso2.carbon.device.mgt.core.common.TestDataHolder; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; @@ -65,42 +67,42 @@ public class GeoLocationProviderServiceTest { } @Test (description = "Create a sample geo exit-alert with relevant details.") - public void createGeoExitAlert() throws GeoLocationBasedServiceException { + public void createGeoExitAlert() throws GeoLocationBasedServiceException, AlertAlreadyExist { Boolean result = geoLocationProviderServiceImpl. createGeoAlert(getExitAlert(), getDeviceIdentifier(), DeviceManagementConstants.GeoServices.ALERT_TYPE_EXIT); Assert.assertEquals(result, Boolean.TRUE); } @Test (description = "Create a sample geo within-alert with relevant details.") - public void createGeoWithinAlert() throws GeoLocationBasedServiceException { + public void createGeoWithinAlert() throws GeoLocationBasedServiceException, AlertAlreadyExist { Boolean result = geoLocationProviderServiceImpl. createGeoAlert(getWithinAlert(), getDeviceIdentifier(), DeviceManagementConstants.GeoServices.ALERT_TYPE_WITHIN); Assert.assertEquals(result, Boolean.TRUE); } @Test (description = "Create a sample geo proximity-alert with relevant details.") - public void createGeoProximityAlert() throws GeoLocationBasedServiceException { + public void createGeoProximityAlert() throws GeoLocationBasedServiceException, AlertAlreadyExist { Boolean result = geoLocationProviderServiceImpl. createGeoAlert(getProximityAlert(), getDeviceIdentifier(), DeviceManagementConstants.GeoServices.ALERT_TYPE_PROXIMITY); Assert.assertEquals(result, Boolean.TRUE); } @Test (description = "Create a sample geo speed-alert with relevant details.") - public void createGeoSpeedAlert() throws GeoLocationBasedServiceException { + public void createGeoSpeedAlert() throws GeoLocationBasedServiceException, AlertAlreadyExist { Boolean result = geoLocationProviderServiceImpl. createGeoAlert(getSpeedAlert(), getDeviceIdentifier(), DeviceManagementConstants.GeoServices.ALERT_TYPE_SPEED); Assert.assertEquals(result, Boolean.TRUE); } @Test (description = "Create a sample geo stationary-alert with relevant details.") - public void createGeoStationaryAlert() throws GeoLocationBasedServiceException { + public void createGeoStationaryAlert() throws GeoLocationBasedServiceException, AlertAlreadyExist { Boolean result = geoLocationProviderServiceImpl. createGeoAlert(getStationaryAlert(), getDeviceIdentifier(), DeviceManagementConstants.GeoServices.ALERT_TYPE_STATIONARY); Assert.assertEquals(result, Boolean.TRUE); } @Test (description = "Create a sample geo traffic-alert with relevant details.") - public void createGeoTrafficAlert() throws GeoLocationBasedServiceException { + public void createGeoTrafficAlert() throws GeoLocationBasedServiceException, AlertAlreadyExist { Boolean result = geoLocationProviderServiceImpl. createGeoAlert(getTrafficAlert(), getDeviceIdentifier(), DeviceManagementConstants.GeoServices.ALERT_TYPE_TRAFFIC); Assert.assertEquals(result, Boolean.TRUE); @@ -143,6 +145,8 @@ public class GeoLocationProviderServiceTest { when(geoLocationProviderServiceImpl).getEventProcessorAdminServiceStub(); Mockito.doReturn("success"). when(mockEventProcessorAdminServiceStub).validateExecutionPlan(Mockito.anyString()); + Mockito.when(mockEventProcessorAdminServiceStub.getActiveExecutionPlan(Mockito.anyString())). + thenThrow(AxisFault.class); } private DeviceIdentifier getDeviceIdentifier() { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.v09.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.v09.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java index a87a8ba9848..848d043b45b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.v09.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.v09.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java @@ -178,6 +178,10 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService { String error = "Error occurred while creating the geo alert for " + deviceType + " with id: " + deviceId; log.error(error, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build(); + } catch (AlertAlreadyExist e) { + String error = "A geo alert with this name already exists."; + log.error(error,e); + return Response.status(Response.Status.BAD_REQUEST).entity(error).build(); } } @@ -211,6 +215,10 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService { String error = "Error occurred while creating the geo alert for " + deviceType + " with id: " + deviceId; log.error(error, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build(); + } catch (AlertAlreadyExist e) { + String error = "A geo alert with this name already exists."; + log.error(error,e); + return Response.status(Response.Status.BAD_REQUEST).entity(error).build(); } }