From f648958143d23c05036d836a532b0d51929935a4 Mon Sep 17 00:00:00 2001 From: Madawa Soysa Date: Thu, 12 Oct 2017 19:43:55 +0530 Subject: [PATCH] Adding more unit test cases to DeviceManagementServiceImpl class --- .../impl/DeviceManagementServiceImplTest.java | 219 +++++++++++++++++- 1 file changed, 208 insertions(+), 11 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImplTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImplTest.java index 669e385375..db74be4dfe 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImplTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImplTest.java @@ -18,15 +18,14 @@ package org.wso2.carbon.device.mgt.jaxrs.service.impl; -import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.junit.Assert; 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; @@ -34,6 +33,8 @@ 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; @@ -42,12 +43,16 @@ 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"}) @@ -62,7 +67,9 @@ public class DeviceManagementServiceImplTest { 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 @@ -77,8 +84,7 @@ public class DeviceManagementServiceImplTest { this.deviceManagementProviderService = Mockito .mock(DeviceManagementProviderServiceImpl.class, Mockito.RETURNS_MOCKS); this.deviceManagementService = new DeviceManagementServiceImpl(); - Mockito.when(this.deviceManagementProviderService.isEnrolled(Mockito.any(DeviceIdentifier.class))) - .thenReturn(true).thenReturn(false).thenThrow(new DeviceManagementException()); + this.deviceAccessAuthorizationService = Mockito.mock(DeviceAccessAuthorizationServiceImpl.class); } @Test(description = "Testing if the device is enrolled when the device is enrolled.") @@ -86,9 +92,12 @@ public class DeviceManagementServiceImplTest { PowerMockito.spy(DeviceMgtAPIUtils.class); PowerMockito.doReturn(this.deviceManagementProviderService) .when(DeviceMgtAPIUtils.class, "getDeviceManagementService"); + 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.", @@ -97,9 +106,12 @@ public class DeviceManagementServiceImplTest { PowerMockito.spy(DeviceMgtAPIUtils.class); PowerMockito.doReturn(this.deviceManagementProviderService) .when(DeviceMgtAPIUtils.class, "getDeviceManagementService"); + 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.", @@ -108,19 +120,20 @@ public class DeviceManagementServiceImplTest { PowerMockito.spy(DeviceMgtAPIUtils.class); PowerMockito.doReturn(this.deviceManagementProviderService) .when(DeviceMgtAPIUtils.class, "getDeviceManagementService"); + 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 { - DeviceAccessAuthorizationService deviceAccessAuthorizationService = Mockito - .mock(DeviceAccessAuthorizationServiceImpl.class); PowerMockito.mockStatic(DeviceMgtAPIUtils.class); PowerMockito.doReturn(this.deviceManagementProviderService) .when(DeviceMgtAPIUtils.class, "getDeviceManagementService"); - PowerMockito.doReturn(deviceAccessAuthorizationService) + PowerMockito.doReturn(this.deviceAccessAuthorizationService) .when(DeviceMgtAPIUtils.class, "getDeviceAccessAuthorizationService"); Response response = this.deviceManagementService .getDevices(TEST_DEVICE_NAME, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP, @@ -130,25 +143,209 @@ public class DeviceManagementServiceImplTest { @Test(description = "Testing get devices with correct request.") public void testGetDevices() throws Exception { - DeviceAccessAuthorizationService deviceAccessAuthorizationService = Mockito - .mock(DeviceAccessAuthorizationServiceImpl.class, Mockito.RETURNS_MOCKS); + PowerMockito.spy(DeviceMgtAPIUtils.class); + PowerMockito.spy(MultitenantUtils.class); + PowerMockito.spy(CarbonContext.class); + + PowerMockito.doReturn(this.deviceManagementProviderService) + .when(DeviceMgtAPIUtils.class, "getDeviceManagementService"); + PowerMockito.doReturn(this.deviceAccessAuthorizationService) + .when(DeviceMgtAPIUtils.class, "getDeviceAccessAuthorizationService"); + PowerMockito.doReturn(TENANT_AWARE_USERNAME) + .when(MultitenantUtils.class, "getTenantAwareUsername", Mockito.anyString()); + PowerMockito.doReturn(Mockito.mock(CarbonContext.class, Mockito.RETURNS_MOCKS)) + .when(CarbonContext.class, "getThreadLocalCarbonContext"); + 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.spy(DeviceMgtAPIUtils.class); + + PowerMockito.doReturn(this.deviceManagementProviderService) + .when(DeviceMgtAPIUtils.class, "getDeviceManagementService"); + PowerMockito.doReturn(null) + .when(DeviceMgtAPIUtils.class, "getDeviceAccessAuthorizationService"); + 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.spy(DeviceMgtAPIUtils.class); PowerMockito.spy(MultitenantUtils.class); PowerMockito.spy(CarbonContext.class); PowerMockito.doReturn(this.deviceManagementProviderService) .when(DeviceMgtAPIUtils.class, "getDeviceManagementService"); - PowerMockito.doReturn(deviceAccessAuthorizationService) + PowerMockito.doReturn(this.deviceAccessAuthorizationService) .when(DeviceMgtAPIUtils.class, "getDeviceAccessAuthorizationService"); PowerMockito.doReturn(TENANT_AWARE_USERNAME) .when(MultitenantUtils.class, "getTenantAwareUsername", Mockito.anyString()); PowerMockito.doReturn(Mockito.mock(CarbonContext.class, Mockito.RETURNS_MOCKS)) .when(CarbonContext.class, "getThreadLocalCarbonContext"); + 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(DeviceMgtAPIUtils.class); + PowerMockito.spy(MultitenantUtils.class); + PowerMockito.spy(CarbonContext.class); + + PowerMockito.doReturn(this.deviceManagementProviderService) + .when(DeviceMgtAPIUtils.class, "getDeviceManagementService"); + PowerMockito.doReturn(this.deviceAccessAuthorizationService) + .when(DeviceMgtAPIUtils.class, "getDeviceAccessAuthorizationService"); + PowerMockito.doReturn(TENANT_AWARE_USERNAME) + .when(MultitenantUtils.class, "getTenantAwareUsername", DEFAULT_USERNAME); + PowerMockito.doReturn("newuser@carbon.super") + .when(MultitenantUtils.class, "getTenantAwareUsername", "newuser"); + PowerMockito.doReturn(Mockito.mock(CarbonContext.class, Mockito.RETURNS_MOCKS)) + .when(CarbonContext.class, "getThreadLocalCarbonContext"); + 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.spy(DeviceMgtAPIUtils.class); + PowerMockito.spy(MultitenantUtils.class); + PowerMockito.spy(CarbonContext.class); + + PowerMockito.doReturn(this.deviceManagementProviderService) + .when(DeviceMgtAPIUtils.class, "getDeviceManagementService"); + PowerMockito.doReturn(this.deviceAccessAuthorizationService) + .when(DeviceMgtAPIUtils.class, "getDeviceAccessAuthorizationService"); + PowerMockito.doReturn(TENANT_AWARE_USERNAME) + .when(MultitenantUtils.class, "getTenantAwareUsername", Mockito.anyString()); + PowerMockito.doReturn(Mockito.mock(CarbonContext.class, Mockito.RETURNS_MOCKS)) + .when(CarbonContext.class, "getThreadLocalCarbonContext"); + + 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.spy(DeviceMgtAPIUtils.class); + PowerMockito.spy(MultitenantUtils.class); + PowerMockito.spy(CarbonContext.class); + + PowerMockito.doReturn(this.deviceManagementProviderService) + .when(DeviceMgtAPIUtils.class, "getDeviceManagementService"); + PowerMockito.doReturn(this.deviceAccessAuthorizationService) + .when(DeviceMgtAPIUtils.class, "getDeviceAccessAuthorizationService"); + PowerMockito.doReturn(TENANT_AWARE_USERNAME) + .when(MultitenantUtils.class, "getTenantAwareUsername", Mockito.anyString()); + PowerMockito.doReturn(Mockito.mock(CarbonContext.class, Mockito.RETURNS_MOCKS)) + .when(CarbonContext.class, "getThreadLocalCarbonContext"); + + 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.spy(DeviceMgtAPIUtils.class); + PowerMockito.spy(MultitenantUtils.class); + PowerMockito.spy(CarbonContext.class); + + PowerMockito.doReturn(this.deviceManagementProviderService) + .when(DeviceMgtAPIUtils.class, "getDeviceManagementService"); + PowerMockito.doReturn(this.deviceAccessAuthorizationService) + .when(DeviceMgtAPIUtils.class, "getDeviceAccessAuthorizationService"); + PowerMockito.doReturn(TENANT_AWARE_USERNAME) + .when(MultitenantUtils.class, "getTenantAwareUsername", Mockito.anyString()); + PowerMockito.doReturn(Mockito.mock(CarbonContext.class, Mockito.RETURNS_MOCKS)) + .when(CarbonContext.class, "getThreadLocalCarbonContext"); + 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.spy(DeviceMgtAPIUtils.class); + PowerMockito.spy(MultitenantUtils.class); + PowerMockito.spy(CarbonContext.class); + + PowerMockito.doReturn(this.deviceManagementProviderService) + .when(DeviceMgtAPIUtils.class, "getDeviceManagementService"); + PowerMockito.doReturn(this.deviceAccessAuthorizationService) + .when(DeviceMgtAPIUtils.class, "getDeviceAccessAuthorizationService"); + PowerMockito.doReturn(TENANT_AWARE_USERNAME) + .when(MultitenantUtils.class, "getTenantAwareUsername", DEFAULT_USERNAME); + PowerMockito.doReturn(Mockito.mock(CarbonContext.class, Mockito.RETURNS_MOCKS)) + .when(CarbonContext.class, "getThreadLocalCarbonContext"); + 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); } }