From c600719f3c303404515e5aaeb76e819e225f6c90 Mon Sep 17 00:00:00 2001 From: charitha Date: Sat, 10 Feb 2018 21:48:30 +0530 Subject: [PATCH] Fixed https://github.com/wso2/product-iots/issues/1671 --- .../provider/fcm/FCMNotificationStrategy.java | 29 +++++++++--- .../DeviceManagementPluginRepository.java | 29 +++--------- .../operation/mgt/OperationManagerImpl.java | 33 +++++++++++--- ...tionManagementNegativeDBOperationTest.java | 12 +++-- .../OperationManagementNoDBSchemaTests.java | 12 +++-- .../operation/OperationManagementTests.java | 44 +++++++++++-------- .../ScheduledTaskOperationTests.java | 9 +++- .../mgt/core/task/DeviceTaskManagerTest.java | 18 +++++--- .../template/DeviceTypeManagerService.java | 42 +++++++++++++----- .../core/PolicyManagerServiceImplTest.java | 7 +-- .../core/mgt/impl/FeatureManagerImplTest.java | 7 +-- .../mgt/impl/MonitoringManagerImplTest.java | 6 ++- .../core/mgt/impl/ProfileManagerImplTest.java | 6 ++- 13 files changed, 162 insertions(+), 92 deletions(-) diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.fcm/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/fcm/FCMNotificationStrategy.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.fcm/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/fcm/FCMNotificationStrategy.java index 7085f1244b..83a9289442 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.fcm/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/fcm/FCMNotificationStrategy.java +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.fcm/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/fcm/FCMNotificationStrategy.java @@ -21,6 +21,8 @@ package org.wso2.carbon.device.mgt.extensions.push.notification.provider.fcm; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.push.notification.NotificationContext; @@ -37,10 +39,13 @@ import java.util.List; public class FCMNotificationStrategy implements NotificationStrategy { + private static final Log log = LogFactory.getLog(FCMNotificationStrategy.class); + + private static final String NOTIFIER_TYPE_FCM = "FCM"; private static final String FCM_TOKEN = "FCM_TOKEN"; private static final String FCM_ENDPOINT = "https://fcm.googleapis.com/fcm/send"; private static final String FCM_API_KEY = "fcmAPIKey"; - private static final int TIME_TO_LIVE = 60; + private static final int TIME_TO_LIVE = 5 * 60; //Set FCM TTL to 5 minutes private static final int HTTP_STATUS_CODE_OK = 200; private final PushNotificationConfig config; @@ -56,9 +61,16 @@ public class FCMNotificationStrategy implements NotificationStrategy { @Override public void execute(NotificationContext ctx) throws PushNotificationExecutionFailedException { try { - Device device = - FCMDataHolder.getInstance().getDeviceManagementProviderService().getDeviceWithTypeProperties(ctx.getDeviceId()); - this.sendWakeUpCall(ctx.getOperation().getCode(), device); + if (NOTIFIER_TYPE_FCM.equals(config.getType())) { + Device device = FCMDataHolder.getInstance().getDeviceManagementProviderService() + .getDeviceWithTypeProperties(ctx.getDeviceId()); + this.sendWakeUpCall(ctx.getOperation().getCode(), device); + } else { + if (log.isDebugEnabled()) { + log.debug("Not using FCM notifier as notifier type is set to " + config.getType() + + " in Platform Configurations."); + } + } } catch (DeviceManagementException e) { throw new PushNotificationExecutionFailedException("Error occurred while retrieving device information", e); } catch (IOException e) { @@ -76,12 +88,12 @@ public class FCMNotificationStrategy implements NotificationStrategy { } - private void sendWakeUpCall(String message, - Device device) throws IOException, PushNotificationExecutionFailedException { + private void sendWakeUpCall(String message, Device device) throws IOException, + PushNotificationExecutionFailedException { OutputStream os = null; byte[] bytes = getFCMRequest(message, getFCMToken(device.getProperties())).getBytes(); - HttpURLConnection conn = null; + HttpURLConnection conn; try { conn = (HttpURLConnection) new URL(FCM_ENDPOINT).openConnection(); conn.setRequestProperty("Content-Type", "application/json"); @@ -96,6 +108,9 @@ public class FCMNotificationStrategy implements NotificationStrategy { } } int status = conn.getResponseCode(); + if (log.isDebugEnabled()) { + log.debug("Result code: " + status + ", Message: " + conn.getResponseMessage()); + } if (status != HTTP_STATUS_CODE_OK) { throw new PushNotificationExecutionFailedException("Push notification sending failed with the HTTP " + "error code '" + status + "'"); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java index 4c06d507ca..93bee26a82 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java @@ -22,22 +22,20 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationSubscriber; -import org.wso2.carbon.device.mgt.core.dto.DeviceManagementServiceHolder; -import org.wso2.carbon.device.mgt.core.dto.DeviceTypeServiceIdentifier; +import org.wso2.carbon.device.mgt.common.DeviceStatusTaskPluginConfig; import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig; import org.wso2.carbon.device.mgt.common.ProvisioningConfig; -import org.wso2.carbon.device.mgt.common.DeviceStatusTaskPluginConfig; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; +import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationSubscriber; import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; -import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig; -import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationProvider; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.common.type.mgt.DeviceTypeDefinitionProvider; import org.wso2.carbon.device.mgt.common.type.mgt.DeviceTypeMetaDefinition; import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig; +import org.wso2.carbon.device.mgt.core.dto.DeviceManagementServiceHolder; import org.wso2.carbon.device.mgt.core.dto.DeviceType; +import org.wso2.carbon.device.mgt.core.dto.DeviceTypeServiceIdentifier; 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.internal.DeviceManagerStartupListener; @@ -248,7 +246,6 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis private void registerPushNotificationStrategy(DeviceManagementService deviceManagementService) throws DeviceManagementException { - PushNotificationConfig pushNoteConfig = deviceManagementService.getPushNotificationConfig(); PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain( deviceManagementService.getProvisioningConfig().getProviderTenantDomain(), true); @@ -261,22 +258,8 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); deviceTypeIdentifier = new DeviceTypeServiceIdentifier(deviceManagementService.getType(), tenantId); } - - if (pushNoteConfig != null) { - PushNotificationProvider provider = DeviceManagementDataHolder.getInstance() - .getPushNotificationProviderRepository().getProvider(pushNoteConfig.getType()); - if (provider == null) { - throw new DeviceManagementException( - "No registered push notification provider found for the type: '" + - pushNoteConfig.getType() + "'."); - } - NotificationStrategy notificationStrategy = provider.getNotificationStrategy(pushNoteConfig); - operationManagerRepository.addOperationManager(deviceTypeIdentifier, - new OperationManagerImpl(deviceTypeIdentifier.getDeviceType(), notificationStrategy)); - } else { - operationManagerRepository.addOperationManager(deviceTypeIdentifier, - new OperationManagerImpl(deviceTypeIdentifier.getDeviceType())); - } + operationManagerRepository.addOperationManager(deviceTypeIdentifier, new OperationManagerImpl( + deviceTypeIdentifier.getDeviceType(), deviceManagementService)); } finally { PrivilegedCarbonContext.endTenantFlow(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index 86c9fa2bbc..8c6c23352c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -39,7 +39,10 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementExcept import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; import org.wso2.carbon.device.mgt.common.push.notification.NotificationContext; import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; +import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig; import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationExecutionFailedException; +import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationProvider; +import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; @@ -61,6 +64,7 @@ import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Calendar; import java.util.Collections; import java.util.Date; import java.util.List; @@ -73,6 +77,8 @@ import java.util.List; public class OperationManagerImpl implements OperationManager { private static final Log log = LogFactory.getLog(OperationManagerImpl.class); + private static final int CACHE_VALIDITY_PERIOD = 5 * 60 * 1000; + private static final String NOTIFIER_TYPE_LOCAL = "LOCAL"; private OperationDAO commandOperationDAO; private OperationDAO configOperationDAO; @@ -84,6 +90,8 @@ public class OperationManagerImpl implements OperationManager { private EnrollmentDAO enrollmentDAO; private NotificationStrategy notificationStrategy; private String deviceType; + private DeviceManagementService deviceManagementService; + private long lastUpdatedTimeStamp = 0; public OperationManagerImpl() { commandOperationDAO = OperationManagementDAOFactory.getCommandOperationDAO(); @@ -96,20 +104,32 @@ public class OperationManagerImpl implements OperationManager { enrollmentDAO = DeviceManagementDAOFactory.getEnrollmentDAO(); } - public OperationManagerImpl(String deviceType) { + public OperationManagerImpl(String deviceType, DeviceManagementService deviceManagementService) { this(); this.deviceType = deviceType; + this.deviceManagementService = deviceManagementService; } public NotificationStrategy getNotificationStrategy() { + if (Calendar.getInstance().getTimeInMillis() - lastUpdatedTimeStamp > CACHE_VALIDITY_PERIOD) { + PushNotificationConfig pushNoteConfig = deviceManagementService.getPushNotificationConfig(); + if (pushNoteConfig != null && !NOTIFIER_TYPE_LOCAL.equals(pushNoteConfig.getType())) { + PushNotificationProvider provider = DeviceManagementDataHolder.getInstance() + .getPushNotificationProviderRepository().getProvider(pushNoteConfig.getType()); + if (provider == null) { + log.error("No registered push notification provider found for the type: '" + + pushNoteConfig.getType() + "'."); + return null; + } + notificationStrategy = provider.getNotificationStrategy(pushNoteConfig); + } else { + notificationStrategy = null; + } + lastUpdatedTimeStamp = Calendar.getInstance().getTimeInMillis(); + } return notificationStrategy; } - public OperationManagerImpl(String deviceType, NotificationStrategy notificationStrategy) { - this(deviceType); - this.notificationStrategy = notificationStrategy; - } - @Override public Activity addOperation(Operation operation, List deviceIds) @@ -144,6 +164,7 @@ public class OperationManagerImpl implements OperationManager { boolean isScheduledOperation = this.isTaskScheduledOperation(operation); boolean isNotRepeated = false; boolean isScheduled = false; + notificationStrategy = getNotificationStrategy(); // check whether device list is greater than batch size notification strategy has enable to send push // notification using scheduler task diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementNegativeDBOperationTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementNegativeDBOperationTest.java index 767cde63ed..b55c77d085 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementNegativeDBOperationTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementNegativeDBOperationTest.java @@ -17,6 +17,7 @@ */ package org.wso2.carbon.device.mgt.core.operation; +import org.powermock.api.mockito.PowerMockito; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -28,7 +29,7 @@ import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; -import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; +import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.core.TestDeviceManagementService; import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; import org.wso2.carbon.device.mgt.core.common.TestDataHolder; @@ -39,10 +40,10 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOF import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; +import javax.sql.DataSource; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; -import javax.sql.DataSource; /** * Negative test cases for {@link OperationManagerImpl} @@ -83,8 +84,11 @@ public class OperationManagementNegativeDBOperationTest extends BaseDeviceManage throw new Exception("Incorrect device with ID - " + device.getDeviceIdentifier() + " returned!"); } } - NotificationStrategy notificationStrategy = new TestNotificationStrategy(); - this.operationMgtService = new OperationManagerImpl(DEVICE_TYPE, notificationStrategy); + DeviceManagementService deviceManagementService + = new TestDeviceManagementService(DEVICE_TYPE, MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); + this.operationMgtService = PowerMockito.spy(new OperationManagerImpl(DEVICE_TYPE, deviceManagementService)); + PowerMockito.when(this.operationMgtService, "getNotificationStrategy") + .thenReturn(new TestNotificationStrategy()); this.setMockDataSource(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementNoDBSchemaTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementNoDBSchemaTests.java index 4bc7b45278..92310af40c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementNoDBSchemaTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementNoDBSchemaTests.java @@ -18,6 +18,7 @@ package org.wso2.carbon.device.mgt.core.operation; +import org.powermock.api.mockito.PowerMockito; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -30,7 +31,7 @@ import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; -import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; +import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.TestDeviceManagementService; import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; @@ -42,9 +43,9 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOF import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; +import javax.sql.DataSource; import java.util.ArrayList; import java.util.List; -import javax.sql.DataSource; /** * This is the testcase which covers the methods from {@link OperationManager} @@ -82,8 +83,11 @@ public class OperationManagementNoDBSchemaTests extends BaseDeviceManagementTest throw new Exception("Incorrect device with ID - " + device.getDeviceIdentifier() + " returned!"); } } - NotificationStrategy notificationStrategy = new TestNotificationStrategy(); - this.operationMgtService = new OperationManagerImpl(DEVICE_TYPE, notificationStrategy); + DeviceManagementService deviceManagementService + = new TestDeviceManagementService(DEVICE_TYPE, MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); + this.operationMgtService = PowerMockito.spy(new OperationManagerImpl(DEVICE_TYPE, deviceManagementService)); + PowerMockito.when(this.operationMgtService, "getNotificationStrategy") + .thenReturn(new TestNotificationStrategy()); } @Test(description = "add operation", expectedExceptions = OperationManagementException.class) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementTests.java index 0ce5541596..dcfd63b995 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementTests.java @@ -18,6 +18,7 @@ package org.wso2.carbon.device.mgt.core.operation; +import org.powermock.api.mockito.PowerMockito; import org.testng.Assert; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -34,7 +35,7 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; -import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; +import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.TestDeviceManagementService; import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; @@ -62,7 +63,7 @@ public class OperationManagementTests extends BaseDeviceManagementTest { private static final String DEVICE_TYPE = "OP_TEST_TYPE"; private static final String DEVICE_ID_PREFIX = "OP-TEST-DEVICE-ID-"; - private static final String COMMAND_OPERATON_CODE = "COMMAND-TEST"; + private static final String COMMAND_OPERATION_CODE = "COMMAND-TEST"; private static final String POLICY_OPERATION_CODE = "POLICY-TEST"; private static final String CONFIG_OPERATION_CODE = "CONFIG-TEST"; private static final String PROFILE_OPERATION_CODE = "PROFILE-TEST"; @@ -75,6 +76,7 @@ public class OperationManagementTests extends BaseDeviceManagementTest { private List deviceIds = new ArrayList<>(); private OperationManager operationMgtService; private DeviceManagementProviderService deviceMgmtProvider; + private DeviceManagementService deviceManagementService; private Activity commandActivity; private long commandActivityBeforeUpdatedTimestamp; @@ -98,18 +100,25 @@ public class OperationManagementTests extends BaseDeviceManagementTest { } } this.deviceMgmtProvider = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider(); - NotificationStrategy notificationStrategy = new TestNotificationStrategy(); - this.operationMgtService = new OperationManagerImpl(DEVICE_TYPE, notificationStrategy); + deviceManagementService = new TestDeviceManagementService(DEVICE_TYPE, + MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); + this.operationMgtService = PowerMockito.spy(new OperationManagerImpl(DEVICE_TYPE, deviceManagementService)); + PowerMockito.when(this.operationMgtService, "getNotificationStrategy").thenReturn(new TestNotificationStrategy()); } @Test public void addCommandOperation() throws DeviceManagementException, OperationManagementException, InvalidDeviceException { - NotificationStrategy notificationStrategy = new TestNotificationStrategy(true); - OperationManager operationManager = new OperationManagerImpl(DEVICE_TYPE, notificationStrategy); + OperationManager operationManager = PowerMockito.spy( + new OperationManagerImpl(DEVICE_TYPE, deviceManagementService)); + try { + PowerMockito.when(operationManager, "getNotificationStrategy") + .thenReturn(new TestNotificationStrategy(true)); + } catch (Exception e) { + throw new RuntimeException("Unable to mock getNotificationStrategy method", e); + } this.commandActivity = operationManager.addOperation( - getOperation(new CommandOperation(), Operation.Type.COMMAND, COMMAND_OPERATON_CODE), - this.deviceIds); + getOperation(new CommandOperation(), Operation.Type.COMMAND, COMMAND_OPERATION_CODE), this.deviceIds); validateOperationResponse(this.commandActivity, ActivityStatus.Status.PENDING); } @@ -123,8 +132,8 @@ public class OperationManagementTests extends BaseDeviceManagementTest { invalidDevices.add(new DeviceIdentifier(INVALID_DEVICE + i, DEVICE_TYPE)); } invalidDevices.addAll(this.deviceIds); - Activity activity = this.operationMgtService.addOperation(getOperation(new CommandOperation(), - Operation.Type.COMMAND, COMMAND_OPERATON_CODE), invalidDevices); + Activity activity = this.operationMgtService.addOperation( + getOperation(new CommandOperation(), Operation.Type.COMMAND, COMMAND_OPERATION_CODE), invalidDevices); Assert.assertEquals(activity.getActivityStatus().size(), invalidDevices.size(), "The operation response for add operation only have - " + activity.getActivityStatus().size()); for (int i = 0; i < activity.getActivityStatus().size(); i++) { @@ -145,8 +154,7 @@ public class OperationManagementTests extends BaseDeviceManagementTest { public void addEmptyDevicesCommandOperation() throws DeviceManagementException, OperationManagementException, InvalidDeviceException { this.operationMgtService.addOperation(getOperation(new CommandOperation(), Operation.Type.COMMAND, - COMMAND_OPERATON_CODE), - new ArrayList<>()); + COMMAND_OPERATION_CODE), new ArrayList<>()); } @Test(expectedExceptions = InvalidDeviceException.class) @@ -156,18 +164,16 @@ public class OperationManagementTests extends BaseDeviceManagementTest { DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); List deviceIdentifiers = new ArrayList<>(); deviceIdentifiers.add(deviceIdentifier); - this.operationMgtService.addOperation(getOperation(new CommandOperation(), Operation.Type.COMMAND, - COMMAND_OPERATON_CODE), - deviceIdentifiers); + this.operationMgtService.addOperation( + getOperation(new CommandOperation(), Operation.Type.COMMAND, COMMAND_OPERATION_CODE), deviceIdentifiers); } @Test public void addNonAdminUserDevicesCommandOperation() throws DeviceManagementException, OperationManagementException, InvalidDeviceException { startTenantFlowAsNonAdmin(); - Activity activity = this.operationMgtService.addOperation(getOperation(new CommandOperation(), - Operation.Type.COMMAND, COMMAND_OPERATON_CODE), - deviceIds); + Activity activity = this.operationMgtService.addOperation( + getOperation(new CommandOperation(), Operation.Type.COMMAND, COMMAND_OPERATION_CODE), deviceIds); PrivilegedCarbonContext.endTenantFlow(); validateOperationResponse(activity, ActivityStatus.Status.UNAUTHORIZED); } @@ -525,7 +531,7 @@ public class OperationManagementTests extends BaseDeviceManagementTest { expectedExceptions = OperationManagementException.class) public void getUpdateOperationForInvalidDevice() throws DeviceManagementException, OperationManagementException { this.operationMgtService.updateOperation(new DeviceIdentifier(INVALID_DEVICE, DEVICE_TYPE), - getOperation(new CommandOperation(), Operation.Type.COMMAND, COMMAND_OPERATON_CODE)); + getOperation(new CommandOperation(), Operation.Type.COMMAND, COMMAND_OPERATION_CODE)); } @Test(dependsOnMethods = "getUpdateOperationForInvalidDevice", diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/ScheduledTaskOperationTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/ScheduledTaskOperationTests.java index 269563b3a8..d064497b0a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/ScheduledTaskOperationTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/ScheduledTaskOperationTests.java @@ -18,6 +18,7 @@ package org.wso2.carbon.device.mgt.core.operation; import org.mockito.Mockito; +import org.powermock.api.mockito.PowerMockito; import org.testng.Assert; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -29,6 +30,7 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; +import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.core.TestDeviceManagementService; import org.wso2.carbon.device.mgt.core.TestTaskServiceImpl; import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; @@ -87,8 +89,11 @@ public class ScheduledTaskOperationTests extends BaseDeviceManagementTest { } } DeviceConfigurationManager.getInstance().initConfig(CDM_CONFIG_LOCATION); - TestNotificationStrategy notificationStrategy = new TestNotificationStrategy(); - this.operationMgtService = new OperationManagerImpl(DEVICE_TYPE, notificationStrategy); + DeviceManagementService deviceManagementService + = new TestDeviceManagementService(DEVICE_TYPE, MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); + this.operationMgtService = PowerMockito.spy(new OperationManagerImpl(DEVICE_TYPE, deviceManagementService)); + PowerMockito.when(this.operationMgtService, "getNotificationStrategy") + .thenReturn(new TestNotificationStrategy()); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManagerTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManagerTest.java index ff020451cf..d76783991c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManagerTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManagerTest.java @@ -19,6 +19,7 @@ package org.wso2.carbon.device.mgt.core.task; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.powermock.api.mockito.PowerMockito; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; @@ -29,23 +30,22 @@ import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; -import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; +import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.core.TestDeviceManagementService; import org.wso2.carbon.device.mgt.core.TestUtils; import org.wso2.carbon.device.mgt.core.authorization.DeviceAccessAuthorizationServiceImpl; import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; import org.wso2.carbon.device.mgt.core.common.TestDataHolder; -import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; 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.operation.TestNotificationStrategy; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl; 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.device.mgt.core.task.impl.DeviceDetailsRetrieverTask; import org.wso2.carbon.device.mgt.core.task.impl.DeviceTaskManagerImpl; import org.wso2.carbon.registry.core.exceptions.RegistryException; +import org.wso2.carbon.utils.multitenancy.MultitenantConstants; import java.util.ArrayList; import java.util.HashMap; @@ -84,8 +84,16 @@ public class DeviceTaskManagerTest extends BaseDeviceManagementTest { DeviceManagementDataHolder.getInstance() .setDeviceAccessAuthorizationService(new DeviceAccessAuthorizationServiceImpl()); DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService(null); - NotificationStrategy notificationStrategy = new TestNotificationStrategy(); - this.operationManager = new OperationManagerImpl(TestDataHolder.TEST_DEVICE_TYPE, notificationStrategy); + DeviceManagementService deviceManagementService = new TestDeviceManagementService( + TestDataHolder.TEST_DEVICE_TYPE, MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); + this.operationManager = PowerMockito.spy( + new OperationManagerImpl(TestDataHolder.TEST_DEVICE_TYPE, deviceManagementService)); + try { + PowerMockito.when(this.operationManager, "getNotificationStrategy") + .thenReturn(new TestNotificationStrategy()); + } catch (Exception e) { + throw new RuntimeException("Unable to mock getNotificationStrategy method", e); + } this.deviceMgtProviderService.registerDeviceType( new TestDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE, TestDataHolder.SUPER_TENANT_DOMAIN)); for (Device device : devices) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerService.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerService.java index 48f011f8db..4c481fc9f3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerService.java @@ -59,6 +59,8 @@ import java.util.Map; public class DeviceTypeManagerService implements DeviceManagementService { private static final Log log = LogFactory.getLog(DeviceTypeManagerService.class); + private static final String NOTIFIER_PROPERTY = "notifierType"; + private static final String NOTIFIER_TYPE_LOCAL = "LOCAL"; private DeviceManager deviceManager; private PushNotificationConfig pushNotificationConfig; @@ -71,6 +73,9 @@ public class DeviceTypeManagerService implements DeviceManagementService { private PullNotificationSubscriber pullNotificationSubscriber; private DeviceStatusTaskPluginConfig deviceStatusTaskPluginConfig; private GeneralConfig generalConfig; + private boolean isRegistryBasedConfigs = false; + private boolean isScheduled = false; + private String notifierType; public DeviceTypeManagerService(DeviceTypeConfigIdentifier deviceTypeConfigIdentifier, DeviceTypeConfiguration deviceTypeConfiguration) { @@ -126,6 +131,7 @@ public class DeviceTypeManagerService implements DeviceManagementService { private void populatePushNotificationConfig(PushNotificationProvider pushNotificationProvider) { if (pushNotificationProvider != null) { if (pushNotificationProvider.isFileBasedProperties()) { + isRegistryBasedConfigs = false; Map staticProps = new HashMap<>(); ConfigProperties configProperties = pushNotificationProvider.getConfigProperties(); if (configProperties != null) { @@ -139,21 +145,30 @@ public class DeviceTypeManagerService implements DeviceManagementService { pushNotificationConfig = new PushNotificationConfig(pushNotificationProvider.getType(), pushNotificationProvider.isScheduled(), staticProps); } else { - try { - PlatformConfiguration deviceTypeConfig = deviceManager.getConfiguration(); - if (deviceTypeConfig != null) { - List configuration = deviceTypeConfig.getConfiguration(); - if (configuration.size() > 0) { - Map properties = this.getConfigProperty(configuration); - pushNotificationConfig = new PushNotificationConfig( - pushNotificationProvider.getType(), pushNotificationProvider.isScheduled(), - properties); - } + isRegistryBasedConfigs = true; + isScheduled = pushNotificationProvider.isScheduled(); + notifierType = pushNotificationProvider.getType(); + refreshPlatformConfigurations(); + } + } + } + + private void refreshPlatformConfigurations() { + try { + PlatformConfiguration deviceTypeConfig = deviceManager.getConfiguration(); + if (deviceTypeConfig != null) { + List configuration = deviceTypeConfig.getConfiguration(); + if (!configuration.isEmpty()) { + Map properties = this.getConfigProperty(configuration); + String notifierValue = properties.get(NOTIFIER_PROPERTY); + if (notifierValue != null && notifierValue.equals("1")) { + notifierType = NOTIFIER_TYPE_LOCAL; } - } catch (DeviceManagementException e) { - log.error("Unable to get the " + type + " platform configuration from registry."); + pushNotificationConfig = new PushNotificationConfig(notifierType, isScheduled, properties); } } + } catch (DeviceManagementException e) { + log.error("Unable to get the " + type + " platform configuration from registry."); } } @@ -174,6 +189,9 @@ public class DeviceTypeManagerService implements DeviceManagementService { @Override public PushNotificationConfig getPushNotificationConfig() { + if (isRegistryBasedConfigs) { + refreshPlatformConfigurations(); + } return pushNotificationConfig; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImplTest.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImplTest.java index 0f17393128..cbc3f5c0bb 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImplTest.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImplTest.java @@ -36,6 +36,7 @@ import org.wso2.carbon.device.mgt.common.policy.mgt.ProfileFeature; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.ComplianceFeature; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.NonComplianceData; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.PolicyComplianceException; +import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl; import org.wso2.carbon.device.mgt.core.operation.mgt.PolicyOperation; @@ -86,9 +87,9 @@ public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest { public void addPolicy() throws DeviceManagementException, GroupManagementException, PolicyManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); policyManagerService = new PolicyManagerServiceImpl(); - - deviceMgtService.registerDeviceType(new TypeXDeviceManagementService(DEVICE_TYPE_A)); - operationManager = new OperationManagerImpl(DEVICE_TYPE_A); + DeviceManagementService deviceManagementService = new TypeXDeviceManagementService(DEVICE_TYPE_A); + deviceMgtService.registerDeviceType(deviceManagementService); + operationManager = new OperationManagerImpl(DEVICE_TYPE_A, deviceManagementService); enrollDevice(DEVICE1, DEVICE_TYPE_A); createDeviceGroup(GROUP1); DeviceGroup group1 = groupMgtService.getGroup(GROUP1); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/FeatureManagerImplTest.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/FeatureManagerImplTest.java index 7a0a756faf..9d2e67be3a 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/FeatureManagerImplTest.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/FeatureManagerImplTest.java @@ -31,6 +31,7 @@ import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; import org.wso2.carbon.device.mgt.common.policy.mgt.Profile; import org.wso2.carbon.device.mgt.common.policy.mgt.ProfileFeature; +import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl; import org.wso2.carbon.policy.mgt.common.FeatureManagementException; import org.wso2.carbon.policy.mgt.core.BasePolicyManagementDAOTest; @@ -77,9 +78,9 @@ public class FeatureManagerImplTest extends BasePolicyManagementDAOTest { super.initializeServices(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - - deviceMgtService.registerDeviceType(new TypeXDeviceManagementService(DEVICE_TYPE_D)); - operationManager = new OperationManagerImpl(DEVICE_TYPE_D); + DeviceManagementService deviceManagementService = new TypeXDeviceManagementService(DEVICE_TYPE_D); + deviceMgtService.registerDeviceType(deviceManagementService); + operationManager = new OperationManagerImpl(DEVICE_TYPE_D, deviceManagementService); featureManager = new FeatureManagerImpl(); enrollDevice(DEVICE4, DEVICE_TYPE_D); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImplTest.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImplTest.java index 5d33c919c4..6da2d3ca0a 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImplTest.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImplTest.java @@ -22,6 +22,7 @@ import org.wso2.carbon.device.mgt.common.policy.mgt.Profile; import org.wso2.carbon.device.mgt.common.policy.mgt.ProfileFeature; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.ComplianceFeature; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.PolicyComplianceException; +import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.policy.mgt.core.BasePolicyManagementDAOTest; @@ -83,8 +84,9 @@ public class MonitoringManagerImplTest extends BasePolicyManagementDAOTest{ int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - deviceMgtService.registerDeviceType(new TypeXDeviceManagementService(DEVICE_TYPE_E)); - operationManager = new OperationManagerImpl(DEVICE_TYPE_E); + DeviceManagementService deviceManagementService = new TypeXDeviceManagementService(DEVICE_TYPE_E); + deviceMgtService.registerDeviceType(deviceManagementService); + operationManager = new OperationManagerImpl(DEVICE_TYPE_E, deviceManagementService); featureManager = new FeatureManagerImpl(); monitoringManager = new MonitoringManagerImpl(); policyManager = new PolicyManagerImpl(); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImplTest.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImplTest.java index 622a3e0414..23898f4a52 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImplTest.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImplTest.java @@ -30,6 +30,7 @@ import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; import org.wso2.carbon.device.mgt.common.policy.mgt.Profile; import org.wso2.carbon.device.mgt.common.policy.mgt.ProfileFeature; +import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl; import org.wso2.carbon.policy.mgt.common.ProfileManagementException; import org.wso2.carbon.policy.mgt.core.BasePolicyManagementDAOTest; @@ -71,8 +72,9 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest { public void initialize() throws Exception { log.info("Initializing policy manager tests"); super.initializeServices(); - deviceMgtService.registerDeviceType(new TypeXDeviceManagementService(DEVICE_TYPE_C)); - operationManager = new OperationManagerImpl(DEVICE_TYPE_C); + DeviceManagementService deviceManagementService = new TypeXDeviceManagementService(DEVICE_TYPE_C); + deviceMgtService.registerDeviceType(deviceManagementService); + operationManager = new OperationManagerImpl(DEVICE_TYPE_C, deviceManagementService); enrollDevice(DEVICE3, DEVICE_TYPE_C); createDeviceGroup(GROUP3); DeviceGroup group1 = groupMgtService.getGroup(GROUP3);