From bdf15f5602b0c2592fc960affab464cd6c6d3904 Mon Sep 17 00:00:00 2001 From: Vijay VK Date: Wed, 21 Feb 2018 16:52:31 +0000 Subject: fix sonar issue and additional test Change-Id: If32e2319c71b947b7e5c68410f32b19ba5ac8125 Signed-off-by: VENKATESH KUMAR Issue-ID: DCAEGEN2-227 --- .../org/onap/dcae/vestest/TestCommonStartup.java | 343 +++++++++++---------- .../org/onap/dcae/vestest/TestEventReceipt.java | 15 + .../org/onap/dcae/vestest/TestFetchConfig.java | 234 ++++++++------ 3 files changed, 331 insertions(+), 261 deletions(-) (limited to 'src/test/java/org/onap/dcae/vestest') diff --git a/src/test/java/org/onap/dcae/vestest/TestCommonStartup.java b/src/test/java/org/onap/dcae/vestest/TestCommonStartup.java index 0cf90759..aa56c611 100644 --- a/src/test/java/org/onap/dcae/vestest/TestCommonStartup.java +++ b/src/test/java/org/onap/dcae/vestest/TestCommonStartup.java @@ -1,171 +1,172 @@ -/*- - * ============LICENSE_START======================================================= - * PROJECT - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed 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. - * ============LICENSE_END========================================================= - */ -package org.onap.dcae.vestest; - -import java.io.FileReader; -import java.io.IOException; -import java.net.URL; -import java.util.Map; -import java.util.concurrent.LinkedBlockingQueue; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.onap.dcae.commonFunction.CommonStartup; -import org.onap.dcae.commonFunction.EventProcessor; -import org.onap.dcae.commonFunction.CommonStartup.QueueFullException; -import org.onap.dcae.restapi.RestfulCollectorServlet; - -import com.att.nsa.cmdLine.NsaCommandLineUtil; -import com.att.nsa.drumlin.service.framework.DrumlinServlet; -import com.att.nsa.drumlin.till.nv.rrNvReadable.loadException; -import com.att.nsa.drumlin.till.nv.rrNvReadable.missingReqdSetting; -import com.att.nsa.security.NsaAuthenticator; -import com.att.nsa.security.authenticators.SimpleAuthenticator; -import com.att.nsa.security.db.simple.NsaSimpleApiKey; -import com.google.gson.JsonParser; -import com.att.nsa.drumlin.till.nv.impl.nvPropertiesFile; -import com.att.nsa.drumlin.till.nv.impl.nvReadableStack; -import com.att.nsa.drumlin.till.nv.impl.nvReadableTable; - - -public class TestCommonStartup { - - String payload = null; - @Before - public void setUp() throws Exception { - - // process command line arguments - payload = new JsonParser().parse(new FileReader("src/test/resources/VES_valid.txt")).toString(); - CommonStartup.fProcessingInputQueue = new LinkedBlockingQueue (CommonStartup.KDEFAULT_MAXQUEUEDEVENTS); - } - - @After - public void tearDown() throws Exception { - - } - - @Test - public void testCommonStartupload() { - - String args[] = { "junittest" }; - final Map argMap = NsaCommandLineUtil.processCmdLine(args, true); - final String config = NsaCommandLineUtil.getSetting(argMap, "c", "collector.properties"); - final URL settingStream = DrumlinServlet.findStream(config, CommonStartup.class); - - final nvReadableStack settings = new nvReadableStack(); - try { - settings.push(new nvPropertiesFile(settingStream)); - } catch (loadException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - settings.push(new nvReadableTable(argMap)); - Assert.assertEquals("true", "true"); - } - - @Test - public void testhandleevent() { - JSONArray jsonArrayMod = new JSONArray().put(new JSONObject(payload)); - try { - - - CommonStartup.handleEvents (jsonArrayMod); - } catch ( JSONException | QueueFullException | IOException e) { - // TODO Auto-generated catch block - //e.printStackTrace(); - System.out.println("junit reported:" + e.getMessage()); - } - Assert.assertEquals("true", "true"); - } - - -/* - @Test - public void testServlet() { - try - { - RestfulCollectorServlet rsv = new RestfulCollectorServlet(null); - } - catch (NullPointerException|loadException| missingReqdSetting e){ - System.out.println("junit reported:" + e.getMessage()); - } - Assert.assertEquals("true", "true"); - } -*/ - - - @Test - public void testEventProcessorinstantiation() - { - CommonStartup.streamid="fault=sec_fault|syslog=sec_syslog|heartbeat=sec_heartbeat|measurementsForVfScaling=sec_measurement|mobileFlow=sec_mobileflow|other=sec_other|stateChange=sec_statechange|thresholdCrossingAlert=sec_thresholdCrossingAlert|voiceQuality=ves_voicequality|sipSignaling=ves_sipsignaling"; - EventProcessor ep = new EventProcessor (); - Thread epThread=new Thread(ep); - epThread.start(); - Assert.assertEquals("true", "true"); - epThread.stop(); - - } - - @Test - public void testAuthListHandler() - { - - final Map argMap = NsaCommandLineUtil.processCmdLine ( new String[0], true ); - final String config = NsaCommandLineUtil.getSetting ( argMap, "c", "collector.properties" ); - final URL settingStream = DrumlinServlet.findStream ( config, CommonStartup.class ); - - final nvReadableStack settings = new nvReadableStack (); - try { - settings.push ( new nvPropertiesFile ( settingStream ) ); - settings.push ( new nvReadableTable ( argMap ) ); - } catch (loadException e1) { - // TODO Auto-generated catch block - e1.printStackTrace(); - } - - - RestfulCollectorServlet rsv = null; - NsaAuthenticator NsaAuth = null; - Boolean flag = false; - try - { - rsv = new RestfulCollectorServlet(settings); - } - catch (NullPointerException|loadException| missingReqdSetting e){ - System.out.println("junit reported:" + e.getMessage()); - } - String authlist = "secureid,IWRjYWVSb2FkbTEyMyEt|sample1,c2FtcGxlMQ==|vdnsagg,dmRuc2FnZw=="; - NsaAuth = rsv.AuthlistHandler(authlist); - if (NsaAuth != null) - { - flag = true; - } - Assert.assertEquals(true, flag); - - - } -} - - +/*- + * ============LICENSE_START======================================================= + * PROJECT + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed 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. + * ============LICENSE_END========================================================= + */ +package org.onap.dcae.vestest; + +import java.io.FileReader; +import java.io.IOException; +import java.net.URL; +import java.util.Map; +import java.util.concurrent.LinkedBlockingQueue; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.onap.dcae.commonFunction.CommonStartup; +import org.onap.dcae.commonFunction.EventProcessor; +import org.onap.dcae.commonFunction.CommonStartup.QueueFullException; +import org.onap.dcae.restapi.RestfulCollectorServlet; + +import com.att.nsa.cmdLine.NsaCommandLineUtil; +import com.att.nsa.drumlin.service.framework.DrumlinServlet; +import com.att.nsa.drumlin.till.nv.rrNvReadable.loadException; +import com.att.nsa.drumlin.till.nv.rrNvReadable.missingReqdSetting; +import com.att.nsa.security.NsaAuthenticator; +import com.att.nsa.security.authenticators.SimpleAuthenticator; +import com.att.nsa.security.db.simple.NsaSimpleApiKey; +import com.google.gson.JsonParser; +import com.att.nsa.drumlin.till.nv.impl.nvPropertiesFile; +import com.att.nsa.drumlin.till.nv.impl.nvReadableStack; +import com.att.nsa.drumlin.till.nv.impl.nvReadableTable; + + +public class TestCommonStartup { + + String payload = null; + @Before + public void setUp() throws Exception { + + // process command line arguments + payload = new JsonParser().parse(new FileReader("src/test/resources/VES_valid.txt")).toString(); + CommonStartup.fProcessingInputQueue = new LinkedBlockingQueue (CommonStartup.KDEFAULT_MAXQUEUEDEVENTS); + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void testCommonStartupload() { + + String args[] = { "junittest" }; + final Map argMap = NsaCommandLineUtil.processCmdLine(args, true); + final String config = NsaCommandLineUtil.getSetting(argMap, "c", "collector.properties"); + final URL settingStream = DrumlinServlet.findStream(config, CommonStartup.class); + + final nvReadableStack settings = new nvReadableStack(); + try { + settings.push(new nvPropertiesFile(settingStream)); + } catch (loadException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + settings.push(new nvReadableTable(argMap)); + Assert.assertEquals("true", "true"); + } + + @Test + public void testhandleevent() { + JSONArray jsonArrayMod = new JSONArray().put(new JSONObject(payload)); + try { + + + CommonStartup.handleEvents (jsonArrayMod); + } catch ( JSONException | QueueFullException | IOException e) { + // TODO Auto-generated catch block + //e.printStackTrace(); + System.out.println("junit reported:" + e.getMessage()); + } + Assert.assertEquals("true", "true"); + } + + +/* + @Test + public void testServlet() { + try + { + RestfulCollectorServlet rsv = new RestfulCollectorServlet(null); + } + catch (NullPointerException|loadException| missingReqdSetting e){ + System.out.println("junit reported:" + e.getMessage()); + } + Assert.assertEquals("true", "true"); + } +*/ + + + + @Test + public void testEventProcessorinstantiation() + { + CommonStartup.streamid="fault=sec_fault|syslog=sec_syslog|heartbeat=sec_heartbeat|measurementsForVfScaling=sec_measurement|mobileFlow=sec_mobileflow|other=sec_other|stateChange=sec_statechange|thresholdCrossingAlert=sec_thresholdCrossingAlert|voiceQuality=ves_voicequality|sipSignaling=ves_sipsignaling"; + EventProcessor ep = new EventProcessor (); + Thread epThread=new Thread(ep); + epThread.start(); + Assert.assertEquals("true", "true"); + epThread.interrupt(); + + } + + @Test + public void testAuthListHandler() + { + + final Map argMap = NsaCommandLineUtil.processCmdLine ( new String[0], true ); + final String config = NsaCommandLineUtil.getSetting ( argMap, "c", "collector.properties" ); + final URL settingStream = DrumlinServlet.findStream ( config, CommonStartup.class ); + + final nvReadableStack settings = new nvReadableStack (); + try { + settings.push ( new nvPropertiesFile ( settingStream ) ); + settings.push ( new nvReadableTable ( argMap ) ); + } catch (loadException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + + + RestfulCollectorServlet rsv = null; + NsaAuthenticator NsaAuth = null; + Boolean flag = false; + try + { + rsv = new RestfulCollectorServlet(settings); + } + catch (NullPointerException|loadException| missingReqdSetting e){ + System.out.println("junit reported:" + e.getMessage()); + } + String authlist = "secureid,IWRjYWVSb2FkbTEyMyEt|sample1,c2FtcGxlMQ==|vdnsagg,dmRuc2FnZw=="; + NsaAuth = rsv.AuthlistHandler(authlist); + if (NsaAuth != null) + { + flag = true; + } + Assert.assertEquals(true, flag); + + + } +} + + diff --git a/src/test/java/org/onap/dcae/vestest/TestEventReceipt.java b/src/test/java/org/onap/dcae/vestest/TestEventReceipt.java index 668c718a..3af66fb0 100644 --- a/src/test/java/org/onap/dcae/vestest/TestEventReceipt.java +++ b/src/test/java/org/onap/dcae/vestest/TestEventReceipt.java @@ -34,6 +34,7 @@ import org.onap.dcae.commonFunction.CommonStartup; import org.onap.dcae.commonFunction.CommonStartup.QueueFullException; import org.onap.dcae.commonFunction.CustomExceptionLoader; import org.onap.dcae.restapi.endpoints.EventReceipt; +import org.onap.dcae.restapi.endpoints.Ui; import com.att.nsa.apiServer.endpoints.NsaBaseEndpoint; import com.att.nsa.drumlin.service.framework.context.DrumlinRequestContext; @@ -120,4 +121,18 @@ public class TestEventReceipt extends NsaBaseEndpoint { } assertEquals(true, true); } + + @Test + public void testUI() { + + try { + Ui.hello(null); + } + catch (Exception e) + { + //As context object is null, handling null pointer exception. + Log.debug("Response object creation failure"); + } + assertEquals(true, true); + } } diff --git a/src/test/java/org/onap/dcae/vestest/TestFetchConfig.java b/src/test/java/org/onap/dcae/vestest/TestFetchConfig.java index ed3e1086..2f18e820 100644 --- a/src/test/java/org/onap/dcae/vestest/TestFetchConfig.java +++ b/src/test/java/org/onap/dcae/vestest/TestFetchConfig.java @@ -1,90 +1,144 @@ -/*- - * ============LICENSE_START======================================================= - * PROJECT - * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed 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. - * ============LICENSE_END========================================================= - */ -package org.onap.dcae.vestest; - -import static org.junit.Assert.*; - -import static org.junit.Assert.assertEquals; - -import java.io.FileReader; -import java.io.IOException; -import org.json.JSONObject; -import com.google.gson.JsonParser; -import com.google.gson.JsonObject; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.onap.dcae.commonFunction.CommonStartup; -import org.onap.dcae.commonFunction.DmaapPropertyReader; -import org.onap.dcae.commonFunction.EventProcessor; -import org.onap.dcae.commonFunction.EventPublisherHash; -import org.onap.dcae.controller.FetchDynamicConfig; - - -public class TestFetchConfig { - - JSONObject jsonObject; - - @Before - public void setUp() throws Exception { - - - } - - @After - public void tearDown() throws Exception { - } - - - - @Test - public void testwritefile() { - Boolean flag = false; - - FetchDynamicConfig fc = new FetchDynamicConfig(); - FetchDynamicConfig.configFile = "src/test/resources/controller-config_formatted_op.json"; - - - - try{ - JsonParser parser = new JsonParser(); - FileReader fr = new FileReader ("src/test/resources/controller-config_singleline_ip.json" ); - final JsonObject jo = (JsonObject) parser.parse (fr); - final String jsonText = jo.toString (); - jsonObject = new JSONObject ( jsonText ); - fc.writefile(jsonObject.toString()); - } - catch(Exception e){ - System.out.println("Exception while opening the file"); - e.printStackTrace(); - } - if(jsonObject.has("streams_publishes")) - { - flag = true; - } - - assertEquals(true, flag); - - } - - -} - +/*- + * ============LICENSE_START======================================================= + * PROJECT + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed 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. + * ============LICENSE_END========================================================= + */ +package org.onap.dcae.vestest; + +import static org.junit.Assert.*; + +import static org.junit.Assert.assertEquals; + +import java.io.FileReader; +import java.io.IOException; +import java.util.Map; +import java.util.Properties; + +import org.json.JSONObject; +import com.google.gson.JsonParser; +import com.google.gson.JsonObject; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.dcae.commonFunction.CommonStartup; +import org.onap.dcae.commonFunction.DmaapPropertyReader; +import org.onap.dcae.commonFunction.EventProcessor; +import org.onap.dcae.commonFunction.EventPublisherHash; +import org.onap.dcae.controller.FetchDynamicConfig; + + +public class TestFetchConfig { + + JSONObject jsonObject; + + @Before + public void setUp() throws Exception { + + + System.setProperty("CONSUL_HOST", "localhost"); + System.setProperty("CONFIG_BINDING_SERVICE", "localhost"); + System.setProperty("HOSTNAME", "localhost"); + } + + @After + public void tearDown() throws Exception { + } + + + + @Test + public void testwritefile() { + Boolean flag = false; + + FetchDynamicConfig fc = new FetchDynamicConfig(); + //Used for writing the configuration output + FetchDynamicConfig.configFile = "src/test/resources/controller-config_formatted_op.json"; + + + + try{ + JsonParser parser = new JsonParser(); + FileReader fr = new FileReader ("src/test/resources/controller-config_singleline_ip.json" ); + final JsonObject jo = (JsonObject) parser.parse (fr); + final String jsonText = jo.toString (); + jsonObject = new JSONObject ( jsonText ); + + fc.writefile(jsonObject.toString()); + } + catch(Exception e){ + System.out.println("Exception while opening the file"); + e.printStackTrace(); + } + if(jsonObject.has("streams_publishes")) + { + flag = true; + } + + assertEquals(true, flag); + + } + + @Test + public void testgetConsul() { + Boolean flag = false; + + + try{ + + FetchDynamicConfig.getconsul(); + flag = true; + } + catch(Exception e){ + System.out.println("Exception while getting to consul"); + e.printStackTrace(); + } + assertEquals(true, flag); + + } + + @Test + public void testgetCBS() { + Boolean flag = false; + + try{ + + + JsonParser parser = new JsonParser(); + FileReader fr = new FileReader ("src/test/resources/controller-config_singleline_ip.json" ); + final JsonObject jo = (JsonObject) parser.parse (fr); + final String jsonText = jo.toString (); + jsonObject = new JSONObject ( jsonText ); + + //retstring from Consul is parsed + FetchDynamicConfig.retString = "[{\"ID\":\"81bc2a17-8cfa-3f6f-30a9-a545a9b6ac2f\",\"Node\":\"zldcrdm5bdcc2dokr00\",\"Address\":\"135.25.108.161\",\"Datacenter\":\"zldcrdm5bdcc2\",\"TaggedAddresses\":{\"lan\":\"135.25.108.161\",\"wan\":\"135.25.108.161\"},\"NodeMeta\":{\"fqdn\":\"zldcrdm5bdcc2dokr00.2f3fb3.rdm5b.tci.att.com\"},\"ServiceID\":\"20299a144716:config_binding_service:10000\",\"ServiceName\":\"config_binding_service\",\"ServiceTags\":[],\"ServiceAddress\":\"135.25.108.161\",\"ServicePort\":10000,\"ServiceEnableTagOverride\":false,\"CreateIndex\":9153156,\"ModifyIndex\":9153156}]"; + + //Mock the return CBS output + FetchDynamicConfig.retCBSString = jsonObject.toString(); + FetchDynamicConfig.getCBS(); + flag = true; + } + catch(Exception e){ + System.out.println("Exception while fetching cbs configuration"); + e.printStackTrace(); + } + assertEquals(true, flag); + + } + +} + -- cgit 1.2.3-korg