diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/pom.xml index f3e62ffd0c3..4b96824d1ed 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/pom.xml @@ -75,16 +75,8 @@ org.wso2.carbon.databridge.commons - org.wso2.carbon.registry - org.wso2.carbon.registry.indexing - - - org.wso2.carbon.registry - org.wso2.carbon.registry.common - - - org.json.wso2 - json + org.wso2.carbon + org.wso2.carbon.securevault @@ -118,9 +110,6 @@ org.wso2.carbon.context;version="${carbon.kernel.version.range}", org.wso2.carbon.utils;version="${carbon.kernel.version.range}", org.wso2.carbon.databridge.*;version="${carbon.analytics.common.version.range}", - org.wso2.carbon.registry.core.*;resolution:=optional, - org.wso2.carbon.registry.common.*;version="${carbon.registry.imp.pkg.version.range}", - org.wso2.carbon.registry.indexing.*; version="${carbon.registry.imp.pkg.version.range}", org.json;version="${commons-json.version}", javax.xml.*, javax.xml.parsers;version="${javax.xml.parsers.import.pkg.version}";resolution:=optional, diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/main/java/org/wso2/carbon/device/mgt/analytics/data/publisher/DataPublisherUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/main/java/org/wso2/carbon/device/mgt/analytics/data/publisher/DataPublisherUtil.java index da430d5fd44..086b04c4a79 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/main/java/org/wso2/carbon/device/mgt/analytics/data/publisher/DataPublisherUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/main/java/org/wso2/carbon/device/mgt/analytics/data/publisher/DataPublisherUtil.java @@ -19,6 +19,7 @@ package org.wso2.carbon.device.mgt.analytics.data.publisher; import org.w3c.dom.Document; +import org.wso2.carbon.databridge.agent.DataPublisher; import org.wso2.carbon.device.mgt.analytics.data.publisher.exception.DataPublisherConfigurationException; import javax.xml.XMLConstants; @@ -28,6 +29,9 @@ import java.io.File; public class DataPublisherUtil { + private DataPublisherUtil(){ + } + public static Document convertToDocument(File file) throws DataPublisherConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/main/java/org/wso2/carbon/device/mgt/analytics/data/publisher/DeviceDataPublisher.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/main/java/org/wso2/carbon/device/mgt/analytics/data/publisher/DeviceDataPublisher.java index 537e5b01b40..8753dc2af7b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/main/java/org/wso2/carbon/device/mgt/analytics/data/publisher/DeviceDataPublisher.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/main/java/org/wso2/carbon/device/mgt/analytics/data/publisher/DeviceDataPublisher.java @@ -18,13 +18,6 @@ */ package org.wso2.carbon.device.mgt.analytics.data.publisher; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.json.JSONException; -import org.json.JSONObject; -import org.wso2.carbon.base.MultitenantConstants; -import org.wso2.carbon.context.CarbonContext; -import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.databridge.agent.DataPublisher; import org.wso2.carbon.databridge.agent.exception.DataEndpointAgentConfigurationException; import org.wso2.carbon.databridge.agent.exception.DataEndpointAuthenticationException; @@ -32,38 +25,16 @@ import org.wso2.carbon.databridge.agent.exception.DataEndpointConfigurationExcep import org.wso2.carbon.databridge.agent.exception.DataEndpointException; import org.wso2.carbon.databridge.commons.exception.TransportException; import org.wso2.carbon.device.mgt.analytics.data.publisher.config.AnalyticsConfiguration; -import org.wso2.carbon.device.mgt.analytics.data.publisher.exception.DataPublisherAlreadyExistsException; import org.wso2.carbon.device.mgt.analytics.data.publisher.exception.DataPublisherConfigurationException; -import org.wso2.carbon.device.mgt.analytics.data.publisher.internal.DataPublisherDataHolder; -import org.wso2.carbon.registry.core.Registry; -import org.wso2.carbon.registry.core.Resource; -import org.wso2.carbon.registry.core.exceptions.RegistryException; -import org.wso2.carbon.registry.core.service.RegistryService; -import org.wso2.carbon.registry.core.service.TenantRegistryLoader; - -import java.nio.charset.Charset; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; /** * This is used to manage data publisher per tenant. */ public class DeviceDataPublisher { - private static final Log log = LogFactory.getLog(DeviceDataPublisher.class); - private static final String TENANT_DAS_CONFIG_LOCATION = "/das/config.json"; - private static final String USERNAME_CONFIG_TAG = "username"; - private static final String PASSWORD_CONFIG_TAG = "password"; - /** - * map to store data publishers for each tenant. - */ - private static Map dataPublisherMap; + private DataPublisher dataPublisher; private static DeviceDataPublisher deviceDataPublisher; - public DeviceDataPublisher() { - dataPublisherMap = new ConcurrentHashMap<>(); - } - public static DeviceDataPublisher getInstance() { if (deviceDataPublisher == null) { synchronized (DeviceDataPublisher.class) { @@ -78,144 +49,43 @@ public class DeviceDataPublisher { /** * this return the data publisher for the tenant. * - * @return + * @return instance of data publisher * @throws DataPublisherConfigurationException * */ public DataPublisher getDataPublisher() throws DataPublisherConfigurationException { - String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true); - //Get LoadBalancingDataPublisher which has been registered for the tenant. - DataPublisher dataPublisher = getDataPublisher(tenantDomain); - //If a LoadBalancingDataPublisher had not been registered for the tenant. - if (dataPublisher == null) { - AnalyticsConfiguration analyticsConfig = AnalyticsConfiguration.getInstance(); - if (!analyticsConfig.isEnable()) { - return null; - } - String analyticsServerUrlGroups = analyticsConfig.getReceiverServerUrl(); - String analyticsServerUsername = analyticsConfig.getAdminUsername(); - String analyticsServerPassword = analyticsConfig.getAdminPassword(); - if (!MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) { - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - String userInfo[] = getAnalyticsServerUserInfo(tenantId); - if (userInfo != null) { - analyticsServerUsername = userInfo[0]; - analyticsServerPassword = userInfo[1]; - } - } - //Create new DataPublisher for the tenant. - try { - dataPublisher = new DataPublisher(analyticsServerUrlGroups, analyticsServerUsername, - analyticsServerPassword); - //Add created DataPublisher. - addDataPublisher(tenantDomain, dataPublisher); - } catch (DataEndpointAgentConfigurationException e) { - throw new DataPublisherConfigurationException("Configuration Exception on data publisher for " + - "ReceiverGroup = " + analyticsServerUrlGroups + " for username " + analyticsServerUsername, e); - } catch (DataEndpointException e) { - throw new DataPublisherConfigurationException("Invalid ReceiverGroup = " + analyticsServerUrlGroups, e); - } catch (DataEndpointConfigurationException e) { - throw new DataPublisherConfigurationException("Invalid Data endpoint configuration.", e); - } catch (DataEndpointAuthenticationException e) { - throw new DataPublisherConfigurationException("Authentication Failed for user " + - analyticsServerUsername, e); - } catch (TransportException e) { - throw new DataPublisherConfigurationException("Error occurred while retrieving data publisher", e); - } catch (DataPublisherAlreadyExistsException e) { - log.warn("Attempting to register a data publisher for the tenant " + tenantDomain + - " when one already exists. Returning existing data publisher"); - return getDataPublisher(tenantDomain); - } - } - return dataPublisher; - } - - /** - * Fetch the data publisher which has been registered under the tenant domain. - * - * @param tenantDomain - The tenant domain under which the data publisher is registered - * @return - Instance of the DataPublisher which was registered. Null if not registered. - */ - private DataPublisher getDataPublisher(String tenantDomain) { - if (dataPublisherMap.containsKey(tenantDomain)) { - return dataPublisherMap.get(tenantDomain); - } - return null; - } - - /** - * Adds a LoadBalancingDataPublisher to the data publisher map. - * - * @param tenantDomain - The tenant domain under which the data publisher will be registered. - * @param dataPublisher - Instance of the LoadBalancingDataPublisher - * @throws DataPublisherAlreadyExistsException - * - - * If a data publisher has already been registered under the tenant - * domain - */ - private void addDataPublisher(String tenantDomain, DataPublisher dataPublisher) - throws DataPublisherAlreadyExistsException { - if (dataPublisherMap.containsKey(tenantDomain)) { - throw new DataPublisherAlreadyExistsException( - "A DataPublisher has already been created for the tenant " + tenantDomain); - } - - dataPublisherMap.put(tenantDomain, dataPublisher); - } - - /** - * retrieve the credential from registry - */ - private String[] getAnalyticsServerUserInfo(int tenantId) throws DataPublisherConfigurationException { - try { - String config = getConfigRegistryResourceContent(tenantId, TENANT_DAS_CONFIG_LOCATION); - JSONObject jsonConfigforDas = new JSONObject(config); - String credential[] = new String[2]; - credential[0] = jsonConfigforDas.getString(USERNAME_CONFIG_TAG); - credential[1] = jsonConfigforDas.getString(PASSWORD_CONFIG_TAG); - return credential; - } catch (RegistryException e) { - throw new DataPublisherConfigurationException("Failed to load the registry for tenant " + tenantId, e); - } catch (JSONException e) { - throw new DataPublisherConfigurationException( - "Failed to parse the credential from the registry for tenant " + tenantId, e); - } - } - - /** - * get the credential detail from the registry for tenants. - * - * @param tenantId for identify tenant space. - * @param registryLocation retrieve the config file from tenant space. - * @return the config for tenant - * @throws RegistryException - */ - private String getConfigRegistryResourceContent(int tenantId, final String registryLocation) - throws RegistryException { - String content = null; - try { - PrivilegedCarbonContext.startTenantFlow(); - PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId, true); - RegistryService registryService = DataPublisherDataHolder.getInstance().getRegistryService(); - if (registryService != null) { - Registry registry = registryService.getConfigSystemRegistry(tenantId); - this.loadTenantRegistry(tenantId); - if (registry.resourceExists(registryLocation)) { - Resource resource = registry.get(registryLocation); - content = new String((byte[]) resource.getContent(), Charset.defaultCharset()); + if (this.dataPublisher == null) { + synchronized (this) { + if (this.dataPublisher == null) { + AnalyticsConfiguration analyticsConfig = AnalyticsConfiguration.getInstance(); + if (!analyticsConfig.isEnable()) { + return null; + } + String analyticsServerUrlGroups = analyticsConfig.getReceiverServerUrl(); + String analyticsServerUsername = analyticsConfig.getAdminUsername(); + String analyticsServerPassword = analyticsConfig.getAdminPassword(); + try { + this.dataPublisher = new DataPublisher(analyticsServerUrlGroups, analyticsServerUsername, + analyticsServerPassword); + } catch (DataEndpointAgentConfigurationException e) { + throw new DataPublisherConfigurationException("Configuration Exception on data publisher for " + + "ReceiverGroup = " + analyticsServerUrlGroups + " for username " + analyticsServerUsername, e); + } catch (DataEndpointException e) { + throw new DataPublisherConfigurationException("Invalid ReceiverGroup = " + analyticsServerUrlGroups, e); + } catch (DataEndpointConfigurationException e) { + throw new DataPublisherConfigurationException("Invalid Data endpoint configuration.", e); + } catch (DataEndpointAuthenticationException e) { + throw new DataPublisherConfigurationException("Authentication Failed for user " + + analyticsServerUsername, e); + } catch (TransportException e) { + throw new DataPublisherConfigurationException("Error occurred while retrieving data publisher", e); + } + } else { + return this.dataPublisher; } } - } finally { - PrivilegedCarbonContext.endTenantFlow(); } - - return content; - } - - private void loadTenantRegistry(int tenantId) throws RegistryException { - TenantRegistryLoader tenantRegistryLoader = DataPublisherDataHolder.getInstance().getTenantRegistryLoader(); - DataPublisherDataHolder.getInstance().getIndexLoaderService().loadTenantIndex(tenantId); - tenantRegistryLoader.loadTenantRegistry(tenantId); + return this.dataPublisher; } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/main/java/org/wso2/carbon/device/mgt/analytics/data/publisher/config/AnalyticsConfiguration.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/main/java/org/wso2/carbon/device/mgt/analytics/data/publisher/config/AnalyticsConfiguration.java index 8c08d5eccf4..b758d4cf1c8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/main/java/org/wso2/carbon/device/mgt/analytics/data/publisher/config/AnalyticsConfiguration.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/main/java/org/wso2/carbon/device/mgt/analytics/data/publisher/config/AnalyticsConfiguration.java @@ -98,8 +98,12 @@ public class AnalyticsConfiguration { } public static void init() throws DataPublisherConfigurationException { + init(AnalyticsConfiguration.DEVICE_ANALYTICS_CONFIG_PATH); + } + + public static void init(String analyticsConfigPath) throws DataPublisherConfigurationException { try { - File authConfig = new File(AnalyticsConfiguration.DEVICE_ANALYTICS_CONFIG_PATH); + File authConfig = new File(analyticsConfigPath); Document doc = DataPublisherUtil.convertToDocument(authConfig); /* Un-marshaling device analytics configuration */ diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/main/java/org/wso2/carbon/device/mgt/analytics/data/publisher/config/InvalidConfigurationStateException.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/main/java/org/wso2/carbon/device/mgt/analytics/data/publisher/config/InvalidConfigurationStateException.java index 8603b21f166..b9686bac42f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/main/java/org/wso2/carbon/device/mgt/analytics/data/publisher/config/InvalidConfigurationStateException.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/main/java/org/wso2/carbon/device/mgt/analytics/data/publisher/config/InvalidConfigurationStateException.java @@ -26,20 +26,4 @@ public class InvalidConfigurationStateException extends RuntimeException { super(message); } - public InvalidConfigurationStateException(String message, Throwable cause) { - super(message, cause); - } - - public InvalidConfigurationStateException(String msg, Exception nestedEx) { - super(msg, nestedEx); - } - - public InvalidConfigurationStateException() { - super(); - } - - public InvalidConfigurationStateException(Throwable cause) { - super(cause); - } - } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/main/java/org/wso2/carbon/device/mgt/analytics/data/publisher/exception/DataPublisherAlreadyExistsException.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/main/java/org/wso2/carbon/device/mgt/analytics/data/publisher/exception/DataPublisherAlreadyExistsException.java deleted file mode 100644 index 8646d92e572..00000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/main/java/org/wso2/carbon/device/mgt/analytics/data/publisher/exception/DataPublisherAlreadyExistsException.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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. - * - */ - -package org.wso2.carbon.device.mgt.analytics.data.publisher.exception; - -public class DataPublisherAlreadyExistsException extends Exception { - public DataPublisherAlreadyExistsException() { - super(); - } - - public DataPublisherAlreadyExistsException(String message) { - super(message); - } - - public DataPublisherAlreadyExistsException(String message, Throwable cause) { - super(message, cause); - } - - public DataPublisherAlreadyExistsException(Throwable cause) { - super(cause); - } - - protected DataPublisherAlreadyExistsException(String message, Throwable cause, - boolean enableSuppression, - boolean writableStackTrace) { - super(message, cause, enableSuppression, writableStackTrace); - } -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/main/java/org/wso2/carbon/device/mgt/analytics/data/publisher/exception/DataPublisherConfigurationException.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/main/java/org/wso2/carbon/device/mgt/analytics/data/publisher/exception/DataPublisherConfigurationException.java index 7db8ae1009c..ea93c62a312 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/main/java/org/wso2/carbon/device/mgt/analytics/data/publisher/exception/DataPublisherConfigurationException.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/main/java/org/wso2/carbon/device/mgt/analytics/data/publisher/exception/DataPublisherConfigurationException.java @@ -19,9 +19,6 @@ package org.wso2.carbon.device.mgt.analytics.data.publisher.exception; public class DataPublisherConfigurationException extends Exception { - public DataPublisherConfigurationException() { - super(); - } public DataPublisherConfigurationException(String message) { super(message); @@ -31,14 +28,4 @@ public class DataPublisherConfigurationException extends Exception { super(message, cause); } - public DataPublisherConfigurationException(Throwable cause) { - super(cause); - } - - protected DataPublisherConfigurationException(String message, Throwable cause, - boolean enableSuppression, - boolean writableStackTrace) { - super(message, cause, enableSuppression, writableStackTrace); - } - } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/main/java/org/wso2/carbon/device/mgt/analytics/data/publisher/internal/DataPublisherDataHolder.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/main/java/org/wso2/carbon/device/mgt/analytics/data/publisher/internal/DataPublisherDataHolder.java deleted file mode 100644 index 6f132b3fb82..00000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/main/java/org/wso2/carbon/device/mgt/analytics/data/publisher/internal/DataPublisherDataHolder.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * 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. - * - */ -package org.wso2.carbon.device.mgt.analytics.data.publisher.internal; - -import org.wso2.carbon.registry.core.service.RegistryService; -import org.wso2.carbon.registry.core.service.TenantRegistryLoader; -import org.wso2.carbon.registry.indexing.service.TenantIndexingLoader; - -public class DataPublisherDataHolder { - private static DataPublisherDataHolder thisInstance = new DataPublisherDataHolder(); - - private TenantRegistryLoader tenantRegistryLoader; - private TenantIndexingLoader indexLoader; - private RegistryService registryService; - private DataPublisherDataHolder() { - } - - - public static DataPublisherDataHolder getInstance() { - return thisInstance; - } - - public void setTenantRegistryLoader(TenantRegistryLoader tenantRegistryLoader){ - this.tenantRegistryLoader = tenantRegistryLoader; - } - - public TenantRegistryLoader getTenantRegistryLoader(){ - return tenantRegistryLoader; - } - - public void setIndexLoaderService(TenantIndexingLoader indexLoader) { - this.indexLoader = indexLoader; - } - - public TenantIndexingLoader getIndexLoaderService(){ - return indexLoader; - } - - public RegistryService getRegistryService() { - return registryService; - } - - public void setRegistryService(RegistryService registryService) { - this.registryService = registryService; - } - -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/main/java/org/wso2/carbon/device/mgt/analytics/data/publisher/internal/DataPublisherServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/main/java/org/wso2/carbon/device/mgt/analytics/data/publisher/internal/DataPublisherServiceComponent.java index 2314b738061..fe2cad0ea19 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/main/java/org/wso2/carbon/device/mgt/analytics/data/publisher/internal/DataPublisherServiceComponent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/main/java/org/wso2/carbon/device/mgt/analytics/data/publisher/internal/DataPublisherServiceComponent.java @@ -26,30 +26,10 @@ import org.osgi.service.component.ComponentContext; import org.wso2.carbon.device.mgt.analytics.data.publisher.config.AnalyticsConfiguration; import org.wso2.carbon.device.mgt.analytics.data.publisher.service.EventsPublisherService; import org.wso2.carbon.device.mgt.analytics.data.publisher.service.EventsPublisherServiceImpl; -import org.wso2.carbon.registry.core.service.RegistryService; -import org.wso2.carbon.registry.core.service.TenantRegistryLoader; -import org.wso2.carbon.registry.indexing.service.TenantIndexingLoader; /** * @scr.component name="org.wso2.carbon.device.mgt.analytics.data.publisher.internal.DataPublisherServiceComponent" * immediate="true" - * @scr.reference name="registry.service" - * interface="org.wso2.carbon.registry.core.service.RegistryService" - * cardinality="1..1" - * policy="dynamic" - * bind="setRegistryService" - * unbind="unsetRegistryService" - * @scr.reference name="tenant.registryloader" - * interface="org.wso2.carbon.registry.core.service.TenantRegistryLoader" - * cardinality="1..1" policy="dynamic" - * bind="setTenantRegistryLoader" - * unbind="unsetTenantRegistryLoader" - * @scr.reference name="tenant.indexloader" - * interface="org.wso2.carbon.registry.indexing.service.TenantIndexingLoader" - * cardinality="1..1" - * policy="dynamic" - * bind="setIndexLoader" - * unbind="unsetIndexLoader" */ public class DataPublisherServiceComponent { @@ -62,11 +42,9 @@ public class DataPublisherServiceComponent { log.debug("Initializing device analytics bundle"); } AnalyticsConfiguration.init(); - BundleContext bundleCtx = componentCtx.getBundleContext(); this.analyticsServiceRef = bundleCtx.registerService(EventsPublisherService.class, new EventsPublisherServiceImpl(), null); - if (log.isDebugEnabled()) { log.debug("Device management analytics bundle has been successfully initialized"); } @@ -86,35 +64,4 @@ public class DataPublisherServiceComponent { log.debug("Device analytics bundle has been successfully deactivated"); } } - - protected void setRegistryService(RegistryService registryService) { - if (registryService != null && log.isDebugEnabled()) { - log.debug("Registry service initialized"); - } - DataPublisherDataHolder.getInstance().setRegistryService(registryService); - } - - protected void unsetRegistryService(RegistryService registryService) { - DataPublisherDataHolder.getInstance().setRegistryService(null); - } - - protected void setTenantRegistryLoader(TenantRegistryLoader tenantRegistryLoader) { - DataPublisherDataHolder.getInstance().setTenantRegistryLoader(tenantRegistryLoader); - } - - protected void unsetTenantRegistryLoader(TenantRegistryLoader tenantRegistryLoader) { - DataPublisherDataHolder.getInstance().setTenantRegistryLoader(null); - } - - protected void setIndexLoader(TenantIndexingLoader indexLoader) { - if (indexLoader != null && log.isDebugEnabled()) { - log.debug("IndexLoader service initialized"); - } - DataPublisherDataHolder.getInstance().setIndexLoaderService(indexLoader); - } - - protected void unsetIndexLoader(TenantIndexingLoader indexLoader) { - DataPublisherDataHolder.getInstance().setIndexLoaderService(null); - } - } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/java/org/wso2/carbon/device/mgt/analytics/data/publisher/BaseAnalyticsDataPublisherTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/java/org/wso2/carbon/device/mgt/analytics/data/publisher/BaseAnalyticsDataPublisherTest.java new file mode 100644 index 00000000000..3d42a3020b3 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/java/org/wso2/carbon/device/mgt/analytics/data/publisher/BaseAnalyticsDataPublisherTest.java @@ -0,0 +1,57 @@ +/* +* 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.analytics.data.publisher; + +import org.testng.annotations.BeforeSuite; +import org.wso2.carbon.base.MultitenantConstants; +import org.wso2.carbon.context.PrivilegedCarbonContext; + +import java.io.File; + +public class BaseAnalyticsDataPublisherTest { + + @BeforeSuite + public void init() { + setUpCarbonHome(); + } + + private void setUpCarbonHome() { + if (System.getProperty("carbon.home") == null) { + File file = new File("src/test/resources/carbon-home"); + if (file.exists()) { + System.setProperty("carbon.home", file.getAbsolutePath()); + } + file = new File("carbon-home"); + if (file.exists()) { + System.setProperty("carbon.home", file.getAbsolutePath()); + } + file = new File("../../resources/carbon-home"); + if (file.exists()) { + System.setProperty("carbon.home", file.getAbsolutePath()); + } + file = new File("../../../resources/carbon-home"); + if (file.exists()) { + System.setProperty("carbon.home", file.getAbsolutePath()); + } + } + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants + .SUPER_TENANT_DOMAIN_NAME); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID); + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/java/org/wso2/carbon/device/mgt/analytics/data/publisher/DataPublisherConfigTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/java/org/wso2/carbon/device/mgt/analytics/data/publisher/DataPublisherConfigTest.java new file mode 100644 index 00000000000..24dbbdfa5ce --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/java/org/wso2/carbon/device/mgt/analytics/data/publisher/DataPublisherConfigTest.java @@ -0,0 +1,75 @@ +/* +* 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.analytics.data.publisher; + +import junit.framework.Assert; +import org.testng.annotations.Test; +import org.wso2.carbon.device.mgt.analytics.data.publisher.config.AnalyticsConfiguration; +import org.wso2.carbon.device.mgt.analytics.data.publisher.config.InvalidConfigurationStateException; +import org.wso2.carbon.device.mgt.analytics.data.publisher.exception.DataPublisherConfigurationException; + +import java.io.File; +import java.net.URL; + +/** + * This test class will validate the Data publisher configuration creation. + */ +public class DataPublisherConfigTest extends BaseAnalyticsDataPublisherTest { + + @Test(description = "Validating the behaviour od getInstance of the config before calling the init", + expectedExceptions = InvalidConfigurationStateException.class) + public void testGetInstanceWithoutInit() { + AnalyticsConfiguration.getInstance(); + } + + @Test(description = "Validating the behaviour od getInstance of the config before calling the init", + expectedExceptions = DataPublisherConfigurationException.class) + public void testInitWithInvalidConfig() throws DataPublisherConfigurationException { + ClassLoader classLoader = this.getClass().getClassLoader(); + URL invalidConfig = classLoader.getResource("carbon-home/repository/conf/etc/" + + "device-analytics-config-invalid.xml"); + Assert.assertTrue("No configuration - device-analytics-config-invalid.xml found in resource dir", + invalidConfig != null); + File file = new File(invalidConfig.getFile()); + AnalyticsConfiguration.init(file.getAbsolutePath()); + } + + + @Test(description = "Validating the behaviour od getInstance of the config before calling the init", + expectedExceptions = DataPublisherConfigurationException.class) + public void testInitWithInvalidXML() throws DataPublisherConfigurationException { + ClassLoader classLoader = this.getClass().getClassLoader(); + URL invalidConfig = classLoader.getResource("carbon-home/repository/conf/etc/" + + "device-analytics-config-invalid-xml.xml"); + Assert.assertTrue("No configuration - device-analytics-config-invalid-xml.xml found in resource dir", + invalidConfig != null); + File file = new File(invalidConfig.getFile()); + AnalyticsConfiguration.init(file.getAbsolutePath()); + } + + + @Test(description = "Validating the init method with all required params", dependsOnMethods = "testInitWithInvalidXML") + public void testInitWithValidConfig() throws DataPublisherConfigurationException { + AnalyticsConfiguration.init(); + AnalyticsConfiguration analyticsConfiguration = AnalyticsConfiguration.getInstance(); + Assert.assertEquals(analyticsConfiguration.getAdminPassword(), "testuserpwd"); + Assert.assertEquals(analyticsConfiguration.getAdminUsername(), "testuser"); + Assert.assertEquals(analyticsConfiguration.getReceiverServerUrl(), "tcp://localhost:7615"); + Assert.assertTrue(analyticsConfiguration.isEnable()); + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/java/org/wso2/carbon/device/mgt/analytics/data/publisher/EventPublisherServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/java/org/wso2/carbon/device/mgt/analytics/data/publisher/EventPublisherServiceTest.java new file mode 100644 index 00000000000..b9b822e4c16 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/java/org/wso2/carbon/device/mgt/analytics/data/publisher/EventPublisherServiceTest.java @@ -0,0 +1,146 @@ +/* +* 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.analytics.data.publisher; + +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.databridge.agent.AgentHolder; +import org.wso2.carbon.databridge.agent.exception.DataEndpointAgentConfigurationException; +import org.wso2.carbon.device.mgt.analytics.data.publisher.config.AnalyticsConfiguration; +import org.wso2.carbon.device.mgt.analytics.data.publisher.config.InvalidConfigurationStateException; +import org.wso2.carbon.device.mgt.analytics.data.publisher.exception.DataPublisherConfigurationException; +import org.wso2.carbon.device.mgt.analytics.data.publisher.service.EventsPublisherService; +import org.wso2.carbon.device.mgt.analytics.data.publisher.service.EventsPublisherServiceImpl; + +import java.io.File; +import java.lang.reflect.Field; +import java.net.URL; + +/** + * This test class will test the methods that are exposed from {@link EventsPublisherService} + */ +public class EventPublisherServiceTest extends BaseAnalyticsDataPublisherTest { + + private static final String STREAM_NAME = "org.wso2.test.stream"; + private static final String TENANT_DOMAIN = "test.com"; + + private EventsPublisherService eventsPublisherService; + + @BeforeClass + public void initTest() { + this.eventsPublisherService = new EventsPublisherServiceImpl(); + } + + @Test(description = "Publish the event before initializing", + expectedExceptions = InvalidConfigurationStateException.class) + public void publishBeforeInit() throws DataPublisherConfigurationException, NoSuchFieldException, + IllegalAccessException, InstantiationException { + Field configField = AnalyticsConfiguration.class.getDeclaredField("config"); + configField.setAccessible(true); + configField.set(configField, null); + this.eventsPublisherService.publishEvent(STREAM_NAME, "1.0.0", getEventProps(), getEventProps(), + getEventProps()); + } + + @Test(description = "Publish with analytics config disabled", dependsOnMethods = "publishBeforeInit") + public void publishWhenAnalyticsConfigDisabled() throws DataPublisherConfigurationException { + AnalyticsConfiguration.init(); + AnalyticsConfiguration.getInstance().setEnable(false); + boolean published = this.eventsPublisherService.publishEvent(STREAM_NAME, "1.0.0", getEventProps(), + getEventProps(), getEventProps()); + Assert.assertFalse(published); + } + + @Test(description = "Publish the event after initializing", dependsOnMethods = "publishWhenAnalyticsConfigDisabled") + public void publishAfterInit() throws DataPublisherConfigurationException { + AnalyticsConfiguration.getInstance().setEnable(true); + boolean published = this.eventsPublisherService.publishEvent(STREAM_NAME, "1.0.0", getEventProps(), + getEventProps(), getEventProps()); + Assert.assertTrue(published); + } + + @Test(description = "Publish as tenant", dependsOnMethods = "publishAfterInit") + public void publishAsTenant() throws DataPublisherConfigurationException { + publishAsTenant(getEventProps()); + } + + @Test(description = "Publish the with no meta data as tenant", dependsOnMethods = "publishAsTenant", + expectedExceptions = DataPublisherConfigurationException.class) + public void publishAsTenantWithNoMetaData() throws DataPublisherConfigurationException { + publishAsTenant(null); + } + + @Test(description = "Publish the with empty meta data as tenant", dependsOnMethods = "publishAsTenant", + expectedExceptions = DataPublisherConfigurationException.class) + public void publishAsTenantWithEmptyMetaData() throws DataPublisherConfigurationException { + publishAsTenant(new Object[0]); + } + + @Test(description = "Publishing with invalid data publisher config", + dependsOnMethods = {"publishAsTenantWithEmptyMetaData", "publishAsTenantWithNoMetaData"}, + expectedExceptions = DataPublisherConfigurationException.class) + public void publishWithDataEndpointConfigException() throws DataPublisherConfigurationException, + NoSuchFieldException, IllegalAccessException { + AnalyticsConfiguration analyticsConfiguration = AnalyticsConfiguration.getInstance(); + analyticsConfiguration.setReceiverServerUrl(""); + Field dataPublisherField = DeviceDataPublisher.class.getDeclaredField("deviceDataPublisher"); + dataPublisherField.setAccessible(true); + dataPublisherField.set(dataPublisherField, null); + publishAsTenant(getEventProps()); + } + + @Test(description = "Publishing with invalid data publisher config", + dependsOnMethods = "publishWithDataEndpointConfigException", + expectedExceptions = DataPublisherConfigurationException.class) + public void publishWithDataAgentConfigException() throws DataPublisherConfigurationException, + NoSuchFieldException, IllegalAccessException, DataEndpointAgentConfigurationException { + AnalyticsConfiguration.init(); + Field defaultAgentName = AgentHolder.getInstance().getClass().getDeclaredField("defaultDataEndpointAgentName"); + defaultAgentName.setAccessible(true); + defaultAgentName.set(AgentHolder.getInstance(), "dummyAgent"); + publishAsTenant(getEventProps()); + } + + @Test(description = "Publishing with invalid data publisher config", + dependsOnMethods = "publishWithDataAgentConfigException") + public void publishWithDataEndpointException() throws DataPublisherConfigurationException, + NoSuchFieldException, IllegalAccessException, DataEndpointAgentConfigurationException { + AnalyticsConfiguration.init(); + + } + + private void publishAsTenant(Object[] metaData) throws DataPublisherConfigurationException { + PrivilegedCarbonContext.startTenantFlow(); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(TENANT_DOMAIN, true); + try { + boolean published = this.eventsPublisherService.publishEvent(STREAM_NAME, "1.0.0", metaData, + getEventProps(), getEventProps()); + Assert.assertTrue(published); + } finally { + PrivilegedCarbonContext.endTenantFlow(); + } + } + + private Object[] getEventProps() { + return new Object[]{"123"}; + } + + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/resources/carbon-home/repository/conf/carbon.xml b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/resources/carbon-home/repository/conf/carbon.xml new file mode 100644 index 00000000000..31752cf9b44 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/resources/carbon-home/repository/conf/carbon.xml @@ -0,0 +1,656 @@ + + + + + + + + + ${product.name} + + + ${product.key} + + + ${product.version} + + + + + + + + + local:/${carbon.context}/services/ + + + + + + + ${default.server.role} + + + + + + + org.wso2.carbon + + + / + + + + + + + + + 15 + + + + + + + + + 0 + + + + + 9999 + + 11111 + + + + + + 10389 + + 8000 + + + + + + 10500 + + + + + + + org.wso2.carbon.tomcat.jndi.CarbonJavaURLContextFactory + + + + + + + + + java + + + + + + + + + + false + + + false + + + 600 + + + + false + + + + + + + + 30 + + + + + + + + + 15 + + + + + + ${carbon.home}/repository/deployment/server/ + + + 15 + + + ${carbon.home}/repository/conf/axis2/axis2.xml + + + 30000 + + + ${carbon.home}/repository/deployment/client/ + + ${carbon.home}/repository/conf/axis2/axis2_client.xml + + true + + + + + + + + + + admin + Default Administrator Role + + + user + Default User Role + + + + + + + + + + + + ${carbon.home}/repository/resources/security/wso2carbon.jks + + JKS + + wso2carbon + + wso2carbon + + wso2carbon + + + + + + ${carbon.home}/repository/resources/security/client-truststore.jks + + JKS + + wso2carbon + + + + + + + + + + + + + + + + + + + UserManager + + + false + + + + + + + ${carbon.home}/tmp/work + + + + + + true + + + 10 + + + 30 + + + + + + 100 + + + + keystore + certificate + * + + org.wso2.carbon.ui.transports.fileupload.AnyFileUploadExecutor + + + + + jarZip + + org.wso2.carbon.ui.transports.fileupload.JarZipUploadExecutor + + + + dbs + + org.wso2.carbon.ui.transports.fileupload.DBSFileUploadExecutor + + + + tools + + org.wso2.carbon.ui.transports.fileupload.ToolsFileUploadExecutor + + + + toolsAny + + org.wso2.carbon.ui.transports.fileupload.ToolsAnyFileUploadExecutor + + + + + + + info + org.wso2.carbon.core.transports.util.InfoProcessor + + + wsdl + org.wso2.carbon.core.transports.util.Wsdl11Processor + + + wsdl2 + org.wso2.carbon.core.transports.util.Wsdl20Processor + + + xsd + org.wso2.carbon.core.transports.util.XsdProcessor + + + + + + false + false + true + svn + http://svnrepo.example.com/repos/ + username + password + true + + + + + + + + + + + + + + + ${require.carbon.servlet} + + + + + true + + + + + + + default repository + ${p2.repo.url} + + + + + + + + true + + + + + + true + + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/resources/carbon-home/repository/conf/data-bridge/data-agent-config-invalid.xml b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/resources/carbon-home/repository/conf/data-bridge/data-agent-config-invalid.xml new file mode 100644 index 00000000000..2d0264e3684 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/resources/carbon-home/repository/conf/data-bridge/data-agent-config-invalid.xml @@ -0,0 +1,69 @@ + + + + + + Thrift + org.wso2.carbon.databridge.agent.endpoint.thrift.ThriftDataEndpointNoClass + src/test/resources/client-truststore.jks + wso2carbon + 32768 + 200 + 1 + 30000 + 1 + 20 + 30 + 250 + 250 + 5500 + 5000 + 250 + 250 + 5500 + 5000 + + + + + + Binary + org.wso2.carbon.databridge.agent.endpoint.binary.BinaryDataEndpoint + src/test/resources/client-truststore.jks + wso2carbon + 32768 + 200 + 1 + 1 + 30000 + 20 + 30 + 250 + 250 + 5500 + 5000 + 250 + 250 + 5500 + 5000 + + + + + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/resources/carbon-home/repository/conf/data-bridge/data-agent-config.xml b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/resources/carbon-home/repository/conf/data-bridge/data-agent-config.xml new file mode 100644 index 00000000000..05215f7fc0c --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/resources/carbon-home/repository/conf/data-bridge/data-agent-config.xml @@ -0,0 +1,69 @@ + + + + + + Thrift + org.wso2.carbon.databridge.agent.endpoint.thrift.ThriftDataEndpoint + src/test/resources/client-truststore.jks + wso2carbon + 32768 + 200 + 1 + 30000 + 1 + 20 + 30 + 250 + 250 + 5500 + 5000 + 250 + 250 + 5500 + 5000 + + + + + + Binary + org.wso2.carbon.databridge.agent.endpoint.binary.BinaryDataEndpoint + src/test/resources/client-truststore.jks + wso2carbon + 32768 + 200 + 1 + 1 + 30000 + 20 + 30 + 250 + 250 + 5500 + 5000 + 250 + 250 + 5500 + 5000 + + + + + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/resources/carbon-home/repository/conf/etc/device-analytics-config-invalid-xml.xml b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/resources/carbon-home/repository/conf/etc/device-analytics-config-invalid-xml.xml new file mode 100644 index 00000000000..4353624e503 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/resources/carbon-home/repository/conf/etc/device-analytics-config-invalid-xml.xml @@ -0,0 +1,35 @@ + + + + + + tcp://localhost:7615 + testuser + testuserpwd diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/resources/carbon-home/repository/conf/etc/device-analytics-config-invalid.xml b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/resources/carbon-home/repository/conf/etc/device-analytics-config-invalid.xml new file mode 100644 index 00000000000..624a05b5f41 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/resources/carbon-home/repository/conf/etc/device-analytics-config-invalid.xml @@ -0,0 +1,35 @@ + + + + + + tcp://localhost:7615 + testuser + testuserpwd + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/resources/carbon-home/repository/conf/etc/device-analytics-config.xml b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/resources/carbon-home/repository/conf/etc/device-analytics-config.xml new file mode 100644 index 00000000000..8edea74667b --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/resources/carbon-home/repository/conf/etc/device-analytics-config.xml @@ -0,0 +1,36 @@ + + + + + + true + tcp://localhost:7615 + testuser + testuserpwd + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/resources/carbon-home/repository/conf/registry.xml b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/resources/carbon-home/repository/conf/registry.xml new file mode 100644 index 00000000000..a226ae80a81 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/resources/carbon-home/repository/conf/registry.xml @@ -0,0 +1,50 @@ + + + + + + + + wso2registry + false + true + / + + + jdbc:h2:./target/databasetest/CARBON_TEST + + org.h2.Driver + 80 + 60000 + 5 + + + false + + + + true + true + true + true + + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/resources/client-truststore.jks b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/resources/client-truststore.jks new file mode 100644 index 00000000000..3b9fdfb9e84 Binary files /dev/null and b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/resources/client-truststore.jks differ diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/resources/testng.xml b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/resources/testng.xml new file mode 100644 index 00000000000..71303997921 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/src/test/resources/testng.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/BaseDeviceManagementTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/BaseDeviceManagementTest.java index 21261bef2b6..466d92becf2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/BaseDeviceManagementTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/BaseDeviceManagementTest.java @@ -44,7 +44,6 @@ import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderServiceImpl; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; -import org.wso2.carbon.email.sender.core.service.EmailSenderServiceImpl; import org.wso2.carbon.registry.core.config.RegistryContext; import org.wso2.carbon.registry.core.exceptions.RegistryException; import org.wso2.carbon.registry.core.internal.RegistryDataHolder; @@ -85,7 +84,7 @@ public abstract class BaseDeviceManagementTest { NotificationManagementDAOFactory.init(dataSource); } - protected void initServices() throws DeviceManagementException, RegistryException { + private void initServices() throws DeviceManagementException, RegistryException, AxisFault { DeviceConfigurationManager.getInstance().initConfig(); DeviceManagementProviderService deviceMgtService = new DeviceManagementProviderServiceImpl(); DeviceManagementServiceComponent.notifyStartupListeners(); @@ -108,17 +107,10 @@ public abstract class BaseDeviceManagementTest { return context.getEmbeddedRegistryService(); } - private ConfigurationContextService getConfigContextService() throws RegistryException { - ConfigurationContext context = - null; - try { - context = ConfigurationContextFactory.createConfigurationContextFromFileSystem - ("src/test/resources/carbon-home/repository/conf/axis2/axis2.xml"); - } catch (AxisFault axisFault) { - axisFault.printStackTrace(); - } - ConfigurationContextService service = new ConfigurationContextService(context, null); - return service; + private ConfigurationContextService getConfigContextService() throws RegistryException, AxisFault { + ConfigurationContext context = ConfigurationContextFactory.createConfigurationContextFromFileSystem + ("src/test/resources/carbon-home/repository/conf/axis2/axis2.xml"); + return new ConfigurationContextService(context, null); } @BeforeClass @@ -183,47 +175,6 @@ public abstract class BaseDeviceManagementTest { } } - public void deleteData() { - Connection conn = null; - try { - conn = getDataSource().getConnection(); - conn.setAutoCommit(false); - String[] cleanupTables = new String[]{"DM_NOTIFICATION","DM_DEVICE_OPERATION_RESPONSE","DM_ENROLMENT_OP_MAPPING", "DM_CONFIG_OPERATION", - "DM_POLICY_OPERATION", "DM_COMMAND_OPERATION", "DM_PROFILE_OPERATION", "DM_DEVICE_GROUP_MAP", - "DM_GROUP", "DM_ENROLMENT", "DM_DEVICE_APPLICATION_MAPPING", - "DM_APPLICATION", "DM_DEVICE", "DM_DEVICE_TYPE"}; - for (String table : cleanupTables) { - this.cleanData(conn, table); - } - conn.commit(); - } catch (SQLException e) { - try { - if (conn != null) { - conn.rollback(); - } - } catch (SQLException e1) { - log.error("Error occurred while roll-backing the transaction", e); - } - String msg = "Error occurred while cleaning up temporary data generated during test execution"; - log.error(msg, e); - Assert.fail(msg, e); - } finally { - if (conn != null) { - try { - conn.close(); - } catch (SQLException e) { - log.warn("Error occurred while closing the connection", e); - } - } - } - } - - private void cleanData(Connection conn, String tableName) throws SQLException { - try (PreparedStatement stmt = conn.prepareStatement("DELETE FROM " + tableName)) { - stmt.execute(); - } - } - protected DataSource getDataSource() { return dataSource; } diff --git a/pom.xml b/pom.xml index c68d340deee..a5bf1d44e04 100644 --- a/pom.xml +++ b/pom.xml @@ -397,6 +397,12 @@ + + org.wso2.carbon + org.wso2.carbon.securevault + ${carbon.kernel.version} + test + org.wso2.carbon.governance org.wso2.carbon.governance.api