From b7e682a0bcd3aff71900060ff293331f59d41151 Mon Sep 17 00:00:00 2001 From: sinthuja Date: Tue, 10 Oct 2017 18:54:22 +0530 Subject: [PATCH] Adding more testcases, and cleaning up the code. --- .../pom.xml | 16 ++--- .../data/publisher/DataPublisherUtil.java | 4 ++ .../internal/DataPublisherDataHolder.java | 63 ----------------- .../DataPublisherServiceComponent.java | 53 -------------- .../publisher/EventPublisherServiceTest.java | 37 ++++++++++ .../data-bridge/data-agent-config-invalid.xml | 69 +++++++++++++++++++ 6 files changed, 115 insertions(+), 127 deletions(-) delete mode 100644 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 create mode 100644 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 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 24ea5e33b6..f9e3345e11 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 @@ -74,14 +74,6 @@ org.wso2.carbon.analytics-common 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 @@ -90,6 +82,11 @@ org.wso2.carbon org.wso2.carbon.securevault + + org.powermock + powermock-api-mockito + test + @@ -122,9 +119,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 da430d5fd4..086b04c4a7 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/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 6f132b3fb8..0000000000 --- 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 2314b73806..fe2cad0ea1 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/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 index b102bdb776..b9b822e4c1 100644 --- 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 @@ -21,13 +21,17 @@ 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} @@ -89,6 +93,39 @@ public class EventPublisherServiceTest extends BaseAnalyticsDataPublisherTest { 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); 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 0000000000..2d0264e368 --- /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 + + + + +