From 5a7cd9dde14a1ffb9a44e6bd5793e34e19d1d050 Mon Sep 17 00:00:00 2001 From: PawelSzalapski Date: Wed, 6 Jun 2018 12:26:55 +0200 Subject: Fix tests to not create files in repository While we run unittests, they create files directly inside the repository, those files are later proposed as changes to commit. Change-Id: Iffcb3820959f85099d3fe0ab9db1a198f8f60d78 Issue-ID: DCAEGEN2-524 Signed-off-by: PawelSzalapski --- .../org/onap/dcae/vestest/TestFetchConfig.java | 188 ++++++--------------- .../onap/dcae/vestest/TestLoadDynamicConfig.java | 106 +++++------- .../org/onap/dcae/vestest/TestingUtilities.java | 84 +++++++++ 3 files changed, 175 insertions(+), 203 deletions(-) create mode 100644 src/test/java/org/onap/dcae/vestest/TestingUtilities.java 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 rethrow(CheckedSupplier supplier) { + try { + return supplier.get(); + } catch (Exception e) { + throw new RuntimeException(); + } + } + + @FunctionalInterface + interface CheckedSupplier { + + T get() throws Exception; + } + + +} -- cgit 1.2.3-korg