From 50503fc7c01039517bd7a33de065ebb8ab9f5615 Mon Sep 17 00:00:00 2001 From: Arcane94 Date: Sat, 24 Feb 2018 15:15:36 +0530 Subject: [PATCH 1/5] Fixing the issue when defining alerts with same name --- .../impl/GeoLocationBasedServiceImpl.java | 8 ++++++ .../common/geo/service/AlertAlreadyExist.java | 20 ++++++++++++++ .../service/GeoLocationProviderService.java | 4 +-- .../GeoLocationProviderServiceImpl.java | 27 ++++++++++++------- .../GeoLocationProviderServiceTest.java | 16 ++++++----- .../impl/GeoLocationBasedServiceImpl.java | 8 ++++++ 6 files changed, 65 insertions(+), 18 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/AlertAlreadyExist.java 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 a87a8ba984..848d043b45 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 0000000000..4e6fd641b7 --- /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 d8375ab583..2d287fa862 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 9110299009..da821ad78b 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 26aad9a908..e8333a0c66 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 a87a8ba984..848d043b45 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(); } } From 832eb0c6a86fbd23ebc405c6eb6b749152b20ee2 Mon Sep 17 00:00:00 2001 From: Arcane94 Date: Sat, 24 Feb 2018 15:29:09 +0530 Subject: [PATCH 2/5] Adding license details to AlertAlreadyExist class --- .../common/geo/service/AlertAlreadyExist.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) 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 index 4e6fd641b7..774ceec9be 100644 --- 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 @@ -1,5 +1,26 @@ +/* + * 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.common.geo.service; +/** + * Custom exception class for geo alert definition operations. + */ + public class AlertAlreadyExist extends Exception { private static final long serialVersionUID = 4709355511911265093L; From 46dbbdbcb8646999b35acce0942ca4afc1d8e422 Mon Sep 17 00:00:00 2001 From: Arcane94 Date: Thu, 1 Mar 2018 23:15:14 +0530 Subject: [PATCH 3/5] Year change in license details --- .../carbon/device/mgt/common/geo/service/AlertAlreadyExist.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 774ceec9be..98a513f3a9 100644 --- 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 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) 2018, 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 From c0024ce26f2074e1455e1f9000cf981f8b9e5e6a Mon Sep 17 00:00:00 2001 From: Arcane94 Date: Mon, 5 Mar 2018 10:42:26 +0530 Subject: [PATCH 4/5] Exception name change and minor modifications --- .../service/impl/GeoLocationBasedServiceImpl.java | 4 ++-- ...yExist.java => AlertAlreadyExistException.java} | 4 ++-- .../geo/service/GeoLocationProviderService.java | 4 ++-- .../service/GeoLocationProviderServiceImpl.java | 11 ++++++----- .../service/GeoLocationProviderServiceTest.java | 14 +++++++------- .../service/impl/GeoLocationBasedServiceImpl.java | 4 ++-- 6 files changed, 21 insertions(+), 20 deletions(-) rename components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/{AlertAlreadyExist.java => AlertAlreadyExistException.java} (90%) 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 848d043b45..487b367b63 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,7 +178,7 @@ 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) { + } catch (AlertAlreadyExistException 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(); @@ -215,7 +215,7 @@ 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) { + } catch (AlertAlreadyExistException 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/AlertAlreadyExistException.java similarity index 90% rename from components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/AlertAlreadyExist.java rename to components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/AlertAlreadyExistException.java index 98a513f3a9..5380aee042 100644 --- 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/AlertAlreadyExistException.java @@ -21,7 +21,7 @@ package org.wso2.carbon.device.mgt.common.geo.service; * Custom exception class for geo alert definition operations. */ -public class AlertAlreadyExist extends Exception { +public class AlertAlreadyExistException extends Exception { private static final long serialVersionUID = 4709355511911265093L; private String errorMessage; @@ -34,7 +34,7 @@ public class AlertAlreadyExist extends Exception { this.errorMessage = errorMessage; } - public AlertAlreadyExist(String msg) { + public AlertAlreadyExistException(String msg) { super(msg); setErrorMessage(msg); } 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 2d287fa862..4c9e02f158 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, AlertAlreadyExist; + throws GeoLocationBasedServiceException, AlertAlreadyExistException; boolean updateGeoAlert(Alert alert, DeviceIdentifier identifier, String alertType) - throws GeoLocationBasedServiceException, AlertAlreadyExist; + throws GeoLocationBasedServiceException, AlertAlreadyExistException; 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 da821ad78b..66c9a0cafe 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,7 +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.common.geo.service.AlertAlreadyExistException; 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; @@ -208,18 +208,18 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic @Override public boolean createGeoAlert(Alert alert, DeviceIdentifier identifier, String alertType) - throws GeoLocationBasedServiceException, AlertAlreadyExist { + throws GeoLocationBasedServiceException, AlertAlreadyExistException { return saveGeoAlert(alert, identifier, alertType, false); } @Override public boolean updateGeoAlert(Alert alert, DeviceIdentifier identifier, String alertType) - throws GeoLocationBasedServiceException, AlertAlreadyExist { + throws GeoLocationBasedServiceException, AlertAlreadyExistException { return saveGeoAlert(alert, identifier, alertType, true); } public boolean saveGeoAlert(Alert alert, DeviceIdentifier identifier, String alertType, boolean isUpdate) - throws GeoLocationBasedServiceException, AlertAlreadyExist { + throws GeoLocationBasedServiceException, AlertAlreadyExistException { Type type = new TypeToken>() { }.getType(); @@ -278,7 +278,8 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic try { existingPlanName = eventprocessorStub.getActiveExecutionPlan(executionPlanName); if (existingPlanName.contains(executionPlanName)) { - throw new AlertAlreadyExist("Execution plan with this name already exists"); + throw new AlertAlreadyExistException("Execution plan already exists with name " + + executionPlanName); } } catch (AxisFault axisFault) { updateRegistry(getRegistryPath(alertType, identifier, alert.getQueryName()), identifier, content, 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 e8333a0c66..9462c3c93a 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 @@ -32,7 +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.common.geo.service.AlertAlreadyExistException; 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; @@ -67,42 +67,42 @@ public class GeoLocationProviderServiceTest { } @Test (description = "Create a sample geo exit-alert with relevant details.") - public void createGeoExitAlert() throws GeoLocationBasedServiceException, AlertAlreadyExist { + public void createGeoExitAlert() throws GeoLocationBasedServiceException, AlertAlreadyExistException { 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, AlertAlreadyExist { + public void createGeoWithinAlert() throws GeoLocationBasedServiceException, AlertAlreadyExistException { 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, AlertAlreadyExist { + public void createGeoProximityAlert() throws GeoLocationBasedServiceException, AlertAlreadyExistException { 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, AlertAlreadyExist { + public void createGeoSpeedAlert() throws GeoLocationBasedServiceException, AlertAlreadyExistException { 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, AlertAlreadyExist { + public void createGeoStationaryAlert() throws GeoLocationBasedServiceException, AlertAlreadyExistException { 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, AlertAlreadyExist { + public void createGeoTrafficAlert() throws GeoLocationBasedServiceException, AlertAlreadyExistException { Boolean result = geoLocationProviderServiceImpl. createGeoAlert(getTrafficAlert(), getDeviceIdentifier(), DeviceManagementConstants.GeoServices.ALERT_TYPE_TRAFFIC); Assert.assertEquals(result, Boolean.TRUE); 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 848d043b45..487b367b63 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,7 +178,7 @@ 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) { + } catch (AlertAlreadyExistException 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(); @@ -215,7 +215,7 @@ 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) { + } catch (AlertAlreadyExistException 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(); From 1716fc4500ba6592498669fa98f7dc8e8274a342 Mon Sep 17 00:00:00 2001 From: Arcane94 Date: Mon, 5 Mar 2018 16:46:50 +0530 Subject: [PATCH 5/5] logic changes of the fix --- .../GeoLocationProviderServiceImpl.java | 29 +++++++++++++------ .../GeoLocationProviderServiceTest.java | 8 +++-- 2 files changed, 26 insertions(+), 11 deletions(-) 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 66c9a0cafe..f888d7c32c 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 @@ -44,6 +44,7 @@ import org.wso2.carbon.device.mgt.common.geo.service.GeoLocationBasedServiceExce import org.wso2.carbon.device.mgt.common.geo.service.AlertAlreadyExistException; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.event.processor.stub.EventProcessorAdminServiceStub; +import org.wso2.carbon.event.processor.stub.types.ExecutionPlanConfigurationDto; import org.wso2.carbon.identity.jwt.client.extension.JWTClient; import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException; import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService; @@ -265,27 +266,35 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic EventProcessorAdminServiceStub eventprocessorStub = null; String action = (isUpdate ? "updating" : "creating"); try { - String existingPlanName = null; + ExecutionPlanConfigurationDto[] allActiveExecutionPlanConfigs = null; + String activeExecutionPlan = 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")) { + allActiveExecutionPlanConfigs = eventprocessorStub.getAllActiveExecutionPlanConfigurations(); if (isUpdate) { - eventprocessorStub.editActiveExecutionPlan(parsedTemplate, executionPlanName); + for (ExecutionPlanConfigurationDto activeExectionPlanConfig:allActiveExecutionPlanConfigs) { + activeExecutionPlan = activeExectionPlanConfig.getExecutionPlan(); + if (activeExecutionPlan.contains(executionPlanName)) { + eventprocessorStub.editActiveExecutionPlan(parsedTemplate, executionPlanName); + return true; + } + } + eventprocessorStub.deployExecutionPlan(parsedTemplate); } else { - try { - existingPlanName = eventprocessorStub.getActiveExecutionPlan(executionPlanName); - if (existingPlanName.contains(executionPlanName)) { + for (ExecutionPlanConfigurationDto activeExectionPlanConfig:allActiveExecutionPlanConfigs) { + activeExecutionPlan = activeExectionPlanConfig.getExecutionPlan(); + if (activeExecutionPlan.contains(executionPlanName)) { throw new AlertAlreadyExistException("Execution plan already exists with name " + executionPlanName); } - } catch (AxisFault axisFault) { - updateRegistry(getRegistryPath(alertType, identifier, alert.getQueryName()), identifier, content, - options); - eventprocessorStub.deployExecutionPlan(parsedTemplate); } + updateRegistry(getRegistryPath(alertType, identifier, alert.getQueryName()), identifier, content, + options); + eventprocessorStub.deployExecutionPlan(parsedTemplate); } } else { if (validationResponse.startsWith( @@ -352,6 +361,8 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic private String getExecutionPlanName(String alertType, String queryName, String deviceId) { if ("Traffic".equals(alertType)) { return "Geo-ExecutionPlan-Traffic_" + queryName + "_alert"; + } else if ("Speed".equals(alertType)) { + return "Geo-ExecutionPlan-" + alertType + "---" + deviceId + "_alert"; } else { return "Geo-ExecutionPlan-" + alertType + "_" + queryName + "---_" + deviceId + "_alert"; } 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 9462c3c93a..7be18ebff9 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 @@ -38,6 +38,7 @@ 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.service.DeviceManagementProviderService; import org.wso2.carbon.event.processor.stub.EventProcessorAdminServiceStub; +import org.wso2.carbon.event.processor.stub.types.ExecutionPlanConfigurationDto; import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; @@ -59,6 +60,7 @@ public class GeoLocationProviderServiceTest { private EventProcessorAdminServiceStub mockEventProcessorAdminServiceStub; private GeoLocationProviderServiceImpl geoLocationProviderServiceImpl; + private ExecutionPlanConfigurationDto[] mockExecutionPlanConfigurationDto = new ExecutionPlanConfigurationDto[1]; @BeforeClass public void init() throws Exception { @@ -141,12 +143,14 @@ public class GeoLocationProviderServiceTest { private void initMocks() throws JWTClientException, RemoteException { mockEventProcessorAdminServiceStub = Mockito.mock(EventProcessorAdminServiceStub.class); geoLocationProviderServiceImpl = Mockito.mock(GeoLocationProviderServiceImpl.class, Mockito.CALLS_REAL_METHODS); + mockExecutionPlanConfigurationDto[0] = Mockito.mock(ExecutionPlanConfigurationDto.class); Mockito.doReturn(mockEventProcessorAdminServiceStub). when(geoLocationProviderServiceImpl).getEventProcessorAdminServiceStub(); Mockito.doReturn("success"). when(mockEventProcessorAdminServiceStub).validateExecutionPlan(Mockito.anyString()); - Mockito.when(mockEventProcessorAdminServiceStub.getActiveExecutionPlan(Mockito.anyString())). - thenThrow(AxisFault.class); + Mockito.when(mockEventProcessorAdminServiceStub.getAllActiveExecutionPlanConfigurations()). + thenReturn(mockExecutionPlanConfigurationDto); + Mockito.when(mockExecutionPlanConfigurationDto[0].getExecutionPlan()).thenReturn("EXECUTION_PLAN"); } private DeviceIdentifier getDeviceIdentifier() {