e8fd9925c098f19a4154e4bf034afe2033da0bfd
[dcaegen2/services.git] / components / kpi-computation-ms / src / test / java / org / onap / dcaegen2 / kpi / dmaap / KpiDmaapClientTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 China Mobile.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.dcaegen2.kpi.dmaap;
22
23 import static org.junit.Assert.assertTrue;
24
25 import java.io.IOException;
26 import java.util.HashMap;
27 import java.util.Map;
28
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.InjectMocks;
33 import org.mockito.Mock;
34 import org.mockito.Mockito;
35 import org.mockito.runners.MockitoJUnitRunner;
36 import org.onap.dcaegen2.kpi.models.Configuration;
37 import org.onap.dcaegen2.kpi.utils.DmaapUtils;
38 import org.springframework.boot.test.context.SpringBootTest;
39
40 import com.att.nsa.cambria.client.CambriaBatchingPublisher;
41 import com.att.nsa.cambria.client.CambriaConsumer;
42
43 @RunWith(MockitoJUnitRunner.class)
44 @SpringBootTest(classes = KpiDmaapClient.class)
45 public class KpiDmaapClientTest {
46
47     @Mock
48     Configuration configurationMock;
49
50     @Mock
51     DmaapUtils dmaapUtilsMock;
52
53     @InjectMocks
54     KpiDmaapClient kpiDmaapClient;
55
56     @Mock
57     CambriaConsumer kpiResponseCambriaConsumerMock;
58
59     @Mock
60     CambriaBatchingPublisher cambriaBatchingPublisherMock;
61
62     @Mock
63     NotificationProducer notificationProducerMock;
64
65     @Before
66     public void setup() {
67         kpiDmaapClient = new KpiDmaapClient(dmaapUtilsMock, configurationMock);
68     }
69
70     @Test
71     public void sendNotificationToPolicyTest() {
72         Map<String, Object> streamsPublishes = new HashMap<>();
73         Map<String, String> topics = new HashMap<>();
74         Map<String, Object> dmaapInfo = new HashMap<>();
75         topics.put("topic_url", "https://message-router.onap.svc.cluster.local:3905/events/DCAE_KPI_OUTPUT");
76         dmaapInfo.put("dmaap_info", topics);
77         streamsPublishes.put("kpi_topic", dmaapInfo);
78         Mockito.when(configurationMock.getStreamsPublishes()).thenReturn(streamsPublishes);
79         Mockito.when(dmaapUtilsMock.buildPublisher(configurationMock, "DCAE_KPI_OUTPUT"))
80                 .thenReturn(cambriaBatchingPublisherMock);
81         try {
82             Mockito.when(cambriaBatchingPublisherMock.send("", "hello")).thenReturn(0);
83         } catch (IOException e) {
84             e.printStackTrace();
85         }
86         assertTrue(kpiDmaapClient.sendNotificationToDmaap("hello"));
87
88     }
89 }