diff options
Diffstat (limited to 'src/test')
7 files changed, 503 insertions, 0 deletions
diff --git a/src/test/java/org/onap/avcnmanager/config/AVCNConfigTest.java b/src/test/java/org/onap/avcnmanager/config/AVCNConfigTest.java new file mode 100644 index 0000000..5d2f43c --- /dev/null +++ b/src/test/java/org/onap/avcnmanager/config/AVCNConfigTest.java @@ -0,0 +1,70 @@ +/*- + * ============LICENSE_START======================================================= + * Simulator + * ================================================================================ + * Copyright (C) 2021 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.avcnmanager.config; + +import org.apache.kafka.streams.StreamsConfig; +import org.apache.kafka.streams.Topology; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.avcnmanager.message.handlers.MessageHandler; + +import java.util.Properties; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class AVCNConfigTest { + + + private AVCNConfig kafkaStreamConfig; + + @Mock + private MessageHandler messageHandler; + + @BeforeEach + void setUp() { + MockitoAnnotations.initMocks(this); + kafkaStreamConfig = new AVCNConfig(); + } + + @Test + void validateKafkaStreamPropertiesAreCorrect() { + final String testServer = "http://kafka/test"; + final String testApplication = "applicationTest"; + + Properties properties = kafkaStreamConfig.getKafkaStreamProperties(testServer, testApplication); + + assertEquals(properties.getProperty(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG), testServer); + assertEquals(properties.getProperty(StreamsConfig.APPLICATION_ID_CONFIG), testApplication); + } + + @Test + void validateKafkaStreamTopologyIsCorrect() { + final String testTopic = "testTopic"; + + Topology topology = kafkaStreamConfig.getKafkaStreamTopology(testTopic, messageHandler); + + assertTrue(topology.describe().toString().contains(testTopic)); + } + +} diff --git a/src/test/java/org/onap/avcnmanager/kafka/stream/AVCNKafkaStreamTest.java b/src/test/java/org/onap/avcnmanager/kafka/stream/AVCNKafkaStreamTest.java new file mode 100644 index 0000000..2d30f92 --- /dev/null +++ b/src/test/java/org/onap/avcnmanager/kafka/stream/AVCNKafkaStreamTest.java @@ -0,0 +1,53 @@ +/*- + * ============LICENSE_START======================================================= + * Simulator + * ================================================================================ + * Copyright (C) 2021 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.avcnmanager.kafka.stream; + +import org.apache.kafka.streams.KafkaStreams; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +class AVCNKafkaStreamTest { + + + @Mock + private KafkaStreams streams; + + @BeforeEach + void setUp() { + MockitoAnnotations.initMocks(this); + } + + @Test + void validateAVCNKafkaStream() { + AVCNKafkaStream stream = new AVCNKafkaStream(streams); + stream.startKafkaStream(); + + verify(streams, times(1) ).setUncaughtExceptionHandler(any(Thread.UncaughtExceptionHandler.class)); + verify(streams, times(1) ).start(); + } + +} diff --git a/src/test/java/org/onap/avcnmanager/message/forwarders/RestForwarderTest.java b/src/test/java/org/onap/avcnmanager/message/forwarders/RestForwarderTest.java new file mode 100644 index 0000000..6d224cb --- /dev/null +++ b/src/test/java/org/onap/avcnmanager/message/forwarders/RestForwarderTest.java @@ -0,0 +1,74 @@ +/*- + * ============LICENSE_START======================================================= + * Simulator + * ================================================================================ + * Copyright (C) 2021 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.avcnmanager.message.forwarders; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; +import org.onap.avcnmanager.message.processing.ParsingResult; +import org.onap.avcnmanager.utils.FileUtils; +import org.onap.avcnmanager.utils.JsonUtils; +import org.springframework.http.HttpEntity; +import org.springframework.web.client.RestTemplate; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +class RestForwarderTest { + private final JsonUtils jsonUtils; + + RestForwarderTest() { + jsonUtils = new JsonUtils(new FileUtils()); + } + + @Test + @SuppressWarnings("unchecked") + void shouldSendResultsAsValidPnfSimRequest() { + RestTemplate restTemplateMock = mock(RestTemplate.class); + + String pnfSimulatorEndpoint = "http://pnfsim_endpoint"; + String vesEndpoint = "http://some_ves_endpoint"; + String expectedPayload = new FileUtils().readStringFromResourceFile("sample_payload_notification.json"); + JsonNode expectedJson = jsonUtils.convertToJsonNode(expectedPayload); + ((ObjectNode)expectedJson.get("simulatorParams")).put("vesServerUrl", vesEndpoint); + Map<String,String> sampleAttributes = new HashMap<>(); + sampleAttributes.put("attribute1", "value1"); + ParsingResult parsingResult = new ParsingResult("some/dn", sampleAttributes); + + ArgumentCaptor<HttpEntity<String>> entityCaptor = ArgumentCaptor.forClass(HttpEntity.class); + ArgumentCaptor<String> endpointCaptor = ArgumentCaptor.forClass(String.class); + + RestForwarder restForwarder = new RestForwarder(pnfSimulatorEndpoint, vesEndpoint, restTemplateMock, jsonUtils); + restForwarder.send(parsingResult); + + verify(restTemplateMock).postForObject(endpointCaptor.capture(), entityCaptor.capture(), eq(String.class)); + String stringBody = entityCaptor.getValue().getBody(); + JsonNode node = jsonUtils.convertToJsonNode(stringBody); + assertEquals(expectedJson, node); + assertEquals(pnfSimulatorEndpoint, endpointCaptor.getValue()); + } +} diff --git a/src/test/java/org/onap/avcnmanager/message/handlers/NewMessageHandlerTest.java b/src/test/java/org/onap/avcnmanager/message/handlers/NewMessageHandlerTest.java new file mode 100644 index 0000000..96543f7 --- /dev/null +++ b/src/test/java/org/onap/avcnmanager/message/handlers/NewMessageHandlerTest.java @@ -0,0 +1,97 @@ +/*- + * ============LICENSE_START======================================================= + * Simulator + * ================================================================================ + * Copyright (C) 2021 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.avcnmanager.message.handlers; + +import org.assertj.core.util.Maps; +import org.junit.jupiter.api.Test; +import org.onap.avcnmanager.message.data.Change; +import org.onap.avcnmanager.message.data.ChangePack; +import org.onap.avcnmanager.message.forwarders.Forwarder; +import org.onap.avcnmanager.message.processing.ParsingResult; +import org.onap.avcnmanager.message.processing.TextProcessor; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +class NewMessageHandlerTest { + + @Test + void emptyChangeShouldBeSkippedFromProcessing() { + TextProcessor mockProcessor = mock(TextProcessor.class); + Forwarder mockForwarder = mock(Forwarder.class); + NewMessageHandler handler = new NewMessageHandler(mockProcessor, mockForwarder); + + String key = "key"; + ChangePack message = new ChangePack(emptyChange(), emptyChange(), ""); + handler.handleMessage(key, message); + + verify(mockProcessor, never()).process(key, message); + verify(mockProcessor, never()).process(anyString(), any(ChangePack.class)); + verify(mockForwarder, never()).send(any(ParsingResult.class)); + } + + @Test + void validMessagesShouldBePassedToTextProcessor() { + TextProcessor mockProcessor = mock(TextProcessor.class); + Forwarder mockForwarder = mock(Forwarder.class); + NewMessageHandler handler = new NewMessageHandler(mockProcessor, mockForwarder); + + String key = "key"; + ChangePack message = new ChangePack(new Change("aaaa", "bbbb"), emptyChange(), "1"); + handler.handleMessage(key, message); + + verify(mockProcessor, times(1)).process(key, message); + verify(mockProcessor, times(1)).process(anyString(), any(ChangePack.class)); + } + + @Test + void processedChangeShouldBePassedToForwarder() { + List<ParsingResult> trapList = new ArrayList<>(); + String key = "key"; + ChangePack message = new ChangePack(new Change("aaaa", "bbbb"), emptyChange(), "1"); + + TextProcessor mockProcessor = this::simpleProcessing; + Forwarder mockForwarder = trapList::add; + + NewMessageHandler handler = new NewMessageHandler(mockProcessor, mockForwarder); + handler.handleMessage(key, message); + + assertEquals(1, trapList.size()); + ParsingResult expected = simpleProcessing(key, message); + assertEquals(expected, trapList.get(0)); + } + + private ParsingResult simpleProcessing(String key, ChangePack message) { + return new ParsingResult(message.getNew().getPath(), Maps.newHashMap(message.getNew().getValue(), message.getType())); + } + + private static Change emptyChange() { + return new Change("", ""); + } +} diff --git a/src/test/java/org/onap/avcnmanager/message/processing/NetconfTextProcessorTest.java b/src/test/java/org/onap/avcnmanager/message/processing/NetconfTextProcessorTest.java new file mode 100644 index 0000000..1afdadd --- /dev/null +++ b/src/test/java/org/onap/avcnmanager/message/processing/NetconfTextProcessorTest.java @@ -0,0 +1,111 @@ +/*- + * ============LICENSE_START======================================================= + * Simulator + * ================================================================================ + * Copyright (C) 2021 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.avcnmanager.message.processing; + +import org.junit.jupiter.api.Test; +import org.onap.avcnmanager.message.data.Change; +import org.onap.avcnmanager.message.data.ChangePack; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; + +class NetconfTextProcessorTest { + + @Test + void shouldReturnCorrentDnWithEmptyAttributeListForOneContainerOneListInstance() { + ChangePack changePack = new ChangePack(new Change("/example-sports:sports/person[name='name 1']",""), emptyChange(), "1"); + + String expectedDn = "example-sports:sports= example-sports:sports , person=name 1"; + ParsingResult expected = new ParsingResult(expectedDn, Collections.emptyMap()); + ParsingResult actual = new NetconfTextProcessor().process("aaa", changePack); + + assertEquals(expected.getDn(), actual.getDn()); + assertEquals(expected.getAttributesList(), actual.getAttributesList()); + } + + @Test + void shouldReturnCorrentDnWithFilledAttributeListForOneContainerOneListOneValue() { + ChangePack changePack = new ChangePack(new Change("/example-sports:sports/person[name='name 1']/name","name 2"), emptyChange(), "1"); + + String expectedDn = "example-sports:sports= example-sports:sports , person=name 1"; + Map<String,String> expectedAttributesMap = new HashMap<>(); + expectedAttributesMap.put("name", "name 2"); + + ParsingResult actual = new NetconfTextProcessor().process("aaa", changePack); + + assertEquals(expectedDn, actual.getDn()); + assertThat(actual.getAttributesList()).hasSameSizeAs(expectedAttributesMap); + assertThat(actual.getAttributesList()).containsAllEntriesOf(expectedAttributesMap); + } + + @Test + void oneContainerTwoListsNestedNoValue() { + ChangePack changePack = new ChangePack( + new Change("/example-sports:sports/team[name='team 1']/player[name='player 1']",""), emptyChange(), "1"); + + String expectedDn = "example-sports:sports= example-sports:sports , player=player 1"; + Map<String,String> expectedAttributesMap = Collections.emptyMap(); + + ParsingResult actual = new NetconfTextProcessor().process("aaa", changePack); + + assertEquals(expectedDn, actual.getDn()); + assertThat(actual.getAttributesList()).hasSameSizeAs(expectedAttributesMap); + assertThat(actual.getAttributesList()).containsAllEntriesOf(expectedAttributesMap); + } + + @Test + void oneContainerTwoListsNestedOneValue() { + ChangePack changePack = new ChangePack( + new Change("/example-sports:sports/team[name='team 1']/player[name='player 1']/name","player 1"), emptyChange(), "1"); + + String expectedDn = "example-sports:sports= example-sports:sports , player=player 1"; + Map<String,String> expectedAttributesMap = new HashMap<>(); + expectedAttributesMap.put("name", "player 1"); + + ParsingResult actual = new NetconfTextProcessor().process("aaa", changePack); + + assertEquals(expectedDn, actual.getDn()); + assertThat(actual.getAttributesList()).hasSameSizeAs(expectedAttributesMap); + assertThat(actual.getAttributesList()).containsAllEntriesOf(expectedAttributesMap); + } + + @Test + void emptyChangeShouldGenerateEmptyResponse() { + ChangePack changePack = new ChangePack(new Change("",""), emptyChange(), "1"); + + String expectedDn = ""; + Map<String,String> expectedAttributesMap = new HashMap<>(); + + ParsingResult actual = new NetconfTextProcessor().process("aaa", changePack); + + assertEquals(expectedDn, actual.getDn()); + assertThat(actual.getAttributesList()).hasSameSizeAs(expectedAttributesMap); + assertThat(actual.getAttributesList()).containsAllEntriesOf(expectedAttributesMap); + } + + private static Change emptyChange() { + return new Change("", ""); + } +} diff --git a/src/test/java/org/onap/avcnmanager/message/serializers/ChangePackSerializersTest.java b/src/test/java/org/onap/avcnmanager/message/serializers/ChangePackSerializersTest.java new file mode 100644 index 0000000..376be47 --- /dev/null +++ b/src/test/java/org/onap/avcnmanager/message/serializers/ChangePackSerializersTest.java @@ -0,0 +1,83 @@ +/*- + * ============LICENSE_START======================================================= + * Simulator + * ================================================================================ + * Copyright (C) 2021 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.avcnmanager.message.serializers; + +import org.apache.kafka.common.serialization.Deserializer; +import org.apache.kafka.common.serialization.Serializer; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.onap.avcnmanager.message.data.Change; +import org.onap.avcnmanager.message.data.ChangePack; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class ChangePackSerializersTest { + private static Serializer<ChangePack> serializer; + private static Deserializer<ChangePack> deserializer; + private final List<ChangePack> testList; + + ChangePackSerializersTest() { + testList = new ArrayList<>(); + for (int i = 0; i < 1000; i++) { + Change aNew = new Change(Integer.toString(i),Integer.toString(i)); + Change old = new Change(Integer.toString(i-1),Integer.toString(i-1)); + ChangePack changePack = new ChangePack(aNew, old, "1"); + testList.add(changePack); + } + } + + String[] getTopics() { + return new String[] { "test" }; + } + + @BeforeAll + static void setup() { + serializer = new ChangePackSerializer(); + deserializer = new ChangePackDeserializer(); + serializer.configure(null, false); + deserializer.configure(null, false); + } + + @Test + void shouldSerializeAndDeserializeCorrectly() { + for(String topic : getTopics()) { + testList.forEach(x -> { + ChangePack changePack = deserializer.deserialize(topic, serializer.serialize(topic, x)); + assertThat(changePack).isNotSameAs(x); + assertThat(changePack).isEqualTo(x); + }); + } + } + + @Test + void shouldThrowWhenDeserializeGarbage() { + byte[] bytes = serializer.serialize("", testList.get(0)); + new Random(System.currentTimeMillis()).nextBytes(bytes); + + assertThrows(RuntimeException.class, () -> deserializer.deserialize("", bytes)); + } + +} diff --git a/src/test/resources/sample_payload_notification.json b/src/test/resources/sample_payload_notification.json new file mode 100644 index 0000000..7528fe0 --- /dev/null +++ b/src/test/resources/sample_payload_notification.json @@ -0,0 +1,15 @@ +{ + "simulatorParams": { + "repeatCount": 1, + "repeatInterval": 1, + "vesServerUrl": "http://ves" + }, + "templateName": "notification.json", + "patch": {}, + "variables": { + "dn": "some/dn", + "attributesList": { + "attribute1": "value1" + } + } +}
\ No newline at end of file |