Replace cambria with DmaaP client
[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 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.util.List;
36
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;
42
43 @RunWith(MockitoJUnitRunner.Silent.class)
44 public class EventSenderTest {
45
46   @Mock
47   private DMaaPEventPublisher eventPublisher;
48
49
50   @Test
51   public void shouldNotSendEventWhenStreamIdIsNotDefined() throws IOException {
52     // given
53     EventSender eventSender = givenConfiguredEventSender(HashMap.empty());
54     List<VesEvent> eventToSend = createEventToSend("/eventsAfterTransformation/ves7_valid_event.json");
55
56     // when
57     assertThatExceptionOfType(EventValidatorException.class)
58             .isThrownBy(() -> eventSender.send(eventToSend));
59
60     // then
61     verifyThatEventWasNotSendAtStream();
62   }
63
64   @Test
65   public void shouldSendStdDefinedEventAtStreamAssignedToEventDomain() throws IOException {
66     // given
67     EventSender eventSender = givenConfiguredEventSender(
68             HashMap.of("3GPP-FaultSupervision", "ves-3gpp-fault-supervision")
69     );
70     List<VesEvent> eventToSend = createEventToSend("/eventsAfterTransformation/ves_stdnDefined_valid.json");
71
72     // when
73     eventSender.send(eventToSend);
74
75     // then
76     verifyThatEventWasSendAtStream("ves-3gpp-fault-supervision");
77   }
78
79   @Test
80   public void shouldNotSendStndEventWhenStreamIsNotDefined() throws IOException {
81     // given
82     EventSender eventSender = givenConfiguredEventSender(HashMap.empty());
83     List<VesEvent> eventToSend = createEventToSend("/eventsAfterTransformation/ves_stdnDefined_valid.json");
84
85     // when
86     assertThatExceptionOfType(EventValidatorException.class)
87             .isThrownBy(() -> eventSender.send(eventToSend));
88
89     // then
90     verifyThatEventWasNotSendAtStream();
91   }
92
93   @Test
94   public void shouldReportThatNoStndDefinedNamespaceParameterIsDefinedInEvent() throws IOException {
95     // given
96     EventSender eventSender = givenConfiguredEventSender(HashMap.empty());
97     List<VesEvent> eventToSend = createEventToSend(
98             "/eventsAfterTransformation/ves_stdnDefined_missing_namespace_invalid.json"
99     );
100
101     // when
102     assertThatExceptionOfType(StndDefinedNamespaceParameterNotDefinedException.class)
103             .isThrownBy(() -> eventSender.send(eventToSend));
104
105     // then
106     verifyThatEventWasNotSendAtStream();
107   }
108
109   private List<VesEvent> createEventToSend(String path) throws IOException {
110     String event = JsonDataLoader.loadContent(path);
111     return givenEventToSend(event);
112   }
113
114   private EventSender givenConfiguredEventSender(io.vavr.collection.Map<String, String> streamIds) {
115     return new EventSender(eventPublisher, streamIds);
116   }
117
118   private List<VesEvent> givenEventToSend(String event) {
119     JSONObject jsonObject = new JSONObject(event);
120     return List.of(new VesEvent(jsonObject));
121   }
122
123   private void verifyThatEventWasNotSendAtStream() {
124       verify(eventPublisher,never()).sendEvent(any(),any());
125   }
126
127   private void verifyThatEventWasSendAtStream(String s) {
128       verify(eventPublisher).sendEvent(any(), eq(s));
129     }
130 }