VESCollector Test optimization
[dcaegen2/collectors/ves.git] / src / test / java / org / onap / dcae / common / EventSenderTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * VES Collector
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.dcae.common;
22
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;
33
34 import java.io.IOException;
35 import java.net.URISyntaxException;
36 import java.util.List;
37
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;
43
44 @RunWith(MockitoJUnitRunner.Silent.class)
45 public class EventSenderTest {
46
47   @Mock
48   private DMaaPEventPublisher eventPublisher;
49
50
51   @Test
52   public void shouldNotSendEventWhenStreamIdIsNotDefined() throws IOException, URISyntaxException {
53     // given
54     EventSender eventSender = givenConfiguredEventSender(HashMap.empty());
55     List<VesEvent> eventToSend = createEventToSend("/eventsAfterTransformation/ves7_valid_event.json");
56
57     // when
58     assertThatExceptionOfType(EventValidatorException.class)
59             .isThrownBy(() -> eventSender.send(eventToSend));
60
61     // then
62     verifyThatEventWasNotSendAtStream();
63   }
64
65   @Test
66   public void shouldSendStdDefinedEventAtStreamAssignedToEventDomain() throws IOException, URISyntaxException {
67     // given
68     EventSender eventSender = givenConfiguredEventSender(
69             HashMap.of("3GPP-FaultSupervision", "ves-3gpp-fault-supervision")
70     );
71     List<VesEvent> eventToSend = createEventToSend("/eventsAfterTransformation/ves_stdnDefined_valid.json");
72
73     // when
74     eventSender.send(eventToSend);
75
76     // then
77     verifyThatEventWasSendAtStream("ves-3gpp-fault-supervision");
78   }
79
80   @Test
81   public void shouldNotSendStndEventWhenStreamIsNotDefined() throws IOException, URISyntaxException {
82     // given
83     EventSender eventSender = givenConfiguredEventSender(HashMap.empty());
84     List<VesEvent> eventToSend = createEventToSend("/eventsAfterTransformation/ves_stdnDefined_valid.json");
85
86     // when
87     assertThatExceptionOfType(EventValidatorException.class)
88             .isThrownBy(() -> eventSender.send(eventToSend));
89
90     // then
91     verifyThatEventWasNotSendAtStream();
92   }
93
94   @Test
95   public void shouldReportThatNoStndDefinedNamespaceParameterIsDefinedInEvent() throws IOException, URISyntaxException {
96     // given
97     EventSender eventSender = givenConfiguredEventSender(HashMap.empty());
98     List<VesEvent> eventToSend = createEventToSend(
99             "/eventsAfterTransformation/ves_stdnDefined_missing_namespace_invalid.json"
100     );
101
102     // when
103     assertThatExceptionOfType(StndDefinedNamespaceParameterNotDefinedException.class)
104             .isThrownBy(() -> eventSender.send(eventToSend));
105
106     // then
107     verifyThatEventWasNotSendAtStream();
108   }
109
110   private List<VesEvent> createEventToSend(String path) throws IOException, URISyntaxException {
111     String event = JsonDataLoader.loadContent(path);
112     return givenEventToSend(event);
113   }
114
115   private EventSender givenConfiguredEventSender(io.vavr.collection.Map<String, String> streamIds) {
116     return new EventSender(eventPublisher, streamIds);
117   }
118
119   private List<VesEvent> givenEventToSend(String event) {
120     JSONObject jsonObject = new JSONObject(event);
121     return List.of(new VesEvent(jsonObject));
122   }
123
124   private void verifyThatEventWasNotSendAtStream() {
125       verify(eventPublisher,never()).sendEvent(any(),any());
126   }
127
128   private void verifyThatEventWasSendAtStream(String s) {
129       verify(eventPublisher).sendEvent(any(), eq(s));
130     }
131 }