From 1716fc4500ba6592498669fa98f7dc8e8274a342 Mon Sep 17 00:00:00 2001 From: Arcane94 Date: Mon, 5 Mar 2018 16:46:50 +0530 Subject: [PATCH] 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() {