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