diff options
author | Mukesh Paliwal <mukesh.paliwal1@huawei.com> | 2022-05-23 22:16:26 +0530 |
---|---|---|
committer | Mukesh Paliwal <mukesh.paliwal1@huawei.com> | 2022-06-07 19:09:25 +0530 |
commit | 670e0a9590465273814bda61cf8391a902cecd82 (patch) | |
tree | 5beabc1f465581b56b275afe2c2d3377eefd1fce | |
parent | dfbae36928e95b9232cb879d880ac6e3e3e01520 (diff) |
CodeCoverage improvement for dcaegen2-collectors-restconf
Issue-ID: DCAEGEN2-3166
Signed-off-by: Mukesh Paliwal <mukesh.paliwal1@huawei.com>
Change-Id: Ie75cfd20d8b8343c3074142a7464ea3ecf57f6b6
12 files changed, 723 insertions, 7 deletions
diff --git a/src/test/java/org/onap/dcae/RestConfCollectorTest.java b/src/test/java/org/onap/dcae/RestConfCollectorTest.java new file mode 100644 index 0000000..6d287ff --- /dev/null +++ b/src/test/java/org/onap/dcae/RestConfCollectorTest.java @@ -0,0 +1,37 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dcaegen2.restconfcollector + * ================================================================================ + * Copyright (C) 2022 Huawei. 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; + +import org.junit.Test; +import org.onap.dcae.common.EventData; +import java.util.Map; +import java.util.concurrent.LinkedBlockingQueue; + +import static org.junit.Assert.*; + +public class RestConfCollectorTest { + + @Test + public void parseStreamIdToStreamHashMappingTest () throws Exception { + RestConfCollector restConfCollector = new RestConfCollector(); + Map<String, String[]> sMap = restConfCollector.parseStreamIdToStreamHashMapping("streamId=c2ltcGxlcGFzc3dvcmQNCg=="); + assertEquals("c2ltcGxlcGFzc3dvcmQNCg==", sMap.get("streamId")[0]); + } +} diff --git a/src/test/java/org/onap/dcae/common/XmlJsonUtilTest.java b/src/test/java/org/onap/dcae/common/XmlJsonUtilTest.java index d34e508..90054b0 100644 --- a/src/test/java/org/onap/dcae/common/XmlJsonUtilTest.java +++ b/src/test/java/org/onap/dcae/common/XmlJsonUtilTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * org.onap.dcaegen2.restconfcollector * ================================================================================ - * Copyright (C) 2018-2019 Huawei. All rights reserved. + * Copyright (C) 2018-2022 Huawei. 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. @@ -55,16 +55,95 @@ public class XmlJsonUtilTest { } } + + @Test + public void getJsonTest() { + String var = "'result'"; + Map<String, String> mm = new HashMap<>(); + + + mm.put("result.time", "2018 12:04"); + mm.put("result.status", "200"); + mm.put("result.output", "xml2json"); + mm.put("result.[", "start"); + mm.put("result.]", "end"); + mm.put("result.list", "<LIST>\n" + + " <LITERAL VALUE=\"\"/>\n" + + " </LIST>"); + + try { + String str = XmlJsonUtil.getXml(mm, var); + assertEquals(str.startsWith("<"), true); + String str2 = XmlJsonUtil.getJson(mm, var); + assertEquals(str2.startsWith("{"), true); + } catch (Exception e) { + System.out.println("Exception " + e); + } + } + + @Test + public void getCreateStructureWithLengthTest() { + String var = "\"result_length"; + Map<String, String> mm = new HashMap<>(); + + + mm.put("result.time", "2018 12:04"); + mm.put("result.status", "200"); + mm.put("result.output", "xml2json"); + mm.put("result.[", "start"); + mm.put("result.]", "end"); + mm.put("result.list", "<LIST>\n" + + " <LITERAL VALUE=\"\"/>\n" + + " </LIST>"); + + try { + String str = XmlJsonUtil.getXml(mm, var); + assertEquals(str.startsWith("<"), true); + String str2 = XmlJsonUtil.getJson(mm, var); + assertEquals(str2.startsWith("{"), true); + } catch (Exception e) { + System.out.println("Exception " + e); + } + } + + @Test + public void getXmlTest() { + String var = "'result'"; + Map<String, String> mm = new HashMap<>(); + + + mm.put("result.time", "2018 12:04"); + mm.put("result.status", "200"); + mm.put("result.output", "xml2json"); + mm.put("result.[", "start"); + mm.put("result.]", "end"); + mm.put("result.list", "<LIST>\n" + + " <LITERAL VALUE=\"\"/>\n" + + " </LIST>"); + + try { + String str = XmlJsonUtil.getXml(mm, var); + assertEquals(str.startsWith("<"), true); + String str2 = XmlJsonUtil.getJson(mm, var); + assertEquals(str2.startsWith("{"), true); + } catch (Exception e) { + System.out.println("Exception " + e); + } + } + @Test public void removeEmptystructFromXml() { String var = "<time>2018 12:04</time>\n" + "<output>t2</output>\n" - + "<start>bad\n" + + "<start></start>\n" + + "<status>200</status>"; + + String verify = "<time>2018 12:04</time>\n" + + "<output>t2</output>\n" + "<status>200</status>"; - Map<String, String> mm = new HashMap<>(); try { String str = XmlJsonUtil.removeEmptyStructXml(var); - + assertEquals(verify, str); } catch (Exception e) { System.out.println("Exception " + e); } @@ -77,6 +156,7 @@ public class XmlJsonUtilTest { try { String str = XmlJsonUtil.removeEmptyStructJson(var); + assertEquals("", str); } catch (Exception e) { System.out.println("Exception " + e); } @@ -85,8 +165,15 @@ public class XmlJsonUtilTest { @Test public void removeLastCommaJson() { String var2 = "{\"name\":\"john\",\"age\":22,\"class\":\"mca\", \"data\":{}, \"arr\" : [\"some\" : {},],}"; + String var3 = "{\"name\":\"john\",\"age\":22,\"class\":\"mca\"}"; + String var4 = "{\"name\":\"john\",\"age\":22,\"class\":\"mca\", \"data\":{},}"; try { - String str = XmlJsonUtil.removeLastCommaJson(var2); + String str2 = XmlJsonUtil.removeLastCommaJson(var2); + assertEquals("{\"name\":\"john\",\"age\":22,\"class\":\"mca\", \"data\":{}, \"arr\" : [\"some\" : {}]}", str2); + String str3 = XmlJsonUtil.removeLastCommaJson(var3); + assertEquals("{\"name\":\"john\",\"age\":22,\"class\":\"mca\"}", str3); + String str4 = XmlJsonUtil.removeLastCommaJson(var4); + assertEquals("{\"name\":\"john\",\"age\":22,\"class\":\"mca\", \"data\":{}}", str4); } catch (Exception e) { System.out.println("Exception " + e); } @@ -109,5 +196,4 @@ public class XmlJsonUtilTest { System.out.println("Exception " + e); } } - -}
\ No newline at end of file +} diff --git a/src/test/java/org/onap/dcae/common/publishing/DMaaPEventPublisherTest.java b/src/test/java/org/onap/dcae/common/publishing/DMaaPEventPublisherTest.java new file mode 100644 index 0000000..4ea6482 --- /dev/null +++ b/src/test/java/org/onap/dcae/common/publishing/DMaaPEventPublisherTest.java @@ -0,0 +1,75 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dcaegen2.restconfcollector + * ================================================================================ + * Copyright (C) 2022 Huawei. 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.common.publishing; + +import io.vavr.API; +import io.vavr.collection.HashMap; +import io.vavr.collection.List; +import io.vavr.collection.Map; +import org.json.JSONObject; +import org.junit.Before; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.util.concurrent.atomic.AtomicReference; + +import static org.junit.Assert.assertEquals; + + +public class DMaaPEventPublisherTest { + + private Map<String, PublisherConfig> dmaapconfigs; + /** + * Setup before test. + */ + @Before + public void setUp() { + dmaapconfigs = API.Map("sampleStream1", new PublisherConfig(API.List("destination1"), "topic1")); + } + + @Test + public void reconfigureEventTest () throws Exception { + List<String> destination = List.of("test"); + PublisherConfig publisherConfig = new PublisherConfig(destination, "Test", "test", + "test123"); + Map<String, PublisherConfig> publisherCache = HashMap.of("tester", publisherConfig); + publisherCache.put("tester", publisherConfig); + DMaaPPublishersCache dmaapPublishersCache = new DMaaPPublishersCache(dmaapconfigs); + DMaaPEventPublisher dMaaPEventPublisher = new DMaaPEventPublisher(publisherCache); + dMaaPEventPublisher.reconfigure(publisherCache); + assertEquals("Test", publisherCache.get()._2.topic()); + } + + @Test + public void sendEventTest () throws Exception { + List<String> destination = List.of("test"); + PublisherConfig publisherConfig = new PublisherConfig(destination, "Test", "test", + "test123"); + DMaaPPublishersCache.CambriaPublishersCacheLoader publishersCache = + Mockito.mock(DMaaPPublishersCache.CambriaPublishersCacheLoader.class); + Map<String, PublisherConfig> publisherCache = HashMap.of("test", publisherConfig); + publisherCache.put("test", publisherConfig); + DMaaPEventPublisher dMaaPEventPublisher = new DMaaPEventPublisher(publisherCache); + JSONObject jo = new JSONObject(); + jo.put("test", "message"); + dMaaPEventPublisher.sendEvent(jo,"test"); + assertEquals("test", publisherCache.get()._1); + } +} diff --git a/src/test/java/org/onap/dcae/common/publishing/MessageRouterHttpStatusMapperTest.java b/src/test/java/org/onap/dcae/common/publishing/MessageRouterHttpStatusMapperTest.java index 75f3998..12f0479 100644 --- a/src/test/java/org/onap/dcae/common/publishing/MessageRouterHttpStatusMapperTest.java +++ b/src/test/java/org/onap/dcae/common/publishing/MessageRouterHttpStatusMapperTest.java @@ -20,10 +20,13 @@ package org.onap.dcae.common.publishing; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterPublishResponse; import org.springframework.http.HttpStatus; import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.onap.dcae.ApplicationSettings.responseCompatibility; @@ -61,4 +64,21 @@ class MessageRouterHttpStatusMapperTest { //then assertSame(HttpStatus.OK, httpStatusResponse); } + + @ParameterizedTest + @EnumSource( + value = HttpStatus.class, + names = {"NOT_FOUND", "REQUEST_TIMEOUT", "TOO_MANY_REQUESTS", "INTERNAL_SERVER_ERROR", "BAD_GATEWAY", + "SERVICE_UNAVAILABLE", "GATEWAY_TIMEOUT"} + ) + void shouldMapErrorsTo503WhenBackwardsCompatibilityIsNone(HttpStatus httpStatus) { + //given + responseCompatibility = BACKWARDS_COMPATIBILITY_NONE; + MessageRouterPublishResponse messageRouterPublishResponse = mock(MessageRouterPublishResponse.class); + when(messageRouterPublishResponse.failReason()).thenReturn(httpStatus.toString()); + + //when + //then + assertThrows(RuntimeException.class,()->getHttpStatus(messageRouterPublishResponse)); + } } diff --git a/src/test/java/org/onap/dcae/common/publishing/PublisherConfigTest.java b/src/test/java/org/onap/dcae/common/publishing/PublisherConfigTest.java new file mode 100644 index 0000000..56f362d --- /dev/null +++ b/src/test/java/org/onap/dcae/common/publishing/PublisherConfigTest.java @@ -0,0 +1,106 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dcaegen2.restconfcollector + * ================================================================================ + * Copyright (C) 2022 Huawei. 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.common.publishing; + +import io.vavr.collection.List; +import io.vavr.control.Option; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class PublisherConfigTest { + List<String> destination = List.of("test"); + @Test + public void isSecuredTest () { + destination.append("test"); + PublisherConfig publisherConfig = new PublisherConfig(destination, "Test", "test", + "test123"); + boolean isSecure = publisherConfig.isSecured(); + assertEquals(true, isSecure); + } + + @Test + public void hashCodeTest () { + PublisherConfig publisherConfig = new PublisherConfig(destination, "Test", "test", + "test123"); + int x = publisherConfig.hashCode(); + int y = publisherConfig.hashCode(); + assertTrue(x == y); + } + + @Test + public void topicTest () { + PublisherConfig publisherConfig = new PublisherConfig(destination, "topic"); + String topic = publisherConfig.topic(); + assertEquals("topic", topic); + } + + @Test + public void usernameTest () { + PublisherConfig publisherConfig = new PublisherConfig(destination, "Test", "test", + "test123"); + Option<String> username = publisherConfig.userName(); + assertEquals("test", username.get()); + } + + @Test + public void passwordTest () { + PublisherConfig publisherConfig = new PublisherConfig(destination, "Test", "test", + "test123"); + Option<String> password = publisherConfig.password(); + assertEquals("test123", password.get()); + } + + @Test + public void destinationTest () { + PublisherConfig publisherConfig = new PublisherConfig(destination, "Test", "test", + "test123"); + List<String> dest = publisherConfig.destinations(); + assertEquals(destination, dest); + } + + @Test + public void getHostNPortTest () { + PublisherConfig publisherConfig = new PublisherConfig(destination, "Test", "test", + "test123"); + String hostAndPort = publisherConfig.getHostAndPort(); + assertEquals("test", hostAndPort); + } + + @Test + public void isEqualTest () { + PublisherConfig publisherConfig = new PublisherConfig(destination, "topic", "test", + "test123"); + boolean isEqual1 = publisherConfig.equals(publisherConfig); + boolean isEqual2 = publisherConfig.equals(null); + assertEquals(true, isEqual1); + } + + @Test + public void toStringTest () { + PublisherConfig publisherConfig = new PublisherConfig(destination, "Test", "test", + "test123"); + String str = publisherConfig.toString(); + assertEquals("PublisherConfig{destinations=List(test), topic='Test', userName='test', " + + "password='test123'}", str); + } +} diff --git a/src/test/java/org/onap/dcae/configuration/ConfigFilesFacadeTest.java b/src/test/java/org/onap/dcae/configuration/ConfigFilesFacadeTest.java new file mode 100644 index 0000000..cf9fbac --- /dev/null +++ b/src/test/java/org/onap/dcae/configuration/ConfigFilesFacadeTest.java @@ -0,0 +1,83 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dcaegen2.restconfcollector + * ================================================================================ + * Copyright (C) 2022 Huawei. 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.configuration; + +import io.vavr.collection.HashMap; +import io.vavr.collection.Map; +import io.vavr.control.Try; +import org.json.JSONObject; +import org.junit.Test; + +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class ConfigFilesFacadeTest { + + protected static final Path RESOURCES = Paths.get("src", "test", "resources"); + protected static final Path DMAAP_FILE = Paths.get(RESOURCES.toString(), "testDmaapConfig_ip.json"); + protected static final Path CONFIG_FILE = Paths.get(RESOURCES.toString(), "testcollector.properties"); + + + @Test + public void setPathsTest () throws Exception { + ConfigFilesFacade configFilesFacade = new ConfigFilesFacade(CONFIG_FILE, DMAAP_FILE); + configFilesFacade.setPaths(CONFIG_FILE, DMAAP_FILE); + String sFileData = new String(Files.readAllBytes(CONFIG_FILE), StandardCharsets.UTF_8); + assertTrue(sFileData.contains("collector.rcc.service.port=9999")); + } + + @Test + public void readCollectorPropertiesTest () throws Exception { + ConfigFilesFacade configFilesFacade = new ConfigFilesFacade(CONFIG_FILE, DMAAP_FILE); + Try<Map<String, String>> sMap = configFilesFacade.readCollectorProperties(); + sMap.onSuccess(value -> assertEquals(value.get("collector.rcc.service.secure.port").get(), "8687")); + } + + @Test + public void readDMaaPConfigurationTest () throws Exception { + ConfigFilesFacade configFilesFacade = new ConfigFilesFacade(CONFIG_FILE, DMAAP_FILE); + Try<JSONObject> job = configFilesFacade.readDMaaPConfiguration(); + job.onSuccess(value -> assertEquals("test1", value.get("config1"))); + } + + @Test + public void writeDMaaPConfigurationTest () throws Exception { + ConfigFilesFacade configFilesFacade = new ConfigFilesFacade(CONFIG_FILE, DMAAP_FILE); + JSONObject jo = new JSONObject("{\"config1\":\"test1\"}"); + configFilesFacade.writeDMaaPConfiguration(jo); + Try<JSONObject> job = configFilesFacade.readDMaaPConfiguration(); + job.onSuccess(value -> assertEquals("test1", value.get("config1"))); + } + + @Test + public void writePropertiesTest () throws Exception { + ConfigFilesFacade configFilesFacade = new ConfigFilesFacade(CONFIG_FILE, DMAAP_FILE); + Map<String, String> jo = HashMap.of("config2", "test2"); + configFilesFacade.writeProperties(jo); + Try<Map<String, String>> sMap = configFilesFacade.readCollectorProperties(); + sMap.onSuccess(value -> assertEquals("test2", value.get("config2").get())); + } +} diff --git a/src/test/java/org/onap/dcae/configuration/ConfigUpdaterTest.java b/src/test/java/org/onap/dcae/configuration/ConfigUpdaterTest.java new file mode 100644 index 0000000..5b74889 --- /dev/null +++ b/src/test/java/org/onap/dcae/configuration/ConfigUpdaterTest.java @@ -0,0 +1,79 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dcaegen2.restconfcollector + * ================================================================================ + * Copyright (C) 2022 Huawei. 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.configuration; + +import io.vavr.collection.Map; +import io.vavr.control.Option; +import io.vavr.control.Try; +import org.json.JSONObject; +import org.junit.Test; +import org.onap.dcae.ApplicationSettings; + +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class ConfigUpdaterTest { + + protected static final Path RESOURCES = Paths.get("src", "test", "resources"); + protected static final Path DMAAP_FILE = Paths.get(RESOURCES.toString(), "testDmaapConfig_ip.json"); + protected static final Path CONFIG_FILE = Paths.get(RESOURCES.toString(), "testcollector.properties"); + + @Test + public void setPathTest() throws Exception { + + ConfigFilesFacade configFilesFacade = mock(ConfigFilesFacade.class); + Runnable runnable = mock(Runnable.class); + ApplicationSettings properties = mock(ApplicationSettings.class); + when(properties.configurationFileLocation()).thenReturn(CONFIG_FILE); + when(properties.dMaaPConfigurationFileLocation()).thenReturn(DMAAP_FILE.toString()); + ConfigUpdater configUpdater = new ConfigUpdater(configFilesFacade, runnable); + configUpdater.setPaths(properties.configurationFileLocation() , + Paths.get(properties.dMaaPConfigurationFileLocation())); + String sFileData = new String(Files.readAllBytes(CONFIG_FILE), StandardCharsets.UTF_8); + assertTrue(sFileData.contains("collector.rcc.service.port=9999")); + } + + @Test + public void updateConfigTest() throws Exception { + try { + JSONObject jo = new JSONObject("{\"collector.dmaapfile\":\"test\"}"); + ConfigFilesFacade configFilesFacade = new ConfigFilesFacade(CONFIG_FILE, DMAAP_FILE); + Runnable runnable = mock(Runnable.class); + ConfigUpdater configUpdater = new ConfigUpdater(configFilesFacade, runnable); + configUpdater.updateConfig(Option.of(jo)); + Try<Map<String, String>> sMap = configFilesFacade.readCollectorProperties(); + sMap.onSuccess(value -> assertEquals(value.get("collector.rcc.service.secure.port").get(), "8687")); + } catch (Exception ex) { + + } + } + + + + +} diff --git a/src/test/java/org/onap/dcae/configuration/ConfigurationHandlerTest.java b/src/test/java/org/onap/dcae/configuration/ConfigurationHandlerTest.java new file mode 100644 index 0000000..544b43a --- /dev/null +++ b/src/test/java/org/onap/dcae/configuration/ConfigurationHandlerTest.java @@ -0,0 +1,76 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dcaegen2.restconfcollector + * ================================================================================ + * Copyright (C) 2022 Huawei. 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.configuration; + +import com.google.gson.JsonObject; +import io.vavr.collection.Map; +import io.vavr.control.Try; +import org.junit.Test; +import org.onap.dcae.configuration.cbs.CbsClientConfigurationProvider; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration; +import reactor.core.Disposable; + +import java.nio.file.Path; +import java.nio.file.Paths; +import java.time.Duration; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; + +public class ConfigurationHandlerTest { + + protected static final Path RESOURCES = Paths.get("src", "test", "resources"); + protected static final Path DMAAP_FILE = Paths.get(RESOURCES.toString(), "testDmaapConfig_ip.json"); + protected static final Path CONFIG_FILE = Paths.get(RESOURCES.toString(), "testcollector.properties"); + + @Test + public void startListenTest () throws Exception { + CbsClientConfigurationProvider cbsClientConfigurationProvider = mock(CbsClientConfigurationProvider.class); + ConfigUpdater configUpdater = mock(ConfigUpdater.class); + ConfigurationHandler configurationHandler = new ConfigurationHandler(cbsClientConfigurationProvider, + configUpdater); + Disposable disposable = configurationHandler.startListen(Duration.ofMinutes(4L)); + assertTrue(disposable.isDisposed()); + } + + @Test + public void handleConfigurationTest () throws Exception { + try { + CbsClientConfigurationProvider cbsClientConfigurationProvider = mock(CbsClientConfigurationProvider.class); + ConfigUpdater configUpdater = mock(ConfigUpdater.class); + configUpdater.setPaths(CONFIG_FILE, DMAAP_FILE); + CbsClientConfiguration cbsClientConfiguration = mock(CbsClientConfiguration.class); + ConfigurationHandler configurationHandler = new ConfigurationHandler(cbsClientConfigurationProvider, + configUpdater); + JsonObject jo = new JsonObject(); + JsonObject jo1 = new JsonObject(); + jo1.addProperty("config", "test"); + jo.add("config", jo1); + configurationHandler.handleConfiguration(jo); + ConfigFilesFacade configFilesFacade = new ConfigFilesFacade(CONFIG_FILE, DMAAP_FILE); + Try<Map<String, String>> sMap = configFilesFacade.readCollectorProperties(); + sMap.onSuccess(value -> assertEquals(value.get("config").get(), "test")); + } catch (Exception ex) { + + } + } +} diff --git a/src/test/java/org/onap/dcae/configuration/ConversionsTest.java b/src/test/java/org/onap/dcae/configuration/ConversionsTest.java new file mode 100644 index 0000000..7380129 --- /dev/null +++ b/src/test/java/org/onap/dcae/configuration/ConversionsTest.java @@ -0,0 +1,43 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dcaegen2.restconfcollector + * ================================================================================ + * Copyright (C) 2022 Huawei. 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.configuration; + +import io.vavr.control.Try; +import org.json.JSONObject; +import org.junit.Test; + +import java.util.List; + +import static org.junit.Assert.assertEquals; + +public class ConversionsTest { + @Test + public void toJSONTest () throws Exception { + Try<JSONObject> job = Conversions.toJson("{conf:test}"); + job.onSuccess(value -> assertEquals("test",value.get("conf"))); + } + + @Test + public void toListTest () throws Exception { + List<String> str = List.of("test"); + io.vavr.collection.List<String> lStr = Conversions.toList(str.iterator()); + assertEquals("test", lStr.get(0)); + } +} diff --git a/src/test/java/org/onap/dcae/configuration/cbs/CbsClientConfigurationProviderTest.java b/src/test/java/org/onap/dcae/configuration/cbs/CbsClientConfigurationProviderTest.java new file mode 100644 index 0000000..f00f7a4 --- /dev/null +++ b/src/test/java/org/onap/dcae/configuration/cbs/CbsClientConfigurationProviderTest.java @@ -0,0 +1,36 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dcaegen2.restconfcollector + * ================================================================================ + * Copyright (C) 2022 Huawei. 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.configuration.cbs; + +import org.junit.Test; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration; + +import static org.junit.Assert.assertEquals; + +public class CbsClientConfigurationProviderTest { + + @Test + public void CbsClientConfigurationTest () throws Exception { + CbsClientConfigurationProvider cbsClientConfigurationProvider = new CbsClientConfigurationProvider(); + CbsClientConfiguration cbsClientConfiguration = cbsClientConfigurationProvider.get(); + assertEquals("config-binding-service", cbsClientConfiguration.hostname()); + } +} diff --git a/src/test/java/org/onap/dcae/restapi/RccRestControllerTest.java b/src/test/java/org/onap/dcae/restapi/RccRestControllerTest.java new file mode 100644 index 0000000..2804fbb --- /dev/null +++ b/src/test/java/org/onap/dcae/restapi/RccRestControllerTest.java @@ -0,0 +1,40 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dcaegen2.restconfcollector + * ================================================================================ + * Copyright (C) 2022 Huawei. 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.restapi; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class RccRestControllerTest { + @Test + public void mainPageTest () throws Exception { + RccRestController rccRestController = new RccRestController(); + String msg = rccRestController.mainPage(); + assertEquals("Welcome to RestConfCollector", msg); + } + + @Test + public void healthyTest () throws Exception { + RccRestController rccRestController = new RccRestController(); + String msg = rccRestController.healthy(); + assertEquals("hello", msg); + } +} diff --git a/src/test/java/org/onap/dcae/restapi/WebMvcConfigTest.java b/src/test/java/org/onap/dcae/restapi/WebMvcConfigTest.java new file mode 100644 index 0000000..3bc6f56 --- /dev/null +++ b/src/test/java/org/onap/dcae/restapi/WebMvcConfigTest.java @@ -0,0 +1,35 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dcaegen2.restconfcollector + * ================================================================================ + * Copyright (C) 2022 Huawei. 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.restapi; + +import org.junit.Test; +import org.springframework.web.servlet.view.InternalResourceViewResolver; + +import static org.junit.Assert.assertNull; + +public class WebMvcConfigTest { + + @Test + public void jspViewResolverTest () throws Exception { + WebMvcConfig webMvcConfig = new WebMvcConfig(); + InternalResourceViewResolver internalResourceViewResolver = webMvcConfig.jspViewResolver(); + assertNull(internalResourceViewResolver.getRedirectHosts()); + } +} |