2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.adapter.messaging.dmaap;
27 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
28 import org.junit.Assert;
29 import org.junit.BeforeClass;
30 import org.junit.Ignore;
31 import org.junit.Test;
32 import org.mockito.Matchers;
33 import org.mockito.Mockito;
34 import org.onap.appc.adapter.message.MessageDestination;
35 import org.onap.appc.adapter.message.Producer;
36 import org.onap.appc.adapter.message.event.EventHeader;
37 import org.onap.appc.adapter.message.event.EventMessage;
38 import org.onap.appc.adapter.message.event.EventStatus;
39 import org.onap.appc.adapter.messaging.dmaap.impl.DmaapProducerImpl;
40 import org.onap.appc.adapter.messaging.dmaap.impl.EventSenderDmaapImpl;
41 import org.onap.appc.configuration.Configuration;
42 import org.onap.appc.configuration.ConfigurationFactory;
43 import org.onap.appc.exceptions.APPCException;
45 import java.util.HashMap;
47 import java.util.Properties;
50 public class TestDmaapEventSender {
52 private static Properties props;
53 private static Map<String,Producer> producerMap = new HashMap<>();
54 private static EventMessage eventMessage;
57 public static void setUp() {
59 Configuration configuration = ConfigurationFactory.getConfiguration(); // test.properties file placed in home dir.
61 props = new Properties();
62 props.setProperty(EventSenderDmaapImpl.EVENT_POOL_MEMBERS,
63 configuration.getProperty(EventSenderDmaapImpl.EVENT_POOL_MEMBERS) != null ?
64 configuration.getProperty(EventSenderDmaapImpl.EVENT_POOL_MEMBERS) : "member1,member2,member3");
65 props.setProperty(EventSenderDmaapImpl.EVENT_TOPIC_WRITE,
66 configuration.getProperty(EventSenderDmaapImpl.EVENT_TOPIC_WRITE) != null ?
67 configuration.getProperty(EventSenderDmaapImpl.EVENT_TOPIC_WRITE) : "topic1");
69 String eventClientKey = configuration.getProperty(EventSenderDmaapImpl.DMAAP_USERNAME);
70 if (eventClientKey != null) {
71 props.setProperty(EventSenderDmaapImpl.DMAAP_USERNAME,eventClientKey);
73 String eventClientSecret = configuration.getProperty(EventSenderDmaapImpl.DMAAP_PASSWORD);
74 if (eventClientSecret != null) {
75 props.setProperty(EventSenderDmaapImpl.DMAAP_PASSWORD, eventClientSecret);
78 Producer producer = Mockito.mock(DmaapProducerImpl.class);
79 producerMap.put(MessageDestination.DCAE.toString(),producer);
80 Mockito.when(producer.post(Matchers.anyString(), Matchers.anyString())).thenReturn(true);
82 eventMessage = new EventMessage(
83 new EventHeader("2016-03-15T10:59:33.79Z", "1.01", "17"),
84 new EventStatus(404, "No krokodil found"));
88 @Ignore // requires connection to a live DMaaP server
89 public void testDmaapEventSenderWithProperties() {
90 EventSenderDmaapImpl eventSender = new EventSenderDmaapImpl();
91 eventSender.initialize();
92 eventSender.setProducerMap(producerMap);
93 Assert.assertTrue(eventSender.sendEvent(MessageDestination.DCAE, eventMessage));
97 public void testDmaapEventSenderWithNullProperties() {
98 EventSenderDmaapImpl eventSender = new EventSenderDmaapImpl();
99 // eventSender.initialize();
100 eventSender.setProducerMap(producerMap);
101 Assert.assertTrue(eventSender.sendEvent(MessageDestination.DCAE, eventMessage));
105 * This test runs agains a real Dmaap (or a simulator) that should be cofigured in test.properties file.
108 @Ignore // requires connection to a live DMaaP server
109 public void testDmaapEventSenderWithDmaapSim() {
110 EventSenderDmaapImpl eventSender = new EventSenderDmaapImpl();
111 eventSender.initialize();
112 Assert.assertTrue(eventSender.sendEvent(MessageDestination.DCAE, eventMessage));
117 @Ignore // requires connection to a live DMaaP server
118 public void testDmaapEventSenderDG() throws APPCException {
119 EventSenderDmaapImpl eventSender = new EventSenderDmaapImpl();
120 eventSender.initialize();
121 eventSender.setProducerMap(producerMap);
122 Map<String,String> params = new HashMap<>();
124 params.put("eventTime", eventMessage.getEventHeader().getEventTime());
125 params.put("apiVer", eventMessage.getEventHeader().getApiVer());
126 params.put("eventId", eventMessage.getEventHeader().getEventId());
127 params.put("reason", eventMessage.getEventStatus().getReason());
128 params.put("code", "200");
130 Assert.assertTrue(eventSender.sendEvent(MessageDestination.DCAE,params, new SvcLogicContext()));
133 @Test(expected = APPCException.class)
134 @Ignore // requires connection to a live DMaaP server
135 public void testDmaapEventSenderDGNoParams() throws APPCException {
136 EventSenderDmaapImpl eventSender = new EventSenderDmaapImpl();
137 eventSender.initialize();
138 eventSender.setProducerMap(producerMap);
139 Map<String,String> params = new HashMap<>();
141 Assert.assertFalse(eventSender.sendEvent(MessageDestination.DCAE,params, new SvcLogicContext()));
145 @Test(expected = APPCException.class)
146 @Ignore // requires connection to a live DMaaP server
147 public void testDmaapEventSenderDGNullParam() throws APPCException {
148 EventSenderDmaapImpl eventSender = new EventSenderDmaapImpl();
149 eventSender.initialize();
150 eventSender.setProducerMap(producerMap);
151 Map<String,String> params = null;
153 Assert.assertFalse(eventSender.sendEvent(MessageDestination.DCAE,params, new SvcLogicContext()));
156 @Test(expected = APPCException.class)
157 @Ignore // requires connection to a live DMaaP server
158 public void testDmaapEventSenderDGNoParam() throws APPCException {
159 EventSenderDmaapImpl eventSender = new EventSenderDmaapImpl();
160 eventSender.initialize();
161 eventSender.setProducerMap(producerMap);
162 Map<String,String> params = new HashMap<>();
164 // params.put("apiVer", eventMessage.getEventHeader().getApiVer());
165 params.put("eventId", eventMessage.getEventHeader().getEventId());
166 params.put("reason", eventMessage.getEventStatus().getReason());
167 params.put("code", "200");
169 eventSender.sendEvent(MessageDestination.DCAE,params, new SvcLogicContext());