Changed to unmaintained
[appc.git] / appc-adapters / appc-dmaap-adapter / appc-dmaap-adapter-bundle / src / test / java / org / onap / appc / adapter / messaging / dmaap / TestDmaapEventSender.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.adapter.messaging.dmaap;
25
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;
43
44 import java.util.HashMap;
45 import java.util.Map;
46 import java.util.Properties;
47
48
49 public class TestDmaapEventSender {
50
51     private static Properties props;
52     private static Map<String,Producer> producerMap = new HashMap<>();
53     private static EventMessage eventMessage;
54
55     @BeforeClass
56     public static void setUp() {
57
58         Configuration configuration = ConfigurationFactory.getConfiguration(); // test.properties file placed in home dir.
59
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");
67
68         String eventClientKey = configuration.getProperty(EventSenderDmaapImpl.DMAAP_USERNAME);
69         if (eventClientKey != null) {
70             props.setProperty(EventSenderDmaapImpl.DMAAP_USERNAME,eventClientKey);
71         }
72         String eventClientSecret = configuration.getProperty(EventSenderDmaapImpl.DMAAP_PASSWORD);
73         if (eventClientSecret != null) {
74             props.setProperty(EventSenderDmaapImpl.DMAAP_PASSWORD, eventClientSecret);
75         }
76
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);
80
81         eventMessage = new EventMessage(
82                 new EventHeader("2016-03-15T10:59:33.79Z", "1.01", "17"),
83                 new EventStatus(404, "No krokodil found"));
84     }
85
86     @Test
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));
93     }
94
95     @Test
96     public void testDmaapEventSenderWithNullProperties() {
97         EventSenderDmaapImpl eventSender = new EventSenderDmaapImpl();
98 //        eventSender.initialize();
99         eventSender.setProducerMap(producerMap);
100         Assert.assertTrue(eventSender.sendEvent(MessageDestination.DCAE, eventMessage));
101     }
102
103     /*
104      * This test runs against a real DMaaP (or a simulator) that should be configured in test.properties file.
105      */
106     @Test
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));
112     }
113
114
115     @Test
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<>();
122
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");
128
129         Assert.assertTrue(eventSender.sendEvent(MessageDestination.DCAE,params, new SvcLogicContext()));
130     }
131
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<>();
139
140         Assert.assertFalse(eventSender.sendEvent(MessageDestination.DCAE,params, new SvcLogicContext()));
141     }
142
143
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;
151
152         Assert.assertFalse(eventSender.sendEvent(MessageDestination.DCAE,params, new SvcLogicContext()));
153     }
154
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<>();
162
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");
167
168         eventSender.sendEvent(MessageDestination.DCAE,params, new SvcLogicContext());
169     }
170
171 }