From 74814b2932c001484fd0b89263f8ffeca1591778 Mon Sep 17 00:00:00 2001 From: GDLMadushanka Date: Fri, 13 Oct 2017 16:06:43 +0530 Subject: [PATCH 1/4] unit tests for DeviceCacheManagerImpl --- .../cache/DeviceCacheManagerImplTest.java | 132 ++++++++++++++++++ .../src/test/resources/testng.xml | 1 + 2 files changed, 133 insertions(+) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/cache/DeviceCacheManagerImplTest.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/cache/DeviceCacheManagerImplTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/cache/DeviceCacheManagerImplTest.java new file mode 100644 index 0000000000..8a19b6e780 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/cache/DeviceCacheManagerImplTest.java @@ -0,0 +1,132 @@ +/* + * 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.core.cache; + +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.Device; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.core.cache.impl.DeviceCacheManagerImpl; +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.config.DeviceManagementConfig; +import org.wso2.carbon.device.mgt.core.config.cache.DeviceCacheConfiguration; +import org.wso2.carbon.utils.multitenancy.MultitenantConstants; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + + +/* + Unit tests for deviceCacheManagerImpl + */ +public class DeviceCacheManagerImplTest extends BaseDeviceManagementTest { + + private static final int NO_OF_DEVICES = 5; + private static final String UPDATE_NAME = "updatedName"; + private static final String DEVICE_TYPE = "TEST_TYPE"; + private static final String DEVICE_ID_PREFIX = "TEST-DEVICE-ID-"; + private DeviceCacheManagerImpl deviceCacheManager; + private List deviceIds = new ArrayList<>(); + private List devices = new ArrayList<>(); + private List deviceCacheKeyList = new ArrayList<>(); + + @BeforeClass + public void init() throws DeviceManagementException, IOException { + DeviceConfigurationManager.getInstance().initConfig(); + for (int i = 0; i < NO_OF_DEVICES; i++) { + deviceIds.add(new DeviceIdentifier(DEVICE_ID_PREFIX + i, DEVICE_TYPE)); + } + devices = TestDataHolder.generateDummyDeviceData(this.deviceIds); + deviceCacheManager = Mockito.mock(DeviceCacheManagerImpl.class, Mockito.CALLS_REAL_METHODS); + DeviceCacheConfiguration configuration = new DeviceCacheConfiguration(); + configuration.setEnabled(true); + DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(); + config.setDeviceCacheConfiguration(configuration); + initializeCarbonContext(); + } + + private void initializeCarbonContext() throws IOException { + 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 = "Adding all test devices to the cache") + public void testAddDeviceToCache() throws DeviceManagementException { + for (int i = 0; i < NO_OF_DEVICES; i++) { + deviceCacheManager.addDeviceToCache(deviceIds.get(i), devices.get(i), MultitenantConstants.SUPER_TENANT_ID); + } + } + + @Test(description = "Adding existing device again", dependsOnMethods = {"testAddDeviceToCache"}) + public void testAddExistingDeviceToCache() throws DeviceManagementException { + deviceCacheManager.addDeviceToCache(deviceIds.get(0), devices.get(0), MultitenantConstants.SUPER_TENANT_ID); + } + + @Test(description = "test updating and getting a device in Cache", dependsOnMethods = {"testAddExistingDeviceToCache"}) + public void testUpdateDeviceInCache() { + devices.get(0).setName(UPDATE_NAME); + deviceCacheManager.updateDeviceInCache(deviceIds.get(0), devices.get(0), MultitenantConstants.SUPER_TENANT_ID); + + Device tempDevice = deviceCacheManager.getDeviceFromCache(deviceIds.get(0), MultitenantConstants + .SUPER_TENANT_ID); + Assert.assertEquals(tempDevice.getName(), UPDATE_NAME); + } + + @Test(description = "test getting a device from cache", dependsOnMethods = {"testAddDeviceToCache"}) + public void testGetDeviceFromCache() throws DeviceManagementException { + Device tempDevice = deviceCacheManager.getDeviceFromCache(deviceIds.get(1), MultitenantConstants + .SUPER_TENANT_ID); + Assert.assertEquals(tempDevice, devices.get(1)); + } + + @Test(description = "test removing a device from cache", dependsOnMethods = {"testUpdateDeviceInCache"}) + public void testRemoveDeviceFromCache() throws DeviceManagementException { + deviceCacheManager.removeDeviceFromCache(deviceIds.get(0), MultitenantConstants.SUPER_TENANT_ID); + } + + @Test(description = "test removing list of devices from cache", dependsOnMethods = {"testRemoveDeviceFromCache"}) + public void testRemoveDevicesFromCache() { + //starting from index 1 since 0 is already deleted + for (int i = 1; i < NO_OF_DEVICES; i++) { + DeviceCacheKey deviceCacheKey = new DeviceCacheKey(); + deviceCacheKey.setDeviceId(devices.get(i).getDeviceIdentifier()); + deviceCacheKey.setDeviceType(devices.get(i).getType()); + deviceCacheKey.setTenantId(MultitenantConstants.SUPER_TENANT_ID); + deviceCacheKeyList.add(deviceCacheKey); + } + deviceCacheManager.removeDevicesFromCache(deviceCacheKeyList); + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml index 8093861d4c..11cf8c467b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml @@ -53,6 +53,7 @@ + From 0a1ba593079bf8203cd25da59488e5144d9a1925 Mon Sep 17 00:00:00 2001 From: GDLMadushanka Date: Fri, 13 Oct 2017 16:10:00 +0530 Subject: [PATCH 2/4] refactoring --- .../device/mgt/core/cache/DeviceCacheManagerImplTest.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/cache/DeviceCacheManagerImplTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/cache/DeviceCacheManagerImplTest.java index 8a19b6e780..e77d9c7cc8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/cache/DeviceCacheManagerImplTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/cache/DeviceCacheManagerImplTest.java @@ -39,12 +39,10 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; - /* Unit tests for deviceCacheManagerImpl */ public class DeviceCacheManagerImplTest extends BaseDeviceManagementTest { - private static final int NO_OF_DEVICES = 5; private static final String UPDATE_NAME = "updatedName"; private static final String DEVICE_TYPE = "TEST_TYPE"; @@ -95,11 +93,11 @@ public class DeviceCacheManagerImplTest extends BaseDeviceManagementTest { deviceCacheManager.addDeviceToCache(deviceIds.get(0), devices.get(0), MultitenantConstants.SUPER_TENANT_ID); } - @Test(description = "test updating and getting a device in Cache", dependsOnMethods = {"testAddExistingDeviceToCache"}) + @Test(description = "test updating and getting a device in Cache", + dependsOnMethods = {"testAddExistingDeviceToCache"}) public void testUpdateDeviceInCache() { devices.get(0).setName(UPDATE_NAME); deviceCacheManager.updateDeviceInCache(deviceIds.get(0), devices.get(0), MultitenantConstants.SUPER_TENANT_ID); - Device tempDevice = deviceCacheManager.getDeviceFromCache(deviceIds.get(0), MultitenantConstants .SUPER_TENANT_ID); Assert.assertEquals(tempDevice.getName(), UPDATE_NAME); From 6aba996065b1b2e40ec4d5e6802da67aca6c67eb Mon Sep 17 00:00:00 2001 From: GDLMadushanka Date: Fri, 13 Oct 2017 16:28:45 +0530 Subject: [PATCH 3/4] required changes for pr --- .../device/mgt/core/cache/DeviceCacheManagerImplTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/cache/DeviceCacheManagerImplTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/cache/DeviceCacheManagerImplTest.java index e77d9c7cc8..dfd5aa2d59 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/cache/DeviceCacheManagerImplTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/cache/DeviceCacheManagerImplTest.java @@ -33,7 +33,6 @@ 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.config.cache.DeviceCacheConfiguration; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; - import java.io.File; import java.io.IOException; import java.util.ArrayList; @@ -100,14 +99,15 @@ public class DeviceCacheManagerImplTest extends BaseDeviceManagementTest { deviceCacheManager.updateDeviceInCache(deviceIds.get(0), devices.get(0), MultitenantConstants.SUPER_TENANT_ID); Device tempDevice = deviceCacheManager.getDeviceFromCache(deviceIds.get(0), MultitenantConstants .SUPER_TENANT_ID); - Assert.assertEquals(tempDevice.getName(), UPDATE_NAME); + Assert.assertEquals(tempDevice.getName(), UPDATE_NAME,"Device name not updated in the cache"); } @Test(description = "test getting a device from cache", dependsOnMethods = {"testAddDeviceToCache"}) public void testGetDeviceFromCache() throws DeviceManagementException { Device tempDevice = deviceCacheManager.getDeviceFromCache(deviceIds.get(1), MultitenantConstants .SUPER_TENANT_ID); - Assert.assertEquals(tempDevice, devices.get(1)); + Assert.assertEquals(tempDevice, devices.get(1),"Received device from cache is not matching " + + "with the saved device"); } @Test(description = "test removing a device from cache", dependsOnMethods = {"testUpdateDeviceInCache"}) From ef83ddaa3a1bca4f807828ef131993a6ba6fb0ca Mon Sep 17 00:00:00 2001 From: GDLMadushanka Date: Fri, 13 Oct 2017 16:30:28 +0530 Subject: [PATCH 4/4] refactoring --- .../device/mgt/core/cache/DeviceCacheManagerImplTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/cache/DeviceCacheManagerImplTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/cache/DeviceCacheManagerImplTest.java index dfd5aa2d59..4d6fa89375 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/cache/DeviceCacheManagerImplTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/cache/DeviceCacheManagerImplTest.java @@ -39,7 +39,7 @@ import java.util.ArrayList; import java.util.List; /* - Unit tests for deviceCacheManagerImpl + Unit tests for DeviceCacheManagerImpl */ public class DeviceCacheManagerImplTest extends BaseDeviceManagementTest { private static final int NO_OF_DEVICES = 5; @@ -79,7 +79,6 @@ public class DeviceCacheManagerImplTest extends BaseDeviceManagementTest { org.wso2.carbon.base.MultitenantConstants.SUPER_TENANT_ID); } - @Test(description = "Adding all test devices to the cache") public void testAddDeviceToCache() throws DeviceManagementException { for (int i = 0; i < NO_OF_DEVICES; i++) {