Adding a common test class to run the beforesuite methods

revert-70aa11f8
megala21 7 years ago
parent a0f1d13964
commit 13f6bc602d

@ -0,0 +1,84 @@
/*
* 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.extensions.device.type.template;
import org.testng.annotations.BeforeSuite;
import org.wso2.carbon.CarbonConstants;
import org.wso2.carbon.base.MultitenantConstants;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.context.RegistryType;
import org.wso2.carbon.context.internal.OSGiDataHolder;
import org.wso2.carbon.device.mgt.extensions.internal.DeviceTypeExtensionDataHolder;
import org.wso2.carbon.device.mgt.extensions.utils.Utils;
import org.wso2.carbon.governance.api.util.GovernanceArtifactConfiguration;
import org.wso2.carbon.governance.api.util.GovernanceUtils;
import org.wso2.carbon.registry.core.Registry;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.core.service.RegistryService;
import org.wso2.carbon.registry.core.session.UserRegistry;
import org.wso2.carbon.utils.FileUtil;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import static org.wso2.carbon.governance.api.util.GovernanceUtils.getGovernanceArtifactConfiguration;
/**
* This class handles all the setup that need to be done before starting to run the test cases.
*/
public class BaseExtensionsTest {
@BeforeSuite
public void init() throws RegistryException, IOException {
ClassLoader classLoader = getClass().getClassLoader();
URL resourceUrl = classLoader.getResource("license.rxt");
String rxt = null;
File carbonHome;
if (resourceUrl != null) {
rxt = FileUtil.readFileToString(resourceUrl.getFile());
}
resourceUrl = classLoader.getResource("carbon-home");
if (resourceUrl != null) {
carbonHome = new File(resourceUrl.getFile());
System.setProperty("carbon.home", carbonHome.getAbsolutePath());
}
PrivilegedCarbonContext.getThreadLocalCarbonContext()
.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID);
RegistryService registryService = Utils.getRegistryService();
OSGiDataHolder.getInstance().setRegistryService(registryService);
UserRegistry systemRegistry =
registryService.getRegistry(CarbonConstants.REGISTRY_SYSTEM_USERNAME);
GovernanceArtifactConfiguration configuration = getGovernanceArtifactConfiguration(rxt);
List<GovernanceArtifactConfiguration> configurations = new ArrayList<>();
configurations.add(configuration);
GovernanceUtils.loadGovernanceArtifacts(systemRegistry, configurations);
Registry governanceSystemRegistry = registryService.getConfigSystemRegistry();
DeviceTypeExtensionDataHolder.getInstance().setRegistryService(registryService);
PrivilegedCarbonContext.getThreadLocalCarbonContext()
.setRegistry(RegistryType.SYSTEM_CONFIGURATION, governanceSystemRegistry);
}
}

@ -23,11 +23,7 @@ import org.mockito.Mockito;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.wso2.carbon.CarbonConstants;
import org.wso2.carbon.base.MultitenantConstants;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.context.RegistryType;
import org.wso2.carbon.context.internal.OSGiDataHolder;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.DeviceStatusTaskPluginConfig;
import org.wso2.carbon.device.mgt.common.InitialOperationConfig;
@ -38,17 +34,15 @@ import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration
import org.wso2.carbon.device.mgt.common.license.mgt.License;
import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException;
import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig;
import org.wso2.carbon.device.mgt.extensions.device.type.template.config.*;
import org.wso2.carbon.device.mgt.extensions.device.type.template.config.DeviceStatusTaskConfiguration;
import org.wso2.carbon.device.mgt.extensions.device.type.template.config.DeviceTypeConfiguration;
import org.wso2.carbon.device.mgt.extensions.device.type.template.config.PolicyMonitoring;
import org.wso2.carbon.device.mgt.extensions.device.type.template.config.PullNotificationSubscriberConfig;
import org.wso2.carbon.device.mgt.extensions.device.type.template.config.PushNotificationProvider;
import org.wso2.carbon.device.mgt.extensions.device.type.template.config.TaskConfiguration;
import org.wso2.carbon.device.mgt.extensions.device.type.template.config.exception.DeviceTypeConfigurationException;
import org.wso2.carbon.device.mgt.extensions.internal.DeviceTypeExtensionDataHolder;
import org.wso2.carbon.device.mgt.extensions.utils.Utils;
import org.wso2.carbon.governance.api.util.GovernanceArtifactConfiguration;
import org.wso2.carbon.governance.api.util.GovernanceUtils;
import org.wso2.carbon.registry.core.Registry;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.core.service.RegistryService;
import org.wso2.carbon.registry.core.session.UserRegistry;
import org.wso2.carbon.utils.FileUtil;
import org.xml.sax.SAXException;
import javax.xml.bind.JAXBException;
@ -63,7 +57,6 @@ import java.util.ArrayList;
import java.util.List;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.wso2.carbon.governance.api.util.GovernanceUtils.getGovernanceArtifactConfiguration;
/**
* This is the test class for {@link DeviceTypeManagerService}
@ -87,7 +80,7 @@ public class DeviceTypeManagerServiceTest {
DeviceTypeConfigurationException, IOException, NoSuchFieldException, IllegalAccessException,
DeviceManagementException, RegistryException {
ClassLoader classLoader = getClass().getClassLoader();
File carbonHome = new File(classLoader.getResource("carbon-home").getFile());
setProvisioningConfig = DeviceTypeManagerService.class
.getDeclaredMethod("setProvisioningConfig", String.class, DeviceTypeConfiguration.class);
setProvisioningConfig.setAccessible(true);
@ -156,12 +149,6 @@ public class DeviceTypeManagerServiceTest {
configurationEntries.add(configurationEntry);
platformConfiguration.setConfiguration(configurationEntries);
if (androidConfiguration != null) {
// This is needed for DeviceTypeManager Initialization
System.setProperty("carbon.home", carbonHome.getAbsolutePath());
}
DeviceTypeManager deviceTypeManager = Mockito.mock(DeviceTypeManager.class);
when(deviceTypeManager.getConfiguration()).thenReturn(platformConfiguration);
deviceManager.set(androidDeviceTypeManagerService, deviceTypeManager);
@ -316,26 +303,8 @@ public class DeviceTypeManagerServiceTest {
private void setupArduinoDeviceType()
throws RegistryException, IOException, SAXException, ParserConfigurationException,
DeviceTypeConfigurationException, JAXBException {
PrivilegedCarbonContext.getThreadLocalCarbonContext()
.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID);
RegistryService registryService = Utils.getRegistryService();
OSGiDataHolder.getInstance().setRegistryService(registryService);
UserRegistry systemRegistry =
registryService.getRegistry(CarbonConstants.REGISTRY_SYSTEM_USERNAME);
ClassLoader classLoader = getClass().getClassLoader();
URL resourceUrl = classLoader.getResource("license.rxt");
String rxt = FileUtil.readFileToString(resourceUrl.getFile());
GovernanceArtifactConfiguration configuration = getGovernanceArtifactConfiguration(rxt);
List<GovernanceArtifactConfiguration> configurations = new ArrayList<>();
configurations.add(configuration);
GovernanceUtils.loadGovernanceArtifacts(systemRegistry, configurations);
Registry governanceSystemRegistry = registryService.getConfigSystemRegistry();
DeviceTypeExtensionDataHolder.getInstance().setRegistryService(registryService);
PrivilegedCarbonContext.getThreadLocalCarbonContext()
.setRegistry(RegistryType.SYSTEM_CONFIGURATION, governanceSystemRegistry);
resourceUrl = classLoader.getResource("arduino.xml");
URL resourceUrl = classLoader.getResource("arduino.xml");
File raspberrypiConfiguration = null;
if (resourceUrl != null) {
raspberrypiConfiguration = new File(resourceUrl.getFile());

@ -22,9 +22,6 @@ import org.mockito.Mockito;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.wso2.carbon.base.MultitenantConstants;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.context.RegistryType;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
@ -35,11 +32,8 @@ import org.wso2.carbon.device.mgt.extensions.device.type.template.dao.DeviceDAOD
import org.wso2.carbon.device.mgt.extensions.device.type.template.dao.DeviceTypeDAOHandler;
import org.wso2.carbon.device.mgt.extensions.device.type.template.dao.DeviceTypePluginDAOImpl;
import org.wso2.carbon.device.mgt.extensions.device.type.template.dao.DeviceTypePluginDAOManager;
import org.wso2.carbon.device.mgt.extensions.internal.DeviceTypeExtensionDataHolder;
import org.wso2.carbon.device.mgt.extensions.utils.Utils;
import org.wso2.carbon.registry.core.Registry;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.core.service.RegistryService;
import org.xml.sax.SAXException;
import javax.xml.bind.JAXBException;
@ -144,16 +138,8 @@ public class DeviceTypeManagerTest {
@Test(description = "This test case tests the addition of platform configuration and retrieval of the same")
public void testAddPlatformConfiguration() throws RegistryException, DeviceManagementException {
PrivilegedCarbonContext.getThreadLocalCarbonContext()
.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID);
PlatformConfiguration platformConfiguration = new PlatformConfiguration();
platformConfiguration.setType(androidDeviceType);
RegistryService registryService = Utils.getRegistryService();
Registry governanceSystemRegistry = registryService.getConfigSystemRegistry();
DeviceTypeExtensionDataHolder.getInstance().setRegistryService(registryService);
PrivilegedCarbonContext.getThreadLocalCarbonContext()
.setRegistry(RegistryType.SYSTEM_CONFIGURATION, governanceSystemRegistry);
androidDeviceTypeManager.saveConfiguration(platformConfiguration);
androidDeviceTypeManager.getConfiguration();
PlatformConfiguration actualPlatformConfiguration = androidDeviceTypeManager.getConfiguration();

@ -2,16 +2,23 @@ package org.wso2.carbon.device.mgt.extensions.device.type.template;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.wso2.carbon.CarbonConstants;
import org.wso2.carbon.base.MultitenantConstants;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.context.RegistryType;
import org.wso2.carbon.context.internal.OSGiDataHolder;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration;
import org.wso2.carbon.device.mgt.common.license.mgt.License;
import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig;
import org.wso2.carbon.device.mgt.common.type.mgt.DeviceTypeMetaDefinition;
import org.wso2.carbon.device.mgt.extensions.device.type.template.config.DeviceTypeConfiguration;
import org.wso2.carbon.device.mgt.extensions.device.type.template.config.Feature;
import org.wso2.carbon.device.mgt.extensions.device.type.template.config.Operation;
import org.wso2.carbon.device.mgt.extensions.device.type.template.config.PushNotificationProvider;
import org.wso2.carbon.device.mgt.extensions.device.type.template.config.exception.DeviceTypeConfigurationException;
import org.wso2.carbon.device.mgt.extensions.internal.DeviceTypeExtensionDataHolder;
import org.wso2.carbon.device.mgt.extensions.license.mgt.registry.RegistryBasedLicenseManager;
@ -39,27 +46,74 @@ import static org.wso2.carbon.governance.api.util.GovernanceUtils.getGovernanceA
* This test case contains the tests for {@link HTTPDeviceTypeManagerService}
*/
public class HttpDeviceTypeManagerServiceTest {
private DeviceTypeMetaDefinition deviceTypeMetaDefinition;
private HTTPDeviceTypeManagerService httpDeviceTypeManagerService;
private String androidSenseDeviceType = "androidsense";
@BeforeTest
public void setup() throws RegistryException, IOException, SAXException, ParserConfigurationException,
DeviceTypeConfigurationException, JAXBException {
createSampleDeviceTypeMetaDefinition();
httpDeviceTypeManagerService = new HTTPDeviceTypeManagerService(androidSenseDeviceType,
deviceTypeMetaDefinition);
}
private void createSampleDeviceTypeMetaDefinition()
throws SAXException, JAXBException, ParserConfigurationException, DeviceTypeConfigurationException,
IOException {
ClassLoader classLoader = getClass().getClassLoader();
URL resourceUrl = classLoader.getResource("android_sense.xml");
File androidSenseConfiguration = null;
if (resourceUrl != null) {
androidSenseConfiguration = new File(resourceUrl.getFile());
}
DeviceTypeConfiguration androidSenseDeviceTypeConfiguration = Utils
.getDeviceTypeConfiguration(androidSenseConfiguration);
PushNotificationProvider pushNotificationProvider = androidSenseDeviceTypeConfiguration
.getPushNotificationProvider();
PushNotificationConfig pushNotificationConfig = new PushNotificationConfig(pushNotificationProvider.getType()
, pushNotificationProvider.isScheduled(), null);
org.wso2.carbon.device.mgt.extensions.device.type.template.config.License license = androidSenseDeviceTypeConfiguration.getLicense();
License androidSenseLicense = new License();
androidSenseLicense.setText(license.getText());
androidSenseLicense.setLanguage(license.getLanguage());
List<Feature> configurationFeatues = androidSenseDeviceTypeConfiguration.getFeatures().getFeature();
List<org.wso2.carbon.device.mgt.common.Feature> features = new ArrayList<>();
for (Feature feature : configurationFeatues) {
org.wso2.carbon.device.mgt.common.Feature commonFeature = new org.wso2.carbon.device.mgt.common.Feature();
commonFeature.setCode(feature.getCode());
commonFeature.setDescription(feature.getDescription());
commonFeature.setName(feature.getName());
features.add(commonFeature);
}
private DeviceTypeMetaDefinition sampleDeviceTypeMetaDefinition() {
DeviceTypeMetaDefinition deviceTypeMetaDefinition = new DeviceTypeMetaDefinition();
Feature feature = new Feature();
Operation operation = new Operation();
operation.setContext("/test");
operation.setMethod("Get");
operation.setType("COMMAND");
feature.setCode("TEST");
feature.setDescription("This is a test feature");
feature.setName("TEST");
deviceTypeMetaDefinition = new DeviceTypeMetaDefinition();
deviceTypeMetaDefinition.setPushNotificationConfig(pushNotificationConfig);
deviceTypeMetaDefinition.setDescription("This is android_sense");
deviceTypeMetaDefinition.setClaimable(true);
deviceTypeMetaDefinition.setDescription("This is a new device type");
// deviceTypeMetaDefinition.setInitialOperationConfig();
return deviceTypeMetaDefinition;
deviceTypeMetaDefinition.setLicense(androidSenseLicense);
deviceTypeMetaDefinition.setFeatures(features);
}
@Test(description = "This test case tests the get type method of the device type manager")
public void testGetType() {
Assert.assertEquals(httpDeviceTypeManagerService.getType(), androidSenseDeviceType,
"HttpDeviceTypeManagerService returns" + " a different device type than initially provided");
}
@Test(description = "This test case tests the enrollment of newly added device type")
public void testEnrollDevice() throws DeviceManagementException {
String deviceId = "testdevice1";
Device sampleDevice1 = new Device(deviceId, androidSenseDeviceType, "test", "testdevice", null, null, null);
Assert.assertTrue(httpDeviceTypeManagerService.getDeviceManager().enrollDevice(sampleDevice1),
"Enrollment of " + androidSenseDeviceType + " device failed");
Assert.assertTrue(httpDeviceTypeManagerService.getDeviceManager()
.isEnrolled(new DeviceIdentifier(deviceId, androidSenseDeviceType)),
"Enrollment of " + androidSenseDeviceType + " device " + "failed");
}
}

@ -0,0 +1,65 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ 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.
-->
<DeviceTypeConfiguration name="android_sense">
<Features>
<Feature code="keywords">
<Name>Add Keywords</Name>
<Description>Send keywords to the device</Description>
<!--<Operation context="/android_sense/device/{deviceId}/words" method="POST">-->
<!--<QueryParameters>-->
<!--<Parameter>keywords</Parameter>-->
<!--</QueryParameters>-->
<!--</Operation>-->
</Feature>
<Feature code="threshold">
<Name>Add Threshold</Name>
<Description>Send Threshold to the device</Description>
<!--<Operation context="/android_sense/device/{deviceId}/words/threshold" method="POST">-->
<!--<QueryParameters>-->
<!--<Parameter>threshold</Parameter>-->
<!--</QueryParameters>-->
<!--</Operation>-->
</Feature>
<Feature code="remove_words">
<Name>Remove words</Name>
<Description>Remove Threshold from the device</Description>
<!--<Operation context="/android_sense/device/{deviceId}/words" method="DELETE">-->
<!--<QueryParameters>-->
<!--<Parameter>words</Parameter>-->
<!--</QueryParameters>-->
<!--</Operation>-->
</Feature>
</Features>
<ProvisioningConfig>
<SharedWithAllTenants>true</SharedWithAllTenants>
</ProvisioningConfig>
<PushNotificationProviderConfig type="MQTT">
<FileBasedProperties>true</FileBasedProperties>
</PushNotificationProviderConfig>
<License>
<Language>en_US</Language>
<Version>1.0.0</Version>
<Text>This is license text</Text>
</License>
</DeviceTypeConfiguration>

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
~ 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

@ -23,8 +23,10 @@
<test name="DeviceType Manager Service Test Cases" preserve-order="true">
<classes>
<class name="org.wso2.carbon.device.mgt.extensions.device.type.template.BaseExtensionsTest"/>
<class name="org.wso2.carbon.device.mgt.extensions.device.type.template.DeviceTypeManagerServiceTest"/>
<class name="org.wso2.carbon.device.mgt.extensions.device.type.template.DeviceTypeManagerTest"/>
<class name="org.wso2.carbon.device.mgt.extensions.device.type.template.HttpDeviceTypeManagerServiceTest"/>
</classes>
</test>
</suite>

Loading…
Cancel
Save