diff options
Diffstat (limited to 'src/test/java/org/onap/dcae/common')
3 files changed, 239 insertions, 32 deletions
diff --git a/src/test/java/org/onap/dcae/common/EventSenderTest.java b/src/test/java/org/onap/dcae/common/EventSenderTest.java index f49d3cd8..e20fa993 100644 --- a/src/test/java/org/onap/dcae/common/EventSenderTest.java +++ b/src/test/java/org/onap/dcae/common/EventSenderTest.java @@ -3,7 +3,7 @@ * PROJECT * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * Copyright (C) 2018 Nokia. All rights reserved.s + * Copyright (C) 2020 Nokia. All rights reserved.s * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,56 +20,122 @@ */ package org.onap.dcae.common; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - import io.vavr.collection.HashMap; -import io.vavr.collection.Map; -import org.json.JSONArray; import org.json.JSONObject; 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.common.model.StndDefinedNamespaceParameterNotDefinedException; +import org.onap.dcae.common.model.VesEvent; import org.onap.dcae.common.publishing.EventPublisher; +import java.io.IOException; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; + @RunWith(MockitoJUnitRunner.Silent.class) public class EventSenderTest { - private String event = "{\"VESversion\":\"v7\",\"VESuniqueId\":\"fd69d432-5cd5-4c15-9d34-407c81c61c6a-0\",\"event\":{\"commonEventHeader\":{\"startEpochMicrosec\":1544016106000000,\"eventId\":\"fault33\",\"timeZoneOffset\":\"UTC+00.00\",\"priority\":\"Normal\",\"version\":\"4.0.1\",\"nfVendorName\":\"Ericsson\",\"reportingEntityName\":\"1\",\"sequence\":1,\"domain\":\"fault\",\"lastEpochMicrosec\":1544016106000000,\"eventName\":\"Fault_KeyFileFault\",\"vesEventListenerVersion\":\"7.0.1\",\"sourceName\":\"1\"},\"faultFields\":{\"eventSeverity\":\"CRITICAL\",\"alarmCondition\":\"KeyFileFault\",\"faultFieldsVersion\":\"4.0\",\"eventCategory\":\"PROCESSINGERRORALARM\",\"specificProblem\":\"License Key File Fault_1\",\"alarmAdditionalInformation\":{\"probableCause\":\"ConfigurationOrCustomizationError\",\"additionalText\":\"test_1\",\"source\":\"ManagedElement=1,SystemFunctions=1,Lm=1\"},\"eventSourceType\":\"Lm\",\"vfStatus\":\"Active\"}}}\n"; - @Mock private EventPublisher eventPublisher; - @Mock - private ApplicationSettings settings; - private EventSender eventSender; + @Test + public void shouldNotSendEventWhenStreamIdIsNotDefined() throws IOException { + // given + EventSender eventSender = givenConfiguredEventSender(HashMap.empty()); + List<VesEvent> eventToSend = createEventToSend("/eventsAfterTransformation/ves7_valid_event.json"); + + // when + eventSender.send(eventToSend); + + // then + verifyThatEventWasNotSendAtStream(); + } @Test - public void shouldntSendEventWhenStreamIdsIsEmpty() { - when(settings.dMaaPStreamsMapping()).thenReturn(HashMap.empty()); - eventSender = new EventSender(eventPublisher, settings ); - JSONObject jsonObject = new JSONObject(event); - JSONArray jsonArray = new JSONArray(); - jsonArray.put(jsonObject); - eventSender.send(jsonArray); - verify(eventPublisher,never()).sendEvent(any(),any()); + public void shouldSendEventAtStreamsAssignedToEventDomain() throws IOException { + // given + EventSender eventSender = givenConfiguredEventSender(HashMap.of("fault", new String[]{"ves-fault", "fault-ves"})); + List<VesEvent> eventToSend = createEventToSend("/eventsAfterTransformation/ves7_valid_event.json"); + + // when + eventSender.send(eventToSend); + + //then + verifyThatEventWasSendAtStream("ves-fault"); + verifyThatEventWasSendAtStream("fault-ves"); + } + + @Test + public void shouldSendStdDefinedEventAtStreamAssignedToEventDomain() throws IOException { + // given + EventSender eventSender = givenConfiguredEventSender( + HashMap.of("3GPP-FaultSupervision", new String[]{"ves-3gpp-fault-supervision"}) + ); + List<VesEvent> eventToSend = createEventToSend("/eventsAfterTransformation/ves_stdnDefined_valid.json"); + + // when + eventSender.send(eventToSend); + + // then + verifyThatEventWasSendAtStream("ves-3gpp-fault-supervision"); } @Test - public void shouldSendEvent() { - Map<String, String[]> streams = HashMap.of("fault", new String[]{"ves-fault", "fault-ves"}); - when(settings.dMaaPStreamsMapping()).thenReturn(streams); - eventSender = new EventSender(eventPublisher, settings ); + public void shouldNotSendStndEventWhenStreamIsNotDefined() throws IOException { + // given + EventSender eventSender = givenConfiguredEventSender(HashMap.empty()); + List<VesEvent> eventToSend = createEventToSend("/eventsAfterTransformation/ves_stdnDefined_valid.json"); + + // when + eventSender.send(eventToSend); + + // then + verifyThatEventWasNotSendAtStream(); + } + + @Test + public void shouldReportThatNoStndDefinedNamespaceParameterIsDefinedInEvent() throws IOException { + // given + EventSender eventSender = givenConfiguredEventSender(HashMap.empty()); + List<VesEvent> eventToSend = createEventToSend( + "/eventsAfterTransformation/ves_stdnDefined_missing_namespace_invalid.json" + ); + + // when + assertThatExceptionOfType(StndDefinedNamespaceParameterNotDefinedException.class) + .isThrownBy(() -> eventSender.send(eventToSend)); + + // then + verifyThatEventWasNotSendAtStream(); + } + + private List<VesEvent> createEventToSend(String path) throws IOException { + String event = JsonDataLoader.loadContent(path); + return givenEventToSend(event); + } + + private EventSender givenConfiguredEventSender(io.vavr.collection.Map<String, String[]> streamIds) { + return new EventSender(eventPublisher, streamIds); + } + + private List<VesEvent> givenEventToSend(String event) { JSONObject jsonObject = new JSONObject(event); - JSONArray jsonArray = new JSONArray(); - jsonArray.put(jsonObject); - eventSender.send(jsonArray); - verify(eventPublisher, times(2)).sendEvent(any(),any()); + return List.of(new VesEvent(jsonObject)); + } + + private void verifyThatEventWasNotSendAtStream() { + verify(eventPublisher,never()).sendEvent(any(),any()); + } + + private void verifyThatEventWasSendAtStream(String s) { + verify(eventPublisher).sendEvent(any(), eq(s)); } -}
\ No newline at end of file +} diff --git a/src/test/java/org/onap/dcae/common/JsonDataLoader.java b/src/test/java/org/onap/dcae/common/JsonDataLoader.java new file mode 100644 index 00000000..2ea59aa0 --- /dev/null +++ b/src/test/java/org/onap/dcae/common/JsonDataLoader.java @@ -0,0 +1,49 @@ +/* + * ============LICENSE_START======================================================= + * VES Collector + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved.s + * ================================================================================ + * 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 java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + +/** + * This class is static and does not have public constructor. + * It is responsible for data loading fot test cases. + * + * @author Zebek + */ +public final class JsonDataLoader { + + private JsonDataLoader() { + } + + /** + * This method is validating given event using schema adn throws exception if event is not valid + * + * @param path to file that will be loaded + * @return contend of the file located under path, given in parameters, as string + * @throws IOException when file under given path was not found + */ + public static String loadContent(String path) throws IOException { + return new String( + Files.readAllBytes(Paths.get(JsonDataLoader.class.getResource(path).getPath())) + ); + } +} diff --git a/src/test/java/org/onap/dcae/common/model/VesEventTest.java b/src/test/java/org/onap/dcae/common/model/VesEventTest.java new file mode 100644 index 00000000..c66e0a9b --- /dev/null +++ b/src/test/java/org/onap/dcae/common/model/VesEventTest.java @@ -0,0 +1,92 @@ +/* + * ============LICENSE_START======================================================= + * VES Collector + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved.s + * ================================================================================ + * 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.model; + +import org.assertj.core.api.Assertions; +import org.json.JSONObject; +import org.junit.Test; +import org.onap.dcae.common.JsonDataLoader; + +import java.io.IOException; + +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +public class VesEventTest { + + private static final String FAULT_DOMAIN = "fault"; + private static final String FAULT_STREAM_ID = "fault"; + private static final String STND_DEFINED_DOMAIN = "stndDefined"; + private static final String STND_DEFINED_STREAM_ID = "3GPP-FaultSupervision"; + + @Test + public void shouldReturnsOriginalDomainForNonStdEvent() throws IOException { + // given + final VesEvent vesEvent = createVesEvent("/eventsAfterTransformation/ves7_valid_event.json"); + + // when/then + Assertions.assertThat(vesEvent.getDomain()).isEqualTo(FAULT_DOMAIN); + Assertions.assertThat(vesEvent.getStreamId()).isEqualTo(FAULT_STREAM_ID); + } + + @Test + public void shouldReturnsDomainStoredInStndDefinedNamespaceParameterForNonStdEvent() throws IOException { + // given + final VesEvent vesEvent = createVesEvent("/eventsAfterTransformation/ves_stdnDefined_valid.json"); + + // when/then + Assertions.assertThat(vesEvent.getDomain()).isEqualTo(STND_DEFINED_DOMAIN); + Assertions.assertThat(vesEvent.getStreamId()).isEqualTo(STND_DEFINED_STREAM_ID); + } + + + @Test + public void shouldReportThatStndDefinedNamespaceParameterIsNotDefinedInEvent() throws IOException { + // given + final VesEvent vesEvent = createVesEvent( + "/eventsAfterTransformation/ves_stdnDefined_missing_namespace_invalid.json" + ); + + // when/then + // when + assertThatExceptionOfType(StndDefinedNamespaceParameterNotDefinedException.class) + .isThrownBy(() -> { + vesEvent.getStreamId(); + }); + } + + @Test + public void shouldReportThatStndDefinedNamespaceParameterHasEmptyValue() throws IOException { + // given + final VesEvent vesEvent = createVesEvent( + "/eventsAfterTransformation/ves_stdnDefined_empty_namespace_invalid.json" + ); + + // when/then + assertThatExceptionOfType(StndDefinedNamespaceParameterHasEmptyValueException.class) + .isThrownBy(() -> { + vesEvent.getStreamId(); + }); + } + + private VesEvent createVesEvent(String path) throws IOException { + String event = JsonDataLoader.loadContent(path); + return new VesEvent(new JSONObject(event)); + } +} |