aboutsummaryrefslogtreecommitdiffstats
path: root/src/test
diff options
context:
space:
mode:
Diffstat (limited to 'src/test')
-rw-r--r--src/test/java/org/onap/dcae/commonFunction/TestCommonStartup.java129
-rw-r--r--src/test/java/org/onap/dcae/vestest/TestCommonStartup.java172
-rw-r--r--src/test/java/org/onap/dcae/vestest/TestFetchConfig.java188
-rw-r--r--src/test/java/org/onap/dcae/vestest/TestLoadDynamicConfig.java106
-rw-r--r--src/test/java/org/onap/dcae/vestest/TestingUtilities.java84
5 files changed, 304 insertions, 375 deletions
diff --git a/src/test/java/org/onap/dcae/commonFunction/TestCommonStartup.java b/src/test/java/org/onap/dcae/commonFunction/TestCommonStartup.java
new file mode 100644
index 00000000..18194864
--- /dev/null
+++ b/src/test/java/org/onap/dcae/commonFunction/TestCommonStartup.java
@@ -0,0 +1,129 @@
+/*-
+ * ============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.commonFunction;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.when;
+
+import com.att.nsa.cmdLine.NsaCommandLineUtil;
+import com.att.nsa.drumlin.service.framework.context.DrumlinRequest;
+import com.att.nsa.drumlin.till.nv.impl.nvReadableStack;
+import com.att.nsa.drumlin.till.nv.impl.nvReadableTable;
+import com.att.nsa.drumlin.till.nv.rrNvReadable.loadException;
+import com.att.nsa.drumlin.till.nv.rrNvReadable.missingReqdSetting;
+import com.att.nsa.security.authenticators.SimpleAuthenticator;
+import com.att.nsa.security.db.simple.NsaSimpleApiKey;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonParser;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.Base64;
+import java.util.Map;
+import java.util.concurrent.LinkedBlockingQueue;
+import org.json.JSONArray;
+import org.json.JSONObject;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.onap.dcae.commonFunction.CommonStartup.QueueFullException;
+import org.onap.dcae.restapi.RestfulCollectorServlet;
+
+
+public class TestCommonStartup {
+
+ @Test
+ public void testParseCLIArguments() {
+ // given
+ String args[] = {"-a", "aa"};
+ Map<String, String> argMap = NsaCommandLineUtil.processCmdLine(args, true);
+ // when
+ nvReadableStack settings = new nvReadableStack();
+ settings.push(new nvReadableTable(argMap));
+
+ // then
+ assertEquals(settings.getString("a", "default"), "aa");
+ }
+
+ @Test
+ public void shouldPutValidVESEventOnProcessingQueueWithoutExceptions() throws IOException, QueueFullException {
+ // given
+ CommonStartup.fProcessingInputQueue = new LinkedBlockingQueue<>(
+ CommonStartup.KDEFAULT_MAXQUEUEDEVENTS);
+ JsonElement vesEvent = new JsonParser().parse(new FileReader("src/test/resources/VES_valid.txt"));
+ JSONObject validVESEvent = new JSONObject(vesEvent.toString());
+ JSONArray jsonArrayMod = new JSONArray().put(validVESEvent);
+
+ // then
+ CommonStartup.handleEvents(jsonArrayMod);
+ }
+
+
+ @Test
+ public void testParseStreamIdToStreamHashMapping() {
+ // given
+ 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 eventProcessor = new EventProcessor();
+
+ // when
+ Map<String, String[]> streamHashMapping = EventProcessor.streamidHash;
+
+ // then
+ assertEquals(streamHashMapping.get("fault")[0], "sec_fault");
+ assertEquals(streamHashMapping.get("measurementsForVfScaling")[0], "sec_measurement");
+ }
+
+ @Test
+ public void testAuthListHandler() throws loadException, missingReqdSetting {
+ // given
+ final nvReadableStack settings = new nvReadableStack();
+
+ String user1 = "secureid";
+ String password1Hashed = "IWRjYWVSb2FkbTEyMyEt";
+ String password1UnHashed = decode("IWRjYWVSb2FkbTEyMyEt");
+ String user2 = "sample1";
+ String password2Hashed = "c2FtcGxlMQ";
+
+ String authlist = user1 + "," + password1Hashed + "|" + user2 + "," + password2Hashed;
+
+ RestfulCollectorServlet rsv = new RestfulCollectorServlet(settings);
+
+ DrumlinRequest drumlinRequestMock = Mockito.mock(DrumlinRequest.class);
+
+ String basicHeaderForUser1 = "Basic " + encode(user1, password1UnHashed);
+ when(drumlinRequestMock.getFirstHeader("Authorization")).thenReturn(basicHeaderForUser1);
+
+ // when
+ SimpleAuthenticator simpleAuthenticator = (SimpleAuthenticator) rsv.AuthlistHandler(authlist);
+ NsaSimpleApiKey authentic = simpleAuthenticator.isAuthentic(drumlinRequestMock);
+
+ // then
+ assertEquals(authentic.getSecret(), password1UnHashed);
+ }
+
+ private String decode(String hashedPassword) {
+ return new String(Base64.getDecoder().decode(hashedPassword.getBytes()));
+ }
+
+ private String encode(String user1, String password1UnHashed) {
+ return Base64.getEncoder().encodeToString((user1 + ":" + password1UnHashed).getBytes());
+ }
+
+}
+
+
diff --git a/src/test/java/org/onap/dcae/vestest/TestCommonStartup.java b/src/test/java/org/onap/dcae/vestest/TestCommonStartup.java
deleted file mode 100644
index aa56c611..00000000
--- a/src/test/java/org/onap/dcae/vestest/TestCommonStartup.java
+++ /dev/null
@@ -1,172 +0,0 @@
-/*-
- * ============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<JSONObject> (CommonStartup.KDEFAULT_MAXQUEUEDEVENTS);
- }
-
- @After
- public void tearDown() throws Exception {
-
- }
-
- @Test
- public void testCommonStartupload() {
-
- String args[] = { "junittest" };
- final Map<String, String> 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<String, String> 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<NsaSimpleApiKey> 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/TestFetchConfig.java b/src/test/java/org/onap/dcae/vestest/TestFetchConfig.java
index 32a2299e..0b6b5027 100644
--- a/src/test/java/org/onap/dcae/vestest/TestFetchConfig.java
+++ b/src/test/java/org/onap/dcae/vestest/TestFetchConfig.java
@@ -7,9 +7,9 @@
* 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.
@@ -19,149 +19,63 @@
*/
package org.onap.dcae.vestest;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertTrue;
+import static org.onap.dcae.vestest.TestingUtilities.createTemporaryFile;
-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 java.nio.file.Path;
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;
+import org.onap.dcae.controller.LoadDynamicConfig;
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);
-
- }
-
-
- @Test
- public void testverifyConfigChange() {
-
-
- Boolean ret= false;
-
- try{
- //File date to be compared
- FetchDynamicConfig.configFile = "src/test/resources/controller-config_singleline_ip.json";
- //Mock the return CBS output
- FetchDynamicConfig.retCBSString = "{\"header.authflag\": \"1\", \"collector.schema.file\": \"{\\\"v1\\\": \\\"./etc/CommonEventFormat_27.2.json\\\", \\\"v2\\\": \\\"./etc/CommonEventFormat_27.2.json\\\", \\\"v3\\\": \\\"./etc/CommonEventFormat_27.2.json\\\", \\\"v4\\\": \\\"./etc/CommonEventFormat_27.2.json\\\", \\\"v5\\\": \\\"./etc/CommonEventFormat_28.4.json\\\"}\", \"collector.keystore.passwordfile\": \"/opt/app/dcae-certificate/.password\", \"tomcat.maxthreads\": \"200\", \"collector.dmaap.streamid\": \"fault=ves-fault|syslog=ves-syslog|heartbeat=ves-heartbeat|measurementsForVfScaling=ves-measurement|mobileFlow=ves-mobileflow|other=ves-other|stateChange=ves-statechange|thresholdCrossingAlert=ves-thresholdCrossingAlert|voiceQuality=ves-voicequality|sipSignaling=ves-sipsignaling\", \"streams_subscribes\": {}, \"collector.inputQueue.maxPending\": \"8096\", \"collector.keystore.alias\": \"dynamically generated\", \"streams_publishes\": {\"ves-mobileflow\": {\"type\": \"message_router\", \"dmaap_info\": {\"client_id\": \"1517590629043\", \"client_role\": \"com.att.secCollector.member\", \"location\": \"rdm5bdcc2\", \"topic_url\": \"https://DMAAPHOST:3905/events/com.att.dcae.dmaap.FTL.24256-SEC-MOBILEFLOW-OUTPUT-v1\"}, \"aaf_username\": \"userid@namespace\", \"aaf_password\": \"authpwd\"}, \"ves-measurement\": {\"type\": \"message_router\", \"dmaap_info\": {\"client_id\": \"1517590433916\", \"client_role\": \"com.att.secCollector.member\", \"location\": \"rdm5bdcc2\", \"topic_url\": \"https://DMAAPHOST:3905/events/com.att.dcae.dmaap.FTL.24256-ENC-MEASUREMENT-OUTPUT-v1\"}, \"aaf_username\": \"userid@namespace\", \"aaf_password\": \"authpwd\"}, \"ves-voicequality\": {\"type\": \"message_router\", \"dmaap_info\": {\"client_id\": \"1517590778397\", \"client_role\": \"com.att.secCollector.member\", \"location\": \"rdm5bdcc2\", \"topic_url\": \"https://DMAAPHOST:3905/events/com.att.dcae.dmaap.FTL.24256-VES-VOICEQUALITY-OUTPUT-v1\"}, \"aaf_username\": \"userid@namespace\", \"aaf_password\": \"authpwd\"}, \"ves-thresholdCrossingAlert\": {\"type\": \"message_router\", \"dmaap_info\": {\"client_id\": \"1517590728150\", \"client_role\": \"com.att.secCollector.member\", \"location\": \"rdm5bdcc2\", \"topic_url\": \"https://DMAAPHOST:3905/events/com.att.dcae.dmaap.FTL.24256-SEC-TCA-OUTPUT-v1\"}, \"aaf_username\": \"userid@namespace\", \"aaf_password\": \"authpwd\"}, \"ves-fault\": {\"type\": \"message_router\", \"dmaap_info\": {\"client_id\": \"1517590384670\", \"client_role\": \"com.att.secCollector.member\", \"location\": \"rdm5bdcc2\", \"topic_url\": \"https://DMAAPHOST:3905/events/com.att.dcae.dmaap.FTL.24256-SEC-FAULT-OUTPUT-v1\"}, \"aaf_username\": \"userid@namespace\", \"aaf_password\": \"authpwd\"}, \"ves-heartbeat\": {\"type\": \"message_router\", \"dmaap_info\": {\"client_id\": \"1517590530041\", \"client_role\": \"com.att.secCollector.member\", \"location\": \"rdm5bdcc2\", \"topic_url\": \"https://DMAAPHOST:3905/events/com.att.dcae.dmaap.FTL.24256-SEC-HEARTBEAT-OUTPUT-v1\"}, \"aaf_username\": \"userid@namespace\", \"aaf_password\": \"authpwd\"}, \"ves-sipsignaling\": {\"type\": \"message_router\", \"dmaap_info\": {\"client_id\": \"1517590828736\", \"client_role\": \"com.att.secCollector.member\", \"location\": \"rdm5bdcc2\", \"topic_url\": \"https://DMAAPHOST:3905/events/com.att.dcae.dmaap.FTL.24256-VES-SIPSIGNALING-OUTPUT-v1\"}, \"aaf_username\": \"userid@namespace\", \"aaf_password\": \"authpwd\"}, \"ves-syslog\": {\"type\": \"message_router\", \"dmaap_info\": {\"client_id\": \"1517590482019\", \"client_role\": \"com.att.secCollector.member\", \"location\": \"rdm5bdcc2\", \"topic_url\": \"https://DMAAPHOST:3905/events/com.att.dcae.dmaap.FTL.24256-SEC-SYSLOG-OUTPUT-v1\"}, \"aaf_username\": \"userid@namespace\", \"aaf_password\": \"authpwd\"}, \"ves-other\": {\"type\": \"message_router\", \"dmaap_info\": {\"client_id\": \"1517590581045\", \"client_role\": \"com.att.secCollector.member\", \"location\": \"rdm5bdcc2\", \"topic_url\": \"https://DMAAPHOST:3905/events/com.att.dcae.dmaap.FTL.24256-SEC-OTHER-OUTPUT-v1\"}, \"aaf_username\": \"userid@namespace\", \"aaf_password\": \"authpwd\"}, \"ves-statechange\": {\"type\": \"message_router\", \"dmaap_info\": {\"client_id\": \"1517590677649\", \"client_role\": \"com.att.secCollector.member\", \"location\": \"rdm5bdcc2\", \"topic_url\": \"https://DMAAPHOST:3905/events/com.att.dcae.dmaap.FTL.24256-SEC-STATECHANGE-OUTPUT-v1\"}, \"aaf_username\": \"userid@namespace\", \"aaf_password\": \"authpwd\"}}, \"collector.schema.checkflag\": \"1\", \"services_calls\": {}, \"event.transform.flag\": \"1\", \"collector.keystore.file.location\": \"/opt/app/dcae-certificate/keystore.jks\", \"header.authlist\": \"sample1,c2FtcGxlMQ==|userid1,base64encodepwd1|userid2,base64encodepwd2\", \"collector.service.secure.port\": \"8443\", \"collector.service.port\": \"-1\"}";
- ret=FetchDynamicConfig.verifyConfigChange();
-
- }
- catch(Exception e){
- System.out.println("Exception on verifyConfigChange");
- //e.printStackTrace();
- }
- assertEquals(true, ret);
-
- }
-
+ private Path temporaryFile;
+
+ @Before
+ public void setUp() {
+ temporaryFile = createTemporaryFile();
+ }
+
+ @Test
+ public void shouldWriteFileAndAttachDMaaPStreamsPropertiesFromConfiguration() {
+ // given
+ FetchDynamicConfig loadDynamicConfig = new FetchDynamicConfig();
+ FetchDynamicConfig.configFile = temporaryFile.toString();
+ String sampleConfiguration = LoadDynamicConfig.readFile("src/test/resources/controller-config_singleline_ip.json");
+
+ // when
+ loadDynamicConfig.writefile(sampleConfiguration);
+
+ // then
+ JsonObject actuallyWrittenJSONContent = TestingUtilities.readJSONFromFile(temporaryFile);
+ assertTrue(actuallyWrittenJSONContent.has("streams_publishes"));
+ }
+
+ @Test
+ public void shouldThrowNoErrorsWhileParsingConsulResponse() {
+ // given
+ 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}]";
+
+ // then
+ FetchDynamicConfig.getCBS();
+ }
+
+
+ @Test
+ public void shouldReturnTrueOnConfigurationChange() {
+ // given
+ FetchDynamicConfig.configFile = "src/test/resources/controller-config_singleline_ip.json";
+ FetchDynamicConfig.retCBSString = "{\"header.authflag\": \"1\", \"collector.schema.file\": \"{\\\"v1\\\": \\\"./etc/CommonEventFormat_27.2.json\\\", \\\"v2\\\": \\\"./etc/CommonEventFormat_27.2.json\\\", \\\"v3\\\": \\\"./etc/CommonEventFormat_27.2.json\\\", \\\"v4\\\": \\\"./etc/CommonEventFormat_27.2.json\\\", \\\"v5\\\": \\\"./etc/CommonEventFormat_28.4.json\\\"}\", \"collector.keystore.passwordfile\": \"/opt/app/dcae-certificate/.password\", \"tomcat.maxthreads\": \"200\", \"collector.dmaap.streamid\": \"fault=ves-fault|syslog=ves-syslog|heartbeat=ves-heartbeat|measurementsForVfScaling=ves-measurement|mobileFlow=ves-mobileflow|other=ves-other|stateChange=ves-statechange|thresholdCrossingAlert=ves-thresholdCrossingAlert|voiceQuality=ves-voicequality|sipSignaling=ves-sipsignaling\", \"streams_subscribes\": {}, \"collector.inputQueue.maxPending\": \"8096\", \"collector.keystore.alias\": \"dynamically generated\", \"streams_publishes\": {\"ves-mobileflow\": {\"type\": \"message_router\", \"dmaap_info\": {\"client_id\": \"1517590629043\", \"client_role\": \"com.att.secCollector.member\", \"location\": \"rdm5bdcc2\", \"topic_url\": \"https://DMAAPHOST:3905/events/com.att.dcae.dmaap.FTL.24256-SEC-MOBILEFLOW-OUTPUT-v1\"}, \"aaf_username\": \"userid@namespace\", \"aaf_password\": \"authpwd\"}, \"ves-measurement\": {\"type\": \"message_router\", \"dmaap_info\": {\"client_id\": \"1517590433916\", \"client_role\": \"com.att.secCollector.member\", \"location\": \"rdm5bdcc2\", \"topic_url\": \"https://DMAAPHOST:3905/events/com.att.dcae.dmaap.FTL.24256-ENC-MEASUREMENT-OUTPUT-v1\"}, \"aaf_username\": \"userid@namespace\", \"aaf_password\": \"authpwd\"}, \"ves-voicequality\": {\"type\": \"message_router\", \"dmaap_info\": {\"client_id\": \"1517590778397\", \"client_role\": \"com.att.secCollector.member\", \"location\": \"rdm5bdcc2\", \"topic_url\": \"https://DMAAPHOST:3905/events/com.att.dcae.dmaap.FTL.24256-VES-VOICEQUALITY-OUTPUT-v1\"}, \"aaf_username\": \"userid@namespace\", \"aaf_password\": \"authpwd\"}, \"ves-thresholdCrossingAlert\": {\"type\": \"message_router\", \"dmaap_info\": {\"client_id\": \"1517590728150\", \"client_role\": \"com.att.secCollector.member\", \"location\": \"rdm5bdcc2\", \"topic_url\": \"https://DMAAPHOST:3905/events/com.att.dcae.dmaap.FTL.24256-SEC-TCA-OUTPUT-v1\"}, \"aaf_username\": \"userid@namespace\", \"aaf_password\": \"authpwd\"}, \"ves-fault\": {\"type\": \"message_router\", \"dmaap_info\": {\"client_id\": \"1517590384670\", \"client_role\": \"com.att.secCollector.member\", \"location\": \"rdm5bdcc2\", \"topic_url\": \"https://DMAAPHOST:3905/events/com.att.dcae.dmaap.FTL.24256-SEC-FAULT-OUTPUT-v1\"}, \"aaf_username\": \"userid@namespace\", \"aaf_password\": \"authpwd\"}, \"ves-heartbeat\": {\"type\": \"message_router\", \"dmaap_info\": {\"client_id\": \"1517590530041\", \"client_role\": \"com.att.secCollector.member\", \"location\": \"rdm5bdcc2\", \"topic_url\": \"https://DMAAPHOST:3905/events/com.att.dcae.dmaap.FTL.24256-SEC-HEARTBEAT-OUTPUT-v1\"}, \"aaf_username\": \"userid@namespace\", \"aaf_password\": \"authpwd\"}, \"ves-sipsignaling\": {\"type\": \"message_router\", \"dmaap_info\": {\"client_id\": \"1517590828736\", \"client_role\": \"com.att.secCollector.member\", \"location\": \"rdm5bdcc2\", \"topic_url\": \"https://DMAAPHOST:3905/events/com.att.dcae.dmaap.FTL.24256-VES-SIPSIGNALING-OUTPUT-v1\"}, \"aaf_username\": \"userid@namespace\", \"aaf_password\": \"authpwd\"}, \"ves-syslog\": {\"type\": \"message_router\", \"dmaap_info\": {\"client_id\": \"1517590482019\", \"client_role\": \"com.att.secCollector.member\", \"location\": \"rdm5bdcc2\", \"topic_url\": \"https://DMAAPHOST:3905/events/com.att.dcae.dmaap.FTL.24256-SEC-SYSLOG-OUTPUT-v1\"}, \"aaf_username\": \"userid@namespace\", \"aaf_password\": \"authpwd\"}, \"ves-other\": {\"type\": \"message_router\", \"dmaap_info\": {\"client_id\": \"1517590581045\", \"client_role\": \"com.att.secCollector.member\", \"location\": \"rdm5bdcc2\", \"topic_url\": \"https://DMAAPHOST:3905/events/com.att.dcae.dmaap.FTL.24256-SEC-OTHER-OUTPUT-v1\"}, \"aaf_username\": \"userid@namespace\", \"aaf_password\": \"authpwd\"}, \"ves-statechange\": {\"type\": \"message_router\", \"dmaap_info\": {\"client_id\": \"1517590677649\", \"client_role\": \"com.att.secCollector.member\", \"location\": \"rdm5bdcc2\", \"topic_url\": \"https://DMAAPHOST:3905/events/com.att.dcae.dmaap.FTL.24256-SEC-STATECHANGE-OUTPUT-v1\"}, \"aaf_username\": \"userid@namespace\", \"aaf_password\": \"authpwd\"}}, \"collector.schema.checkflag\": \"1\", \"services_calls\": {}, \"event.transform.flag\": \"1\", \"collector.keystore.file.location\": \"/opt/app/dcae-certificate/keystore.jks\", \"header.authlist\": \"sample1,c2FtcGxlMQ==|userid1,base64encodepwd1|userid2,base64encodepwd2\", \"collector.service.secure.port\": \"8443\", \"collector.service.port\": \"-1\"}";
+
+ // when
+ boolean didConfigsChange = FetchDynamicConfig.verifyConfigChange();
+
+ // then
+ assertTrue(didConfigsChange);
+ }
+
}
diff --git a/src/test/java/org/onap/dcae/vestest/TestLoadDynamicConfig.java b/src/test/java/org/onap/dcae/vestest/TestLoadDynamicConfig.java
index 765d9c61..ee0a3cba 100644
--- a/src/test/java/org/onap/dcae/vestest/TestLoadDynamicConfig.java
+++ b/src/test/java/org/onap/dcae/vestest/TestLoadDynamicConfig.java
@@ -7,9 +7,9 @@
* 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.
@@ -19,85 +19,59 @@
*/
package org.onap.dcae.vestest;
-import static org.junit.Assert.*;
-
-import java.io.FileReader;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.onap.dcae.vestest.TestingUtilities.correctQuotes;
+import static org.onap.dcae.vestest.TestingUtilities.createTemporaryFile;
+import com.github.fge.jackson.JsonLoader;
+import com.google.gson.JsonObject;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
import org.json.JSONObject;
-import org.junit.After;
-import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
-import org.onap.dcae.controller.FetchDynamicConfig;
import org.onap.dcae.controller.LoadDynamicConfig;
-import com.google.gson.JsonObject;
-import com.google.gson.JsonParser;
-
public class TestLoadDynamicConfig {
- LoadDynamicConfig lc;
- String propop = "src/test/resources/test_collector_ip_op.properties";
-
-
- @Before
- public void setUp() throws Exception {
-
- lc = new LoadDynamicConfig();
-
- }
-
- @After
- public void tearDown() throws Exception {
- }
+ private Path temporaryFile;
- @Test
- public void testLoad() {
+ @Before
+ public void setUp() {
+ temporaryFile = createTemporaryFile();
+ }
-
- Boolean flag=false;
-
+ @Test
+ public void shouldReadFileContent() throws IOException {
+ // given
+ String expectedJSON = correctQuotes("{ 'field' : 1 }");
+ Files.write(temporaryFile, expectedJSON.getBytes());
- lc.propFile = "src/test/resources/test_collector_ip_op.properties";
- lc.configFile = "src/test/resources/controller-config_dmaap_ip.json";
-
- String data = LoadDynamicConfig.readFile(propop);
- assertEquals(data.isEmpty(), flag);
- }
+ // when
+ String readFileContent = LoadDynamicConfig.readFile(temporaryFile.toString());
+ // then
+ assertEquals(JsonLoader.fromString(expectedJSON), JsonLoader.fromString(readFileContent));
+ }
- @Test
- public void testwrite() {
+ @Test
+ public void shouldWriteFileAndAttachDMaaPRelatedPropertiesFromConfiguration() {
+ // given
+ LoadDynamicConfig loadDynamicConfig = new LoadDynamicConfig();
+ loadDynamicConfig.propFile = "src/test/resources/test_collector_ip_op.properties";
+ loadDynamicConfig.configFile = "src/test/resources/controller-config_dmaap_ip.json";
+ loadDynamicConfig.dmaapoutputfile = temporaryFile.toString();
+ String sampleConfiguration = LoadDynamicConfig.readFile(loadDynamicConfig.configFile);
-
- Boolean flag=false;
-
- lc.propFile = "src/test/resources/test_collector_ip_op.properties";
- lc.configFile = "src/test/resources/controller-config_dmaap_ip.json";
- lc.dmaapoutputfile = "src/test/resources/DmaapConfig-op.json";
-
- String data = LoadDynamicConfig.readFile(lc.configFile);
- JSONObject jsonObject = new JSONObject(data);
- lc.writeconfig(jsonObject);
+ // when
+ loadDynamicConfig.writeconfig(new JSONObject(sampleConfiguration));
- try{
- JsonParser parser = new JsonParser();
- FileReader fr = new FileReader ( lc.dmaapoutputfile );
- final JsonObject jo = (JsonObject) parser.parse (fr);
- final String jsonText = jo.toString ();
- jsonObject = new JSONObject ( jsonText );
- }
- catch(Exception e){
- System.out.println("Exception while opening the file");
- e.printStackTrace();
- }
- if(jsonObject.has("ves-fault-secondary"))
- {
- flag = true;
- }
-
- assertEquals(true, flag);
+ // then
+ JsonObject actuallyWrittenJSONContent = TestingUtilities.readJSONFromFile(temporaryFile);
+ assertTrue(actuallyWrittenJSONContent.has("ves-fault-secondary"));
+ }
- }
}
diff --git a/src/test/java/org/onap/dcae/vestest/TestingUtilities.java b/src/test/java/org/onap/dcae/vestest/TestingUtilities.java
new file mode 100644
index 00000000..43e7a84e
--- /dev/null
+++ b/src/test/java/org/onap/dcae/vestest/TestingUtilities.java
@@ -0,0 +1,84 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * org.onap.dcaegen2.collectors.ves
+ * ================================================================================
+ * Copyright (C) 2018 Nokia. 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 java.nio.file.Files.readAllBytes;
+
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+/**
+ * @author Pawel Szalapski (pawel.szalapski@nokia.com)
+ */
+final class TestingUtilities {
+
+ private TestingUtilities() {
+ // utility class, no objects allowed
+ }
+
+ static String correctQuotes(String s) {
+ return s.replace("'", "\"");
+ }
+
+ static JsonObject readJSONFromFile(Path path) {
+ return rethrow(() -> (JsonObject) new JsonParser().parse(new String(readAllBytes(path))));
+ }
+
+ static Path createTemporaryFile() {
+ return rethrow(() -> {
+ Path temporaryDirectory = Files.createTempDirectory("temporaryDirectory");
+ Path temporaryFile = TestingUtilities.createFile(temporaryDirectory + "/testFile");
+ TestingUtilities.scheduleToBeDeletedAfterTests(temporaryDirectory);
+ TestingUtilities.scheduleToBeDeletedAfterTests(temporaryFile);
+ return temporaryFile;
+ });
+ }
+
+ private static Path createFile(String path) {
+ return rethrow(() -> Files.createFile(Paths.get(path)));
+ }
+
+ private static void scheduleToBeDeletedAfterTests(Path path) {
+ path.toFile().deleteOnExit();
+ }
+
+ /**
+ * Exception in test case usually means there is something wrong, it should never be catched, but rather thrown to
+ * be handled by JUnit framework.
+ */
+ private static <T> T rethrow(CheckedSupplier<T> supplier) {
+ try {
+ return supplier.get();
+ } catch (Exception e) {
+ throw new RuntimeException();
+ }
+ }
+
+ @FunctionalInterface
+ interface CheckedSupplier<T> {
+
+ T get() throws Exception;
+ }
+
+
+}