2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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 * ============LICENSE_END=========================================================
24 package org.onap.appc.adapter.messaging.dmaap;
26 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
27 import org.junit.Assert;
28 import org.junit.BeforeClass;
29 import org.junit.Ignore;
30 import org.junit.Test;
31 import org.mockito.Matchers;
32 import org.mockito.Mockito;
33 import org.onap.appc.adapter.message.MessageDestination;
34 import org.onap.appc.adapter.message.Producer;
35 import org.onap.appc.adapter.message.event.EventHeader;
36 import org.onap.appc.adapter.message.event.EventMessage;
37 import org.onap.appc.adapter.message.event.EventStatus;
38 import org.onap.appc.adapter.messaging.dmaap.http.HttpDmaapProducerImpl;
39 import org.onap.appc.adapter.messaging.dmaap.impl.EventSenderDmaapImpl;
40 import org.onap.appc.configuration.Configuration;
41 import org.onap.appc.configuration.ConfigurationFactory;
42 import org.onap.appc.exceptions.APPCException;
44 import java.util.HashMap;
46 import java.util.Properties;
49 public class TestDmaapEventSender {
51 private static Properties props;
52 private static Map<String,Producer> producerMap = new HashMap<>();
53 private static EventMessage eventMessage;
56 public static void setUp() {
58 Configuration configuration = ConfigurationFactory.getConfiguration(); // test.properties file placed in home dir.
60 props = new Properties();
61 props.setProperty(EventSenderDmaapImpl.EVENT_POOL_MEMBERS,
62 configuration.getProperty(EventSenderDmaapImpl.EVENT_POOL_MEMBERS) != null ?
63 configuration.getProperty(EventSenderDmaapImpl.EVENT_POOL_MEMBERS) : "member1,member2,member3");
64 props.setProperty(EventSenderDmaapImpl.EVENT_TOPIC_WRITE,
65 configuration.getProperty(EventSenderDmaapImpl.EVENT_TOPIC_WRITE) != null ?
66 configuration.getProperty(EventSenderDmaapImpl.EVENT_TOPIC_WRITE) : "topic1");
68 String eventClientKey = configuration.getProperty(EventSenderDmaapImpl.DMAAP_USERNAME);
69 if (eventClientKey != null) {
70 props.setProperty(EventSenderDmaapImpl.DMAAP_USERNAME,eventClientKey);
72 String eventClientSecret = configuration.getProperty(EventSenderDmaapImpl.DMAAP_PASSWORD);
73 if (eventClientSecret != null) {
74 props.setProperty(EventSenderDmaapImpl.DMAAP_PASSWORD, eventClientSecret);
77 Producer producer = Mockito.mock(HttpDmaapProducerImpl.class);
78 producerMap.put(MessageDestination.DCAE.toString(),producer);
79 Mockito.when(producer.post(Matchers.anyString(), Matchers.anyString())).thenReturn(true);
81 eventMessage = new EventMessage(
82 new EventHeader("2016-03-15T10:59:33.79Z", "1.01", "17"),
83 new EventStatus(404, "No krokodil found"));
87 @Ignore // requires connection to a live DMaaP server
88 public void testDmaapEventSenderWithProperties() {
89 EventSenderDmaapImpl eventSender = new EventSenderDmaapImpl();
90 eventSender.initialize();
91 eventSender.setProducerMap(producerMap);
92 Assert.assertTrue(eventSender.sendEvent(MessageDestination.DCAE, eventMessage));
96 public void testDmaapEventSenderWithNullProperties() {
97 EventSenderDmaapImpl eventSender = new EventSenderDmaapImpl();
98 // eventSender.initialize();
99 eventSender.setProducerMap(producerMap);
100 Assert.assertTrue(eventSender.sendEvent(MessageDestination.DCAE, eventMessage));
104 * This test runs against a real DMaaP (or a simulator) that should be configured in test.properties file.
107 @Ignore // requires connection to a live DMaaP server
108 public void testDmaapEventSenderWithDmaapSim() {
109 EventSenderDmaapImpl eventSender = new EventSenderDmaapImpl();
110 eventSender.initialize();
111 Assert.assertTrue(eventSender.sendEvent(MessageDestination.DCAE, eventMessage));
116 @Ignore // requires connection to a live DMaaP server
117 public void testDmaapEventSenderDG() throws APPCException {
118 EventSenderDmaapImpl eventSender = new EventSenderDmaapImpl();
119 eventSender.initialize();
120 eventSender.setProducerMap(producerMap);
121 Map<String,String> params = new HashMap<>();
123 params.put("eventTime", eventMessage.getEventHeader().getEventTime());
124 params.put("apiVer", eventMessage.getEventHeader().getApiVer());
125 params.put("eventId", eventMessage.getEventHeader().getEventId());
126 params.put("reason", eventMessage.getEventStatus().getReason());
127 params.put("code", "200");
129 Assert.assertTrue(eventSender.sendEvent(MessageDestination.DCAE,params, new SvcLogicContext()));
132 @Test(expected = APPCException.class)
133 @Ignore // requires connection to a live DMaaP server
134 public void testDmaapEventSenderDGNoParams() throws APPCException {
135 EventSenderDmaapImpl eventSender = new EventSenderDmaapImpl();
136 eventSender.initialize();
137 eventSender.setProducerMap(producerMap);
138 Map<String,String> params = new HashMap<>();
140 Assert.assertFalse(eventSender.sendEvent(MessageDestination.DCAE,params, new SvcLogicContext()));
144 @Test(expected = APPCException.class)
145 @Ignore // requires connection to a live DMaaP server
146 public void testDmaapEventSenderDGNullParam() throws APPCException {
147 EventSenderDmaapImpl eventSender = new EventSenderDmaapImpl();
148 eventSender.initialize();
149 eventSender.setProducerMap(producerMap);
150 Map<String,String> params = null;
152 Assert.assertFalse(eventSender.sendEvent(MessageDestination.DCAE,params, new SvcLogicContext()));
155 @Test(expected = APPCException.class)
156 @Ignore // requires connection to a live DMaaP server
157 public void testDmaapEventSenderDGNoParam() throws APPCException {
158 EventSenderDmaapImpl eventSender = new EventSenderDmaapImpl();
159 eventSender.initialize();
160 eventSender.setProducerMap(producerMap);
161 Map<String,String> params = new HashMap<>();
163 // params.put("apiVer", eventMessage.getEventHeader().getApiVer());
164 params.put("eventId", eventMessage.getEventHeader().getEventId());
165 params.put("reason", eventMessage.getEventStatus().getReason());
166 params.put("code", "200");
168 eventSender.sendEvent(MessageDestination.DCAE,params, new SvcLogicContext());