Test coverage in EventSenderDmaapImpl
[appc.git] / appc-adapters / appc-dmaap-adapter / appc-dmaap-adapter-bundle / src / test / java / org / onap / appc / adapter / messaging / dmaap / impl / EventSenderDmaapImplTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2019 Ericsson
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.appc.adapter.messaging.dmaap.impl;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27 import java.util.HashMap;
28 import java.util.Map;
29 import java.util.Properties;
30 import org.junit.Before;
31 import org.junit.Rule;
32 import org.junit.Test;
33 import org.junit.rules.ExpectedException;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mockito;
36 import org.onap.appc.adapter.message.MessageDestination;
37 import org.onap.appc.adapter.message.Producer;
38 import org.onap.appc.adapter.message.event.EventHeader;
39 import org.onap.appc.adapter.message.event.EventMessage;
40 import org.onap.appc.configuration.Configuration;
41 import org.onap.appc.configuration.ConfigurationFactory;
42 import org.onap.appc.exceptions.APPCException;
43 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
44 import org.powermock.api.mockito.PowerMockito;
45 import org.powermock.core.classloader.annotations.PrepareForTest;
46 import org.powermock.modules.junit4.PowerMockRunner;
47
48 @RunWith(PowerMockRunner.class)
49 @PrepareForTest(ConfigurationFactory.class)
50 public class EventSenderDmaapImplTest {
51
52     @Rule
53     public ExpectedException expectedEx = ExpectedException.none();
54
55     @Before
56     public void setup() {
57         Configuration configuration = Mockito.mock(Configuration.class);
58         Properties properties = new Properties();
59         properties.put(MessageDestination.DCAE + "." + EventSenderDmaapImpl.EVENT_POOL_MEMBERS, "host1,host2");
60         Mockito.when(configuration.getProperties()).thenReturn(properties);
61         PowerMockito.mockStatic(ConfigurationFactory.class);
62         PowerMockito.when(ConfigurationFactory.getConfiguration()).thenReturn(configuration);
63     }
64
65     @Test
66     public void testInit() {
67         EventSenderDmaapImpl sender = new EventSenderDmaapImpl();
68         sender.initialize();
69         assertEquals(1, sender.getProducerMap().size());
70     }
71
72     @Test
73     public void testSendEvent() {
74         EventSenderDmaapImpl sender = new EventSenderDmaapImpl();
75         EventMessage eventMessage = Mockito.mock(EventMessage.class);
76         EventHeader eventHeader = Mockito.mock(EventHeader.class);
77         Mockito.when(eventHeader.getEventId()).thenReturn("EVENT_ID");
78         Mockito.when(eventMessage.getEventHeader()).thenReturn(eventHeader);
79         assertTrue(sender.sendEvent(MessageDestination.DCAE, eventMessage, "TOPIC NAME"));
80     }
81
82     @Test
83     public void testSendEventSvcLogicContext() throws APPCException {
84         EventSenderDmaapImpl sender = new EventSenderDmaapImpl();
85         expectedEx.expect(APPCException.class);
86         expectedEx.expectMessage("Missing input parameters: ");
87         sender.sendEvent(MessageDestination.DCAE, new HashMap<String, String>(), new SvcLogicContext());
88     }
89
90     @Test
91     public void testSendEventSvcLogicContextNullParams() throws APPCException {
92         EventSenderDmaapImpl sender = new EventSenderDmaapImpl();
93         expectedEx.expect(APPCException.class);
94         expectedEx.expectMessage("Parameters map is empty (null)");
95         sender.sendEvent(MessageDestination.DCAE, null, new SvcLogicContext());
96     }
97
98     @Test
99     public void testSendEventSvcLogicContextWithParams() throws APPCException {
100         EventSenderDmaapImpl sender = new EventSenderDmaapImpl();
101         Map<String, String> params = new HashMap<>();
102         params.put("apiVer", "apiVer");
103         params.put("eventId", "eventId");
104         params.put("reason", "reason");
105         params.put("entityId", "entityId");
106         Producer producer = Mockito.mock(Producer.class);
107         Mockito.when(producer.post(Mockito.anyString(), Mockito.anyString())).thenReturn(false);
108         Map<String, Producer> producerMap = new HashMap<>();
109         producerMap.put(MessageDestination.DCAE.toString(), producer);
110         sender.setProducerMap(producerMap);
111         assertFalse(sender.sendEvent(MessageDestination.DCAE, params, new SvcLogicContext()));
112     }
113 }