forked from community/device-mgt-core
Merge branch 'master' of https://github.com/wso2/carbon-device-mgt
commit
cd3bb2a5aa
@ -1,22 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2015, 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.operation.mgt;
|
||||
|
||||
public interface OperationManagementServiceProvider {
|
||||
}
|
@ -1,154 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
*/
|
||||
package org.wso2.carbon.device.mgt.core.push.notification.mgt;
|
||||
|
||||
import org.wso2.carbon.device.mgt.common.*;
|
||||
import org.wso2.carbon.device.mgt.common.operation.mgt.Activity;
|
||||
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
|
||||
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException;
|
||||
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager;
|
||||
import org.wso2.carbon.device.mgt.common.push.notification.NotificationContext;
|
||||
import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy;
|
||||
import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationExecutionFailedException;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class PushNotificationBasedOperationManager implements OperationManager {
|
||||
|
||||
private OperationManager operationManager;
|
||||
private NotificationStrategy notificationProvider;
|
||||
|
||||
public PushNotificationBasedOperationManager(
|
||||
OperationManager operationManager, NotificationStrategy notificationProvider) {
|
||||
this.operationManager = operationManager;
|
||||
this.notificationProvider = notificationProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Activity addOperation(Operation operation,
|
||||
List<DeviceIdentifier> devices) throws OperationManagementException, InvalidDeviceException {
|
||||
Activity activity = this.operationManager.addOperation(operation, devices);
|
||||
for (DeviceIdentifier deviceId : devices) {
|
||||
try {
|
||||
this.notificationProvider.execute(new NotificationContext(deviceId, operation));
|
||||
} catch (PushNotificationExecutionFailedException e) {
|
||||
throw new OperationManagementException("Error occurred while sending push notification to device", e);
|
||||
}
|
||||
}
|
||||
return activity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<? extends Operation> getOperations(
|
||||
DeviceIdentifier deviceId) throws OperationManagementException {
|
||||
return this.operationManager.getOperations(deviceId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PaginationResult getOperations(DeviceIdentifier deviceId,
|
||||
PaginationRequest request) throws OperationManagementException {
|
||||
return this.operationManager.getOperations(deviceId, request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<? extends Operation> getPendingOperations(
|
||||
DeviceIdentifier deviceId) throws OperationManagementException {
|
||||
return this.operationManager.getPendingOperations(deviceId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Operation getNextPendingOperation(DeviceIdentifier deviceId) throws OperationManagementException {
|
||||
return this.operationManager.getNextPendingOperation(deviceId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateOperation(DeviceIdentifier deviceId,
|
||||
Operation operation) throws OperationManagementException {
|
||||
this.operationManager.updateOperation(deviceId, operation);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteOperation(int operationId) throws OperationManagementException {
|
||||
this.operationManager.deleteOperation(operationId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Operation getOperationByDeviceAndOperationId(
|
||||
DeviceIdentifier deviceId, int operationId) throws OperationManagementException {
|
||||
return this.operationManager.getOperationByDeviceAndOperationId(deviceId, operationId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<? extends Operation> getOperationsByDeviceAndStatus(
|
||||
DeviceIdentifier deviceId,
|
||||
Operation.Status status) throws OperationManagementException {
|
||||
try {
|
||||
return this.operationManager.getOperationsByDeviceAndStatus(deviceId, status);
|
||||
} catch (DeviceManagementException e) {
|
||||
throw new OperationManagementException("Error occurred while retrieving the list of operations by " +
|
||||
"device and status", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Operation getOperation(int operationId) throws OperationManagementException {
|
||||
return this.operationManager.getOperation(operationId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Activity getOperationByActivityId(String activity) throws OperationManagementException {
|
||||
return this.operationManager.getOperationByActivityId(activity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Activity getOperationByActivityIdAndDevice(String activity, DeviceIdentifier deviceId) throws OperationManagementException {
|
||||
return this.operationManager.getOperationByActivityIdAndDevice(activity, deviceId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Operation> getOperationUpdatedAfter(long timestamp) throws OperationManagementException {
|
||||
return this.operationManager.getOperationUpdatedAfter(timestamp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Activity> getActivitiesUpdatedAfter(long timestamp) throws OperationManagementException {
|
||||
return this.operationManager.getActivitiesUpdatedAfter(timestamp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Activity> getActivitiesUpdatedAfter(long timestamp, int limit, int offset) throws OperationManagementException {
|
||||
return this.operationManager.getActivitiesUpdatedAfter(timestamp, limit, offset);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getActivityCountUpdatedAfter(long timestamp) throws OperationManagementException {
|
||||
return this.operationManager.getActivityCountUpdatedAfter(timestamp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setNotificationStrategy(NotificationStrategy notificationStrategy) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public NotificationStrategy getNotificationStrategy() {
|
||||
return notificationProvider;
|
||||
}
|
||||
|
||||
}
|
@ -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,51 @@
|
||||
/*
|
||||
* 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.operation;
|
||||
|
||||
import org.wso2.carbon.device.mgt.common.push.notification.NotificationContext;
|
||||
import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy;
|
||||
import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig;
|
||||
import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationExecutionFailedException;
|
||||
|
||||
public class TestNotificationStrategy implements NotificationStrategy {
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(NotificationContext ctx) throws PushNotificationExecutionFailedException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public NotificationContext buildContext() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void undeploy() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public PushNotificationConfig getConfig() {
|
||||
return null;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -0,0 +1,285 @@
|
||||
/*
|
||||
* 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.service;
|
||||
|
||||
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
import org.testng.annotations.Test;
|
||||
import org.wso2.carbon.device.mgt.common.*;
|
||||
import org.wso2.carbon.device.mgt.common.group.mgt.*;
|
||||
import org.wso2.carbon.device.mgt.core.TestUtils;
|
||||
import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest;
|
||||
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
|
||||
import org.wso2.carbon.device.mgt.core.config.cache.DeviceCacheConfiguration;
|
||||
import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOFactory;
|
||||
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
|
||||
import org.wso2.carbon.registry.core.jdbc.realm.InMemoryRealmService;
|
||||
import org.wso2.carbon.user.api.Permission;
|
||||
import org.wso2.carbon.user.api.UserStoreException;
|
||||
import org.wso2.carbon.user.api.UserStoreManager;
|
||||
import org.wso2.carbon.user.core.service.RealmService;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest {
|
||||
|
||||
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"};
|
||||
|
||||
@BeforeClass
|
||||
@Override
|
||||
public void init() throws Exception {
|
||||
groupManagementProviderService = new GroupManagementProviderServiceImpl();
|
||||
RealmService realmService = new InMemoryRealmService();
|
||||
DeviceManagementDataHolder.getInstance().setRealmService(realmService);
|
||||
realmService.getTenantManager().getSuperTenantDomain();
|
||||
DeviceConfigurationManager.getInstance().initConfig();
|
||||
}
|
||||
|
||||
@Test(expectedExceptions = {GroupManagementException.class, GroupAlreadyExistException.class})
|
||||
public void createGroupNull() throws GroupManagementException, GroupAlreadyExistException {
|
||||
groupManagementProviderService.createGroup(null, null, null);
|
||||
}
|
||||
|
||||
|
||||
@Test(expectedExceptions = {GroupManagementException.class, GroupAlreadyExistException.class, TransactionManagementException.class})
|
||||
public void createGroupError() throws GroupManagementException, GroupAlreadyExistException, TransactionManagementException {
|
||||
GroupManagementDAOFactory.beginTransaction();
|
||||
groupManagementProviderService.createGroup(TestUtils.createDeviceGroup4(), DEFAULT_ADMIN_ROLE, DEFAULT_ADMIN_PERMISSIONS);
|
||||
}
|
||||
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = ("createGroup"))
|
||||
public void deleteGroup() throws GroupManagementException {
|
||||
DeviceGroup deviceGroup = groupManagementProviderService.getGroup(TestUtils.createDeviceGroup4().getName());
|
||||
Assert.assertTrue(groupManagementProviderService.deleteGroup(deviceGroup.getGroupId()));
|
||||
}
|
||||
|
||||
|
||||
@Test(dependsOnMethods = ("createGroup"))
|
||||
public void deleteGroupNotExists() throws GroupManagementException {
|
||||
groupManagementProviderService.deleteGroup(8);
|
||||
}
|
||||
|
||||
|
||||
@Test(dependsOnMethods = ("createGroup"))
|
||||
public void getGroup() throws GroupManagementException {
|
||||
|
||||
DeviceGroup deviceGroup = groupManagementProviderService.getGroup(TestUtils.createDeviceGroup3().getName());
|
||||
Assert.assertNotNull(groupManagementProviderService.getGroup(deviceGroup.getGroupId()));
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = ("createGroup"))
|
||||
public void getGroupByName() throws GroupManagementException {
|
||||
Assert.assertNotNull(groupManagementProviderService.getGroup(TestUtils.createDeviceGroup3().getName()));
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = ("createGroup"))
|
||||
public void getGroups() throws GroupManagementException {
|
||||
List<DeviceGroup> deviceGroups = groupManagementProviderService.getGroups();
|
||||
Assert.assertNotNull(deviceGroups);
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = ("createGroup"))
|
||||
public void getGroupsByUsername() throws GroupManagementException {
|
||||
List<DeviceGroup> deviceGroups = groupManagementProviderService.getGroups("admin");
|
||||
Assert.assertNotNull(deviceGroups);
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = ("createGroup"), expectedExceptions = {GroupManagementException.class})
|
||||
public void getGroupsByUsernameError() throws GroupManagementException {
|
||||
groupManagementProviderService.getGroups((String) null);
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = ("createGroup"))
|
||||
public void getGroupsByPagination() throws GroupManagementException {
|
||||
PaginationResult result = groupManagementProviderService.getGroups(TestUtils.createPaginationRequest());
|
||||
Assert.assertNotNull(result);
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = ("createGroup"), expectedExceptions = {GroupManagementException.class})
|
||||
public void getGroupsByPaginationError() throws GroupManagementException {
|
||||
GroupPaginationRequest request = null;
|
||||
groupManagementProviderService.getGroups(request);
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = ("createGroup"))
|
||||
public void getGroupsByUsernameAndPagination()
|
||||
throws GroupManagementException {
|
||||
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();
|
||||
Assert.assertNotNull(x);
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = ("createGroup"))
|
||||
public void getGroupCountByUsername(String username) throws GroupManagementException {
|
||||
int x = groupManagementProviderService.getGroupCount(username);
|
||||
Assert.assertNotNull(x);
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = ("updateGroupSecondTime"))
|
||||
public void manageGroupSharing() throws GroupManagementException, RoleDoesNotExistException, UserStoreException {
|
||||
groupManagementProviderService.manageGroupSharing(0, null);
|
||||
List<String> 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 {
|
||||
List<String> roles = groupManagementProviderService.getRoles(1);
|
||||
Assert.assertNotNull(roles);
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = ("createGroup"))
|
||||
public void getDevices() throws GroupManagementException {
|
||||
List<Device> devices = groupManagementProviderService.getDevices(1, 1, 50);
|
||||
Assert.assertNotNull(devices);
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = ("createGroup"))
|
||||
public void getDeviceCount() throws GroupManagementException {
|
||||
int x = groupManagementProviderService.getDeviceCount(1);
|
||||
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<DeviceIdentifier> 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<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"))
|
||||
public void getGroupsByUsernameAndPermissions() throws GroupManagementException {
|
||||
List<DeviceGroup> groups = groupManagementProviderService.getGroups("admin", "/permission/device-mgt/admin/groups");
|
||||
Assert.assertNotNull(groups);
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = ("addDevices"))
|
||||
public void getGroupsByDeviceIdentifier() throws GroupManagementException {
|
||||
DeviceIdentifier identifier = new DeviceIdentifier();
|
||||
identifier.setId("12345");
|
||||
identifier.setType("Test");
|
||||
List<DeviceGroup> 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");
|
||||
}
|
||||
|
||||
}
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue