From be11dee889f5a740d584458b62804e5fd4296e53 Mon Sep 17 00:00:00 2001 From: s00370346 Date: Thu, 14 Mar 2019 15:06:40 +0530 Subject: Issue-ID: DCAEGEN2-1055 Generic RestConfCollector Change-Id: I1800affa2b34cbb7487c0d8411e078adec5a0c48 Signed-off-by: s00370346 --- .../org/onap/dcae/common/ApiExceptionTest.java | 54 +++++++++ .../java/org/onap/dcae/common/AuthTypeTest.java | 42 +++++++ .../dcae/common/DataChangeEventListnerTest.java | 69 +++++++++++ .../onap/dcae/common/EventConnectionStateTest.java | 40 +++++++ .../org/onap/dcae/common/EventProcessorTest.java | 104 +++++++++++++++++ src/test/java/org/onap/dcae/common/FormatTest.java | 40 +++++++ .../org/onap/dcae/common/RestConfContextTest.java | 52 +++++++++ .../java/org/onap/dcae/common/XmlJsonUtilTest.java | 81 +++++++++++++ .../java/org/onap/dcae/common/XmlParserTest.java | 46 ++++++++ .../common/publishing/DMaaPEventPublisherTest.java | 79 +++++++++++++ .../publishing/DMaaPPublishersCacheTest.java | 127 +++++++++++++++++++++ .../dcae/common/publishing/JsonParserTest.java | 38 ++++++ 12 files changed, 772 insertions(+) create mode 100644 src/test/java/org/onap/dcae/common/ApiExceptionTest.java create mode 100644 src/test/java/org/onap/dcae/common/AuthTypeTest.java create mode 100644 src/test/java/org/onap/dcae/common/DataChangeEventListnerTest.java create mode 100644 src/test/java/org/onap/dcae/common/EventConnectionStateTest.java create mode 100644 src/test/java/org/onap/dcae/common/EventProcessorTest.java create mode 100644 src/test/java/org/onap/dcae/common/FormatTest.java create mode 100644 src/test/java/org/onap/dcae/common/RestConfContextTest.java create mode 100644 src/test/java/org/onap/dcae/common/XmlJsonUtilTest.java create mode 100644 src/test/java/org/onap/dcae/common/XmlParserTest.java create mode 100644 src/test/java/org/onap/dcae/common/publishing/DMaaPEventPublisherTest.java create mode 100644 src/test/java/org/onap/dcae/common/publishing/DMaaPPublishersCacheTest.java create mode 100644 src/test/java/org/onap/dcae/common/publishing/JsonParserTest.java (limited to 'src/test/java/org/onap/dcae/common') diff --git a/src/test/java/org/onap/dcae/common/ApiExceptionTest.java b/src/test/java/org/onap/dcae/common/ApiExceptionTest.java new file mode 100644 index 0000000..58cf9e4 --- /dev/null +++ b/src/test/java/org/onap/dcae/common/ApiExceptionTest.java @@ -0,0 +1,54 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dcaegen2.restconfcollector + * ================================================================================ + * Copyright (C) 2018 Nokia. All rights reserved. + * Copyright (C) 2018-2019 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; + +import static org.junit.Assert.assertEquals; + +import org.json.JSONObject; +import org.junit.Test; +import org.onap.dcae.restapi.ApiException; +import org.onap.dcae.restapi.ApiException.ExceptionType; + +public class ApiExceptionTest { + + + @Test + public void shouldStringifyPolicyExceptionTypeAccordingToSpecification() { + assertEquals(ExceptionType.POLICY_EXCEPTION.toString(), "PolicyException"); + } + + @Test + public void shouldConvertExceptionToBackwardCompatibleFormat() { + JSONObject responseBody = ApiException.UNAUTHORIZED_USER.toJSON(); + assertEquals(responseBody.toString(), new JSONObject("" + + "{ " + + " 'requestError': { " + + " 'PolicyException': { " + + " 'messageId': 'POL2000', " + + " 'text': 'Unauthorized user' " + + " } " + + " } " + + "} " + .replace("'", "\"") + ).toString()); + } +} diff --git a/src/test/java/org/onap/dcae/common/AuthTypeTest.java b/src/test/java/org/onap/dcae/common/AuthTypeTest.java new file mode 100644 index 0000000..a8a8729 --- /dev/null +++ b/src/test/java/org/onap/dcae/common/AuthTypeTest.java @@ -0,0 +1,42 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dcaegen2.restconfcollector + * ================================================================================ + * Copyright (C) 2018-2019 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; + +import org.junit.Test; +import org.onap.dcae.common.AuthType; + +import static org.junit.Assert.*; + +public class AuthTypeTest { + + @Test + public void fromString() { + assertEquals(AuthType.BASIC, AuthType.fromString("basic")); + assertEquals(AuthType.DIGEST, AuthType.fromString("digest")); + assertEquals(AuthType.OAUTH, AuthType.fromString("oauth")); + assertEquals(AuthType.NONE, AuthType.fromString("none")); + assertEquals(AuthType.Unspecified, AuthType.fromString("unspecified")); + } + + @Test(expected = IllegalArgumentException.class) + public void fromStringWithException() { + AuthType.fromString("test"); + } +} \ No newline at end of file diff --git a/src/test/java/org/onap/dcae/common/DataChangeEventListnerTest.java b/src/test/java/org/onap/dcae/common/DataChangeEventListnerTest.java new file mode 100644 index 0000000..95c8012 --- /dev/null +++ b/src/test/java/org/onap/dcae/common/DataChangeEventListnerTest.java @@ -0,0 +1,69 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dcaegen2.restconfcollector + * ================================================================================ + * Copyright (C) 2018-2019 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; + + +import org.glassfish.jersey.media.sse.EventInput; +import org.glassfish.jersey.media.sse.InboundEvent; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import static org.mockito.Mockito.*; + +import org.onap.dcae.RestConfCollector; +import org.onap.dcae.common.publishing.DMaaPConfigurationParser; +import org.onap.dcae.common.publishing.EventPublisher; + + +import org.slf4j.LoggerFactory; + +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.concurrent.LinkedBlockingQueue; + +@RunWith(MockitoJUnitRunner.class) +public class DataChangeEventListnerTest { + @Mock + InboundEvent event; + + @Test + public void testDataChangeEventListenerString() { + when(event.readData()).thenReturn(""); + DataChangeEventListener listner = new DataChangeEventListener(null); + listner.onEvent(event); + } + + @Test + public void testDataChangeEventListenerJsonObject() { + when(event.readData()).thenReturn("{\"name\":\"john\",\"age\":22,\"class\":\"mca\"}"); + RestConfCollector.fProcessingInputQueue = new LinkedBlockingQueue<>(4); + DataChangeEventListener listner = new DataChangeEventListener(null); + listner.onEvent(event); + } + @Test + public void testDataChangeEventListenerJsonArray() { + when(event.readData()).thenReturn("[{ \"name\":\"Ford\", \"models\":[ \"Fiesta\",\"Focus\", \"Mustang\" ] },{\"name\":\"BMW\", \"models\":[ \"320\", \"X3\",\"X5\" ] },{\"name\":\"Fiat\",\"models\":[ \"500\", \"Panda\" ]}]"); + RestConfCollector.fProcessingInputQueue = new LinkedBlockingQueue<>(4); + DataChangeEventListener listner = new DataChangeEventListener(null); + listner.onEvent(event); + } +} \ No newline at end of file diff --git a/src/test/java/org/onap/dcae/common/EventConnectionStateTest.java b/src/test/java/org/onap/dcae/common/EventConnectionStateTest.java new file mode 100644 index 0000000..c6327bc --- /dev/null +++ b/src/test/java/org/onap/dcae/common/EventConnectionStateTest.java @@ -0,0 +1,40 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dcaegen2.restconfcollector + * ================================================================================ + * Copyright (C) 2018-2019 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; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class EventConnectionStateTest { + + @Test + public void fromString() { + assertEquals(EventConnectionState.INIT, EventConnectionState.fromString("init")); + assertEquals(EventConnectionState.SUBSCRIBED, EventConnectionState.fromString("subscribed")); + assertEquals(EventConnectionState.UNSUBSCRIBED, EventConnectionState.fromString("unsubscribed")); + assertEquals(EventConnectionState.Unspecified, EventConnectionState.fromString("unspecified")); + } + + @Test(expected = IllegalArgumentException.class) + public void fromStringWithException() { + EventConnectionState.fromString("test"); + } +} \ No newline at end of file diff --git a/src/test/java/org/onap/dcae/common/EventProcessorTest.java b/src/test/java/org/onap/dcae/common/EventProcessorTest.java new file mode 100644 index 0000000..f32cc2e --- /dev/null +++ b/src/test/java/org/onap/dcae/common/EventProcessorTest.java @@ -0,0 +1,104 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dcaegen2.restconfcollector + * ================================================================================ + * Copyright (C) 2018-2019 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; + + + +import org.json.JSONObject; +import org.junit.Before; + + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.dcae.ApplicationSettings; +import org.onap.dcae.RestConfCollector; +import org.onap.dcae.common.publishing.DMaaPConfigurationParser; +import org.onap.dcae.common.publishing.EventPublisher; +import org.onap.dcae.common.publishing.PublisherConfig; +import org.onap.dcae.controller.AccessController; +import org.onap.dcae.controller.PersistentEventConnection; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.test.mock.mockito.MockBean; + + +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.concurrent.LinkedBlockingQueue; + + + +import static io.vavr.API.Map; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.when; +import static org.onap.dcae.common.publishing.DMaaPConfigurationParser.parseToDomainMapping; + +@RunWith(MockitoJUnitRunner.Silent.class) +public class EventProcessorTest { + + + + @Mock + private ApplicationSettings properties; + + private EventPublisher eventPublisher; + + Path path = Paths.get("src/test/resources/testParseDMaaPCredentialsLegacy.json"); + java.util.Map streamMap; + protected static final Path RESOURCES = Paths.get("src", "test", "resources"); + protected static final Path KEYSTORE = Paths.get(RESOURCES.toString(), "keystore"); + protected static final Path KEYSTORE_PASSWORD_FILE = Paths.get(RESOURCES.toString(), "passwordfile"); + protected static final Path TRUSTSTORE = Paths.get(RESOURCES.toString(), "truststore"); + protected static final Path TRUSTSTORE_PASSWORD_FILE = Paths.get(RESOURCES.toString(), "trustpasswordfile"); + protected static final Path RCC_KEYSTORE_PASSWORD_FILE = Paths.get(RESOURCES.toString(), "rcc_passwordfile"); + protected static final Path RCC_KEYSTORE = Paths.get(RESOURCES.toString(), "sdnc.p12"); + + @Before + public void setUp() { + eventPublisher = EventPublisher.createPublisher(LoggerFactory.getLogger("some_log"), DMaaPConfigurationParser. + parseToDomainMapping(path).get()); + streamMap = RestConfCollector.parseStreamIdToStreamHashMapping("notification=device-registration"); + + } + + @Test + public void testEventProcessorRunException() { + when(properties.truststoreFileLocation()).thenReturn(TRUSTSTORE.toString()); + when(properties.truststorePasswordFileLocation()).thenReturn(TRUSTSTORE_PASSWORD_FILE.toString()); + when(properties.keystoreFileLocation()).thenReturn(KEYSTORE.toString()); + when(properties.keystorePasswordFileLocation()).thenReturn(KEYSTORE_PASSWORD_FILE.toString()); + when(properties.rcc_keystoreFileLocation()).thenReturn(RCC_KEYSTORE.toString()); + when(properties.rcc_keystorePasswordFileLocation()).thenReturn(RCC_KEYSTORE_PASSWORD_FILE.toString()); + JSONObject controller = new JSONObject("{\"controller_name\":\"AccessM&C\",\"controller_restapiUrl\":\"10.118.191.43:26335\",\"controller_restapiUser\":\"access\",\"controller_restapiPassword\":\"Huawei@123\",\"controller_accessTokenUrl\":\"/rest/plat/smapp/v1/oauth/token\",\"controller_accessTokenFile\":\"./etc/access-token.json\",\"controller_accessTokenMethod\":\"put\",\"controller_subsMethod\":\"post\",\"controller_subscriptionUrl\":\"/restconf/v1/operations/huawei-nce-notification-action:establish-subscription\",\"event_details\":[{\"event_name\":\"ONT_registration\",\"event_description\":\"ONTregistartionevent\",\"event_sseventUrlEmbed\":\"true\",\"event_sseventsField\":\"output.url\",\"event_sseventsUrl\":\"null\",\"event_subscriptionTemplate\":\"./etc/ont_registartion_subscription_template.json\",\"event_unSubscriptionTemplate\":\"./etc/ont_registartion_unsubscription_template.json\",\"event_ruleId\":\"777777777\"}]}"); + AccessController acClr = new AccessController(controller, + properties); + + PersistentEventConnection p = new PersistentEventConnection("","",true, "", + "","","","1234646346", acClr); + RestConfCollector.fProcessingInputQueue = new LinkedBlockingQueue<>(4); + RestConfCollector.fProcessingInputQueue.offer(new EventData(p, new JSONObject("{}"))); + RestConfCollector.fProcessingInputQueue.offer(new EventData(null, null)); + EventProcessor ev = new EventProcessor(eventPublisher, streamMap); + ev.run(); + } +} \ No newline at end of file diff --git a/src/test/java/org/onap/dcae/common/FormatTest.java b/src/test/java/org/onap/dcae/common/FormatTest.java new file mode 100644 index 0000000..e54dd9b --- /dev/null +++ b/src/test/java/org/onap/dcae/common/FormatTest.java @@ -0,0 +1,40 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dcaegen2.restconfcollector + * ================================================================================ + * Copyright (C) 2018-2019 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; + +import org.junit.Test; +import org.onap.dcae.common.Format; + +import static org.junit.Assert.assertEquals; + +public class FormatTest { + + @Test + public void fromString() { + assertEquals(Format.JSON, Format.fromString("json")); + assertEquals(Format.XML, Format.fromString("xml")); + assertEquals(Format.NONE, Format.fromString("none")); + } + + @Test(expected = IllegalArgumentException.class) + public void fromStringWithException() { + Format.fromString("test"); + } +} \ No newline at end of file diff --git a/src/test/java/org/onap/dcae/common/RestConfContextTest.java b/src/test/java/org/onap/dcae/common/RestConfContextTest.java new file mode 100644 index 0000000..00b07f8 --- /dev/null +++ b/src/test/java/org/onap/dcae/common/RestConfContextTest.java @@ -0,0 +1,52 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dcaegen2.restconfcollector + * ================================================================================ + * Copyright (C) 2018-2019 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; + +import org.junit.Test; +import org.onap.dcae.common.RestConfContext; + +import static org.junit.Assert.*; + +public class RestConfContextTest { + + @Test + public void setAttribute() { + String key = "key"; + String value = "value"; + RestConfContext restConfContext = new RestConfContext(); + restConfContext.setAttribute(key, value); + assertEquals(value, restConfContext.getAttribute(key)); + restConfContext.setAttribute(key, null); + assertFalse(restConfContext.getAttributeKeySet().contains(key)); + } + + @Test + public void getAttributeKeySet() { + String key = "key"; + String value = "value"; + String key1 = "key1"; + String value1 = "value1"; + RestConfContext restConfContext = new RestConfContext(); + restConfContext.setAttribute(key,value); + restConfContext.setAttribute(key1,value1); + assertTrue(restConfContext.getAttributeKeySet().contains(key) && restConfContext.getAttributeKeySet().contains(key1)); + } +} \ No newline at end of file diff --git a/src/test/java/org/onap/dcae/common/XmlJsonUtilTest.java b/src/test/java/org/onap/dcae/common/XmlJsonUtilTest.java new file mode 100644 index 0000000..3cf065e --- /dev/null +++ b/src/test/java/org/onap/dcae/common/XmlJsonUtilTest.java @@ -0,0 +1,81 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dcaegen2.restconfcollector + * ================================================================================ + * Copyright (C) 2018-2019 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; + +import org.json.XML; +import org.junit.Test; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import static org.junit.Assert.assertEquals; + +public class XmlJsonUtilTest { + + @Test + public void getJsonOrXmlTest() { + String var = "result"; + Map 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"); + try { + String str = XmlJsonUtil.getXml(mm, var); + assertEquals(str.startsWith("<"), true); + String str2 = XmlJsonUtil.getJson(mm, var); + assertEquals(str2.startsWith("{"), true); + }catch (Exception e) {} + } + + @Test + public void removeEmptystructFromXml() { + String var = "\n" + + "t2\n" + + "bad\n" + + "200"; + Map mm = new HashMap<>(); + try { + String str = XmlJsonUtil.removeEmptyStructXml(var); + + }catch (Exception e) {} + } + + @Test + public void removeEmptystructFromJson() { + String var = "{\n" + + "\t\"output\": \"xml2json\",\n" + + "\t\"time\": \"2018 12:04\",\n" + + "\t\"\": \"end\",\n" + + "\t\"status\": \"200\"\n" + + "}"; + Map mm = new HashMap<>(); + + try { + String str = XmlJsonUtil.removeEmptyStructJson(var); + }catch (Exception e) {} + } +} \ No newline at end of file diff --git a/src/test/java/org/onap/dcae/common/XmlParserTest.java b/src/test/java/org/onap/dcae/common/XmlParserTest.java new file mode 100644 index 0000000..b005c89 --- /dev/null +++ b/src/test/java/org/onap/dcae/common/XmlParserTest.java @@ -0,0 +1,46 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dcaegen2.restconfcollector + * ================================================================================ + * Copyright (C) 2018-2019 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; + +import org.junit.Test; + +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import static org.junit.Assert.*; + +public class XmlParserTest { + + @Test + public void setAttribute() { + String convert = "\n" + + "t2\n" + + "200"; + Set listNameList = new HashSet<>(); + listNameList.add("result"); + Map propMap; + try { + propMap = XmlParser.convertToProperties(convert, listNameList); + System.out.println(propMap); + }catch (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..2865b75 --- /dev/null +++ b/src/test/java/org/onap/dcae/common/publishing/DMaaPEventPublisherTest.java @@ -0,0 +1,79 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dcaegen2.restconfcollector + * ================================================================================ + * Copyright (C) 2018 Nokia. All rights reserved. + * Copyright (C) 2018-2019 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 static io.vavr.API.Option; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.att.nsa.cambria.client.CambriaBatchingPublisher; +import java.io.IOException; +import org.json.JSONObject; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; + +public class DMaaPEventPublisherTest { + + private static final String STREAM_ID = "sampleStreamId"; + + private DMaaPEventPublisher eventPublisher; + private CambriaBatchingPublisher cambriaPublisher; + private DMaaPPublishersCache DMaaPPublishersCache; + + @Before + public void setUp() { + cambriaPublisher = mock(CambriaBatchingPublisher.class); + DMaaPPublishersCache = mock(DMaaPPublishersCache.class); + when(DMaaPPublishersCache.getPublisher(anyString())).thenReturn(Option(cambriaPublisher)); + eventPublisher = new DMaaPEventPublisher(DMaaPPublishersCache, mock(Logger.class)); + } + + @Test + public void shouldSendEventToTopic() throws Exception { + // given + JSONObject event = new JSONObject("{}"); + + // when + eventPublisher.sendEvent(event, STREAM_ID); + + // then + verify(cambriaPublisher).send("MyPartitionKey", event.toString()); + } + + + + @Test + public void shouldCloseConnectionWhenExceptionOccurred() throws Exception { + // given + JSONObject event = new JSONObject("{}"); + given(cambriaPublisher.send(anyString(), anyString())).willThrow(new IOException("epic fail")); + + // when + eventPublisher.sendEvent(event, STREAM_ID); + + // then + verify(DMaaPPublishersCache).closePublisherFor(STREAM_ID); + } +} \ No newline at end of file diff --git a/src/test/java/org/onap/dcae/common/publishing/DMaaPPublishersCacheTest.java b/src/test/java/org/onap/dcae/common/publishing/DMaaPPublishersCacheTest.java new file mode 100644 index 0000000..2884e44 --- /dev/null +++ b/src/test/java/org/onap/dcae/common/publishing/DMaaPPublishersCacheTest.java @@ -0,0 +1,127 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dcaegen2.restconfcollector + * ================================================================================ + * Copyright (C) 2018 Nokia. All rights reserved. + * Copyright (C) 2018-2019 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 static io.vavr.API.List; +import static io.vavr.API.Map; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.when; + +import com.att.nsa.cambria.client.CambriaBatchingPublisher; +import io.vavr.collection.Map; +import io.vavr.control.Option; +import java.io.IOException; +import java.util.concurrent.TimeUnit; +import org.junit.Before; +import org.junit.Test; +import org.onap.dcae.common.publishing.DMaaPPublishersCache.CambriaPublishersCacheLoader; +import org.onap.dcae.common.publishing.DMaaPPublishersCache.OnPublisherRemovalListener; + + +public class DMaaPPublishersCacheTest { + + private String streamId1; + private Map dMaaPConfigs; + + @Before + public void setUp() { + streamId1 = "sampleStream1"; + dMaaPConfigs = Map("sampleStream1", new PublisherConfig(List("destination1"), "topic1")); + } + + @Test + public void shouldReturnTheSameCachedInstanceOnConsecutiveRetrievals() { + // given + DMaaPPublishersCache dMaaPPublishersCache = new DMaaPPublishersCache(dMaaPConfigs); + + // when + Option firstPublisher = dMaaPPublishersCache.getPublisher(streamId1); + Option secondPublisher = dMaaPPublishersCache.getPublisher(streamId1); + + // then + assertSame("should return same instance", firstPublisher.get(), secondPublisher.get()); + } + + @Test + public void shouldCloseCambriaPublisherOnCacheInvalidate() throws IOException, InterruptedException { + // given + CambriaBatchingPublisher cambriaPublisherMock1 = mock(CambriaBatchingPublisher.class); + CambriaPublishersCacheLoader cacheLoaderMock = mock(CambriaPublishersCacheLoader.class); + DMaaPPublishersCache dMaaPPublishersCache = new DMaaPPublishersCache(cacheLoaderMock, + new OnPublisherRemovalListener(), + dMaaPConfigs); + when(cacheLoaderMock.load(streamId1)).thenReturn(cambriaPublisherMock1); + + // when + dMaaPPublishersCache.getPublisher(streamId1); + dMaaPPublishersCache.closePublisherFor(streamId1); + + // then + verify(cambriaPublisherMock1).close(20, TimeUnit.SECONDS); + + } + + @Test + public void shouldReturnNoneIfThereIsNoDMaaPConfigurationForGivenStreamID() { + // given + DMaaPPublishersCache dMaaPPublishersCache = new DMaaPPublishersCache(dMaaPConfigs); + + // then + assertTrue("should not exist", dMaaPPublishersCache.getPublisher("non-existing").isEmpty()); + } + + + @Test + public void shouldCloseOnlyChangedPublishers() throws IOException, InterruptedException { + // given + CambriaBatchingPublisher cambriaPublisherMock1 = mock(CambriaBatchingPublisher.class); + CambriaBatchingPublisher cambriaPublisherMock2 = mock(CambriaBatchingPublisher.class); + CambriaPublishersCacheLoader cacheLoaderMock = mock(CambriaPublishersCacheLoader.class); + String firstDomain = "domain1"; + String secondDomain = "domain2"; + Map oldConfig = Map(firstDomain, + new PublisherConfig(List("destination1"), "topic1"), + secondDomain, + new PublisherConfig(List("destination2"), "topic2", + "user", "pass")); + Map newConfig = Map(firstDomain, new PublisherConfig(List("destination1"), "topic1"), + secondDomain, new PublisherConfig(List("destination2"), "topic2")); + DMaaPPublishersCache dMaaPPublishersCache = new DMaaPPublishersCache(cacheLoaderMock, + new OnPublisherRemovalListener(), + oldConfig); + when(cacheLoaderMock.load(firstDomain)).thenReturn(cambriaPublisherMock1); + when(cacheLoaderMock.load(secondDomain)).thenReturn(cambriaPublisherMock2); + + dMaaPPublishersCache.getPublisher(firstDomain); + dMaaPPublishersCache.getPublisher(secondDomain); + + // when + dMaaPPublishersCache.reconfigure(newConfig); + + // then + verify(cambriaPublisherMock2).close(20, TimeUnit.SECONDS); + verifyZeroInteractions(cambriaPublisherMock1); + } +} \ No newline at end of file diff --git a/src/test/java/org/onap/dcae/common/publishing/JsonParserTest.java b/src/test/java/org/onap/dcae/common/publishing/JsonParserTest.java new file mode 100644 index 0000000..f00df2c --- /dev/null +++ b/src/test/java/org/onap/dcae/common/publishing/JsonParserTest.java @@ -0,0 +1,38 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dcaegen2.restconfcollector + * ================================================================================ + * Copyright (C) 2018-2019 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 java.util.Map; + +import org.junit.Test; +import org.onap.dcae.common.JsonParser; + +import junit.framework.Assert; + + +public class JsonParserTest { + + @Test + public void convertToPropertiesTest() throws Exception { + String testJson="{\"prop2\"=\"value\", \"prop1\"=\"value\"}"; + Map response= JsonParser.convertToProperties(testJson); + Assert.assertEquals("value", response.get("prop2")); + } +} -- cgit 1.2.3-korg