From b1aba20200546bd2848dcb27b5705ef63c1bc107 Mon Sep 17 00:00:00 2001 From: Menaka Jayawardena Date: Wed, 27 Sep 2017 13:59:16 +0530 Subject: [PATCH 1/7] Test cases for Permission management. --- .../org.wso2.carbon.device.mgt.core/pom.xml | 12 ++ .../mgt/PermissionManagerServiceTest.java | 104 ++++++++++++++++++ .../src/test/resources/testng.xml | 1 + 3 files changed, 117 insertions(+) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceTest.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml index 40f3ab5f85..a7771512a1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml @@ -221,6 +221,18 @@ org.testng testng + + org.powermock + powermock-module-testng + 1.7.0 + test + + + org.powermock + powermock-api-mockito + 1.7.0 + test + org.wso2.carbon org.wso2.carbon.user.core diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceTest.java new file mode 100644 index 0000000000..8cd89bbb84 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceTest.java @@ -0,0 +1,104 @@ +/* + * 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.permission.mgt; + +import org.apache.log4j.Logger; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.testng.Assert; +import org.testng.IObjectFactory; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.ObjectFactory; +import org.testng.annotations.Test; +import org.wso2.carbon.device.mgt.common.permission.mgt.Permission; +import org.wso2.carbon.device.mgt.common.permission.mgt.PermissionManagementException; +import org.wso2.carbon.device.mgt.common.permission.mgt.PermissionManagerService; +import org.wso2.carbon.registry.core.exceptions.RegistryException; + +import java.util.Properties; + +import static org.mockito.MockitoAnnotations.initMocks; + +@PrepareForTest(PermissionUtils.class) +public class PermissionManagerServiceTest { + + private Permission permission; + private Logger log = Logger.getLogger(PermissionManagerServiceTest.class); + private String PERMISSION_URL = "permission/admin/device-mgt/test/testPermission"; + private String PERMISSION_PATH = "permission/admin/device-mgt/test/testPermission"; + private String PERMiSSION_METHOD = "ui.execute"; + private String PERMISSION_NAME = "Test Permission"; + + //For create properties to retrieve permission. + private String HTTP_METHOD = "HTTP_METHOD"; + private String URL = "URL"; + + private PermissionManagerService permissionManagerService; + + @ObjectFactory + public IObjectFactory getObjectFactory() { + return new org.powermock.modules.testng.PowerMockObjectFactory(); + } + + @BeforeClass + public void init() throws RegistryException { + permissionManagerService = PermissionManagerServiceImpl.getInstance(); + initMocks(this); + this.permission = new Permission(); + permission.setName(PERMISSION_NAME); + permission.setPath(PERMISSION_PATH); + permission.setMethod(PERMiSSION_METHOD); + permission.setUrl(PERMISSION_URL); + } + + @Test (description = "Create a new permission in the permission tree.") + public void testCreatePermission() { + try { + + PowerMockito.mockStatic(PermissionUtils.class); + PowerMockito.when(PermissionUtils.putPermission(permission)).thenReturn(true); + + Assert.assertTrue(permissionManagerService.addPermission(permission)); + + } catch (PermissionManagementException e) { + log.error("Error creating permission " + e.getErrorMessage()); + } + } + + @Test (dependsOnMethods = {"testCreatePermission"}, description = "Test for retrieving the created permission " + + "from the permission tree.") + public void testGetPermission() throws PermissionManagementException { + Permission permission = permissionManagerService.getPermission(createProperties()); + + Assert.assertEquals(permission.getMethod(), PERMiSSION_METHOD); + Assert.assertEquals(permission.getName(), PERMISSION_NAME); + Assert.assertEquals(permission.getPath(), PERMISSION_PATH); + Assert.assertEquals(permission.getUrl(), PERMISSION_URL); + } + + /** + * Create a Property object which will be passed to getPermission method to retrieve a permission. + * @return : Property object which contains permission url and method. + * */ + private Properties createProperties() { + Properties properties = new Properties(); + properties.setProperty(URL, PERMISSION_URL); + properties.setProperty(HTTP_METHOD, PERMiSSION_METHOD); + return properties; + } +} 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 e562ed1740..ea17e19e71 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 @@ -41,6 +41,7 @@ + From 52b4b46e7cb176c85dbf0f853b0368f02f17f476 Mon Sep 17 00:00:00 2001 From: Menaka Jayawardena Date: Wed, 27 Sep 2017 14:02:11 +0530 Subject: [PATCH 2/7] Minor code formatting. --- .../mgt/core/permission/mgt/PermissionManagerServiceTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceTest.java index 8cd89bbb84..20675811cf 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceTest.java @@ -69,12 +69,10 @@ public class PermissionManagerServiceTest { @Test (description = "Create a new permission in the permission tree.") public void testCreatePermission() { try { - PowerMockito.mockStatic(PermissionUtils.class); PowerMockito.when(PermissionUtils.putPermission(permission)).thenReturn(true); Assert.assertTrue(permissionManagerService.addPermission(permission)); - } catch (PermissionManagementException e) { log.error("Error creating permission " + e.getErrorMessage()); } From ebe2a7bd6b2d127024e5db852e01430750e5f505 Mon Sep 17 00:00:00 2001 From: Menaka Jayawardena Date: Wed, 27 Sep 2017 14:56:43 +0530 Subject: [PATCH 3/7] Moved dependency versioning to parent pom and fixed the test failure. --- .../org.wso2.carbon.device.mgt.core/pom.xml | 4 --- .../mgt/PermissionManagerServiceTest.java | 34 +++++++++++++++---- pom.xml | 8 ++++- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml index a7771512a1..f5193fe978 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml @@ -224,14 +224,10 @@ org.powermock powermock-module-testng - 1.7.0 - test org.powermock powermock-api-mockito - 1.7.0 - test org.wso2.carbon diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceTest.java index 20675811cf..7f6c20ba81 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceTest.java @@ -17,7 +17,8 @@ */ package org.wso2.carbon.device.mgt.core.permission.mgt; -import org.apache.log4j.Logger; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.testng.Assert; @@ -38,10 +39,10 @@ import static org.mockito.MockitoAnnotations.initMocks; public class PermissionManagerServiceTest { private Permission permission; - private Logger log = Logger.getLogger(PermissionManagerServiceTest.class); + private Log log; private String PERMISSION_URL = "permission/admin/device-mgt/test/testPermission"; private String PERMISSION_PATH = "permission/admin/device-mgt/test/testPermission"; - private String PERMiSSION_METHOD = "ui.execute"; + private String PERMISSION_METHOD = "ui.execute"; private String PERMISSION_NAME = "Test Permission"; //For create properties to retrieve permission. @@ -57,12 +58,13 @@ public class PermissionManagerServiceTest { @BeforeClass public void init() throws RegistryException { - permissionManagerService = PermissionManagerServiceImpl.getInstance(); initMocks(this); + log = LogFactory.getLog(PermissionManagerServiceTest.class); + permissionManagerService = PermissionManagerServiceImpl.getInstance(); this.permission = new Permission(); permission.setName(PERMISSION_NAME); permission.setPath(PERMISSION_PATH); - permission.setMethod(PERMiSSION_METHOD); + permission.setMethod(PERMISSION_METHOD); permission.setUrl(PERMISSION_URL); } @@ -83,12 +85,19 @@ public class PermissionManagerServiceTest { public void testGetPermission() throws PermissionManagementException { Permission permission = permissionManagerService.getPermission(createProperties()); - Assert.assertEquals(permission.getMethod(), PERMiSSION_METHOD); + Assert.assertEquals(permission.getMethod(), PERMISSION_METHOD); Assert.assertEquals(permission.getName(), PERMISSION_NAME); Assert.assertEquals(permission.getPath(), PERMISSION_PATH); Assert.assertEquals(permission.getUrl(), PERMISSION_URL); } + @Test (dependsOnMethods = {"testCreatePermission"}, + expectedExceptions = {PermissionManagementException.class}, + expectedExceptionsMessageRegExp = "Resource URI/HTTP method is empty") + public void testGetPermissionError() throws PermissionManagementException { + Permission permission = permissionManagerService.getPermission(createErrorProperty()); + } + /** * Create a Property object which will be passed to getPermission method to retrieve a permission. * @return : Property object which contains permission url and method. @@ -96,7 +105,18 @@ public class PermissionManagerServiceTest { private Properties createProperties() { Properties properties = new Properties(); properties.setProperty(URL, PERMISSION_URL); - properties.setProperty(HTTP_METHOD, PERMiSSION_METHOD); + properties.setProperty(HTTP_METHOD, PERMISSION_METHOD); + return properties; + } + + /** + * Creates property object with empty properties. + * @return : Properties object with empty set of properties. + * */ + private Properties createErrorProperty() { + Properties properties = new Properties(); + properties.setProperty(URL, ""); + properties.setProperty(HTTP_METHOD, ""); return properties; } } diff --git a/pom.xml b/pom.xml index c30878f9e1..595af10314 100644 --- a/pom.xml +++ b/pom.xml @@ -1532,6 +1532,12 @@ ${power.mock.version} test + + org.powermock + powermock-module-testng + ${power.mock.version} + test + @@ -1961,7 +1967,7 @@ 0.7.8 0.7.5.201505241946 1.0b3 - 1.6.4 + 1.7.0 From c7779b63c08ed5fa358353193e2296e8a4a76b22 Mon Sep 17 00:00:00 2001 From: Menaka Jayawardena Date: Wed, 27 Sep 2017 15:01:20 +0530 Subject: [PATCH 4/7] String constants were changed to static final. --- .../mgt/PermissionManagerServiceTest.java | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceTest.java index 7f6c20ba81..a5ca659e31 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceTest.java @@ -38,17 +38,17 @@ import static org.mockito.MockitoAnnotations.initMocks; @PrepareForTest(PermissionUtils.class) public class PermissionManagerServiceTest { - private Permission permission; - private Log log; - private String PERMISSION_URL = "permission/admin/device-mgt/test/testPermission"; - private String PERMISSION_PATH = "permission/admin/device-mgt/test/testPermission"; - private String PERMISSION_METHOD = "ui.execute"; - private String PERMISSION_NAME = "Test Permission"; + private static final Log log = LogFactory.getLog(PermissionManagerServiceTest.class);; + private static final String PERMISSION_URL = "permission/admin/device-mgt/test/testPermission"; + private static final String PERMISSION_PATH = "permission/admin/device-mgt/test/testPermission"; + private static final String PERMISSION_METHOD = "ui.execute"; + private static final String PERMISSION_NAME = "Test Permission"; //For create properties to retrieve permission. - private String HTTP_METHOD = "HTTP_METHOD"; - private String URL = "URL"; + private static final String HTTP_METHOD = "HTTP_METHOD"; + private static final String URL = "URL"; + private Permission permission; private PermissionManagerService permissionManagerService; @ObjectFactory @@ -59,7 +59,6 @@ public class PermissionManagerServiceTest { @BeforeClass public void init() throws RegistryException { initMocks(this); - log = LogFactory.getLog(PermissionManagerServiceTest.class); permissionManagerService = PermissionManagerServiceImpl.getInstance(); this.permission = new Permission(); permission.setName(PERMISSION_NAME); From a6eb95e2e8be3cd8a8b76e6bb5f5fabb9b04544b Mon Sep 17 00:00:00 2001 From: Nirothipan Date: Wed, 27 Sep 2017 15:37:57 +0530 Subject: [PATCH 5/7] NotificationManagemet testcase --- .../NotificationManagementServiceImpl.java | 2 - .../dao/impl/AbstractNotificationDAOImpl.java | 2 +- .../core/common/BaseDeviceManagementTest.java | 4 +- .../mgt/NotificationManagementTests.java | 108 ++++++++++++++++++ .../core/search/util/MockNotification.java | 37 ++++++ .../src/test/resources/testng.xml | 1 + 6 files changed, 150 insertions(+), 4 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementTests.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/MockNotification.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementServiceImpl.java index 90867dbc3d..09e1eca4f9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementServiceImpl.java @@ -50,11 +50,9 @@ public class NotificationManagementServiceImpl implements NotificationManagement private static final Log log = LogFactory.getLog(NotificationManagementServiceImpl.class); private NotificationDAO notificationDAO; - private DeviceDAO deviceDAO; public NotificationManagementServiceImpl() { this.notificationDAO = NotificationManagementDAOFactory.getNotificationDAO(); - this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); } @Override diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/dao/impl/AbstractNotificationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/dao/impl/AbstractNotificationDAOImpl.java index bf80177509..9ae0a693f4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/dao/impl/AbstractNotificationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/dao/impl/AbstractNotificationDAOImpl.java @@ -279,4 +279,4 @@ public abstract class AbstractNotificationDAOImpl implements NotificationDAO { } return notificationCountByStatus; } -} \ No newline at end of file +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/BaseDeviceManagementTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/BaseDeviceManagementTest.java index a1752db7fb..e22cd0ac62 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/BaseDeviceManagementTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/BaseDeviceManagementTest.java @@ -32,6 +32,7 @@ import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.core.TestUtils; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOFactory; +import org.wso2.carbon.device.mgt.core.notification.mgt.dao.NotificationManagementDAOFactory; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; @@ -62,6 +63,7 @@ public abstract class BaseDeviceManagementTest { DeviceManagementDAOFactory.init(dataSource); GroupManagementDAOFactory.init(dataSource); OperationManagementDAOFactory.init(dataSource); + NotificationManagementDAOFactory.init(dataSource); } @BeforeClass @@ -131,7 +133,7 @@ public abstract class BaseDeviceManagementTest { try { conn = getDataSource().getConnection(); conn.setAutoCommit(false); - String[] cleanupTables = new String[]{"DM_DEVICE_OPERATION_RESPONSE","DM_ENROLMENT_OP_MAPPING", "DM_CONFIG_OPERATION", + String[] cleanupTables = new String[]{"DM_NOTIFICATION","DM_DEVICE_OPERATION_RESPONSE","DM_ENROLMENT_OP_MAPPING", "DM_CONFIG_OPERATION", "DM_POLICY_OPERATION", "DM_COMMAND_OPERATION", "DM_PROFILE_OPERATION", "DM_DEVICE_GROUP_MAP", "DM_GROUP", "DM_ENROLMENT", "DM_DEVICE_APPLICATION_MAPPING", "DM_APPLICATION", "DM_DEVICE", "DM_DEVICE_TYPE"}; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementTests.java new file mode 100644 index 0000000000..7f70461a14 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementTests.java @@ -0,0 +1,108 @@ +/* +* 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.notification.mgt; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.notification.mgt.Notification; +import org.wso2.carbon.device.mgt.core.TestDeviceManagementService; +import org.wso2.carbon.device.mgt.core.authorization.DeviceAccessAuthorizationServiceImpl; +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.search.util.MockNotification; +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.registry.core.config.RegistryContext; +import org.wso2.carbon.registry.core.exceptions.RegistryException; +import org.wso2.carbon.registry.core.internal.RegistryDataHolder; +import org.wso2.carbon.registry.core.jdbc.realm.InMemoryRealmService; +import org.wso2.carbon.registry.core.service.RegistryService; +import org.wso2.carbon.user.core.service.RealmService; +import org.wso2.carbon.utils.multitenancy.MultitenantConstants; + +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; + +public class NotificationManagementTests { + + private static final Log log = LogFactory.getLog(NotificationManagementTests.class); + private static final String DEVICE_TYPE = "NOTIFICATION_TEST_DEVICE"; + private static final String DEVICE_ID_PREFIX = "NOTIFICATION-TEST-DEVICE-ID-"; + private static final int NO_OF_DEVICES = 10; + private List deviceIds = new ArrayList<>(); + private MockNotification mockNotification = new MockNotification(); + + @BeforeClass + public void init() throws Exception { + DeviceConfigurationManager.getInstance().initConfig(); + log.info("Initializing"); + for (int i = 0; i < NO_OF_DEVICES; i++) { + deviceIds.add(new DeviceIdentifier(DEVICE_ID_PREFIX + i, DEVICE_TYPE)); + } + List devices = TestDataHolder.generateDummyDeviceData(this.deviceIds); + DeviceManagementProviderService deviceMgtService = new DeviceManagementProviderServiceImpl(); + DeviceManagementServiceComponent.notifyStartupListeners(); + DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(deviceMgtService); + DeviceManagementDataHolder.getInstance().setRegistryService(getRegistryService()); + DeviceManagementDataHolder.getInstance().setDeviceAccessAuthorizationService(new DeviceAccessAuthorizationServiceImpl()); + DeviceManagementDataHolder.getInstance().setGroupManagementProviderService(new GroupManagementProviderServiceImpl()); + DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService(null); + deviceMgtService.registerDeviceType(new TestDeviceManagementService(DEVICE_TYPE, + MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)); + for (Device device : devices) { + deviceMgtService.enrollDevice(device); + } + List returnedDevices = deviceMgtService.getAllDevices(DEVICE_TYPE); + + for (Device device : returnedDevices) { + if (!device.getDeviceIdentifier().startsWith(DEVICE_ID_PREFIX)) { + throw new Exception("Incorrect device with ID - " + device.getDeviceIdentifier() + " returned!"); + } + } + } + + private RegistryService getRegistryService() throws RegistryException { + RealmService realmService = new InMemoryRealmService(); + RegistryDataHolder.getInstance().setRealmService(realmService); + DeviceManagementDataHolder.getInstance().setRealmService(realmService); + InputStream is = this.getClass().getClassLoader().getResourceAsStream("carbon-home/repository/conf/registry.xml"); + RegistryContext context = RegistryContext.getBaseInstance(is, realmService); + context.setSetup(true); + return context.getEmbeddedRegistryService(); + } + + @Test + public void addNotification() throws Exception { + for (int i = 0; i < NO_OF_DEVICES; i++) { + DeviceIdentifier testDeviceIdentifier = new DeviceIdentifier(DEVICE_ID_PREFIX + i, DEVICE_TYPE); + Notification notification = mockNotification.getNotification(i, "CHECKED", + testDeviceIdentifier.toString(), DEVICE_ID_PREFIX + i, 1, DEVICE_TYPE); + NotificationManagementServiceImpl notificationManagementService = new NotificationManagementServiceImpl(); + Assert.assertTrue(notificationManagementService.addNotification(testDeviceIdentifier, notification)); + } + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/MockNotification.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/MockNotification.java new file mode 100644 index 0000000000..d6787defbe --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/MockNotification.java @@ -0,0 +1,37 @@ +/* +* 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.search.util; + +import org.wso2.carbon.device.mgt.common.notification.mgt.Notification; + +public class MockNotification { + + public Notification getNotification(int notificationId,String status,String deviceId,String deviceName, + int operationId, String deviceType){ + Notification notification = new Notification(); + notification.setNotificationId(notificationId); + notification.setStatus(status); + notification.setDeviceIdentifier(deviceId); + notification.setDescription("test description"); + notification.setDeviceName(deviceName); + notification.setOperationId(operationId); + notification.setDeviceType(deviceType); + return notification; + } +} + 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 ba8e8de691..331aac6c38 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 @@ -43,6 +43,7 @@ + From 8a6c208b012872bcb766da1bb6e31061612848bb Mon Sep 17 00:00:00 2001 From: charitha Date: Wed, 27 Sep 2017 15:46:07 +0530 Subject: [PATCH 6/7] Add additional test cases for grouping --- .../GroupManagementProviderServiceImpl.java | 73 +-------- .../GroupManagementProviderServiceTest.java | 145 ++++++++++-------- 2 files changed, 89 insertions(+), 129 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java index 13f87ad01f..69efaa107d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java @@ -259,7 +259,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid if (log.isDebugEnabled()) { log.debug("Get groups"); } - List deviceGroups = new ArrayList<>(); + List deviceGroups; try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); GroupManagementDAOFactory.openConnection(); @@ -293,7 +293,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid log.debug("Get groups with pagination " + request.toString()); } request = DeviceManagerUtil.validateGroupListPageSize(request); - List deviceGroups = new ArrayList<>(); + List deviceGroups; try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); GroupManagementDAOFactory.openConnection(); @@ -345,14 +345,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid for (DeviceGroup deviceGroup : deviceGroups) { groups.put(deviceGroup.getGroupId(), deviceGroup); } -// } catch (UserStoreException e) { -// String msg = "Error occurred while getting user store manager."; -// log.error(msg, e); -// throw new GroupManagementException(msg, e); -// } catch (SQLException e) { -// String msg = "Error occurred while retrieving all groups accessible to user."; -// log.error(msg, e); -// throw new GroupManagementException(msg, e); } catch (UserStoreException | SQLException | GroupManagementDAOException e) { String msg = "Error occurred while retrieving all groups accessible to user."; log.error(msg, e); @@ -368,16 +360,11 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } private List getGroupIds(String username) throws GroupManagementException { - if (username == null || username.isEmpty()) { - String msg = "Received empty user name for getGroupIds"; - log.error(msg); - throw new GroupManagementException(msg); - } if (log.isDebugEnabled()) { log.debug("Get groups Ids of owner '" + username + "'"); } UserStoreManager userStoreManager; - List deviceGroupIds = new ArrayList<>(); + List deviceGroupIds; try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); userStoreManager = DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId) @@ -386,14 +373,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid GroupManagementDAOFactory.openConnection(); deviceGroupIds = this.groupDAO.getOwnGroupIds(username, tenantId); deviceGroupIds.addAll(this.groupDAO.getGroupIds(roleList, tenantId)); -// } catch (UserStoreException e) { -// String msg = "Error occurred while getting user store manager."; -// log.error(msg, e); -// throw new GroupManagementException(msg, e); -// } catch (SQLException e) { -// String msg = "Error occurred while retrieving all groups accessible to user."; -// log.error(msg, e); -// throw new GroupManagementException(msg, e); } catch (UserStoreException | SQLException | GroupManagementDAOException e) { String msg = "Error occurred while retrieving all groups accessible to user."; log.error(msg, e); @@ -421,7 +400,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } request = DeviceManagerUtil.validateGroupListPageSize(request); List allDeviceGroupIdsOfUser = getGroupIds(currentUser); - List allMatchingGroups = new ArrayList<>(); + List allMatchingGroups; try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); GroupManagementDAOFactory.openConnection(); @@ -430,10 +409,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid String msg = "Error occurred while retrieving all groups in tenant"; log.error(msg, e); throw new GroupManagementException(msg, e); -// } catch (SQLException e) { -// String msg = "Error occurred while opening a connection to the data source."; -// log.error(msg, e); -// throw new GroupManagementException(msg, e); } catch (Exception e) { String msg = "Error occurred in getGroups"; log.error(msg, e); @@ -460,10 +435,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid String msg = "Error occurred while retrieving all groups in tenant"; log.error(msg, e); throw new GroupManagementException(msg, e); -// } catch (SQLException e) { -// String msg = "Error occurred while opening a connection to the data source."; -// log.error(msg, e); -// throw new GroupManagementException(msg, e); } catch (Exception e) { String msg = "Error occurred"; log.error(msg, e); @@ -490,10 +461,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid String msg = "Error occurred while retrieving all groups in tenant"; log.error(msg, e); throw new GroupManagementException(msg, e); -// } catch (SQLException e) { -// String msg = "Error occurred while opening a connection to the data source."; -// log.error(msg, e); -// throw new GroupManagementException(msg, e); } catch (Exception e) { String msg = "Error occurred in getGroupCount"; log.error(msg, e); @@ -527,18 +494,10 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid count = groupDAO.getOwnGroupsCount(username, tenantId); count += groupDAO.getGroupsCount(roleList, tenantId); return count; -// } catch (UserStoreException e) { -// String msg = "Error occurred while getting user store manager."; -// log.error(msg, e); -// throw new GroupManagementException(msg, e); } catch (UserStoreException | GroupManagementDAOException | SQLException e) { String msg = "Error occurred while retrieving group count of user '" + username + "'"; log.error(msg, e); throw new GroupManagementException(msg, e); -// } catch (SQLException e) { -// String msg = "Error occurred while opening a connection to the data source."; -// log.error(msg, e); -// throw new GroupManagementException(msg, e); } catch (Exception e) { String msg = "Error occurred in getGroupCount for username '" + username + "'"; log.error(msg, e); @@ -619,10 +578,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid String msg = "Error occurred while retrieving all groups in tenant"; log.error(msg, e); throw new GroupManagementException(msg, e); -// } catch (SQLException e) { -// String msg = "Error occurred while opening a connection to the data source."; -// log.error(msg, e); -// throw new GroupManagementException(msg, e); } catch (Exception e) { String msg = "Error occurred in getRoles for groupId: " + groupId; log.error(msg, e); @@ -651,14 +606,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid String msg = "Error occurred while getting devices in group."; log.error(msg, e); throw new GroupManagementException(msg, e); -// } catch (SQLException e) { -// String msg = "Error occurred while opening a connection to the data source."; -// log.error(msg, e); -// throw new GroupManagementException(msg, e); -// } catch (DeviceManagementException e) { -// String msg = "Error occurred while validating the limit of the devices to be returned"; -// log.error(msg, e); -// throw new GroupManagementException(msg, e); } catch (Exception e) { String msg = "Error occurred in getDevices for groupId: " + groupId; log.error(msg, e); @@ -684,10 +631,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid String msg = "Error occurred while retrieving all groups in tenant"; log.error(msg, e); throw new GroupManagementException(msg, e); -// } catch (SQLException e) { -// String msg = "Error occurred while opening a connection to the data source."; -// log.error(msg, e); -// throw new GroupManagementException(msg, e); } catch (Exception e) { String msg = "Error occurred in getDeviceCount for groupId: " + groupId; log.error(msg, e); @@ -838,18 +781,10 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid GroupManagementDAOFactory.openConnection(); return groupDAO.getGroups(device.getId(), PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); -// } catch (DeviceManagementException e) { -// String msg = "Error occurred while retrieving the device details."; -// log.error(msg, e); -// throw new GroupManagementException(msg, e); } catch (DeviceManagementException | GroupManagementDAOException | SQLException e) { String msg = "Error occurred while retrieving device groups."; log.error(msg, e); throw new GroupManagementException(msg, e); -// } catch (SQLException e) { -// String msg = "Error occurred while opening database connection."; -// log.error(msg, e); -// throw new GroupManagementException(msg, e); } catch (Exception e) { String msg = "Error occurred in getGroups"; log.error(msg, e); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceTest.java index 6a13e27b70..994ef33e8e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceTest.java @@ -42,7 +42,7 @@ import java.util.List; public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest { - GroupManagementProviderService groupManagementProviderService; + private GroupManagementProviderService groupManagementProviderService; private static final String DEFAULT_ADMIN_ROLE = "admin"; private static final String[] DEFAULT_ADMIN_PERMISSIONS = {"/permission/device-mgt/admin/groups", "/permission/device-mgt/user/groups"}; @@ -54,6 +54,7 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest RealmService realmService = new InMemoryRealmService(); DeviceManagementDataHolder.getInstance().setRealmService(realmService); realmService.getTenantManager().getSuperTenantDomain(); + DeviceConfigurationManager.getInstance().initConfig(); } @Test(expectedExceptions = {GroupManagementException.class, GroupAlreadyExistException.class}) @@ -71,24 +72,32 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest @Test public void createGroup() throws GroupManagementException, GroupAlreadyExistException { - groupManagementProviderService.createGroup(TestUtils.createDeviceGroup1(), DEFAULT_ADMIN_ROLE, DEFAULT_ADMIN_PERMISSIONS); - groupManagementProviderService.createGroup(TestUtils.createDeviceGroup2(), DEFAULT_ADMIN_ROLE, DEFAULT_ADMIN_PERMISSIONS); - groupManagementProviderService.createGroup(TestUtils.createDeviceGroup3(), DEFAULT_ADMIN_ROLE, DEFAULT_ADMIN_PERMISSIONS); - groupManagementProviderService.createGroup(TestUtils.createDeviceGroup4(), DEFAULT_ADMIN_ROLE, DEFAULT_ADMIN_PERMISSIONS); } @Test(dependsOnMethods = ("createGroup")) public void updateGroup() throws GroupManagementException, GroupNotExistException { - DeviceGroup deviceGroup = groupManagementProviderService.getGroup(TestUtils.createDeviceGroup1().getName()); deviceGroup.setName(deviceGroup.getName() + "_UPDATED"); groupManagementProviderService.updateGroup(deviceGroup, deviceGroup.getGroupId()); } + @Test(dependsOnMethods = ("createGroup"), expectedExceptions = {GroupManagementException.class}) + public void getGroupNull() throws GroupManagementException, GroupNotExistException { + groupManagementProviderService.getGroup(null); + } + + // Rename again to use in different place. + @Test(dependsOnMethods = ("updateGroup")) + public void updateGroupSecondTime() throws GroupManagementException, GroupNotExistException { + DeviceGroup deviceGroup = groupManagementProviderService.getGroup(TestUtils.createDeviceGroup1().getName() + "_UPDATED"); + deviceGroup.setName(TestUtils.createDeviceGroup1().getName()); + groupManagementProviderService.updateGroup(deviceGroup, deviceGroup.getGroupId()); + } + @Test(dependsOnMethods = ("createGroup"), expectedExceptions = {GroupManagementException.class, GroupNotExistException.class}) public void updateGroupError() throws GroupManagementException, GroupNotExistException { groupManagementProviderService.updateGroup(null, 1); @@ -96,7 +105,6 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest @Test(dependsOnMethods = ("createGroup"), expectedExceptions = {GroupManagementException.class, GroupNotExistException.class}) public void updateGroupErrorNotExist() throws GroupManagementException, GroupNotExistException { - DeviceGroup deviceGroup = groupManagementProviderService.getGroup(TestUtils.createDeviceGroup2().getName()); deviceGroup.setName(deviceGroup.getName() + "_UPDATED"); groupManagementProviderService.updateGroup(deviceGroup, 6); @@ -141,8 +149,7 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest @Test(dependsOnMethods = ("createGroup"), expectedExceptions = {GroupManagementException.class}) public void getGroupsByUsernameError() throws GroupManagementException { - String username = null; - groupManagementProviderService.getGroups(username); + groupManagementProviderService.getGroups((String) null); } @Test(dependsOnMethods = ("createGroup")) @@ -158,12 +165,19 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest } @Test(dependsOnMethods = ("createGroup")) - public void getGroupsByUsernameAndPagination(String username, GroupPaginationRequest paginationRequest) + public void getGroupsByUsernameAndPagination() throws GroupManagementException { - PaginationResult result = groupManagementProviderService.getGroups(username, paginationRequest); + PaginationResult result = groupManagementProviderService.getGroups("admin", TestUtils.createPaginationRequest()); Assert.assertNotNull(result); } + + @Test(dependsOnMethods = ("createGroup"), expectedExceptions = {GroupManagementException.class}) + public void getGroupsByUsernameAndPaginationError() + throws GroupManagementException { + groupManagementProviderService.getGroups(null, TestUtils.createPaginationRequest()); + } + @Test(dependsOnMethods = ("createGroup")) public void getGroupCount() throws GroupManagementException { int x = groupManagementProviderService.getGroupCount(); @@ -176,27 +190,28 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest Assert.assertNotNull(x); } -// @Test -// public void manageGroupSharing() throws GroupManagementException, RoleDoesNotExistException, UserStoreException { -// groupManagementProviderService.manageGroupSharing(0, null); -// List newRoles = new ArrayList<>(); -// newRoles.add("TEST_ROLE_1"); -// newRoles.add("TEST_ROLE_2"); -// newRoles.add("TEST_ROLE_3"); -// -// UserStoreManager userStoreManager = -// DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm( -// -1234).getUserStoreManager(); -// Permission[] permissions = new Permission[1]; -// Permission perm = new Permission("/admin/test/perm", "add"); -//// perm.setAction("execute.ui"); -//// perm.setResourceId("/admin/test/perm"); -// permissions[0] = perm; -// -// userStoreManager.addRole("TEST_ROLE_1", null, permissions); -// -// groupManagementProviderService.manageGroupSharing(1, newRoles); -// } + @Test(dependsOnMethods = ("updateGroupSecondTime")) + public void manageGroupSharing() throws GroupManagementException, RoleDoesNotExistException, UserStoreException { + groupManagementProviderService.manageGroupSharing(0, null); + List newRoles = new ArrayList<>(); + newRoles.add("TEST_ROLE_1"); + newRoles.add("TEST_ROLE_2"); + newRoles.add("TEST_ROLE_3"); + + UserStoreManager userStoreManager = + DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm( + -1234).getUserStoreManager(); + Permission[] permissions = new Permission[1]; + Permission perm = new Permission("/admin/test/perm", "add"); + permissions[0] = perm; + + userStoreManager.addRole("TEST_ROLE_1", null, permissions); + userStoreManager.addRole("TEST_ROLE_2", null, permissions); + userStoreManager.addRole("TEST_ROLE_3", null, permissions); + + groupManagementProviderService.manageGroupSharing(groupManagementProviderService.getGroup( + TestUtils.createDeviceGroup1().getName()).getGroupId(), newRoles); + } @Test(dependsOnMethods = ("createGroup")) public void getRoles() throws GroupManagementException { @@ -216,26 +231,30 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest Assert.assertEquals(0, x); } -// @Test(dependsOnMethods = ("createGroup")) -// public void addDevices() throws GroupManagementException, DeviceNotFoundException { -// -// DeviceCacheConfiguration configuration = new DeviceCacheConfiguration(); -// configuration.setEnabled(false); -// -// DeviceConfigurationManager.getInstance().getDeviceManagementConfig().setDeviceCacheConfiguration(configuration); -// -// List list = TestUtils.getDeviceIdentifiersList(); -// groupManagementProviderService.addDevices(1, list); -// groupManagementProviderService.addDevices(2, list); -// groupManagementProviderService.addDevices(3, list); -// } -// -// @Test(dependsOnMethods = ("addDevices")) -// public void removeDevice() throws GroupManagementException, DeviceNotFoundException { -// List list = TestUtils.getDeviceIdentifiersList(); -// groupManagementProviderService.removeDevice(2, list); -// groupManagementProviderService.removeDevice(3, list); -// } + @Test(dependsOnMethods = ("createGroup")) + public void addDevices() throws GroupManagementException, DeviceNotFoundException { + + DeviceCacheConfiguration configuration = new DeviceCacheConfiguration(); + configuration.setEnabled(false); + + DeviceConfigurationManager.getInstance().getDeviceManagementConfig().setDeviceCacheConfiguration(configuration); + List list = TestUtils.getDeviceIdentifiersList(); + groupManagementProviderService.addDevices(groupManagementProviderService.getGroup( + TestUtils.createDeviceGroup1().getName()).getGroupId(), list); + groupManagementProviderService.addDevices(groupManagementProviderService.getGroup( + TestUtils.createDeviceGroup2().getName()).getGroupId(), list); + groupManagementProviderService.addDevices(groupManagementProviderService.getGroup( + TestUtils.createDeviceGroup3().getName()).getGroupId(), list); + } + + @Test(dependsOnMethods = ("addDevices")) + public void removeDevice() throws GroupManagementException, DeviceNotFoundException { + List list = TestUtils.getDeviceIdentifiersList(); + groupManagementProviderService.removeDevice(groupManagementProviderService.getGroup( + TestUtils.createDeviceGroup2().getName()).getGroupId(), list); + groupManagementProviderService.removeDevice(groupManagementProviderService.getGroup( + TestUtils.createDeviceGroup3().getName()).getGroupId(), list); + } @Test(dependsOnMethods = ("createGroup")) public void getGroupsByUsernameAndPermissions() throws GroupManagementException { @@ -243,18 +262,24 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest Assert.assertNotNull(groups); } -// @Test(dependsOnMethods = ("addDevices")) -// public void getGroupsByDeviceIdentifier() throws GroupManagementException { -// DeviceIdentifier identifier = new DeviceIdentifier(); -// identifier.setId("12345"); -// identifier.setType("Test"); -// List groups = groupManagementProviderService.getGroups(identifier); -// Assert.assertNull(groups); -// } + @Test(dependsOnMethods = ("addDevices")) + public void getGroupsByDeviceIdentifier() throws GroupManagementException { + DeviceIdentifier identifier = new DeviceIdentifier(); + identifier.setId("12345"); + identifier.setType("Test"); + List groups = groupManagementProviderService.getGroups(identifier); + Assert.assertNotNull(groups); + } @Test public void createDefaultGroup() throws GroupManagementException { groupManagementProviderService.createDefaultGroup("BYOD"); } + + @Test(dependsOnMethods = ("createDefaultGroup")) + public void createDefaultGroupTwice() throws GroupManagementException { + groupManagementProviderService.createDefaultGroup("BYOD"); + } + } From 046fad6bdb7ffed1afa8bb5fd3563a21021d0faf Mon Sep 17 00:00:00 2001 From: Nirothipan Date: Wed, 27 Sep 2017 15:49:11 +0530 Subject: [PATCH 7/7] making required changes --- .../mgt/core/common/TestDataHolder.java | 14 +++++++ .../mgt/NotificationManagementTests.java | 10 +++-- .../core/search/util/MockNotification.java | 37 ------------------- 3 files changed, 20 insertions(+), 41 deletions(-) delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/MockNotification.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDataHolder.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDataHolder.java index 180fae6b1b..356efb2426 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDataHolder.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDataHolder.java @@ -20,6 +20,7 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.app.mgt.Application; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; +import org.wso2.carbon.device.mgt.common.notification.mgt.Notification; import org.wso2.carbon.device.mgt.core.dto.DeviceType; import java.util.ArrayList; @@ -52,6 +53,19 @@ public class TestDataHolder { return device; } + public static Notification getNotification(int notificationId, String status, String deviceId, String deviceName, + int operationId, String deviceType){ + Notification notification = new Notification(); + notification.setNotificationId(notificationId); + notification.setStatus(status); + notification.setDeviceIdentifier(deviceId); + notification.setDescription("test description"); + notification.setDeviceName(deviceName); + notification.setOperationId(operationId); + notification.setDeviceType(deviceType); + return notification; + } + public static List generateDummyDeviceData(List deviceIds) { List devices = new ArrayList<>(); for (DeviceIdentifier deviceId : deviceIds) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementTests.java index 7f70461a14..8257785fc1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementTests.java @@ -31,7 +31,6 @@ 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.search.util.MockNotification; 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; @@ -47,6 +46,9 @@ import java.io.InputStream; import java.util.ArrayList; import java.util.List; +/** + * This class is used to test NotificationManagementServiceImpl. + */ public class NotificationManagementTests { private static final Log log = LogFactory.getLog(NotificationManagementTests.class); @@ -54,7 +56,6 @@ public class NotificationManagementTests { private static final String DEVICE_ID_PREFIX = "NOTIFICATION-TEST-DEVICE-ID-"; private static final int NO_OF_DEVICES = 10; private List deviceIds = new ArrayList<>(); - private MockNotification mockNotification = new MockNotification(); @BeforeClass public void init() throws Exception { @@ -95,14 +96,15 @@ public class NotificationManagementTests { return context.getEmbeddedRegistryService(); } - @Test + @Test(description = "Add notifications using addNotification method and check whether it returns true.") public void addNotification() throws Exception { for (int i = 0; i < NO_OF_DEVICES; i++) { DeviceIdentifier testDeviceIdentifier = new DeviceIdentifier(DEVICE_ID_PREFIX + i, DEVICE_TYPE); - Notification notification = mockNotification.getNotification(i, "CHECKED", + Notification notification = TestDataHolder.getNotification(i, "CHECKED", testDeviceIdentifier.toString(), DEVICE_ID_PREFIX + i, 1, DEVICE_TYPE); NotificationManagementServiceImpl notificationManagementService = new NotificationManagementServiceImpl(); Assert.assertTrue(notificationManagementService.addNotification(testDeviceIdentifier, notification)); } } } + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/MockNotification.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/MockNotification.java deleted file mode 100644 index d6787defbe..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/MockNotification.java +++ /dev/null @@ -1,37 +0,0 @@ -/* -* 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.search.util; - -import org.wso2.carbon.device.mgt.common.notification.mgt.Notification; - -public class MockNotification { - - public Notification getNotification(int notificationId,String status,String deviceId,String deviceName, - int operationId, String deviceType){ - Notification notification = new Notification(); - notification.setNotificationId(notificationId); - notification.setStatus(status); - notification.setDeviceIdentifier(deviceId); - notification.setDescription("test description"); - notification.setDeviceName(deviceName); - notification.setOperationId(operationId); - notification.setDeviceType(deviceType); - return notification; - } -} -