From 4f591bd41db2061776c1f73285ccf79f42781486 Mon Sep 17 00:00:00 2001 From: GDLMadushanka Date: Wed, 28 Mar 2018 09:49:21 +0530 Subject: [PATCH] Fix issues in test-cases --- .../mqtt/CarbonContextInitializationUtil.java | 43 +++++++++++++++++++ ...MQTTBasedPushNotificationProviderTest.java | 19 ++------ .../mqtt/MQTTNotificationStrategyTest.java | 15 +------ .../DeviceAccessAuthorizationServiceTest.java | 3 +- 4 files changed, 49 insertions(+), 31 deletions(-) create mode 100644 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/CarbonContextInitializationUtil.java 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/CarbonContextInitializationUtil.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/CarbonContextInitializationUtil.java new file mode 100644 index 0000000000..b69482751a --- /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/CarbonContextInitializationUtil.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * 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.wso2.carbon.context.PrivilegedCarbonContext; + +import java.io.File; + +/** + * Util class to initialize carbon-context. + */ +public class CarbonContextInitializationUtil { + private final static String CARBON_HOME = "carbon.home"; + + public static void initializeCarbonContext() { + 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); + } +} 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 index c29eb52c43..c655a19067 100644 --- 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 @@ -35,8 +35,8 @@ import java.io.IOException; import java.util.HashMap; import java.util.Map; -/* - Unit tests for MQTTBasedPushNotificationProvider class +/** + * Unit tests for MQTTBasedPushNotificationProvider class */ public class MQTTBasedPushNotificationProviderTest { private MQTTBasedPushNotificationProvider mqttBasedPushNotificationProvider; @@ -47,7 +47,7 @@ public class MQTTBasedPushNotificationProviderTest { @BeforeClass public void init() throws NoSuchFieldException, IllegalAccessException, IOException, RegistryException, OutputEventAdapterException { - initializeCarbonContext(); + CarbonContextInitializationUtil.initializeCarbonContext(); mqttBasedPushNotificationProvider = Mockito.mock(MQTTBasedPushNotificationProvider.class, Mockito.CALLS_REAL_METHODS); carbonOutputEventAdapterService = Mockito.mock(CarbonOutputEventAdapterService.class, @@ -55,19 +55,6 @@ public class MQTTBasedPushNotificationProviderTest { 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(); 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 index 741bf2d9d2..f005eac4a3 100644 --- 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 @@ -63,7 +63,7 @@ public class MQTTNotificationStrategyTest { @BeforeClass public void init() throws NoSuchFieldException, IllegalAccessException, IOException, RegistryException, OutputEventAdapterException { - initializeCarbonContext(); + CarbonContextInitializationUtil.initializeCarbonContext(); mqttNotificationStrategy = Mockito.mock(MQTTNotificationStrategy.class, Mockito.CALLS_REAL_METHODS); carbonOutputEventAdapterService = Mockito.mock(CarbonOutputEventAdapterService.class, Mockito.CALLS_REAL_METHODS); @@ -72,19 +72,6 @@ public class MQTTNotificationStrategyTest { 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<>(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/authorization/DeviceAccessAuthorizationServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/authorization/DeviceAccessAuthorizationServiceTest.java index 0d47d369cd..5a47f0f877 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/authorization/DeviceAccessAuthorizationServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/authorization/DeviceAccessAuthorizationServiceTest.java @@ -88,6 +88,7 @@ public class DeviceAccessAuthorizationServiceTest { private static final String LAST_NAME = "lastName"; private static final String EMAIL = "email"; private static final String PASSWORD = "password"; + private static final String DRIVER_CLASS_NAME = "org.h2.Driver"; private DeviceAccessAuthorizationServiceImpl deviceAccessAuthorizationService; private List deviceIds = new ArrayList<>(); private List groupDeviceIds = new ArrayList<>(); @@ -134,7 +135,7 @@ public class DeviceAccessAuthorizationServiceTest { BasicDataSource dataSource = new BasicDataSource(); String connectionUrl = "jdbc:h2:./target/databasetest/CARBON_TEST"; dataSource.setUrl(connectionUrl); - dataSource.setDriverClassName("org.h2.Driver"); + dataSource.setDriverClassName(DRIVER_CLASS_NAME); JDBCTenantManager jdbcTenantManager = new JDBCTenantManager(dataSource, MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); realmService.setTenantManager(jdbcTenantManager);