From 73a613489fa1c95da0beab615383b017d745db95 Mon Sep 17 00:00:00 2001 From: Madawa Soysa Date: Fri, 13 Oct 2017 10:39:09 +0530 Subject: [PATCH] Improving test cases so that offline instrumentation is not needed This commit improves the test cases written to the device management rest API component with partial mocking so that the offline instrumentation is not needed to show the code coverage with the jacoco plugin. --- .../org.wso2.carbon.device.mgt.api/pom.xml | 20 -- .../impl/DeviceManagementServiceImplTest.java | 178 +++++++----------- 2 files changed, 71 insertions(+), 127 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.api/pom.xml index 97a8590a0e9..44f36e405d9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/pom.xml @@ -61,23 +61,6 @@ prepare-agent - - - org.wso2.carbon.device.mgt.jaxrs.* - - - - - default-instrument - - instrument - - - - default-restore-instrumented-classes - - restore-instrumented-classes - jacoco-site @@ -86,9 +69,6 @@ report - - org.wso2.carbon.device.mgt.jaxrs.* - ${basedir}/target/coverage-reports/jacoco-unit.exec ${basedir}/target/coverage-reports/site 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 db74be4dfef..e329a6219e7 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 @@ -89,9 +89,8 @@ public class DeviceManagementServiceImplTest { @Test(description = "Testing if the device is enrolled when the device is enrolled.") public void testIsEnrolledWhenDeviceIsEnrolled() throws Exception { - PowerMockito.spy(DeviceMgtAPIUtils.class); - PowerMockito.doReturn(this.deviceManagementProviderService) - .when(DeviceMgtAPIUtils.class, "getDeviceManagementService"); + 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()); @@ -103,9 +102,8 @@ public class DeviceManagementServiceImplTest { @Test(description = "Testing if the device is enrolled when the device is not enrolled.", dependsOnMethods = "testIsEnrolledWhenDeviceIsEnrolled") public void testIsEnrolledWhenDeviceIsNotEnrolled() throws Exception { - PowerMockito.spy(DeviceMgtAPIUtils.class); - PowerMockito.doReturn(this.deviceManagementProviderService) - .when(DeviceMgtAPIUtils.class, "getDeviceManagementService"); + 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()); @@ -117,9 +115,8 @@ public class DeviceManagementServiceImplTest { @Test(description = "Testing if the device enrolled api when exception occurred.", dependsOnMethods = "testIsEnrolledWhenDeviceIsNotEnrolled") public void testIsEnrolledError() throws Exception { - PowerMockito.spy(DeviceMgtAPIUtils.class); - PowerMockito.doReturn(this.deviceManagementProviderService) - .when(DeviceMgtAPIUtils.class, "getDeviceManagementService"); + 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()); @@ -130,11 +127,10 @@ public class DeviceManagementServiceImplTest { @Test(description = "Testing get devices when request exists both name and role.") public void testGetDevicesWhenBothNameAndRoleAvailable() throws Exception { - PowerMockito.mockStatic(DeviceMgtAPIUtils.class); - PowerMockito.doReturn(this.deviceManagementProviderService) - .when(DeviceMgtAPIUtils.class, "getDeviceManagementService"); - PowerMockito.doReturn(this.deviceAccessAuthorizationService) - .when(DeviceMgtAPIUtils.class, "getDeviceAccessAuthorizationService"); + 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); @@ -143,18 +139,14 @@ public class DeviceManagementServiceImplTest { @Test(description = "Testing get devices with correct request.") public void testGetDevices() 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"); + 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, @@ -176,12 +168,10 @@ public class DeviceManagementServiceImplTest { @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"); + 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); @@ -190,18 +180,14 @@ public class DeviceManagementServiceImplTest { @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(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"); + 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 @@ -216,25 +202,21 @@ public class DeviceManagementServiceImplTest { @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.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"); - PowerMockito.doReturn(Mockito.mock(CarbonContext.class, Mockito.RETURNS_MOCKS)) - .when(CarbonContext.class, "getThreadLocalCarbonContext"); + 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); + .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); } @@ -242,19 +224,14 @@ public class DeviceManagementServiceImplTest { @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"); + 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, @@ -273,19 +250,14 @@ public class DeviceManagementServiceImplTest { @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"); + 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, @@ -303,18 +275,14 @@ public class DeviceManagementServiceImplTest { @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"); + 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()); @@ -327,18 +295,14 @@ public class DeviceManagementServiceImplTest { @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"); + 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());