Adding license test cases

4.x.x
megala21 7 years ago
parent 9b76760c4a
commit 4d9357876d

@ -86,12 +86,36 @@
<groupId>commons-dbcp.wso2</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4.0.wso2v1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-pool.wso2</groupId>
<artifactId>commons-pool</artifactId>
<version>1.5.6.wso2v1</version>
</dependency>
<dependency>
<groupId>org.wso2.carbon.governance</groupId>
<artifactId>org.wso2.carbon.governance.registry.extensions</artifactId>
<version>4.7.8</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.wso2.carbon.registry</groupId>
<artifactId>org.wso2.carbon.registry.indexing</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>

@ -23,7 +23,11 @@ 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;
@ -31,15 +35,20 @@ import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig;
import org.wso2.carbon.device.mgt.common.ProvisioningConfig;
import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry;
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.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.*;
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;
@ -54,6 +63,7 @@ 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}
@ -63,6 +73,8 @@ public class DeviceTypeManagerServiceTest {
private DeviceTypeConfiguration androidDeviceConfiguration;
private DeviceTypeManagerService rasberrypiDeviceTypeManagerService;
private DeviceTypeConfiguration rasberrypiDeviceConfiguration;
private DeviceTypeManagerService arduinoDeviceTypeManagerService;
private DeviceTypeConfiguration arduinoDeviceTypeConfiguration;
private Method setProvisioningConfig;
private Method setOperationMonitoringConfig;
private Method setDeviceStatusTaskPluginConfig;
@ -73,7 +85,9 @@ public class DeviceTypeManagerServiceTest {
@BeforeTest
public void setup() throws NoSuchMethodException, SAXException, JAXBException, ParserConfigurationException,
DeviceTypeConfigurationException, IOException, NoSuchFieldException, IllegalAccessException,
DeviceManagementException {
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);
@ -117,7 +131,6 @@ public class DeviceTypeManagerServiceTest {
operationMonitoringConfigs.set(rasberrypiDeviceTypeManagerService, new OperationMonitoringTaskConfig());
initialOperationConfig.set(rasberrypiDeviceTypeManagerService, new InitialOperationConfig());
ClassLoader classLoader = getClass().getClassLoader();
URL resourceUrl = classLoader.getResource("android.xml");
File androidConfiguration = null;
@ -144,13 +157,15 @@ public class DeviceTypeManagerServiceTest {
configurationEntries.add(configurationEntry);
platformConfiguration.setConfiguration(configurationEntries);
if (androidConfiguration != null) {
// This is needed for DeviceTypeManager Initialization
System.setProperty("carbon.home", androidConfiguration.getAbsolutePath());
System.setProperty("carbon.home", carbonHome.getAbsolutePath());
}
DeviceTypeManager deviceTypeManager = Mockito.mock(DeviceTypeManager.class);
when(deviceTypeManager.getConfiguration()).thenReturn(platformConfiguration);
deviceManager.set(androidDeviceTypeManagerService, deviceTypeManager);
setupArduinoDeviceType();
}
@Test(description = "This test cases tests the retrieval of provisioning config after providing the configurations "
@ -273,4 +288,60 @@ public class DeviceTypeManagerServiceTest {
rasberrypiDeviceConfiguration.getPullNotificationSubscriberConfig() != null);
}
@Test (description = "This test case tests the addition and retrieval of the license")
public void testGetLicense () throws LicenseManagementException {
License license = arduinoDeviceTypeManagerService.getDeviceManager().getLicense("en_Us");
Assert.assertEquals(license.getText(), arduinoDeviceTypeConfiguration.getLicense().getText(),
"The retrieved" + " license is different from added license");
license.setLanguage("eu");
license.setText("This is a EU License");
arduinoDeviceTypeManagerService.getDeviceManager().addLicense(license);
License newLicense = arduinoDeviceTypeManagerService.getDeviceManager().getLicense("eu");
Assert.assertEquals(newLicense.getText(), license.getText(),
"The retrieved license is different from added license");
Assert.assertNull(arduinoDeviceTypeManagerService.getDeviceManager().getLicense("tn"),
"License is retrieved for a non-existing language code");
}
/**
* Setting the Arduino Device Type
* @throws RegistryException Registry Exception
* @throws IOException IO Exception
* @throws SAXException SAX Exception
* @throws ParserConfigurationException Parser Configuration Exception
* @throws DeviceTypeConfigurationException Device Type Configuration Exception
* @throws JAXBException JAXB Exception
*/
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");
File raspberrypiConfiguration = null;
if (resourceUrl != null) {
raspberrypiConfiguration = new File(resourceUrl.getFile());
}
arduinoDeviceTypeConfiguration = Utils.getDeviceTypeConfiguration(raspberrypiConfiguration);
arduinoDeviceTypeManagerService = new DeviceTypeManagerService(new
DeviceTypeConfigIdentifier("arduino", "carbon.super"), arduinoDeviceTypeConfiguration);
}
}

@ -0,0 +1,65 @@
package org.wso2.carbon.device.mgt.extensions.device.type.template;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
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.configuration.mgt.PlatformConfiguration;
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.exception.DeviceTypeConfigurationException;
import org.wso2.carbon.device.mgt.extensions.internal.DeviceTypeExtensionDataHolder;
import org.wso2.carbon.device.mgt.extensions.license.mgt.registry.RegistryBasedLicenseManager;
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;
import javax.xml.parsers.ParserConfigurationException;
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 test case contains the tests for {@link HTTPDeviceTypeManagerService}
*/
public class HttpDeviceTypeManagerServiceTest {
@BeforeTest
public void setup() throws RegistryException, IOException, SAXException, ParserConfigurationException,
DeviceTypeConfigurationException, JAXBException {
}
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.setClaimable(true);
deviceTypeMetaDefinition.setDescription("This is a new device type");
// deviceTypeMetaDefinition.setInitialOperationConfig();
return deviceTypeMetaDefinition;
}
}

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ 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.
-->
<DeviceTypeConfiguration name="arduino">
<Features>
<Feature code="bulb">
<Name>Control Bulb</Name>
<Description>Control Bulb on Arduino Uno</Description>
<Operation context="/arduino/device/{deviceId}/bulb" method="POST">
<QueryParameters>
<Parameter>state</Parameter>
</QueryParameters>
</Operation>
</Feature>
</Features>
<ProvisioningConfig>
<SharedWithAllTenants>true</SharedWithAllTenants>
</ProvisioningConfig>
<License>
<Language>en_US</Language>
<Version>1.0.0</Version>
<Text>This is license text</Text>
</License>
</DeviceTypeConfiguration>

@ -0,0 +1,41 @@
<?xml version="1.0"?>
<artifactType type="application/vnd.wso2-license+xml" shortName="license" singularLabel="License" pluralLabel="Licenses"
hasNamespace="false" iconSet="10">
<storagePath>/device-mgt/license/@{overview_name}/@{overview_language}/@{overview_version}</storagePath>
<nameAttribute>overview_name</nameAttribute>
<ui>
<list>
<column name="Device Type">
<data type="path" value="overview_name" href="@{storagePath}"/>
</column>
<column name="Language">
<data type="path" value="overview_language" href="@{storagePath}"/>
</column>
<column name="Version">
<data type="path" value="overview_version" href="@{storagePath}"/>
</column>
</list>
</ui>
<content>
<table name="Overview">
<field type="text" required="true">
<name>Name</name>
</field>
<field type="text" required="true">
<name>Language</name>
</field>
<field type="text" required="true">
<name>Version</name>
</field>
<field type="text">
<name>Validity From</name>
</field>
<field type="text">
<name>Validity To</name>
</field>
<field type="text-area">
<name>License</name>
</field>
</table>
</content>
</artifactType>
Loading…
Cancel
Save