Merge remote-tracking branch 'upstream/master'

revert-70aa11f8
megala21 7 years ago
commit 2dabd4cfe0

@ -221,6 +221,14 @@
<groupId>org.testng</groupId> <groupId>org.testng</groupId>
<artifactId>testng</artifactId> <artifactId>testng</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-testng</artifactId>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.wso2.carbon</groupId> <groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.user.core</artifactId> <artifactId>org.wso2.carbon.user.core</artifactId>

@ -50,11 +50,9 @@ public class NotificationManagementServiceImpl implements NotificationManagement
private static final Log log = LogFactory.getLog(NotificationManagementServiceImpl.class); private static final Log log = LogFactory.getLog(NotificationManagementServiceImpl.class);
private NotificationDAO notificationDAO; private NotificationDAO notificationDAO;
private DeviceDAO deviceDAO;
public NotificationManagementServiceImpl() { public NotificationManagementServiceImpl() {
this.notificationDAO = NotificationManagementDAOFactory.getNotificationDAO(); this.notificationDAO = NotificationManagementDAOFactory.getNotificationDAO();
this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO();
} }
@Override @Override

@ -279,4 +279,4 @@ public abstract class AbstractNotificationDAOImpl implements NotificationDAO {
} }
return notificationCountByStatus; return notificationCountByStatus;
} }
} }

@ -259,7 +259,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Get groups"); log.debug("Get groups");
} }
List<DeviceGroup> deviceGroups = new ArrayList<>(); List<DeviceGroup> deviceGroups;
try { try {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
GroupManagementDAOFactory.openConnection(); GroupManagementDAOFactory.openConnection();
@ -293,7 +293,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
log.debug("Get groups with pagination " + request.toString()); log.debug("Get groups with pagination " + request.toString());
} }
request = DeviceManagerUtil.validateGroupListPageSize(request); request = DeviceManagerUtil.validateGroupListPageSize(request);
List<DeviceGroup> deviceGroups = new ArrayList<>(); List<DeviceGroup> deviceGroups;
try { try {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
GroupManagementDAOFactory.openConnection(); GroupManagementDAOFactory.openConnection();
@ -345,14 +345,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
for (DeviceGroup deviceGroup : deviceGroups) { for (DeviceGroup deviceGroup : deviceGroups) {
groups.put(deviceGroup.getGroupId(), deviceGroup); 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) { } catch (UserStoreException | SQLException | GroupManagementDAOException e) {
String msg = "Error occurred while retrieving all groups accessible to user."; String msg = "Error occurred while retrieving all groups accessible to user.";
log.error(msg, e); log.error(msg, e);
@ -368,16 +360,11 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
} }
private List<Integer> getGroupIds(String username) throws GroupManagementException { private List<Integer> 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()) { if (log.isDebugEnabled()) {
log.debug("Get groups Ids of owner '" + username + "'"); log.debug("Get groups Ids of owner '" + username + "'");
} }
UserStoreManager userStoreManager; UserStoreManager userStoreManager;
List<Integer> deviceGroupIds = new ArrayList<>(); List<Integer> deviceGroupIds;
try { try {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
userStoreManager = DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId) userStoreManager = DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId)
@ -386,14 +373,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
GroupManagementDAOFactory.openConnection(); GroupManagementDAOFactory.openConnection();
deviceGroupIds = this.groupDAO.getOwnGroupIds(username, tenantId); deviceGroupIds = this.groupDAO.getOwnGroupIds(username, tenantId);
deviceGroupIds.addAll(this.groupDAO.getGroupIds(roleList, 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) { } catch (UserStoreException | SQLException | GroupManagementDAOException e) {
String msg = "Error occurred while retrieving all groups accessible to user."; String msg = "Error occurred while retrieving all groups accessible to user.";
log.error(msg, e); log.error(msg, e);
@ -421,7 +400,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
} }
request = DeviceManagerUtil.validateGroupListPageSize(request); request = DeviceManagerUtil.validateGroupListPageSize(request);
List<Integer> allDeviceGroupIdsOfUser = getGroupIds(currentUser); List<Integer> allDeviceGroupIdsOfUser = getGroupIds(currentUser);
List<DeviceGroup> allMatchingGroups = new ArrayList<>(); List<DeviceGroup> allMatchingGroups;
try { try {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
GroupManagementDAOFactory.openConnection(); GroupManagementDAOFactory.openConnection();
@ -430,10 +409,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
String msg = "Error occurred while retrieving all groups in tenant"; String msg = "Error occurred while retrieving all groups in tenant";
log.error(msg, e); log.error(msg, e);
throw new GroupManagementException(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) { } catch (Exception e) {
String msg = "Error occurred in getGroups"; String msg = "Error occurred in getGroups";
log.error(msg, e); log.error(msg, e);
@ -460,10 +435,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
String msg = "Error occurred while retrieving all groups in tenant"; String msg = "Error occurred while retrieving all groups in tenant";
log.error(msg, e); log.error(msg, e);
throw new GroupManagementException(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) { } catch (Exception e) {
String msg = "Error occurred"; String msg = "Error occurred";
log.error(msg, e); log.error(msg, e);
@ -490,10 +461,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
String msg = "Error occurred while retrieving all groups in tenant"; String msg = "Error occurred while retrieving all groups in tenant";
log.error(msg, e); log.error(msg, e);
throw new GroupManagementException(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) { } catch (Exception e) {
String msg = "Error occurred in getGroupCount"; String msg = "Error occurred in getGroupCount";
log.error(msg, e); log.error(msg, e);
@ -527,18 +494,10 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
count = groupDAO.getOwnGroupsCount(username, tenantId); count = groupDAO.getOwnGroupsCount(username, tenantId);
count += groupDAO.getGroupsCount(roleList, tenantId); count += groupDAO.getGroupsCount(roleList, tenantId);
return count; 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) { } catch (UserStoreException | GroupManagementDAOException | SQLException e) {
String msg = "Error occurred while retrieving group count of user '" + username + "'"; String msg = "Error occurred while retrieving group count of user '" + username + "'";
log.error(msg, e); log.error(msg, e);
throw new GroupManagementException(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) { } catch (Exception e) {
String msg = "Error occurred in getGroupCount for username '" + username + "'"; String msg = "Error occurred in getGroupCount for username '" + username + "'";
log.error(msg, e); log.error(msg, e);
@ -619,10 +578,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
String msg = "Error occurred while retrieving all groups in tenant"; String msg = "Error occurred while retrieving all groups in tenant";
log.error(msg, e); log.error(msg, e);
throw new GroupManagementException(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) { } catch (Exception e) {
String msg = "Error occurred in getRoles for groupId: " + groupId; String msg = "Error occurred in getRoles for groupId: " + groupId;
log.error(msg, e); log.error(msg, e);
@ -651,14 +606,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
String msg = "Error occurred while getting devices in group."; String msg = "Error occurred while getting devices in group.";
log.error(msg, e); log.error(msg, e);
throw new GroupManagementException(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) { } catch (Exception e) {
String msg = "Error occurred in getDevices for groupId: " + groupId; String msg = "Error occurred in getDevices for groupId: " + groupId;
log.error(msg, e); log.error(msg, e);
@ -684,10 +631,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
String msg = "Error occurred while retrieving all groups in tenant"; String msg = "Error occurred while retrieving all groups in tenant";
log.error(msg, e); log.error(msg, e);
throw new GroupManagementException(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) { } catch (Exception e) {
String msg = "Error occurred in getDeviceCount for groupId: " + groupId; String msg = "Error occurred in getDeviceCount for groupId: " + groupId;
log.error(msg, e); log.error(msg, e);
@ -838,18 +781,10 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
GroupManagementDAOFactory.openConnection(); GroupManagementDAOFactory.openConnection();
return groupDAO.getGroups(device.getId(), return groupDAO.getGroups(device.getId(),
PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); 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) { } catch (DeviceManagementException | GroupManagementDAOException | SQLException e) {
String msg = "Error occurred while retrieving device groups."; String msg = "Error occurred while retrieving device groups.";
log.error(msg, e); log.error(msg, e);
throw new GroupManagementException(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) { } catch (Exception e) {
String msg = "Error occurred in getGroups"; String msg = "Error occurred in getGroups";
log.error(msg, e); log.error(msg, e);

@ -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.TestUtils;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; 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.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.operation.mgt.dao.OperationManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
@ -62,6 +63,7 @@ public abstract class BaseDeviceManagementTest {
DeviceManagementDAOFactory.init(dataSource); DeviceManagementDAOFactory.init(dataSource);
GroupManagementDAOFactory.init(dataSource); GroupManagementDAOFactory.init(dataSource);
OperationManagementDAOFactory.init(dataSource); OperationManagementDAOFactory.init(dataSource);
NotificationManagementDAOFactory.init(dataSource);
} }
@BeforeClass @BeforeClass
@ -131,7 +133,7 @@ public abstract class BaseDeviceManagementTest {
try { try {
conn = getDataSource().getConnection(); conn = getDataSource().getConnection();
conn.setAutoCommit(false); 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_POLICY_OPERATION", "DM_COMMAND_OPERATION", "DM_PROFILE_OPERATION", "DM_DEVICE_GROUP_MAP",
"DM_GROUP", "DM_ENROLMENT", "DM_DEVICE_APPLICATION_MAPPING", "DM_GROUP", "DM_ENROLMENT", "DM_DEVICE_APPLICATION_MAPPING",
"DM_APPLICATION", "DM_DEVICE", "DM_DEVICE_TYPE"}; "DM_APPLICATION", "DM_DEVICE", "DM_DEVICE_TYPE"};

@ -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.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.app.mgt.Application; 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.group.mgt.DeviceGroup;
import org.wso2.carbon.device.mgt.common.notification.mgt.Notification;
import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import java.util.ArrayList; import java.util.ArrayList;
@ -52,6 +53,19 @@ public class TestDataHolder {
return device; 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<Device> generateDummyDeviceData(List<DeviceIdentifier> deviceIds) { public static List<Device> generateDummyDeviceData(List<DeviceIdentifier> deviceIds) {
List<Device> devices = new ArrayList<>(); List<Device> devices = new ArrayList<>();
for (DeviceIdentifier deviceId : deviceIds) { for (DeviceIdentifier deviceId : deviceIds) {

@ -0,0 +1,110 @@
/*
* 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.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;
/**
* This class is used to test NotificationManagementServiceImpl.
*/
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<DeviceIdentifier> deviceIds = new ArrayList<>();
@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<Device> 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<Device> 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(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 = TestDataHolder.getNotification(i, "CHECKED",
testDeviceIdentifier.toString(), DEVICE_ID_PREFIX + i, 1, DEVICE_TYPE);
NotificationManagementServiceImpl notificationManagementService = new NotificationManagementServiceImpl();
Assert.assertTrue(notificationManagementService.addNotification(testDeviceIdentifier, notification));
}
}
}

@ -0,0 +1,121 @@
/*
* 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.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;
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 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 static final String HTTP_METHOD = "HTTP_METHOD";
private static final String URL = "URL";
private Permission permission;
private PermissionManagerService permissionManagerService;
@ObjectFactory
public IObjectFactory getObjectFactory() {
return new org.powermock.modules.testng.PowerMockObjectFactory();
}
@BeforeClass
public void init() throws RegistryException {
initMocks(this);
permissionManagerService = PermissionManagerServiceImpl.getInstance();
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);
}
@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.
* */
private Properties createProperties() {
Properties properties = new Properties();
properties.setProperty(URL, PERMISSION_URL);
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;
}
}

@ -42,7 +42,7 @@ import java.util.List;
public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest { public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest {
GroupManagementProviderService groupManagementProviderService; private GroupManagementProviderService groupManagementProviderService;
private static final String DEFAULT_ADMIN_ROLE = "admin"; private static final String DEFAULT_ADMIN_ROLE = "admin";
private static final String[] DEFAULT_ADMIN_PERMISSIONS = {"/permission/device-mgt/admin/groups", private static final String[] DEFAULT_ADMIN_PERMISSIONS = {"/permission/device-mgt/admin/groups",
"/permission/device-mgt/user/groups"}; "/permission/device-mgt/user/groups"};
@ -54,6 +54,7 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest
RealmService realmService = new InMemoryRealmService(); RealmService realmService = new InMemoryRealmService();
DeviceManagementDataHolder.getInstance().setRealmService(realmService); DeviceManagementDataHolder.getInstance().setRealmService(realmService);
realmService.getTenantManager().getSuperTenantDomain(); realmService.getTenantManager().getSuperTenantDomain();
DeviceConfigurationManager.getInstance().initConfig();
} }
@Test(expectedExceptions = {GroupManagementException.class, GroupAlreadyExistException.class}) @Test(expectedExceptions = {GroupManagementException.class, GroupAlreadyExistException.class})
@ -71,24 +72,32 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest
@Test @Test
public void createGroup() throws GroupManagementException, GroupAlreadyExistException { public void createGroup() throws GroupManagementException, GroupAlreadyExistException {
groupManagementProviderService.createGroup(TestUtils.createDeviceGroup1(), DEFAULT_ADMIN_ROLE, DEFAULT_ADMIN_PERMISSIONS); groupManagementProviderService.createGroup(TestUtils.createDeviceGroup1(), DEFAULT_ADMIN_ROLE, DEFAULT_ADMIN_PERMISSIONS);
groupManagementProviderService.createGroup(TestUtils.createDeviceGroup2(), 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.createDeviceGroup3(), DEFAULT_ADMIN_ROLE, DEFAULT_ADMIN_PERMISSIONS);
groupManagementProviderService.createGroup(TestUtils.createDeviceGroup4(), DEFAULT_ADMIN_ROLE, DEFAULT_ADMIN_PERMISSIONS); groupManagementProviderService.createGroup(TestUtils.createDeviceGroup4(), DEFAULT_ADMIN_ROLE, DEFAULT_ADMIN_PERMISSIONS);
} }
@Test(dependsOnMethods = ("createGroup")) @Test(dependsOnMethods = ("createGroup"))
public void updateGroup() throws GroupManagementException, GroupNotExistException { public void updateGroup() throws GroupManagementException, GroupNotExistException {
DeviceGroup deviceGroup = groupManagementProviderService.getGroup(TestUtils.createDeviceGroup1().getName()); DeviceGroup deviceGroup = groupManagementProviderService.getGroup(TestUtils.createDeviceGroup1().getName());
deviceGroup.setName(deviceGroup.getName() + "_UPDATED"); deviceGroup.setName(deviceGroup.getName() + "_UPDATED");
groupManagementProviderService.updateGroup(deviceGroup, deviceGroup.getGroupId()); 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}) @Test(dependsOnMethods = ("createGroup"), expectedExceptions = {GroupManagementException.class, GroupNotExistException.class})
public void updateGroupError() throws GroupManagementException, GroupNotExistException { public void updateGroupError() throws GroupManagementException, GroupNotExistException {
groupManagementProviderService.updateGroup(null, 1); groupManagementProviderService.updateGroup(null, 1);
@ -96,7 +105,6 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest
@Test(dependsOnMethods = ("createGroup"), expectedExceptions = {GroupManagementException.class, GroupNotExistException.class}) @Test(dependsOnMethods = ("createGroup"), expectedExceptions = {GroupManagementException.class, GroupNotExistException.class})
public void updateGroupErrorNotExist() throws GroupManagementException, GroupNotExistException { public void updateGroupErrorNotExist() throws GroupManagementException, GroupNotExistException {
DeviceGroup deviceGroup = groupManagementProviderService.getGroup(TestUtils.createDeviceGroup2().getName()); DeviceGroup deviceGroup = groupManagementProviderService.getGroup(TestUtils.createDeviceGroup2().getName());
deviceGroup.setName(deviceGroup.getName() + "_UPDATED"); deviceGroup.setName(deviceGroup.getName() + "_UPDATED");
groupManagementProviderService.updateGroup(deviceGroup, 6); groupManagementProviderService.updateGroup(deviceGroup, 6);
@ -141,8 +149,7 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest
@Test(dependsOnMethods = ("createGroup"), expectedExceptions = {GroupManagementException.class}) @Test(dependsOnMethods = ("createGroup"), expectedExceptions = {GroupManagementException.class})
public void getGroupsByUsernameError() throws GroupManagementException { public void getGroupsByUsernameError() throws GroupManagementException {
String username = null; groupManagementProviderService.getGroups((String) null);
groupManagementProviderService.getGroups(username);
} }
@Test(dependsOnMethods = ("createGroup")) @Test(dependsOnMethods = ("createGroup"))
@ -158,12 +165,19 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest
} }
@Test(dependsOnMethods = ("createGroup")) @Test(dependsOnMethods = ("createGroup"))
public void getGroupsByUsernameAndPagination(String username, GroupPaginationRequest paginationRequest) public void getGroupsByUsernameAndPagination()
throws GroupManagementException { throws GroupManagementException {
PaginationResult result = groupManagementProviderService.getGroups(username, paginationRequest); PaginationResult result = groupManagementProviderService.getGroups("admin", TestUtils.createPaginationRequest());
Assert.assertNotNull(result); Assert.assertNotNull(result);
} }
@Test(dependsOnMethods = ("createGroup"), expectedExceptions = {GroupManagementException.class})
public void getGroupsByUsernameAndPaginationError()
throws GroupManagementException {
groupManagementProviderService.getGroups(null, TestUtils.createPaginationRequest());
}
@Test(dependsOnMethods = ("createGroup")) @Test(dependsOnMethods = ("createGroup"))
public void getGroupCount() throws GroupManagementException { public void getGroupCount() throws GroupManagementException {
int x = groupManagementProviderService.getGroupCount(); int x = groupManagementProviderService.getGroupCount();
@ -176,27 +190,28 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest
Assert.assertNotNull(x); Assert.assertNotNull(x);
} }
// @Test @Test(dependsOnMethods = ("updateGroupSecondTime"))
// public void manageGroupSharing() throws GroupManagementException, RoleDoesNotExistException, UserStoreException { public void manageGroupSharing() throws GroupManagementException, RoleDoesNotExistException, UserStoreException {
// groupManagementProviderService.manageGroupSharing(0, null); groupManagementProviderService.manageGroupSharing(0, null);
// List<String> newRoles = new ArrayList<>(); List<String> newRoles = new ArrayList<>();
// newRoles.add("TEST_ROLE_1"); newRoles.add("TEST_ROLE_1");
// newRoles.add("TEST_ROLE_2"); newRoles.add("TEST_ROLE_2");
// newRoles.add("TEST_ROLE_3"); newRoles.add("TEST_ROLE_3");
//
// UserStoreManager userStoreManager = UserStoreManager userStoreManager =
// DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm( DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm(
// -1234).getUserStoreManager(); -1234).getUserStoreManager();
// Permission[] permissions = new Permission[1]; Permission[] permissions = new Permission[1];
// Permission perm = new Permission("/admin/test/perm", "add"); Permission perm = new Permission("/admin/test/perm", "add");
//// perm.setAction("execute.ui"); permissions[0] = perm;
//// perm.setResourceId("/admin/test/perm");
// permissions[0] = perm; userStoreManager.addRole("TEST_ROLE_1", null, permissions);
// userStoreManager.addRole("TEST_ROLE_2", null, permissions);
// userStoreManager.addRole("TEST_ROLE_1", null, permissions); userStoreManager.addRole("TEST_ROLE_3", null, permissions);
//
// groupManagementProviderService.manageGroupSharing(1, newRoles); groupManagementProviderService.manageGroupSharing(groupManagementProviderService.getGroup(
// } TestUtils.createDeviceGroup1().getName()).getGroupId(), newRoles);
}
@Test(dependsOnMethods = ("createGroup")) @Test(dependsOnMethods = ("createGroup"))
public void getRoles() throws GroupManagementException { public void getRoles() throws GroupManagementException {
@ -216,26 +231,30 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest
Assert.assertEquals(0, x); Assert.assertEquals(0, x);
} }
// @Test(dependsOnMethods = ("createGroup")) @Test(dependsOnMethods = ("createGroup"))
// public void addDevices() throws GroupManagementException, DeviceNotFoundException { public void addDevices() throws GroupManagementException, DeviceNotFoundException {
//
// DeviceCacheConfiguration configuration = new DeviceCacheConfiguration(); DeviceCacheConfiguration configuration = new DeviceCacheConfiguration();
// configuration.setEnabled(false); configuration.setEnabled(false);
//
// DeviceConfigurationManager.getInstance().getDeviceManagementConfig().setDeviceCacheConfiguration(configuration); DeviceConfigurationManager.getInstance().getDeviceManagementConfig().setDeviceCacheConfiguration(configuration);
// List<DeviceIdentifier> list = TestUtils.getDeviceIdentifiersList();
// List<DeviceIdentifier> list = TestUtils.getDeviceIdentifiersList(); groupManagementProviderService.addDevices(groupManagementProviderService.getGroup(
// groupManagementProviderService.addDevices(1, list); TestUtils.createDeviceGroup1().getName()).getGroupId(), list);
// groupManagementProviderService.addDevices(2, list); groupManagementProviderService.addDevices(groupManagementProviderService.getGroup(
// groupManagementProviderService.addDevices(3, list); TestUtils.createDeviceGroup2().getName()).getGroupId(), list);
// } groupManagementProviderService.addDevices(groupManagementProviderService.getGroup(
// TestUtils.createDeviceGroup3().getName()).getGroupId(), list);
// @Test(dependsOnMethods = ("addDevices")) }
// public void removeDevice() throws GroupManagementException, DeviceNotFoundException {
// List<DeviceIdentifier> list = TestUtils.getDeviceIdentifiersList(); @Test(dependsOnMethods = ("addDevices"))
// groupManagementProviderService.removeDevice(2, list); public void removeDevice() throws GroupManagementException, DeviceNotFoundException {
// groupManagementProviderService.removeDevice(3, list); List<DeviceIdentifier> list = TestUtils.getDeviceIdentifiersList();
// } groupManagementProviderService.removeDevice(groupManagementProviderService.getGroup(
TestUtils.createDeviceGroup2().getName()).getGroupId(), list);
groupManagementProviderService.removeDevice(groupManagementProviderService.getGroup(
TestUtils.createDeviceGroup3().getName()).getGroupId(), list);
}
@Test(dependsOnMethods = ("createGroup")) @Test(dependsOnMethods = ("createGroup"))
public void getGroupsByUsernameAndPermissions() throws GroupManagementException { public void getGroupsByUsernameAndPermissions() throws GroupManagementException {
@ -243,18 +262,24 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest
Assert.assertNotNull(groups); Assert.assertNotNull(groups);
} }
// @Test(dependsOnMethods = ("addDevices")) @Test(dependsOnMethods = ("addDevices"))
// public void getGroupsByDeviceIdentifier() throws GroupManagementException { public void getGroupsByDeviceIdentifier() throws GroupManagementException {
// DeviceIdentifier identifier = new DeviceIdentifier(); DeviceIdentifier identifier = new DeviceIdentifier();
// identifier.setId("12345"); identifier.setId("12345");
// identifier.setType("Test"); identifier.setType("Test");
// List<DeviceGroup> groups = groupManagementProviderService.getGroups(identifier); List<DeviceGroup> groups = groupManagementProviderService.getGroups(identifier);
// Assert.assertNull(groups); Assert.assertNotNull(groups);
// } }
@Test @Test
public void createDefaultGroup() throws GroupManagementException { public void createDefaultGroup() throws GroupManagementException {
groupManagementProviderService.createDefaultGroup("BYOD"); groupManagementProviderService.createDefaultGroup("BYOD");
} }
@Test(dependsOnMethods = ("createDefaultGroup"))
public void createDefaultGroupTwice() throws GroupManagementException {
groupManagementProviderService.createDefaultGroup("BYOD");
}
} }

@ -42,6 +42,8 @@
<class name="org.wso2.carbon.device.mgt.core.app.mgt.ApplicationManagementProviderServiceTest"/> <class name="org.wso2.carbon.device.mgt.core.app.mgt.ApplicationManagementProviderServiceTest"/>
<class name="org.wso2.carbon.device.mgt.core.service.GroupManagementProviderServiceTest"/> <class name="org.wso2.carbon.device.mgt.core.service.GroupManagementProviderServiceTest"/>
<class name="org.wso2.carbon.device.mgt.core.operation.OperationManagementTests"/> <class name="org.wso2.carbon.device.mgt.core.operation.OperationManagementTests"/>
<class name="org.wso2.carbon.device.mgt.core.permission.mgt.PermissionManagerServiceTest" />
<class name="org.wso2.carbon.device.mgt.core.notification.mgt.NotificationManagementTests"/>
</classes> </classes>
</test> </test>
</suite> </suite>

@ -1532,6 +1532,12 @@
<version>${power.mock.version}</version> <version>${power.mock.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-testng</artifactId>
<version>${power.mock.version}</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>
@ -1961,7 +1967,7 @@
<jacoco.maven.plugin.version>0.7.8</jacoco.maven.plugin.version> <jacoco.maven.plugin.version>0.7.8</jacoco.maven.plugin.version>
<jacoco.ant.verision>0.7.5.201505241946</jacoco.ant.verision> <jacoco.ant.verision>0.7.5.201505241946</jacoco.ant.verision>
<ant.contrib.version>1.0b3</ant.contrib.version> <ant.contrib.version>1.0b3</ant.contrib.version>
<power.mock.version>1.6.4</power.mock.version> <power.mock.version>1.7.0</power.mock.version>
</properties> </properties>
</project> </project>

Loading…
Cancel
Save