e4b6fd9132939a64cb87dd9e9ad112869b1361b7
[dcaegen2/collectors/ves.git] / src / test / java / org / onap / dcae / common / publishing / DMaaPEventPublisherTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dcaegen2.collectors.ves
4  * ================================================================================
5  * Copyright (C) 2018,2020 Nokia. All rights reserved.
6  * Copyright (C) 2020 AT&T. 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.publishing;
22
23 import com.att.nsa.cambria.client.CambriaBatchingPublisher;
24 import org.json.JSONObject;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.onap.dcae.common.model.VesEvent;
28
29 import java.io.IOException;
30
31 import static io.vavr.API.Option;
32 import static org.mockito.ArgumentMatchers.anyString;
33 import static org.mockito.BDDMockito.given;
34 import static org.mockito.Mockito.mock;
35 import static org.mockito.Mockito.verify;
36 import static org.mockito.Mockito.when;
37
38 public class DMaaPEventPublisherTest {
39
40   private static final String STREAM_ID = "sampleStreamId";
41
42   private static final JSONObject EXPECTED_EVENT =
43       new JSONObject(
44           "{\"VESversion\":\"v7\",\"event\":{"
45               + "\"commonEventHeader\":{\"startEpochMicrosec\":1537562659253019,"
46               + "\"sourceId\":\"79e90d76-513a-4f79-886d-470a0037c5cf\","
47               + "\"eventId\":\"Heartbeat_vDNS_100.100.10.10\",\"nfcNamingCode\":\"DNS\","
48               + "\"reportingEntityId\":\"79e90d76-513a-4f79-886d-470a0037c5cf\","
49               + "\"eventType\":\"applicationVnf\",\"priority\":\"Normal\",\"version\":3,"
50               + "\"reportingEntityName\":\"dns01cmd004\",\"sequence\":36312,\"domain\":\"heartbeat\","
51               + "\"lastEpochMicrosec\":1537562659253019,\"eventName\":\"Heartbeat_vDNS\","
52               + "\"sourceName\":\"dns01cmd004\",\"nfNamingCode\":\"MDNS\"}}}");
53
54   private static final String PARTITION = "dns01cmd004";
55
56   private DMaaPEventPublisher eventPublisher;
57   private CambriaBatchingPublisher cambriaPublisher;
58   private DMaaPPublishersCache DMaaPPublishersCache;
59
60   @Before
61   public void setUp() {
62     cambriaPublisher = mock(CambriaBatchingPublisher.class);
63     DMaaPPublishersCache = mock(DMaaPPublishersCache.class);
64     when(DMaaPPublishersCache.getPublisher(anyString())).thenReturn(Option(cambriaPublisher));
65     eventPublisher = new DMaaPEventPublisher(DMaaPPublishersCache);
66   }
67
68   @Test
69   public void shouldSendEventToTopic() throws Exception {
70     // when
71     eventPublisher.sendEvent(givenVesEventWithoutVESuniqueIdField(), STREAM_ID);
72
73     // then
74     verify(cambriaPublisher).send(PARTITION, EXPECTED_EVENT.toString());
75   }
76
77   @Test
78   public void shouldRemoveInternalVESUIDBeforeSending() throws Exception {
79     // when
80     eventPublisher.sendEvent(givenVesEventWithVESUniqueIdField(), STREAM_ID);
81
82     // then
83     verify(cambriaPublisher).send(PARTITION, EXPECTED_EVENT.toString());
84   }
85
86   @Test
87   public void shouldCloseConnectionWhenExceptionOccurred() throws Exception {
88     // given
89     given(cambriaPublisher.send(anyString(), anyString()))
90         .willThrow(new IOException("Expected exception - test case scenario!"));
91
92     // when
93     eventPublisher.sendEvent(givenVesEventWithVESUniqueIdField(), STREAM_ID);
94
95     // then
96     verify(DMaaPPublishersCache).closePublisherFor(STREAM_ID);
97   }
98
99   private VesEvent givenVesEventWithVESUniqueIdField() {
100     return new VesEvent(
101         new JSONObject(
102             "{\"VESversion\":\"v7\",\"VESuniqueId\":\"fd69d432-5cd5-4c15-9d34-407c81c61c6a-0\"," +
103                     "\"event\":{" +
104                     "\"commonEventHeader\":{\"startEpochMicrosec\":1537562659253019," +
105                     "\"sourceId\":\"79e90d76-513a-4f79-886d-470a0037c5cf\"," +
106                     "\"eventId\":\"Heartbeat_vDNS_100.100.10.10\",\"nfcNamingCode\":\"DNS\"," +
107                     "\"reportingEntityId\":\"79e90d76-513a-4f79-886d-470a0037c5cf\",\"eventType\":\"applicationVnf\"," +
108                     "\"priority\":\"Normal\",\"version\":3,\"reportingEntityName\":\"dns01cmd004\",\"sequence\":36312," +
109                     "\"domain\":\"heartbeat\",\"lastEpochMicrosec\":1537562659253019,\"eventName\":\"Heartbeat_vDNS\"," +
110                     "\"sourceName\":\"dns01cmd004\",\"nfNamingCode\":\"MDNS\"}}}"));
111   }
112
113   private VesEvent givenVesEventWithoutVESuniqueIdField() {
114     return new VesEvent(
115             new JSONObject(
116                     "{\"VESversion\":\"v7\"," +
117                             "\"event\":{" +
118                             "\"commonEventHeader\":{\"startEpochMicrosec\":1537562659253019," +
119                             "\"sourceId\":\"79e90d76-513a-4f79-886d-470a0037c5cf\"," +
120                             "\"eventId\":\"Heartbeat_vDNS_100.100.10.10\",\"nfcNamingCode\":\"DNS\"," +
121                             "\"reportingEntityId\":\"79e90d76-513a-4f79-886d-470a0037c5cf\",\"eventType\":\"applicationVnf\"," +
122                             "\"priority\":\"Normal\",\"version\":3,\"reportingEntityName\":\"dns01cmd004\",\"sequence\":36312," +
123                             "\"domain\":\"heartbeat\",\"lastEpochMicrosec\":1537562659253019,\"eventName\":\"Heartbeat_vDNS\"," +
124                             "\"sourceName\":\"dns01cmd004\",\"nfNamingCode\":\"MDNS\"}}}"));
125   }
126 }