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 7085f1244b0..83a9289442f 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 4c06d507ca0..93bee26a824 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 86c9fa2bbcc..8c6c23352c1 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 767cde63ed9..b55c77d0853 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 4bc7b452783..92310af40c1 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 0ce5541596f..dcfd63b9959 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 269563b3a80..d064497b0aa 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 ff020451cf7..d76783991c6 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 48f011f8dbd..4c481fc9f35 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 0f173931283..cbc3f5c0bb7 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 7a0a756fafd..9d2e67be3a3 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 5d33c919c4b..6da2d3ca0ac 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 622a3e04141..23898f4a523 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);