From 80eb7d2c2831514805cbf291e73af3595b198b59 Mon Sep 17 00:00:00 2001 From: Madawa Soysa Date: Wed, 11 Oct 2017 10:35:15 +0530 Subject: [PATCH 1/3] Initial unit tests for device management rest API This commit includes seting up testNG, jacoco for the device.mgt.api module --- .../org.wso2.carbon.device.mgt.api/pom.xml | 70 +++++++- .../mgt/jaxrs/util/DeviceMgtAPIUtils.java | 1 - .../impl/DeviceManagementServiceImplTest.java | 154 ++++++++++++++++++ .../src/test/resources/log4j.properties | 34 ++++ .../src/test/resources/testng.xml | 30 ++++ components/test-coverage/pom.xml | 4 + pom.xml | 2 +- 7 files changed, 287 insertions(+), 8 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImplTest.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/resources/log4j.properties create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/resources/testng.xml 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 75aa2adf1e..97a8590a0e 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,6 +61,23 @@ prepare-agent + + + org.wso2.carbon.device.mgt.jaxrs.* + + + + + default-instrument + + instrument + + + + default-restore-instrumented-classes + + restore-instrumented-classes + jacoco-site @@ -69,12 +86,27 @@ report + + org.wso2.carbon.device.mgt.jaxrs.* + ${basedir}/target/coverage-reports/jacoco-unit.exec ${basedir}/target/coverage-reports/site + + org.apache.maven.plugins + maven-surefire-plugin + + + file:src/test/resources/log4j.properties + + + src/test/resources/testng.xml + + + @@ -148,11 +180,6 @@ cxf-rt-transports-http provided - - junit - junit - test - commons-httpclient.wso2 commons-httpclient @@ -212,6 +239,14 @@ org.apache.axis2.wso2 axis2-client + + org.mockito + mockito-core + + + javassist + javassist + @@ -223,6 +258,14 @@ org.apache.axis2.wso2 axis2-client + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.core + + + org.mockito + mockito-core + @@ -249,6 +292,12 @@ org.wso2.carbon.devicemgt org.wso2.carbon.certificate.mgt.core provided + + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.core + + io.swagger @@ -378,6 +427,15 @@ org.wso2.carbon.apimgt.integration.client provided + + org.powermock + powermock-module-testng + test + + + org.powermock + powermock-api-mockito + test + - diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java index 254ad9a95e..cbbfaf32ce 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java @@ -101,7 +101,6 @@ import java.util.List; */ public class DeviceMgtAPIUtils { - public static final MediaType DEFAULT_CONTENT_TYPE = MediaType.APPLICATION_JSON_TYPE; private static final String NOTIFIER_FREQUENCY = "notifierFrequency"; private static final String STREAM_DEFINITION_PREFIX = "iot.per.device.stream."; private static final String DEFAULT_HTTP_PROTOCOL = "https"; 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 new file mode 100644 index 0000000000..669e385375 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImplTest.java @@ -0,0 +1,154 @@ +/* + * 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.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.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.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.util.UUID; + +import javax.ws.rs.core.Response; + +import static org.mockito.MockitoAnnotations.initMocks; + +@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 DeviceManagementService deviceManagementService; + 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(); + Mockito.when(this.deviceManagementProviderService.isEnrolled(Mockito.any(DeviceIdentifier.class))) + .thenReturn(true).thenReturn(false).thenThrow(new DeviceManagementException()); + } + + @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"); + Response response = this.deviceManagementService.isEnrolled(TEST_DEVICE_TYPE, UUID.randomUUID().toString()); + Assert.assertNotNull(response); + Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); + } + + @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"); + Response response = this.deviceManagementService.isEnrolled(TEST_DEVICE_TYPE, UUID.randomUUID().toString()); + Assert.assertNotNull(response); + Assert.assertEquals(response.getStatus(), Response.Status.NO_CONTENT.getStatusCode()); + } + + @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"); + Response response = this.deviceManagementService.isEnrolled(TEST_DEVICE_TYPE, UUID.randomUUID().toString()); + Assert.assertNotNull(response); + Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); + } + + @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) + .when(DeviceMgtAPIUtils.class, "getDeviceAccessAuthorizationService"); + 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 { + 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(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()); + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/resources/log4j.properties b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/resources/log4j.properties new file mode 100644 index 0000000000..e415fd607d --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/resources/log4j.properties @@ -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 diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/resources/testng.xml b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/resources/testng.xml new file mode 100644 index 0000000000..cd7bbbfa50 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/resources/testng.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + diff --git a/components/test-coverage/pom.xml b/components/test-coverage/pom.xml index ac05f968d3..510253aed6 100644 --- a/components/test-coverage/pom.xml +++ b/components/test-coverage/pom.xml @@ -195,6 +195,10 @@ + + + + diff --git a/pom.xml b/pom.xml index 46cdf2a0e1..3fc843f887 100644 --- a/pom.xml +++ b/pom.xml @@ -1890,7 +1890,7 @@ [4.4.8, 5.0.0) - 2.7.16 + 3.2.0 2.5.11 1.1.wso2v1 1.9.0 From f648958143d23c05036d836a532b0d51929935a4 Mon Sep 17 00:00:00 2001 From: Madawa Soysa Date: Thu, 12 Oct 2017 19:43:55 +0530 Subject: [PATCH 2/3] 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); } } From 73a613489fa1c95da0beab615383b017d745db95 Mon Sep 17 00:00:00 2001 From: Madawa Soysa Date: Fri, 13 Oct 2017 10:39:09 +0530 Subject: [PATCH 3/3] 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 97a8590a0e..44f36e405d 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 db74be4dfe..e329a6219e 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());