Initial unit tests for device management rest API

This commit includes seting up testNG, jacoco for the device.mgt.api module
revert-70aa11f8
Madawa Soysa 7 years ago
parent 658b5f36ae
commit 80eb7d2c28

@ -61,6 +61,23 @@
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<excludes>
<exclude>org.wso2.carbon.device.mgt.jaxrs.*</exclude>
</excludes>
</configuration>
</execution>
<execution>
<id>default-instrument</id>
<goals>
<goal>instrument</goal>
</goals>
</execution>
<execution>
<id>default-restore-instrumented-classes</id>
<goals>
<goal>restore-instrumented-classes</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
@ -69,12 +86,27 @@
<goal>report</goal>
</goals>
<configuration>
<includes>
<include>org.wso2.carbon.device.mgt.jaxrs.*</include>
</includes>
<dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile>
<outputDirectory>${basedir}/target/coverage-reports/site</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<log4j.configuration>file:src/test/resources/log4j.properties</log4j.configuration>
</systemPropertyVariables>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
@ -148,11 +180,6 @@
<artifactId>cxf-rt-transports-http</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-httpclient.wso2</groupId>
<artifactId>commons-httpclient</artifactId>
@ -212,6 +239,14 @@
<groupId>org.apache.axis2.wso2</groupId>
<artifactId>axis2-client</artifactId>
</exclusion>
<exclusion>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</exclusion>
<exclusion>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
@ -223,6 +258,14 @@
<groupId>org.apache.axis2.wso2</groupId>
<artifactId>axis2-client</artifactId>
</exclusion>
<exclusion>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.device.mgt.core</artifactId>
</exclusion>
<exclusion>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
@ -249,6 +292,12 @@
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.certificate.mgt.core</artifactId>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.device.mgt.core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
@ -378,6 +427,15 @@
<artifactId>org.wso2.carbon.apimgt.integration.client</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-testng</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

@ -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";

@ -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());
}
}

@ -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,30 @@
<!--
~ 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"/>
</classes>
</test>
</suite>

@ -195,6 +195,10 @@
<fileset dir="${device.extensions.deployer}/${target}/${coverge-report}">
<include name="${individual.test.report.name}" />
</fileset>
<fileset dir="${device.mgt.api}/${target}/${coverge-report}">
<include name="${individual.test.report.name}" />
</fileset>
<!-- Need to list the newly added exec files here -->
</executiondata>
<structure name="Carbon Device Management Component Coverage Report">

@ -1890,7 +1890,7 @@
<carbon.registry.imp.pkg.version.range>[4.4.8, 5.0.0)</carbon.registry.imp.pkg.version.range>
<!--CXF properties-->
<cxf.version>2.7.16</cxf.version>
<cxf.version>3.2.0</cxf.version>
<cxf.bindings.version>2.5.11</cxf.bindings.version>
<json-simple.version>1.1.wso2v1</json-simple.version>
<jackson.version>1.9.0</jackson.version>

Loading…
Cancel
Save