2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017,2023 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2018-2021 Nokia. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.dcae.common;
23 import io.vavr.collection.HashMap;
24 import org.json.JSONObject;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.Mock;
28 import org.mockito.junit.MockitoJUnitRunner;
29 import org.onap.dcae.common.model.StndDefinedNamespaceParameterNotDefinedException;
30 import org.onap.dcae.common.model.VesEvent;
31 import org.onap.dcae.common.publishing.DMaaPEventPublisher;
32 import org.onap.dcae.restapi.EventValidatorException;
34 import java.io.IOException;
35 import java.net.URISyntaxException;
36 import java.util.List;
38 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
39 import static org.mockito.ArgumentMatchers.any;
40 import static org.mockito.ArgumentMatchers.eq;
41 import static org.mockito.Mockito.never;
42 import static org.mockito.Mockito.verify;
44 @RunWith(MockitoJUnitRunner.Silent.class)
45 public class EventSenderTest {
48 private DMaaPEventPublisher eventPublisher;
52 public void shouldNotSendEventWhenStreamIdIsNotDefined() throws IOException, URISyntaxException {
54 EventSender eventSender = givenConfiguredEventSender(HashMap.empty());
55 List<VesEvent> eventToSend = createEventToSend("/eventsAfterTransformation/ves7_valid_event.json");
58 assertThatExceptionOfType(EventValidatorException.class)
59 .isThrownBy(() -> eventSender.send(eventToSend));
62 verifyThatEventWasNotSendAtStream();
66 public void shouldSendStdDefinedEventAtStreamAssignedToEventDomain() throws IOException, URISyntaxException {
68 EventSender eventSender = givenConfiguredEventSender(
69 HashMap.of("3GPP-FaultSupervision", "ves-3gpp-fault-supervision")
71 List<VesEvent> eventToSend = createEventToSend("/eventsAfterTransformation/ves_stdnDefined_valid.json");
74 eventSender.send(eventToSend);
77 verifyThatEventWasSendAtStream("ves-3gpp-fault-supervision");
81 public void shouldNotSendStndEventWhenStreamIsNotDefined() throws IOException, URISyntaxException {
83 EventSender eventSender = givenConfiguredEventSender(HashMap.empty());
84 List<VesEvent> eventToSend = createEventToSend("/eventsAfterTransformation/ves_stdnDefined_valid.json");
87 assertThatExceptionOfType(EventValidatorException.class)
88 .isThrownBy(() -> eventSender.send(eventToSend));
91 verifyThatEventWasNotSendAtStream();
95 public void shouldReportThatNoStndDefinedNamespaceParameterIsDefinedInEvent() throws IOException, URISyntaxException {
97 EventSender eventSender = givenConfiguredEventSender(HashMap.empty());
98 List<VesEvent> eventToSend = createEventToSend(
99 "/eventsAfterTransformation/ves_stdnDefined_missing_namespace_invalid.json"
103 assertThatExceptionOfType(StndDefinedNamespaceParameterNotDefinedException.class)
104 .isThrownBy(() -> eventSender.send(eventToSend));
107 verifyThatEventWasNotSendAtStream();
110 private List<VesEvent> createEventToSend(String path) throws IOException, URISyntaxException {
111 String event = JsonDataLoader.loadContent(path);
112 return givenEventToSend(event);
115 private EventSender givenConfiguredEventSender(io.vavr.collection.Map<String, String> streamIds) {
116 return new EventSender(eventPublisher, streamIds);
119 private List<VesEvent> givenEventToSend(String event) {
120 JSONObject jsonObject = new JSONObject(event);
121 return List.of(new VesEvent(jsonObject));
124 private void verifyThatEventWasNotSendAtStream() {
125 verify(eventPublisher,never()).sendEvent(any(),any());
128 private void verifyThatEventWasSendAtStream(String s) {
129 verify(eventPublisher).sendEvent(any(), eq(s));