2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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=========================================================
22 package org.openecomp.appc.adapter.messaging.dmaap;
24 import org.openecomp.sdnc.sli.SvcLogicContext;
25 import org.junit.Assert;
26 import org.junit.BeforeClass;
27 import org.junit.Ignore;
28 import org.junit.Test;
29 import org.mockito.Matchers;
30 import org.mockito.Mockito;
31 import org.openecomp.appc.adapter.message.MessageDestination;
32 import org.openecomp.appc.adapter.message.Producer;
33 import org.openecomp.appc.adapter.message.event.EventHeader;
34 import org.openecomp.appc.adapter.message.event.EventMessage;
35 import org.openecomp.appc.adapter.message.event.EventStatus;
36 import org.openecomp.appc.adapter.messaging.dmaap.impl.DmaapProducerImpl;
37 import org.openecomp.appc.adapter.messaging.dmaap.impl.EventSenderDmaapImpl;
38 import org.openecomp.appc.configuration.Configuration;
39 import org.openecomp.appc.configuration.ConfigurationFactory;
40 import org.openecomp.appc.exceptions.APPCException;
42 import java.util.HashMap;
44 import java.util.Properties;
47 public class TestDmaapEventSender {
49 private static Properties props;
50 private static Map<String,Producer> producerMap = new HashMap<>();
51 private static EventMessage eventMessage;
54 public static void setUp() {
56 Configuration configuration = ConfigurationFactory.getConfiguration(); // test.properties file placed in home dir.
58 props = new Properties();
59 props.setProperty(EventSenderDmaapImpl.EVENT_POOL_MEMBERS,
60 configuration.getProperty(EventSenderDmaapImpl.EVENT_POOL_MEMBERS) != null ?
61 configuration.getProperty(EventSenderDmaapImpl.EVENT_POOL_MEMBERS) : "member1,member2,member3");
62 props.setProperty(EventSenderDmaapImpl.EVENT_TOPIC_WRITE,
63 configuration.getProperty(EventSenderDmaapImpl.EVENT_TOPIC_WRITE) != null ?
64 configuration.getProperty(EventSenderDmaapImpl.EVENT_TOPIC_WRITE) : "topic1");
66 String eventClientKey = configuration.getProperty(EventSenderDmaapImpl.DMAAP_USERNAME);
67 if (eventClientKey != null) {
68 props.setProperty(EventSenderDmaapImpl.DMAAP_USERNAME,eventClientKey);
70 String eventClientSecret = configuration.getProperty(EventSenderDmaapImpl.DMAAP_PASSWORD);
71 if (eventClientSecret != null) {
72 props.setProperty(EventSenderDmaapImpl.DMAAP_PASSWORD, eventClientSecret);
75 Producer producer = Mockito.mock(DmaapProducerImpl.class);
76 producerMap.put(MessageDestination.DCAE.toString(),producer);
77 Mockito.when(producer.post(Matchers.anyString(), Matchers.anyString())).thenReturn(true);
79 eventMessage = new EventMessage(
80 new EventHeader("2016-03-15T10:59:33.79Z", "1.01", "17"),
81 new EventStatus(404, "No krokodil found"));
85 @Ignore // requires connection to a live DMaaP server
86 public void testDmaapEventSenderWithProperties() {
87 EventSenderDmaapImpl eventSender = new EventSenderDmaapImpl();
88 eventSender.initialize();
89 eventSender.setProducerMap(producerMap);
90 Assert.assertTrue(eventSender.sendEvent(MessageDestination.DCAE, eventMessage));
94 public void testDmaapEventSenderWithNullProperties() {
95 EventSenderDmaapImpl eventSender = new EventSenderDmaapImpl();
96 // eventSender.initialize();
97 eventSender.setProducerMap(producerMap);
98 Assert.assertTrue(eventSender.sendEvent(MessageDestination.DCAE, eventMessage));
102 * This test runs agains a real Dmaap (or a simulator) that should be cofigured in test.properties file.
105 @Ignore // requires connection to a live DMaaP server
106 public void testDmaapEventSenderWithDmaapSim() {
107 EventSenderDmaapImpl eventSender = new EventSenderDmaapImpl();
108 eventSender.initialize();
109 Assert.assertTrue(eventSender.sendEvent(MessageDestination.DCAE, eventMessage));
114 @Ignore // requires connection to a live DMaaP server
115 public void testDmaapEventSenderDG() throws APPCException {
116 EventSenderDmaapImpl eventSender = new EventSenderDmaapImpl();
117 eventSender.initialize();
118 eventSender.setProducerMap(producerMap);
119 Map<String,String> params = new HashMap<>();
121 params.put("eventTime", eventMessage.getEventHeader().getEventTime());
122 params.put("apiVer", eventMessage.getEventHeader().getApiVer());
123 params.put("eventId", eventMessage.getEventHeader().getEventId());
124 params.put("reason", eventMessage.getEventStatus().getReason());
125 params.put("code", "200");
127 Assert.assertTrue(eventSender.sendEvent(MessageDestination.DCAE,params, new SvcLogicContext()));
130 @Test(expected = APPCException.class)
131 @Ignore // requires connection to a live DMaaP server
132 public void testDmaapEventSenderDGNoParams() throws APPCException {
133 EventSenderDmaapImpl eventSender = new EventSenderDmaapImpl();
134 eventSender.initialize();
135 eventSender.setProducerMap(producerMap);
136 Map<String,String> params = new HashMap<>();
138 Assert.assertFalse(eventSender.sendEvent(MessageDestination.DCAE,params, new SvcLogicContext()));
142 @Test(expected = APPCException.class)
143 @Ignore // requires connection to a live DMaaP server
144 public void testDmaapEventSenderDGNullParam() throws APPCException {
145 EventSenderDmaapImpl eventSender = new EventSenderDmaapImpl();
146 eventSender.initialize();
147 eventSender.setProducerMap(producerMap);
148 Map<String,String> params = null;
150 Assert.assertFalse(eventSender.sendEvent(MessageDestination.DCAE,params, new SvcLogicContext()));
153 @Test(expected = APPCException.class)
154 @Ignore // requires connection to a live DMaaP server
155 public void testDmaapEventSenderDGNoParam() throws APPCException {
156 EventSenderDmaapImpl eventSender = new EventSenderDmaapImpl();
157 eventSender.initialize();
158 eventSender.setProducerMap(producerMap);
159 Map<String,String> params = new HashMap<>();
161 // params.put("apiVer", eventMessage.getEventHeader().getApiVer());
162 params.put("eventId", eventMessage.getEventHeader().getEventId());
163 params.put("reason", eventMessage.getEventStatus().getReason());
164 params.put("code", "200");
166 eventSender.sendEvent(MessageDestination.DCAE,params, new SvcLogicContext());