forked from community/device-mgt-core
commit
14ebe3e9e7
@ -0,0 +1,315 @@
|
||||
/*
|
||||
* 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.jaxrs.service.impl;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.mockito.Mockito;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PowerMockIgnore;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
|
||||
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.context.CarbonContext;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||
import org.wso2.carbon.device.mgt.common.PaginationRequest;
|
||||
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException;
|
||||
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService;
|
||||
import org.wso2.carbon.device.mgt.core.authorization.DeviceAccessAuthorizationServiceImpl;
|
||||
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
|
||||
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl;
|
||||
import org.wso2.carbon.device.mgt.jaxrs.service.api.DeviceManagementService;
|
||||
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils;
|
||||
import org.wso2.carbon.utils.multitenancy.MultitenantUtils;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
import static org.mockito.MockitoAnnotations.initMocks;
|
||||
|
||||
/**
|
||||
* This class includes unit tests for testing the functionality of {@link DeviceManagementServiceImpl}
|
||||
*/
|
||||
@PowerMockIgnore("javax.ws.rs.*")
|
||||
@SuppressStaticInitializationFor({"org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils",
|
||||
"org.wso2.carbon.context.CarbonContext"})
|
||||
@PrepareForTest({DeviceMgtAPIUtils.class, MultitenantUtils.class, CarbonContext.class})
|
||||
public class DeviceManagementServiceImplTest {
|
||||
|
||||
private static final Log log = LogFactory.getLog(DeviceManagementServiceImplTest.class);
|
||||
private static final String TEST_DEVICE_TYPE = "TEST-DEVICE-TYPE";
|
||||
private static final String TEST_DEVICE_NAME = "TEST-DEVICE";
|
||||
private static final String DEFAULT_USERNAME = "admin";
|
||||
private static final String TENANT_AWARE_USERNAME = "admin@carbon.super";
|
||||
private static final String DEFAULT_ROLE = "admin";
|
||||
private static final String DEFAULT_OWNERSHIP = "BYOD";
|
||||
private static final String DEFAULT_STATUS = "ACTIVE";
|
||||
private static final String DEFAULT_DATE_FORMAT = "EEE, d MMM yyyy HH:mm:ss Z";
|
||||
private DeviceManagementService deviceManagementService;
|
||||
private DeviceAccessAuthorizationService deviceAccessAuthorizationService;
|
||||
private DeviceManagementProviderService deviceManagementProviderService;
|
||||
|
||||
@ObjectFactory
|
||||
public IObjectFactory getObjectFactory() {
|
||||
return new org.powermock.modules.testng.PowerMockObjectFactory();
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public void init() throws Exception {
|
||||
log.info("Initializing DeviceManagementServiceImpl tests");
|
||||
initMocks(this);
|
||||
this.deviceManagementProviderService = Mockito
|
||||
.mock(DeviceManagementProviderServiceImpl.class, Mockito.RETURNS_MOCKS);
|
||||
this.deviceManagementService = new DeviceManagementServiceImpl();
|
||||
this.deviceAccessAuthorizationService = Mockito.mock(DeviceAccessAuthorizationServiceImpl.class);
|
||||
}
|
||||
|
||||
@Test(description = "Testing if the device is enrolled when the device is enrolled.")
|
||||
public void testIsEnrolledWhenDeviceIsEnrolled() throws Exception {
|
||||
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
|
||||
.toReturn(this.deviceManagementProviderService);
|
||||
Mockito.when(this.deviceManagementProviderService.isEnrolled(Mockito.any(DeviceIdentifier.class)))
|
||||
.thenReturn(true);
|
||||
Response response = this.deviceManagementService.isEnrolled(TEST_DEVICE_TYPE, UUID.randomUUID().toString());
|
||||
Assert.assertNotNull(response);
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
|
||||
Mockito.reset(this.deviceManagementProviderService);
|
||||
}
|
||||
|
||||
@Test(description = "Testing if the device is enrolled when the device is not enrolled.",
|
||||
dependsOnMethods = "testIsEnrolledWhenDeviceIsEnrolled")
|
||||
public void testIsEnrolledWhenDeviceIsNotEnrolled() throws Exception {
|
||||
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
|
||||
.toReturn(this.deviceManagementProviderService);
|
||||
Mockito.when(this.deviceManagementProviderService.isEnrolled(Mockito.any(DeviceIdentifier.class)))
|
||||
.thenReturn(false);
|
||||
Response response = this.deviceManagementService.isEnrolled(TEST_DEVICE_TYPE, UUID.randomUUID().toString());
|
||||
Assert.assertNotNull(response);
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.NO_CONTENT.getStatusCode());
|
||||
Mockito.reset(this.deviceManagementProviderService);
|
||||
}
|
||||
|
||||
@Test(description = "Testing if the device enrolled api when exception occurred.",
|
||||
dependsOnMethods = "testIsEnrolledWhenDeviceIsNotEnrolled")
|
||||
public void testIsEnrolledError() throws Exception {
|
||||
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
|
||||
.toReturn(this.deviceManagementProviderService);
|
||||
Mockito.when(this.deviceManagementProviderService.isEnrolled(Mockito.any(DeviceIdentifier.class)))
|
||||
.thenThrow(new DeviceManagementException());
|
||||
Response response = this.deviceManagementService.isEnrolled(TEST_DEVICE_TYPE, UUID.randomUUID().toString());
|
||||
Assert.assertNotNull(response);
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
||||
Mockito.reset(this.deviceManagementProviderService);
|
||||
}
|
||||
|
||||
@Test(description = "Testing get devices when request exists both name and role.")
|
||||
public void testGetDevicesWhenBothNameAndRoleAvailable() throws Exception {
|
||||
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
|
||||
.toReturn(this.deviceManagementProviderService);
|
||||
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceAccessAuthorizationService"))
|
||||
.toReturn(this.deviceAccessAuthorizationService);
|
||||
Response response = this.deviceManagementService
|
||||
.getDevices(TEST_DEVICE_NAME, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP,
|
||||
DEFAULT_STATUS, 1, null, null, false, 10, 5);
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode());
|
||||
}
|
||||
|
||||
@Test(description = "Testing get devices with correct request.")
|
||||
public void testGetDevices() throws Exception {
|
||||
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
|
||||
.toReturn(this.deviceManagementProviderService);
|
||||
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceAccessAuthorizationService"))
|
||||
.toReturn(this.deviceAccessAuthorizationService);
|
||||
PowerMockito.stub(PowerMockito.method(MultitenantUtils.class, "getTenantAwareUsername"))
|
||||
.toReturn(TENANT_AWARE_USERNAME);
|
||||
PowerMockito.stub(PowerMockito.method(CarbonContext.class, "getThreadLocalCarbonContext"))
|
||||
.toReturn(Mockito.mock(CarbonContext.class, Mockito.RETURNS_MOCKS));
|
||||
|
||||
Response response = this.deviceManagementService
|
||||
.getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP,
|
||||
DEFAULT_STATUS, 1, null, null, false, 10, 5);
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
|
||||
response = this.deviceManagementService
|
||||
.getDevices(TEST_DEVICE_NAME, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, null, DEFAULT_OWNERSHIP,
|
||||
DEFAULT_STATUS, 1, null, null, false, 10, 5);
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
|
||||
response = this.deviceManagementService
|
||||
.getDevices(TEST_DEVICE_NAME, TEST_DEVICE_TYPE, null, null, null, DEFAULT_OWNERSHIP,
|
||||
DEFAULT_STATUS, 1, null, null, false, 10, 5);
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
|
||||
response = this.deviceManagementService
|
||||
.getDevices(TEST_DEVICE_NAME, TEST_DEVICE_TYPE, null, null, null, DEFAULT_OWNERSHIP,
|
||||
DEFAULT_STATUS, 1, null, null, true, 10, 5);
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
|
||||
}
|
||||
|
||||
@Test(description = "Testing get devices when DeviceAccessAuthorizationService is not available")
|
||||
public void testGetDevicesWithErroneousDeviceAccessAuthorizationService() throws Exception {
|
||||
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
|
||||
.toReturn(this.deviceManagementProviderService);
|
||||
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceAccessAuthorizationService"))
|
||||
.toReturn(null);
|
||||
Response response = this.deviceManagementService
|
||||
.getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP,
|
||||
DEFAULT_STATUS, 1, null, null, false, 10, 5);
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
||||
}
|
||||
|
||||
@Test(description = "Testing get devices when user is the device admin")
|
||||
public void testGetDevicesWhenUserIsAdmin() throws Exception {
|
||||
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
|
||||
.toReturn(this.deviceManagementProviderService);
|
||||
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceAccessAuthorizationService"))
|
||||
.toReturn(this.deviceAccessAuthorizationService);
|
||||
PowerMockito.stub(PowerMockito.method(MultitenantUtils.class, "getTenantAwareUsername"))
|
||||
.toReturn(TENANT_AWARE_USERNAME);
|
||||
PowerMockito.stub(PowerMockito.method(CarbonContext.class, "getThreadLocalCarbonContext"))
|
||||
.toReturn(Mockito.mock(CarbonContext.class, Mockito.RETURNS_MOCKS));
|
||||
Mockito.when(deviceAccessAuthorizationService.isDeviceAdminUser()).thenReturn(true);
|
||||
|
||||
Response response = this.deviceManagementService
|
||||
.getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP,
|
||||
DEFAULT_STATUS, 1, null, null, false, 10, 5);
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
|
||||
response = this.deviceManagementService
|
||||
.getDevices(null, TEST_DEVICE_TYPE, null, DEFAULT_USERNAME, DEFAULT_ROLE, DEFAULT_OWNERSHIP,
|
||||
DEFAULT_STATUS, 1, null, null, false, 10, 5);
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
|
||||
}
|
||||
|
||||
@Test(description = "Testing get devices when user is unauthorized.")
|
||||
public void testGetDevicesWhenUserIsUnauthorized() throws Exception {
|
||||
PowerMockito.spy(MultitenantUtils.class);
|
||||
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
|
||||
.toReturn(this.deviceManagementProviderService);
|
||||
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceAccessAuthorizationService"))
|
||||
.toReturn(this.deviceAccessAuthorizationService);
|
||||
PowerMockito.stub(PowerMockito.method(CarbonContext.class, "getThreadLocalCarbonContext"))
|
||||
.toReturn(Mockito.mock(CarbonContext.class, Mockito.RETURNS_MOCKS));
|
||||
PowerMockito.doReturn(TENANT_AWARE_USERNAME)
|
||||
.when(MultitenantUtils.class, "getTenantAwareUsername", DEFAULT_USERNAME);
|
||||
PowerMockito.doReturn("newuser@carbon.super").when(MultitenantUtils.class, "getTenantAwareUsername", "newuser");
|
||||
Mockito.when(this.deviceAccessAuthorizationService.isDeviceAdminUser()).thenReturn(false);
|
||||
|
||||
Response response = this.deviceManagementService
|
||||
.getDevices(null, TEST_DEVICE_TYPE, "newuser", null, DEFAULT_ROLE, DEFAULT_OWNERSHIP, DEFAULT_STATUS, 1,
|
||||
null, null, false, 10, 5);
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode());
|
||||
Mockito.reset(this.deviceAccessAuthorizationService);
|
||||
}
|
||||
|
||||
@Test(description = "Testing get devices with IF-Modified-Since")
|
||||
public void testGetDevicesWithModifiedSince() throws Exception {
|
||||
String ifModifiedSince = new SimpleDateFormat(DEFAULT_DATE_FORMAT).format(new Date());
|
||||
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
|
||||
.toReturn(this.deviceManagementProviderService);
|
||||
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceAccessAuthorizationService"))
|
||||
.toReturn(this.deviceAccessAuthorizationService);
|
||||
PowerMockito.stub(PowerMockito.method(MultitenantUtils.class, "getTenantAwareUsername"))
|
||||
.toReturn(TENANT_AWARE_USERNAME);
|
||||
PowerMockito.stub(PowerMockito.method(CarbonContext.class, "getThreadLocalCarbonContext"))
|
||||
.toReturn(Mockito.mock(CarbonContext.class, Mockito.RETURNS_MOCKS));
|
||||
|
||||
Response response = this.deviceManagementService
|
||||
.getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP,
|
||||
DEFAULT_STATUS, 1, null, ifModifiedSince, false, 10, 5);
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.NOT_MODIFIED.getStatusCode());
|
||||
response = this.deviceManagementService
|
||||
.getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP,
|
||||
DEFAULT_STATUS, 1, null, ifModifiedSince, true, 10, 5);
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.NOT_MODIFIED.getStatusCode());
|
||||
response = this.deviceManagementService
|
||||
.getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP,
|
||||
DEFAULT_STATUS, 1, null, "ErrorModifiedSince", false, 10, 5);
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode());
|
||||
}
|
||||
|
||||
@Test(description = "Testing get devices with Since")
|
||||
public void testGetDevicesWithSince() throws Exception {
|
||||
String since = new SimpleDateFormat(DEFAULT_DATE_FORMAT).format(new Date());
|
||||
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
|
||||
.toReturn(this.deviceManagementProviderService);
|
||||
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceAccessAuthorizationService"))
|
||||
.toReturn(this.deviceAccessAuthorizationService);
|
||||
PowerMockito.stub(PowerMockito.method(MultitenantUtils.class, "getTenantAwareUsername"))
|
||||
.toReturn(TENANT_AWARE_USERNAME);
|
||||
PowerMockito.stub(PowerMockito.method(CarbonContext.class, "getThreadLocalCarbonContext"))
|
||||
.toReturn(Mockito.mock(CarbonContext.class, Mockito.RETURNS_MOCKS));
|
||||
|
||||
Response response = this.deviceManagementService
|
||||
.getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP,
|
||||
DEFAULT_STATUS, 1, since, null, false, 10, 5);
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
|
||||
response = this.deviceManagementService
|
||||
.getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP,
|
||||
DEFAULT_STATUS, 1, since, null, true, 10, 5);
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
|
||||
response = this.deviceManagementService
|
||||
.getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP,
|
||||
DEFAULT_STATUS, 1, "ErrorSince", null, false, 10, 5);
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode());
|
||||
}
|
||||
|
||||
@Test(description = "Testing get devices when unable to retrieve devices")
|
||||
public void testGetDeviceServerErrorWhenGettingDeviceList() throws Exception {
|
||||
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
|
||||
.toReturn(this.deviceManagementProviderService);
|
||||
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceAccessAuthorizationService"))
|
||||
.toReturn(this.deviceAccessAuthorizationService);
|
||||
PowerMockito.stub(PowerMockito.method(MultitenantUtils.class, "getTenantAwareUsername"))
|
||||
.toReturn(TENANT_AWARE_USERNAME);
|
||||
PowerMockito.stub(PowerMockito.method(CarbonContext.class, "getThreadLocalCarbonContext"))
|
||||
.toReturn(Mockito.mock(CarbonContext.class, Mockito.RETURNS_MOCKS));
|
||||
Mockito.when(this.deviceManagementProviderService.getAllDevices(Mockito.any(PaginationRequest.class), Mockito.anyBoolean()))
|
||||
.thenThrow(new DeviceManagementException());
|
||||
|
||||
Response response = this.deviceManagementService
|
||||
.getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP,
|
||||
DEFAULT_STATUS, 1, null, null, false, 10, 5);
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
||||
Mockito.reset(this.deviceManagementProviderService);
|
||||
}
|
||||
|
||||
@Test(description = "Testing get devices when unable to check if the user is the admin user")
|
||||
public void testGetDevicesServerErrorWhenCheckingAdminUser() throws Exception {
|
||||
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
|
||||
.toReturn(this.deviceManagementProviderService);
|
||||
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceAccessAuthorizationService"))
|
||||
.toReturn(this.deviceAccessAuthorizationService);
|
||||
PowerMockito.stub(PowerMockito.method(MultitenantUtils.class, "getTenantAwareUsername"))
|
||||
.toReturn(TENANT_AWARE_USERNAME);
|
||||
PowerMockito.stub(PowerMockito.method(CarbonContext.class, "getThreadLocalCarbonContext"))
|
||||
.toReturn(Mockito.mock(CarbonContext.class, Mockito.RETURNS_MOCKS));
|
||||
Mockito.when(this.deviceAccessAuthorizationService.isDeviceAdminUser())
|
||||
.thenThrow(new DeviceAccessAuthorizationException());
|
||||
|
||||
Response response = this.deviceManagementService
|
||||
.getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP,
|
||||
DEFAULT_STATUS, 1, null, null, false, 10, 5);
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
||||
Mockito.reset(this.deviceAccessAuthorizationService);
|
||||
}
|
||||
}
|
@ -0,0 +1,236 @@
|
||||
/*
|
||||
* 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.jaxrs.service.impl;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.mockito.Mockito;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PowerMockIgnore;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
|
||||
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.DeviceManagementException;
|
||||
import org.wso2.carbon.device.mgt.common.FeatureManager;
|
||||
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
|
||||
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
|
||||
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl;
|
||||
import org.wso2.carbon.device.mgt.jaxrs.service.api.DeviceTypeManagementService;
|
||||
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.DeviceMgtAPITestHelper;
|
||||
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils;
|
||||
|
||||
import javax.ws.rs.core.Response;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
|
||||
import static org.mockito.MockitoAnnotations.initMocks;
|
||||
|
||||
/**
|
||||
* This class holds the unit tests for the class {@link DeviceTypeManagementService}
|
||||
*/
|
||||
@PowerMockIgnore("javax.ws.rs.*")
|
||||
@SuppressStaticInitializationFor({"org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils"})
|
||||
@PrepareForTest({DeviceMgtAPIUtils.class, DeviceManagementProviderService.class})
|
||||
public class DeviceTypeManagementServiceTest {
|
||||
|
||||
private static final Log log = LogFactory.getLog(DeviceManagementServiceImplTest.class);
|
||||
private static final String TEST_DEVICE_TYPE = "TEST-DEVICE-TYPE";
|
||||
private static final int TEST_DEVICE_TYPE_ID = 12345;
|
||||
private static final String MODIFIED_SINCE = "1234503934242";
|
||||
private DeviceTypeManagementService deviceTypeManagementService;
|
||||
private DeviceManagementProviderService deviceManagementProviderService;
|
||||
|
||||
@ObjectFactory
|
||||
public IObjectFactory getObjectFactory() {
|
||||
return new org.powermock.modules.testng.PowerMockObjectFactory();
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public void init() throws DeviceManagementException {
|
||||
log.info("Initializing DeviceTypeManagement tests");
|
||||
initMocks(this);
|
||||
this.deviceManagementProviderService = Mockito
|
||||
.mock(DeviceManagementProviderServiceImpl.class, Mockito.RETURNS_MOCKS);
|
||||
this.deviceTypeManagementService = new DeviceTypeManagementServiceImpl();
|
||||
}
|
||||
|
||||
@Test(description = "Testing for existing device types.")
|
||||
public void testExistingDeviceType() throws Exception {
|
||||
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
|
||||
.toReturn(this.deviceManagementProviderService);
|
||||
Response response = this.deviceTypeManagementService.getDeviceTypes("");
|
||||
Assert.assertNotNull(response, "The response object is null.");
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(),
|
||||
"The response states should be 200.");
|
||||
}
|
||||
|
||||
@Test(description = "Testing get existing device types error")
|
||||
public void testExistingDeviceTypesError() throws Exception {
|
||||
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
|
||||
.toReturn(this.deviceManagementProviderService);
|
||||
Mockito.when(this.deviceManagementProviderService.getDeviceTypes()).thenThrow(new DeviceManagementException());
|
||||
|
||||
Response response = this.deviceTypeManagementService.getDeviceTypes();
|
||||
Assert.assertNotNull(response, "The response object is null.");
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(),
|
||||
"The response status should be 500.");
|
||||
Mockito.reset(deviceManagementProviderService);
|
||||
}
|
||||
|
||||
@Test(description = "Testing get existing device types error")
|
||||
public void testExistingDeviceTypesModifiedError() throws Exception {
|
||||
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
|
||||
.toReturn(this.deviceManagementProviderService);
|
||||
Mockito.when(this.deviceManagementProviderService.getAvailableDeviceTypes()).thenThrow(new
|
||||
DeviceManagementException());
|
||||
|
||||
Response response = this.deviceTypeManagementService.getDeviceTypes(MODIFIED_SINCE);
|
||||
Assert.assertNotNull(response, "The response object is null.");
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(),
|
||||
"The response status should be 500.");
|
||||
Mockito.reset(deviceManagementProviderService);
|
||||
}
|
||||
|
||||
@Test(description = "Test case to retrieve the Features of specified device type.")
|
||||
public void testGetDeviceTypeFeatures() throws Exception {
|
||||
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
|
||||
.toReturn(this.deviceManagementProviderService);
|
||||
Response response = this.deviceTypeManagementService.getFeatures(TEST_DEVICE_TYPE, MODIFIED_SINCE);
|
||||
Assert.assertNotNull(response, "The response object is null.");
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(),
|
||||
"The response status should be 200.");
|
||||
}
|
||||
|
||||
@Test(description = "Test case to test the error scenario when retrieving the Features of specified device type.")
|
||||
public void testGetDeviceTypeFeaturesError() throws Exception {
|
||||
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
|
||||
.toReturn(this.deviceManagementProviderService);
|
||||
FeatureManager featureManager = Mockito.mock(FeatureManager.class);
|
||||
Mockito.when(this.deviceManagementProviderService.getFeatureManager(Mockito.anyString())).thenReturn
|
||||
(featureManager);
|
||||
Mockito.when((featureManager).getFeatures()).thenThrow(new DeviceManagementException());
|
||||
Response response = this.deviceTypeManagementService.getFeatures(TEST_DEVICE_TYPE, MODIFIED_SINCE);
|
||||
Assert.assertNotNull(response, "The response object is null.");
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(),
|
||||
"The response status should be 500.");
|
||||
Mockito.reset(deviceManagementProviderService);
|
||||
Mockito.reset(featureManager);
|
||||
}
|
||||
|
||||
@Test(description = "Test getting device type features when feature manager is null.")
|
||||
public void testGetDeviceTypeFeaturesWithNoFeatureManager() throws Exception {
|
||||
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
|
||||
.toReturn(this.deviceManagementProviderService);
|
||||
Mockito.when(this.deviceManagementProviderService.getFeatureManager(Mockito.anyString())).thenReturn(null);
|
||||
Response response = this.deviceTypeManagementService.getFeatures(TEST_DEVICE_TYPE, MODIFIED_SINCE);
|
||||
Assert.assertNotNull(response, "The response object is null.");
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode(),
|
||||
"The response status should be 404.");
|
||||
Mockito.reset(deviceManagementProviderService);
|
||||
}
|
||||
|
||||
@Test(description = "Test to get all the device types.")
|
||||
public void testGetDeviceTypes() throws Exception {
|
||||
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
|
||||
.toReturn(this.deviceManagementProviderService);
|
||||
Response response = this.deviceTypeManagementService.getDeviceTypes();
|
||||
Assert.assertNotNull(response, "The response object is null.");
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(),
|
||||
"The response status should be 200.");
|
||||
}
|
||||
|
||||
@Test(description = "Test to get all the device types.")
|
||||
public void testGetDeviceTypesWithDeviceTypes() throws Exception {
|
||||
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
|
||||
.toReturn(this.deviceManagementProviderService);
|
||||
|
||||
List<DeviceType> deviceTypes = DeviceMgtAPITestHelper.getDummyDeviceTypeList(5);
|
||||
Mockito.when(this.deviceManagementProviderService.getDeviceTypes()).thenReturn(deviceTypes);
|
||||
|
||||
Response response = this.deviceTypeManagementService.getDeviceTypes();
|
||||
Assert.assertNotNull(response, "The response object is null.");
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(),
|
||||
"The response state should be 200");
|
||||
Mockito.reset(deviceManagementProviderService);
|
||||
}
|
||||
|
||||
@Test(description = "Test to get all the device types for the given name")
|
||||
public void testGetDeviceTypeByName() throws Exception {
|
||||
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
|
||||
.toReturn(this.deviceManagementProviderService);
|
||||
Response response = this.deviceTypeManagementService.getDeviceTypeByName(TEST_DEVICE_TYPE);
|
||||
Assert.assertNotNull(response, "The response object is null.");
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(),
|
||||
"The response status should be 200.");
|
||||
}
|
||||
|
||||
@Test(description = "Test the scenario when there are no device types for the given name.")
|
||||
public void testGetDeviceTypeByNameError() throws Exception {
|
||||
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
|
||||
.toReturn(this.deviceManagementProviderService);
|
||||
Mockito.when(this.deviceManagementProviderService.getDeviceType(Mockito.anyString())).thenReturn(null);
|
||||
|
||||
Response response = this.deviceTypeManagementService.getDeviceTypeByName(TEST_DEVICE_TYPE);
|
||||
Assert.assertNotNull(response, "The response object is null.");
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.NO_CONTENT.getStatusCode(),
|
||||
"The response status should be 204.");
|
||||
Mockito.reset(deviceManagementProviderService);
|
||||
}
|
||||
|
||||
@Test(description = "Test the scenario when there are no device types for the given name.")
|
||||
public void testGetDeviceTypeByNameException() throws Exception {
|
||||
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
|
||||
.toReturn(this.deviceManagementProviderService);
|
||||
Mockito.when(this.deviceManagementProviderService.getDeviceType(Mockito.anyString()))
|
||||
.thenThrow(new DeviceManagementException());
|
||||
|
||||
Response response = this.deviceTypeManagementService.getDeviceTypeByName(TEST_DEVICE_TYPE);
|
||||
Assert.assertNotNull(response, "The response object is null.");
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(),
|
||||
"The response status should be 500");
|
||||
Mockito.reset(deviceManagementProviderService);
|
||||
}
|
||||
|
||||
@Test(description = "Test to get all the device types when given name is null")
|
||||
public void testGetDeviceTypeByNameBadRequest() throws Exception {
|
||||
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
|
||||
.toReturn(this.deviceManagementProviderService);
|
||||
Response response = this.deviceTypeManagementService.getDeviceTypeByName(null);
|
||||
Assert.assertNotNull(response, "The response object is null.");
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode(),
|
||||
"The response status should be 400");
|
||||
}
|
||||
|
||||
@Test(description = "Test to clear the sensitive metadata information of device type")
|
||||
public void testClearMetaEntryInfo() throws NoSuchMethodException, InvocationTargetException,
|
||||
IllegalAccessException {
|
||||
Method clearMetaEntryInfo = DeviceTypeManagementServiceImpl.class.getDeclaredMethod("clearMetaEntryInfo",
|
||||
DeviceType.class);
|
||||
clearMetaEntryInfo.setAccessible(true);
|
||||
|
||||
DeviceType deviceType = DeviceMgtAPITestHelper.getDummyDeviceType(TEST_DEVICE_TYPE, TEST_DEVICE_TYPE_ID);
|
||||
DeviceType returned = (DeviceType) clearMetaEntryInfo.invoke(this.deviceTypeManagementService, deviceType);
|
||||
|
||||
Assert.assertNotNull(returned.getDeviceTypeMetaDefinition(), "The response object is null.");
|
||||
}
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
/*
|
||||
* 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.jaxrs.service.impl.util;
|
||||
|
||||
import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig;
|
||||
import org.wso2.carbon.device.mgt.common.type.mgt.DeviceTypeMetaDefinition;
|
||||
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Helper class for Device Management API test cases.
|
||||
* */
|
||||
public class DeviceMgtAPITestHelper {
|
||||
|
||||
private static final String DEVICE_TYPE_DESCRIPTION = "Dummy Description";
|
||||
private static final String DEVICE_TYPE = "TEST_DEVICE_TYPE";
|
||||
|
||||
/**
|
||||
* Creates a Device Type with given name and given id.
|
||||
* If the name is null, the TEST_DEVICE_TYPE will be used as the name.
|
||||
*
|
||||
* @param name : Name of the device type.
|
||||
* @param deviceTypeId : The Id of the device type.
|
||||
* @return DeviceType
|
||||
*/
|
||||
public static DeviceType getDummyDeviceType(String name, int deviceTypeId) {
|
||||
DeviceType deviceType = new DeviceType();
|
||||
deviceType.setId(deviceTypeId);
|
||||
deviceType.setName(name != null ? name : DEVICE_TYPE);
|
||||
|
||||
DeviceTypeMetaDefinition deviceTypeMetaDefinition = new DeviceTypeMetaDefinition();
|
||||
deviceTypeMetaDefinition.setClaimable(true);
|
||||
deviceTypeMetaDefinition.setDescription(DEVICE_TYPE_DESCRIPTION);
|
||||
|
||||
PushNotificationConfig pushNotificationConfig =
|
||||
new PushNotificationConfig(name, true, null);
|
||||
deviceTypeMetaDefinition.setPushNotificationConfig(pushNotificationConfig);
|
||||
|
||||
deviceType.setDeviceTypeMetaDefinition(deviceTypeMetaDefinition);
|
||||
return deviceType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a list of device types.
|
||||
*
|
||||
* @param count: The number of device types that is needed.
|
||||
* @return List<DeviceType> : A list of device types.
|
||||
*/
|
||||
public static List<DeviceType> getDummyDeviceTypeList(int count) {
|
||||
List<DeviceType> deviceTypes = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
DeviceType deviceType = getDummyDeviceType(DEVICE_TYPE + count, count);
|
||||
deviceTypes.add(deviceType);
|
||||
}
|
||||
|
||||
return deviceTypes;
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
|
||||
#
|
||||
# This is the log4j configuration file used by WSO2 Carbon
|
||||
#
|
||||
# IMPORTANT : Please do not remove or change the names of any
|
||||
# of the Appender defined here. The layout pattern & log file
|
||||
# can be changed using the WSO2 Carbon Management Console, and those
|
||||
# settings will override the settings in this file.
|
||||
#
|
||||
|
||||
log4j.rootLogger=DEBUG, STD_OUT
|
||||
|
||||
# Redirect log messages to console
|
||||
log4j.appender.STD_OUT=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.STD_OUT.Target=System.out
|
||||
log4j.appender.STD_OUT.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.STD_OUT.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
|
@ -0,0 +1,31 @@
|
||||
|
||||
<!--
|
||||
~ 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.
|
||||
-->
|
||||
|
||||
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
|
||||
|
||||
<suite name="DeviceManagementAPI">
|
||||
<parameter name="useDefaultListeners" value="false"/>
|
||||
|
||||
<test name="API Unit Tests" preserve-order="true">
|
||||
<classes>
|
||||
<class name="org.wso2.carbon.device.mgt.jaxrs.service.impl.DeviceManagementServiceImplTest"/>
|
||||
<class name="org.wso2.carbon.device.mgt.jaxrs.service.impl.DeviceTypeManagementServiceTest"/>
|
||||
</classes>
|
||||
</test>
|
||||
</suite>
|
@ -0,0 +1,349 @@
|
||||
package org.wso2.carbon.policy.mgt.core.mgt.impl;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
import org.testng.annotations.Test;
|
||||
import org.testng.internal.collections.Pair;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
||||
import org.wso2.carbon.device.mgt.common.IllegalTransactionStateException;
|
||||
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
|
||||
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager;
|
||||
import org.wso2.carbon.device.mgt.common.policy.mgt.Profile;
|
||||
import org.wso2.carbon.device.mgt.common.policy.mgt.ProfileFeature;
|
||||
import org.wso2.carbon.device.mgt.core.authorization.DeviceAccessAuthorizationServiceImpl;
|
||||
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.operation.mgt.OperationManagerImpl;
|
||||
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl;
|
||||
import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderServiceImpl;
|
||||
import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint;
|
||||
import org.wso2.carbon.policy.mgt.common.ProfileManagementException;
|
||||
import org.wso2.carbon.policy.mgt.core.BasePolicyManagementDAOTest;
|
||||
import org.wso2.carbon.policy.mgt.core.PolicyManagerServiceImpl;
|
||||
import org.wso2.carbon.policy.mgt.core.dao.FeatureDAO;
|
||||
import org.wso2.carbon.policy.mgt.core.dao.FeatureManagerDAOException;
|
||||
import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory;
|
||||
import org.wso2.carbon.policy.mgt.core.dao.ProfileDAO;
|
||||
import org.wso2.carbon.policy.mgt.core.dao.ProfileManagerDAOException;
|
||||
import org.wso2.carbon.policy.mgt.core.dao.impl.ProfileDAOImpl;
|
||||
import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder;
|
||||
import org.wso2.carbon.policy.mgt.core.mock.TypeXDeviceManagementService;
|
||||
import org.wso2.carbon.policy.mgt.core.services.SimplePolicyEvaluationTest;
|
||||
import org.wso2.carbon.policy.mgt.core.util.FeatureCreator;
|
||||
import org.wso2.carbon.policy.mgt.core.util.ProfileCreator;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyBoolean;
|
||||
import static org.mockito.Matchers.anyInt;
|
||||
import static org.mockito.Matchers.anyListOf;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class ProfileManagerImplTest extends BasePolicyManagementDAOTest {
|
||||
private static final Log log = LogFactory.getLog(PolicyManagerServiceImpl.class);
|
||||
|
||||
private static final String DEVICE3 = "device3";
|
||||
private static final String GROUP3 = "group3";
|
||||
private static final String POLICY3 = "policy3";
|
||||
private static final String DEVICE_TYPE_C = "deviceTypeC";
|
||||
|
||||
private Profile profile1;
|
||||
private OperationManager operationManager;
|
||||
|
||||
@BeforeClass
|
||||
public void initialize() throws Exception {
|
||||
log.info("Initializing policy manager tests");
|
||||
super.initializeServices();
|
||||
deviceMgtService.registerDeviceType(new TypeXDeviceManagementService(DEVICE_TYPE_C));
|
||||
operationManager = new OperationManagerImpl(DEVICE_TYPE_C);
|
||||
enrollDevice(DEVICE3, DEVICE_TYPE_C);
|
||||
createDeviceGroup(GROUP3);
|
||||
DeviceGroup group1 = groupMgtService.getGroup(GROUP3);
|
||||
addDeviceToGroup(new DeviceIdentifier(DEVICE3, DEVICE_TYPE_C), GROUP3);
|
||||
}
|
||||
|
||||
@Test(description = "This test case tests adding new profile")
|
||||
public void testAddProfile() throws Exception {
|
||||
//Creating profile object
|
||||
Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_C);
|
||||
//Adding profile
|
||||
profile1 = profileManager.addProfile(profile);
|
||||
Assert.assertEquals(profile1.getProfileName(), profile.getProfileName());
|
||||
Assert.assertEquals(profile1.getTenantId(), profile.getTenantId());
|
||||
Assert.assertEquals(profile1.getDeviceType(), profile.getDeviceType());
|
||||
}
|
||||
|
||||
@Test(description = "This test case tests handling ProfileManagerDAOException when adding new profile",
|
||||
dependsOnMethods = "testAddProfile")
|
||||
public void testAddProfileThrowingProfileManagerDAOException() throws Exception {
|
||||
ProfileDAO profileDAO = mock(ProfileDAOImpl.class);
|
||||
when(profileDAO.addProfile(any(Profile.class))).thenThrow(new ProfileManagerDAOException());
|
||||
//Creating profile object
|
||||
Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_C);
|
||||
testThrowingException(profile, p -> profileManager.addProfile(p), "profileDAO", profileDAO,
|
||||
ProfileManagerDAOException.class);
|
||||
}
|
||||
|
||||
@Test(description = "This test case tests handling FeatureManagerDAOException when adding new profile",
|
||||
dependsOnMethods = "testAddProfileThrowingProfileManagerDAOException")
|
||||
public void testAddProfileThrowingFeatureManagerDAOException() throws Exception {
|
||||
FeatureDAO featureDAO = mock(FeatureDAO.class);
|
||||
when(featureDAO.addProfileFeatures(anyListOf(ProfileFeature.class), anyInt())).thenThrow(
|
||||
new FeatureManagerDAOException());
|
||||
//Creating profile object
|
||||
Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_C);
|
||||
testThrowingException(profile, p -> profileManager.addProfile(p), "featureDAO", featureDAO,
|
||||
FeatureManagerDAOException.class);
|
||||
}
|
||||
|
||||
@Test(description = "This test case tests handling SQLException when adding new profile",
|
||||
dependsOnMethods = "testAddProfileThrowingFeatureManagerDAOException",
|
||||
expectedExceptions = IllegalTransactionStateException.class)
|
||||
public void testAddProfileThrowingIllegalTransactionStateException() throws Exception {
|
||||
//Creating profile object
|
||||
Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_C);
|
||||
Pair<Connection, Pair<DataSource, DataSource>> pair = mockConnection();
|
||||
PowerMockito.doThrow(new SQLException()).when(pair.first()).setAutoCommit(anyBoolean());
|
||||
try {
|
||||
profileManager.addProfile(profile);
|
||||
} finally {
|
||||
PolicyManagementDAOFactory.init(pair.second().first());
|
||||
}
|
||||
}
|
||||
|
||||
@Test(description = "This test case tests updating profile",
|
||||
dependsOnMethods = "testAddProfile")
|
||||
public void testUpdateProfile() throws Exception {
|
||||
String newProfileName = "Updated Test Profile";
|
||||
Profile savedProfile = profileManager.getProfile(profile1.getProfileId());
|
||||
savedProfile.setProfileName(newProfileName);
|
||||
Profile updateProfile = profileManager.updateProfile(savedProfile);
|
||||
Assert.assertEquals(updateProfile.getProfileName(), newProfileName);
|
||||
}
|
||||
|
||||
@Test(description = "This test case tests handling ProfileManagerDAOException when updating profile",
|
||||
dependsOnMethods = "testUpdateProfile")
|
||||
public void testUpdateProfileThrowingProfileManagerDAOException() throws Exception {
|
||||
ProfileDAO profileDAO = mock(ProfileDAOImpl.class);
|
||||
when(profileDAO.updateProfile(any(Profile.class))).thenThrow(new ProfileManagerDAOException());
|
||||
|
||||
String newProfileName = "Updated Test Profile";
|
||||
Profile savedProfile = profileManager.getProfile(profile1.getProfileId());
|
||||
savedProfile.setProfileName(newProfileName);
|
||||
testThrowingException(savedProfile, p -> profileManager.updateProfile(p), "profileDAO", profileDAO,
|
||||
ProfileManagerDAOException.class);
|
||||
}
|
||||
|
||||
@Test(description = "This test case tests handling FeatureManagerDAOException when updating profile",
|
||||
dependsOnMethods = "testUpdateProfileThrowingProfileManagerDAOException")
|
||||
public void testUpdateProfileThrowingFeatureManagerDAOException() throws Exception {
|
||||
FeatureDAO featureDAO = mock(FeatureDAO.class);
|
||||
when(featureDAO.updateProfileFeatures(anyListOf(ProfileFeature.class), anyInt())).thenThrow(
|
||||
new FeatureManagerDAOException());
|
||||
|
||||
String newProfileName = "Updated Test Profile";
|
||||
Profile savedProfile = profileManager.getProfile(profile1.getProfileId());
|
||||
savedProfile.setProfileName(newProfileName);
|
||||
testThrowingException(savedProfile, p -> profileManager.updateProfile(p), "featureDAO", featureDAO,
|
||||
FeatureManagerDAOException.class);
|
||||
}
|
||||
|
||||
@Test(description = "This test case tests handling SQLException when updating profile",
|
||||
dependsOnMethods = {"testUpdateProfileThrowingFeatureManagerDAOException"},
|
||||
expectedExceptions = IllegalTransactionStateException.class)
|
||||
public void testUpdateProfileThrowingIllegalTransactionStateException() throws Exception {
|
||||
//Retrieving profile object
|
||||
Profile savedProfile = profileManager.getProfile(profile1.getProfileId());
|
||||
|
||||
Pair<Connection, Pair<DataSource, DataSource>> pair = mockConnection();
|
||||
PowerMockito.doThrow(new SQLException()).when(pair.first()).setAutoCommit(anyBoolean());
|
||||
|
||||
String newProfileName = "Updated Test Profile";
|
||||
savedProfile.setProfileName(newProfileName);
|
||||
try {
|
||||
profileManager.updateProfile(savedProfile);
|
||||
} finally {
|
||||
PolicyManagementDAOFactory.init(pair.second().first());
|
||||
}
|
||||
}
|
||||
|
||||
@Test(description = "This test case tests retrieving profile", dependsOnMethods = "testAddProfile")
|
||||
public void testGetProfile() throws Exception {
|
||||
Profile savedProfile = profileManager.getProfile(profile1.getProfileId());
|
||||
Assert.assertEquals(profile1.getProfileName(), savedProfile.getProfileName());
|
||||
Assert.assertEquals(profile1.getTenantId(), savedProfile.getTenantId());
|
||||
Assert.assertEquals(profile1.getDeviceType(), savedProfile.getDeviceType());
|
||||
}
|
||||
|
||||
@Test(description = "This test case tests retrieving non existent profile", dependsOnMethods = "testGetProfile",
|
||||
expectedExceptions = ProfileManagementException.class)
|
||||
public void testGetProfileThrowingProfileManagementException() throws Exception {
|
||||
int nonExistentProfileId = 9999;
|
||||
profileManager.getProfile(nonExistentProfileId);
|
||||
}
|
||||
|
||||
@Test(description = "This test case tests handling ProfileManagerDAOException when retrieving profile",
|
||||
dependsOnMethods = "testGetProfile")
|
||||
public void testGetProfileThrowingProfileManagerDAOException() throws Exception {
|
||||
ProfileDAO profileDAO = mock(ProfileDAOImpl.class);
|
||||
when(profileDAO.getProfile(anyInt())).thenThrow(new ProfileManagerDAOException());
|
||||
testThrowingException(profile1, p -> profileManager.getProfile(p.getProfileId()), "profileDAO", profileDAO,
|
||||
ProfileManagerDAOException.class);
|
||||
}
|
||||
|
||||
@Test(description = "This test case tests handling FeatureManagerDAOException when retrieving profile",
|
||||
dependsOnMethods = "testGetProfileThrowingProfileManagerDAOException")
|
||||
public void testGetProfileThrowingFeatureManagerDAOException() throws Exception {
|
||||
FeatureDAO featureDAO = mock(FeatureDAO.class);
|
||||
when(featureDAO.getFeaturesForProfile(anyInt())).thenThrow(new FeatureManagerDAOException());
|
||||
testThrowingException(profile1, p -> profileManager.getProfile(p.getProfileId()), "featureDAO", featureDAO,
|
||||
FeatureManagerDAOException.class);
|
||||
}
|
||||
|
||||
@Test(description = "This test case tests handling SQLException when retrieving profile",
|
||||
dependsOnMethods = "testGetProfileThrowingFeatureManagerDAOException",
|
||||
expectedExceptions = IllegalTransactionStateException.class)
|
||||
public void testGetProfileThrowingIllegalTransactionStateException() throws Exception {
|
||||
//Creating profile object
|
||||
Pair<Connection, Pair<DataSource, DataSource>> pair = mockConnection();
|
||||
PowerMockito.doThrow(new SQLException()).when(pair.second().second()).getConnection();
|
||||
|
||||
try {
|
||||
profileManager.getProfile(profile1.getProfileId());
|
||||
} finally {
|
||||
PolicyManagementDAOFactory.init(pair.second().first());
|
||||
}
|
||||
}
|
||||
|
||||
@Test(description = "This test case tests retrieving all profiles",
|
||||
dependsOnMethods = "testAddProfile")
|
||||
public void testGetAllProfiles() throws Exception {
|
||||
profileManager.getAllProfiles();
|
||||
}
|
||||
|
||||
@Test(description = "This test case tests handling ProfileManagerDAOException when retrieving all profiles",
|
||||
dependsOnMethods = "testGetAllProfiles")
|
||||
public void testGetAllProfilesThrowingProfileManagerDAOException() throws Exception {
|
||||
ProfileDAO profileDAO = mock(ProfileDAOImpl.class);
|
||||
when(profileDAO.getAllProfiles()).thenThrow(new ProfileManagerDAOException());
|
||||
testThrowingException(profile1, p -> profileManager.getAllProfiles(), "profileDAO", profileDAO,
|
||||
ProfileManagerDAOException.class);
|
||||
}
|
||||
|
||||
@Test(description = "This test case tests handling FeatureManagerDAOException when retrieving all profiles",
|
||||
dependsOnMethods = "testGetAllProfilesThrowingProfileManagerDAOException")
|
||||
public void testGetAllProfilesThrowingFeatureManagerDAOException() throws Exception {
|
||||
FeatureDAO featureDAO = mock(FeatureDAO.class);
|
||||
when(featureDAO.getAllProfileFeatures()).thenThrow(new FeatureManagerDAOException());
|
||||
testThrowingException(profile1, p -> profileManager.getAllProfiles(), "featureDAO", featureDAO,
|
||||
FeatureManagerDAOException.class);
|
||||
}
|
||||
|
||||
@Test(description = "This test case tests handling SQLException when retrieving all profiles",
|
||||
dependsOnMethods = "testGetAllProfilesThrowingFeatureManagerDAOException",
|
||||
expectedExceptions = IllegalTransactionStateException.class)
|
||||
public void testGetAllProfilesThrowingIllegalTransactionStateException() throws Exception {
|
||||
//Creating profile object
|
||||
Pair<Connection, Pair<DataSource, DataSource>> pair = mockConnection();
|
||||
PowerMockito.doThrow(new SQLException()).when(pair.second().second()).getConnection();
|
||||
|
||||
try {
|
||||
profileManager.getAllProfiles();
|
||||
} finally {
|
||||
PolicyManagementDAOFactory.init(pair.second().first());
|
||||
}
|
||||
}
|
||||
|
||||
@Test(description = "This test case tests retrieving profiles of a device type",
|
||||
dependsOnMethods = "testAddProfile")
|
||||
public void testGetProfilesOfDeviceType() throws Exception {
|
||||
profileManager.getProfilesOfDeviceType(DEVICE_TYPE_C);
|
||||
}
|
||||
|
||||
@Test(description = "This test case tests handling ProfileManagerDAOException when retrieving all profiles of a " +
|
||||
"device type",
|
||||
dependsOnMethods = "testGetProfilesOfDeviceType")
|
||||
public void testGetProfilesOfDeviceTypeThrowingProfileManagerDAOException() throws Exception {
|
||||
ProfileDAO profileDAO = mock(ProfileDAOImpl.class);
|
||||
when(profileDAO.getProfilesOfDeviceType(anyString())).thenThrow(new ProfileManagerDAOException());
|
||||
testThrowingException(profile1, p -> profileManager.getProfilesOfDeviceType(DEVICE_TYPE_C), "profileDAO",
|
||||
profileDAO,
|
||||
ProfileManagerDAOException.class);
|
||||
}
|
||||
|
||||
@Test(description = "This test case tests handling FeatureManagerDAOException when retrieving all profiles of a " +
|
||||
"device type",
|
||||
dependsOnMethods = "testGetProfilesOfDeviceTypeThrowingProfileManagerDAOException")
|
||||
public void testGetProfilesOfDeviceTypeThrowingFeatureManagerDAOException() throws Exception {
|
||||
FeatureDAO featureDAO = mock(FeatureDAO.class);
|
||||
when(featureDAO.getAllProfileFeatures()).thenThrow(new FeatureManagerDAOException());
|
||||
testThrowingException(profile1, p -> profileManager.getProfilesOfDeviceType(DEVICE_TYPE_C), "featureDAO",
|
||||
featureDAO,
|
||||
FeatureManagerDAOException.class);
|
||||
}
|
||||
|
||||
@Test(description = "This test case tests handling SQLException when retrieving all profiles of a device type",
|
||||
dependsOnMethods = "testGetProfilesOfDeviceTypeThrowingFeatureManagerDAOException",
|
||||
expectedExceptions = IllegalTransactionStateException.class)
|
||||
public void testGetProfilesOfDeviceTypeThrowingIllegalTransactionStateException() throws Exception {
|
||||
//Creating profile object
|
||||
Pair<Connection, Pair<DataSource, DataSource>> pair = mockConnection();
|
||||
PowerMockito.doThrow(new SQLException()).when(pair.second().second()).getConnection();
|
||||
|
||||
try {
|
||||
profileManager.getProfilesOfDeviceType(DEVICE_TYPE_C);
|
||||
} finally {
|
||||
PolicyManagementDAOFactory.init(pair.second().first());
|
||||
}
|
||||
}
|
||||
|
||||
@Test(description = "This test case tests handling ProfileManagerDAOException when deleting a profile",
|
||||
dependsOnMethods = "testGetProfilesOfDeviceTypeThrowingIllegalTransactionStateException")
|
||||
public void testDeleteProfileThrowingProfileManagerDAOException() throws Exception {
|
||||
ProfileDAO profileDAO = mock(ProfileDAOImpl.class);
|
||||
when(profileDAO.deleteProfile(any(Profile.class))).thenThrow(new ProfileManagerDAOException());
|
||||
testThrowingException(profile1, p -> profileManager.deleteProfile(profile1), "profileDAO", profileDAO,
|
||||
ProfileManagerDAOException.class);
|
||||
}
|
||||
|
||||
@Test(description = "This test case tests handling FeatureManagerDAOException when deleting a profile",
|
||||
dependsOnMethods = "testDeleteProfileThrowingProfileManagerDAOException")
|
||||
public void testDeleteProfileThrowingFeatureManagerDAOException() throws Exception {
|
||||
FeatureDAO featureDAO = mock(FeatureDAO.class);
|
||||
when(featureDAO.deleteFeaturesOfProfile(any(Profile.class))).thenThrow(new FeatureManagerDAOException());
|
||||
testThrowingException(profile1, p -> profileManager.deleteProfile(profile1), "featureDAO", featureDAO,
|
||||
FeatureManagerDAOException.class);
|
||||
}
|
||||
|
||||
@Test(description = "This test case tests handling SQLException when deleting a profile",
|
||||
dependsOnMethods = "testDeleteProfileThrowingFeatureManagerDAOException",
|
||||
expectedExceptions = IllegalTransactionStateException.class)
|
||||
public void testDeleteProfileThrowingIllegalTransactionStateException() throws Exception {
|
||||
//Creating profile object
|
||||
Pair<Connection, Pair<DataSource, DataSource>> pair = mockConnection();
|
||||
PowerMockito.doThrow(new SQLException()).when(pair.second().second()).getConnection();
|
||||
|
||||
try {
|
||||
profileManager.deleteProfile(profile1);
|
||||
} finally {
|
||||
PolicyManagementDAOFactory.init(pair.second().first());
|
||||
}
|
||||
}
|
||||
|
||||
@Test(description = "This test case tests deleting a profile",
|
||||
dependsOnMethods = "testDeleteProfileThrowingIllegalTransactionStateException",
|
||||
expectedExceptions = {ProfileManagementException.class})
|
||||
public void testDeleteProfile() throws Exception {
|
||||
profileManager.deleteProfile(profile1);
|
||||
Profile savedProfile = profileManager.getProfile(profile1.getProfileId());
|
||||
}
|
||||
}
|
Loading…
Reference in new issue