diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/pom.xml b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/pom.xml index cbd8953f822..1b287ccd77b 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/pom.xml +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/pom.xml @@ -111,8 +111,16 @@ org.wso2.carbon.analytics-common org.wso2.carbon.event.output.adapter.core + + org.testng + testng + + + org.powermock + powermock-module-testng + test + - diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/test/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/MQTTBasedPushNotificationProviderTest.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/test/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/MQTTBasedPushNotificationProviderTest.java new file mode 100644 index 00000000000..a813dfea555 --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/test/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/MQTTBasedPushNotificationProviderTest.java @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt; + +import org.mockito.Mockito; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; +import org.wso2.carbon.context.PrivilegedCarbonContext; +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.extensions.push.notification.provider.mqtt.internal.MQTTDataHolder; +import org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt.internal.util.MQTTAdapterConstants; +import org.wso2.carbon.event.output.adapter.core.exception.OutputEventAdapterException; +import org.wso2.carbon.event.output.adapter.core.internal.CarbonOutputEventAdapterService; +import org.wso2.carbon.registry.core.exceptions.RegistryException; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +/* + Unit tests for MQTTBasedPushNotificationProvider class + */ +public class MQTTBasedPushNotificationProviderTest { + private MQTTBasedPushNotificationProvider mqttBasedPushNotificationProvider; + private CarbonOutputEventAdapterService carbonOutputEventAdapterService; + private static final String ADAPTER_NAME = "SampleMqttAdapterName"; + private static final String BROKER_URL = "SampleBrokerUrl"; + + @BeforeClass + public void init() throws NoSuchFieldException, IllegalAccessException, IOException, RegistryException, + OutputEventAdapterException { + initializeCarbonContext(); + mqttBasedPushNotificationProvider = Mockito.mock(MQTTBasedPushNotificationProvider.class, + Mockito.CALLS_REAL_METHODS); + carbonOutputEventAdapterService = Mockito.mock(CarbonOutputEventAdapterService.class, + Mockito.CALLS_REAL_METHODS); + Mockito.doReturn(true).when(carbonOutputEventAdapterService).isPolled(ADAPTER_NAME); + } + + private void initializeCarbonContext() throws IOException, RegistryException { + if (System.getProperty("carbon.home") == null) { + File file = new File("src/test/resources"); + if (file.exists()) { + System.setProperty("carbon.home", file.getAbsolutePath()); + } + } + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain( + org.wso2.carbon.base.MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId( + org.wso2.carbon.base.MultitenantConstants.SUPER_TENANT_ID); + } + + @Test(description = "test getType method") + public void testGetType() { + String type = mqttBasedPushNotificationProvider.getType(); + Assert.assertEquals(type, "MQTT"); + } + + @Test(description = "test get notification strategy method") + public void getNotificationStrategy() { + Map properties = new HashMap<>(); + properties.put(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_BROKER_URL, BROKER_URL); + properties.put(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_NAME, ADAPTER_NAME); + PushNotificationConfig pushNotificationConfig = new PushNotificationConfig("MQTT", true, properties); + MQTTDataHolder mqttDataHolder = MQTTDataHolder.getInstance(); + mqttDataHolder.setOutputEventAdapterService(carbonOutputEventAdapterService); + NotificationStrategy notificationStrategy = mqttBasedPushNotificationProvider. + getNotificationStrategy(pushNotificationConfig); + Assert.assertNotNull(notificationStrategy,"null notificationStrategyReceived"); + } +} diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/test/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/MQTTNotificationStrategyTest.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/test/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/MQTTNotificationStrategyTest.java new file mode 100644 index 00000000000..21ca45658fa --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/test/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/MQTTNotificationStrategyTest.java @@ -0,0 +1,178 @@ +/* + * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt; + +import org.mockito.Mockito; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.push.notification.NotificationContext; +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.core.operation.mgt.PolicyOperation; +import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation; +import org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt.internal.MQTTDataHolder; +import org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt.internal.util.MQTTAdapterConstants; +import org.wso2.carbon.event.output.adapter.core.exception.OutputEventAdapterException; +import org.wso2.carbon.event.output.adapter.core.internal.CarbonOutputEventAdapterService; +import org.wso2.carbon.registry.core.exceptions.RegistryException; +import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; + + +import static org.wso2.carbon.device.mgt.core.operation.mgt.PolicyOperation.POLICY_OPERATION_CODE; + +/* + Unit tests for MQTTNotificationStrategy class + */ +public class MQTTNotificationStrategyTest { + private MQTTNotificationStrategy mqttNotificationStrategy; + private CarbonOutputEventAdapterService carbonOutputEventAdapterService; + private static final String ADAPTER_NAME = "SampleMqttAdapterName"; + private static final String BROKER_URL = "SampleBrokerUrl"; + private PushNotificationConfig pushNotificationConfig; + private static final String MQTT_ADAPTER_TOPIC = "mqtt.adapter.topic"; + private DeviceIdentifier deviceIdentifier; + private Operation operation; + private Map propertiesMap; + private NotificationContext notificationContext; + + @BeforeClass + public void init() throws NoSuchFieldException, IllegalAccessException, IOException, RegistryException, + OutputEventAdapterException { + initializeCarbonContext(); + mqttNotificationStrategy = Mockito.mock(MQTTNotificationStrategy.class, Mockito.CALLS_REAL_METHODS); + carbonOutputEventAdapterService = Mockito.mock(CarbonOutputEventAdapterService.class, + Mockito.CALLS_REAL_METHODS); + Mockito.doReturn(true).when(carbonOutputEventAdapterService).isPolled(Mockito.any()); + Mockito.doNothing().when(carbonOutputEventAdapterService).publish(Mockito.any(), Mockito.any(), Mockito.any()); + Mockito.doNothing().when(carbonOutputEventAdapterService).destroy(ADAPTER_NAME); + } + + private void initializeCarbonContext() throws IOException, RegistryException { + if (System.getProperty("carbon.home") == null) { + File file = new File("src/test/resources"); + if (file.exists()) { + System.setProperty("carbon.home", file.getAbsolutePath()); + } + } + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain( + org.wso2.carbon.base.MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId( + org.wso2.carbon.base.MultitenantConstants.SUPER_TENANT_ID); + } + + @Test(description = "Testing the constructor of MQTTNotificationStrategy class") + public void getNotificationStrategy() { + Map properties = new HashMap<>(); + properties.put(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_BROKER_URL, BROKER_URL); + properties.put(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_NAME, ADAPTER_NAME); + pushNotificationConfig = new PushNotificationConfig("MQTT", true, properties); + MQTTDataHolder mqttDataHolder = MQTTDataHolder.getInstance(); + mqttDataHolder.setOutputEventAdapterService(carbonOutputEventAdapterService); + mqttNotificationStrategy = new MQTTNotificationStrategy(pushNotificationConfig); + Assert.assertNotNull(mqttNotificationStrategy, "Null MQTTNotificationStrategy after initializing"); + } + + @Test(dependsOnMethods = {"getNotificationStrategy"}, description = "Testing getConfig method") + public void getConfigTest() { + PushNotificationConfig temp = mqttNotificationStrategy.getConfig(); + Assert.assertEquals(temp, pushNotificationConfig, "Not matching pushNotificationConfig received"); + } + + @Test(description = "testing un-deploy method") + public void testUndeploy() { + mqttNotificationStrategy.undeploy(); + } + + @Test(description = "testing build context method") + public void testBuildContext() { + Assert.assertNull(mqttNotificationStrategy.buildContext(), "not null buildContext received"); + } + + @Test(description = "testing execute method without properties and operation type command") + public void testExecuteWithoutProperties() throws PushNotificationExecutionFailedException { + deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId("1"); + deviceIdentifier.setType("SampleDeviceType"); + operation = new Operation(); + operation.setPayLoad(new Object()); + operation.setType(Operation.Type.COMMAND); + operation.setCode("SampleCode"); + operation.setId(1); + propertiesMap = new HashMap<>(); + propertiesMap.put(MQTT_ADAPTER_TOPIC, "SampleTopic"); + notificationContext = new NotificationContext(deviceIdentifier, operation); + notificationContext.setProperties(propertiesMap); + mqttNotificationStrategy.execute(notificationContext); + } + + @Test(dependsOnMethods = "testExecuteWithoutProperties", description = "testing execute method without properties " + + "and operation type config") + public void testExecutionWithoutPropertiesNonCommandType() throws PushNotificationExecutionFailedException { + operation.setType(Operation.Type.CONFIG); + operation.setProperties(null); + notificationContext = new NotificationContext(deviceIdentifier, operation); + mqttNotificationStrategy.execute(notificationContext); + } + + @Test(dependsOnMethods = {"testExecutionWithoutPropertiesNonCommandType"}, description = "test execute method " + + "with a profile operation") + public void testExecutePolicyOperation() throws PushNotificationExecutionFailedException { + PolicyOperation policyOperation = new PolicyOperation(); + policyOperation.setCode(POLICY_OPERATION_CODE); + policyOperation.setProperties(null); + ProfileOperation profileOperation = new ProfileOperation(); + profileOperation.setActivityId("1"); + profileOperation.setCode("SampleCode"); + List profileOperationList = new ArrayList<>(); + profileOperationList.add(profileOperation); + policyOperation.setProfileOperations(profileOperationList); + notificationContext = new NotificationContext(deviceIdentifier, policyOperation); + mqttNotificationStrategy.execute(notificationContext); + } + + @Test(dependsOnMethods = "testExecuteWithoutProperties", description = "testing execute method with properties") + public void testExecute() throws PushNotificationExecutionFailedException { + Properties properties = new Properties(); + properties.setProperty(MQTT_ADAPTER_TOPIC, "SampleTopic"); + operation.setProperties(properties); + notificationContext = new NotificationContext(deviceIdentifier, operation); + notificationContext.setProperties(propertiesMap); + mqttNotificationStrategy.execute(notificationContext); + } + + @Test(dependsOnMethods = {"testExecute"}, description = "testing execute method without the default tenant domain") + public void testExecutionWithoutTenantDomain() throws NoSuchFieldException, IllegalAccessException, + PushNotificationExecutionFailedException { + Field providerTenantDomain = MQTTNotificationStrategy.class.getDeclaredField("providerTenantDomain"); + providerTenantDomain.setAccessible(true); + providerTenantDomain.set(mqttNotificationStrategy, "SampleTenantDomain"); + mqttNotificationStrategy.execute(notificationContext); + } +} diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/test/resources/conf/carbon.xml b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/test/resources/conf/carbon.xml new file mode 100644 index 00000000000..f24ee57be23 --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/test/resources/conf/carbon.xml @@ -0,0 +1,656 @@ + + + + + + + + + ${product.name} + + + ${product.key} + + + ${product.version} + + + + + + + + + local:/${carbon.context}/services/ + + + + + + + ${default.server.role} + + + + + + + org.wso2.carbon + + + / + + + + + + + + + 15 + + + + + + + + + 0 + + + + + 9999 + + 11111 + + + + + + 10389 + + 8000 + + + + + + 10500 + + + + + + + org.wso2.carbon.tomcat.jndi.CarbonJavaURLContextFactory + + + + + + + + + java + + + + + + + + + + false + + + false + + + 600 + + + + false + + + + + + + + 30 + + + + + + + + + 15 + + + + + + ${carbon.home}/repository/deployment/server/ + + + 15 + + + ${carbon.home}/repository/conf/axis2/axis2.xml + + + 30000 + + + ${carbon.home}/repository/deployment/client/ + + ${carbon.home}/repository/conf/axis2/axis2_client.xml + + true + + + + + + + + + + admin + Default Administrator Role + + + user + Default User Role + + + + + + + + + + + + ${carbon.home}/repository/resources/security/wso2carbon.jks + + JKS + + wso2carbon + + wso2carbon + + wso2carbon + + + + + + ${carbon.home}/repository/resources/security/client-truststore.jks + + JKS + + wso2carbon + + + + + + + + + + + + + + + + + + + UserManager + + + false + + + + + + + ${carbon.home}/tmp/work + + + + + + true + + + 10 + + + 30 + + + + + + 100 + + + + keystore + certificate + * + + org.wso2.carbon.ui.transports.fileupload.AnyFileUploadExecutor + + + + + jarZip + + org.wso2.carbon.ui.transports.fileupload.JarZipUploadExecutor + + + + dbs + + org.wso2.carbon.ui.transports.fileupload.DBSFileUploadExecutor + + + + tools + + org.wso2.carbon.ui.transports.fileupload.ToolsFileUploadExecutor + + + + toolsAny + + org.wso2.carbon.ui.transports.fileupload.ToolsAnyFileUploadExecutor + + + + + + + info + org.wso2.carbon.core.transports.util.InfoProcessor + + + wsdl + org.wso2.carbon.core.transports.util.Wsdl11Processor + + + wsdl2 + org.wso2.carbon.core.transports.util.Wsdl20Processor + + + xsd + org.wso2.carbon.core.transports.util.XsdProcessor + + + + + + false + false + true + svn + http://svnrepo.example.com/repos/ + username + password + true + + + + + + + + + + + + + + + ${require.carbon.servlet} + + + + + true + + + + + + + default repository + ${p2.repo.url} + + + + + + + + true + + + + + + true + + diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/test/resources/log4j.properties b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/test/resources/log4j.properties new file mode 100644 index 00000000000..e415fd607d3 --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/test/resources/log4j.properties @@ -0,0 +1,34 @@ +# +# Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +# +# WSO2 Inc. licenses this file to you under the Apache License, +# Version 2.0 (the "License"); you may not use this file except +# in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +# +# This is the log4j configuration file used by WSO2 Carbon +# +# IMPORTANT : Please do not remove or change the names of any +# of the Appender defined here. The layout pattern & log file +# can be changed using the WSO2 Carbon Management Console, and those +# settings will override the settings in this file. +# + +log4j.rootLogger=DEBUG, STD_OUT + +# Redirect log messages to console +log4j.appender.STD_OUT=org.apache.log4j.ConsoleAppender +log4j.appender.STD_OUT.Target=System.out +log4j.appender.STD_OUT.layout=org.apache.log4j.PatternLayout +log4j.appender.STD_OUT.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/test/resources/testng.xml b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/test/resources/testng.xml new file mode 100644 index 00000000000..d76f7ab144c --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/test/resources/testng.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + +