2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.util.List;
37 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
38 import static org.mockito.ArgumentMatchers.any;
39 import static org.mockito.ArgumentMatchers.eq;
40 import static org.mockito.Mockito.never;
41 import static org.mockito.Mockito.verify;
43 @RunWith(MockitoJUnitRunner.Silent.class)
44 public class EventSenderTest {
47 private DMaaPEventPublisher eventPublisher;
51 public void shouldNotSendEventWhenStreamIdIsNotDefined() throws IOException {
53 EventSender eventSender = givenConfiguredEventSender(HashMap.empty());
54 List<VesEvent> eventToSend = createEventToSend("/eventsAfterTransformation/ves7_valid_event.json");
57 assertThatExceptionOfType(EventValidatorException.class)
58 .isThrownBy(() -> eventSender.send(eventToSend));
61 verifyThatEventWasNotSendAtStream();
65 public void shouldSendStdDefinedEventAtStreamAssignedToEventDomain() throws IOException {
67 EventSender eventSender = givenConfiguredEventSender(
68 HashMap.of("3GPP-FaultSupervision", "ves-3gpp-fault-supervision")
70 List<VesEvent> eventToSend = createEventToSend("/eventsAfterTransformation/ves_stdnDefined_valid.json");
73 eventSender.send(eventToSend);
76 verifyThatEventWasSendAtStream("ves-3gpp-fault-supervision");
80 public void shouldNotSendStndEventWhenStreamIsNotDefined() throws IOException {
82 EventSender eventSender = givenConfiguredEventSender(HashMap.empty());
83 List<VesEvent> eventToSend = createEventToSend("/eventsAfterTransformation/ves_stdnDefined_valid.json");
86 assertThatExceptionOfType(EventValidatorException.class)
87 .isThrownBy(() -> eventSender.send(eventToSend));
90 verifyThatEventWasNotSendAtStream();
94 public void shouldReportThatNoStndDefinedNamespaceParameterIsDefinedInEvent() throws IOException {
96 EventSender eventSender = givenConfiguredEventSender(HashMap.empty());
97 List<VesEvent> eventToSend = createEventToSend(
98 "/eventsAfterTransformation/ves_stdnDefined_missing_namespace_invalid.json"
102 assertThatExceptionOfType(StndDefinedNamespaceParameterNotDefinedException.class)
103 .isThrownBy(() -> eventSender.send(eventToSend));
106 verifyThatEventWasNotSendAtStream();
109 private List<VesEvent> createEventToSend(String path) throws IOException {
110 String event = JsonDataLoader.loadContent(path);
111 return givenEventToSend(event);
114 private EventSender givenConfiguredEventSender(io.vavr.collection.Map<String, String> streamIds) {
115 return new EventSender(eventPublisher, streamIds);
118 private List<VesEvent> givenEventToSend(String event) {
119 JSONObject jsonObject = new JSONObject(event);
120 return List.of(new VesEvent(jsonObject));
123 private void verifyThatEventWasNotSendAtStream() {
124 verify(eventPublisher,never()).sendEvent(any(),any());
127 private void verifyThatEventWasSendAtStream(String s) {
128 verify(eventPublisher).sendEvent(any(), eq(s));