2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * ============LICENSE_END=========================================================
22 package org.onap.appc.adapter.messaging.dmaap.impl;
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;
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;
48 @RunWith(PowerMockRunner.class)
49 @PrepareForTest(ConfigurationFactory.class)
50 public class EventSenderDmaapImplTest {
53 public ExpectedException expectedEx = ExpectedException.none();
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);
66 public void testInit() {
67 EventSenderDmaapImpl sender = new EventSenderDmaapImpl();
69 assertEquals(1, sender.getProducerMap().size());
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"));
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());
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());
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()));