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
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.publishing;
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;
29 import java.io.IOException;
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;
38 public class DMaaPEventPublisherTest {
40 private static final String STREAM_ID = "sampleStreamId";
42 private static final JSONObject EXPECTED_EVENT =
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\"}}}");
54 private static final String PARTITION = "dns01cmd004";
56 private DMaaPEventPublisher eventPublisher;
57 private CambriaBatchingPublisher cambriaPublisher;
58 private DMaaPPublishersCache DMaaPPublishersCache;
62 cambriaPublisher = mock(CambriaBatchingPublisher.class);
63 DMaaPPublishersCache = mock(DMaaPPublishersCache.class);
64 when(DMaaPPublishersCache.getPublisher(anyString())).thenReturn(Option(cambriaPublisher));
65 eventPublisher = new DMaaPEventPublisher(DMaaPPublishersCache);
69 public void shouldSendEventToTopic() throws Exception {
71 eventPublisher.sendEvent(givenVesEventWithoutVESuniqueIdField(), STREAM_ID);
74 verify(cambriaPublisher).send(PARTITION, EXPECTED_EVENT.toString());
78 public void shouldRemoveInternalVESUIDBeforeSending() throws Exception {
80 eventPublisher.sendEvent(givenVesEventWithVESUniqueIdField(), STREAM_ID);
83 verify(cambriaPublisher).send(PARTITION, EXPECTED_EVENT.toString());
87 public void shouldCloseConnectionWhenExceptionOccurred() throws Exception {
89 given(cambriaPublisher.send(anyString(), anyString()))
90 .willThrow(new IOException("Expected exception - test case scenario!"));
93 eventPublisher.sendEvent(givenVesEventWithVESUniqueIdField(), STREAM_ID);
96 verify(DMaaPPublishersCache).closePublisherFor(STREAM_ID);
99 private VesEvent givenVesEventWithVESUniqueIdField() {
102 "{\"VESversion\":\"v7\",\"VESuniqueId\":\"fd69d432-5cd5-4c15-9d34-407c81c61c6a-0\"," +
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\"}}}"));
113 private VesEvent givenVesEventWithoutVESuniqueIdField() {
116 "{\"VESversion\":\"v7\"," +
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\"}}}"));