Merge of new rebased code
[appc.git] / appc-adapters / appc-dmaap-adapter / appc-dmaap-adapter-bundle / src / test / java / org / openecomp / appc / adapter / messaging / dmaap / TestDmaapEventSender.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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=========================================================
20  */
21
22 package org.openecomp.appc.adapter.messaging.dmaap;
23
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;
41
42 import java.util.HashMap;
43 import java.util.Map;
44 import java.util.Properties;
45
46
47 public class TestDmaapEventSender {
48
49     private static Properties props;
50     private static Map<String,Producer> producerMap = new HashMap<>();
51     private static EventMessage eventMessage;
52
53     @BeforeClass
54     public static void setUp() {
55
56         Configuration configuration = ConfigurationFactory.getConfiguration(); // test.properties file placed in home dir.
57
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");
65
66         String eventClientKey = configuration.getProperty(EventSenderDmaapImpl.DMAAP_USERNAME);
67         if (eventClientKey != null) {
68             props.setProperty(EventSenderDmaapImpl.DMAAP_USERNAME,eventClientKey);
69         }
70         String eventClientSecret = configuration.getProperty(EventSenderDmaapImpl.DMAAP_PASSWORD);
71         if (eventClientSecret != null) {
72             props.setProperty(EventSenderDmaapImpl.DMAAP_PASSWORD, eventClientSecret);
73         }
74
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);
78
79         eventMessage = new EventMessage(
80                 new EventHeader("2016-03-15T10:59:33.79Z", "1.01", "17"),
81                 new EventStatus(404, "No krokodil found"));
82     }
83
84     @Test
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));
91     }
92
93     @Test
94     public void testDmaapEventSenderWithNullProperties() {
95         EventSenderDmaapImpl eventSender = new EventSenderDmaapImpl();
96 //        eventSender.initialize();
97         eventSender.setProducerMap(producerMap);
98         Assert.assertTrue(eventSender.sendEvent(MessageDestination.DCAE, eventMessage));
99     }
100
101     /*
102      * This test runs agains a real Dmaap (or a simulator) that should be cofigured in test.properties file.
103      */
104     @Test
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));
110     }
111
112
113     @Test
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<>();
120
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");
126
127         Assert.assertTrue(eventSender.sendEvent(MessageDestination.DCAE,params, new SvcLogicContext()));
128     }
129
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<>();
137
138         Assert.assertFalse(eventSender.sendEvent(MessageDestination.DCAE,params, new SvcLogicContext()));
139     }
140
141
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;
149
150         Assert.assertFalse(eventSender.sendEvent(MessageDestination.DCAE,params, new SvcLogicContext()));
151     }
152
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<>();
160
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");
165
166         eventSender.sendEvent(MessageDestination.DCAE,params, new SvcLogicContext());
167     }
168
169 }