First part of onap rename
[appc.git] / appc-adapters / appc-dmaap-adapter / appc-dmaap-adapter-bundle / src / test / java / org / onap / appc / adapter / messaging / dmaap / impl / TestConsumerProducerImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.adapter.messaging.dmaap.impl;
26
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertFalse;
29 import static org.junit.Assert.assertNotNull;
30 import static org.junit.Assert.assertTrue;
31
32 import java.util.*;
33
34 import org.junit.Before;
35 import org.junit.Ignore;
36 import org.junit.Test;
37 import org.onap.appc.adapter.message.Consumer;
38 import org.onap.appc.adapter.message.Producer;
39 import org.onap.appc.adapter.messaging.dmaap.impl.DmaapConsumerImpl;
40 import org.onap.appc.adapter.messaging.dmaap.impl.DmaapProducerImpl;
41 import org.onap.appc.configuration.Configuration;
42 import org.onap.appc.configuration.ConfigurationFactory;
43
44 public class TestConsumerProducerImpl {
45
46     private Collection<String> urls;
47     private String topicRead;
48     private String topicWrite;
49     private String group;
50     private String groupId;
51     private String user;
52     private String password;
53
54     @Before
55     public void setup() {
56         System.out.println("setup entry...");
57 //        urls = new HashSet<String>();
58 //        urls.add("dmaaphost1");
59 //        urls.add("dmaaphost2");
60 //        //remove unavailable dmaap instance for build
61 //        //urls.add("dmaaphost3");
62 //
63 //        topicRead = "APPC-UNIT-TEST";
64 //        topicWrite = "APPC-UNIT-TEST";
65 //        group = "APPC-CLIENT";
66 //        groupId = "0";
67         Configuration configuration = ConfigurationFactory.getConfiguration();
68         List<String> hosts = Arrays.asList(configuration.getProperty("poolMembers").split(","));
69         urls = new HashSet<String>(hosts);
70         topicRead = configuration.getProperty("topic.read");
71         topicWrite = configuration.getProperty("topic.write");
72         user = configuration.getProperty("dmaap.appc.username");
73         password = configuration.getProperty("dmaap.appc.password");
74         group = "APPC-CLIENT";
75         groupId = "0";
76
77
78         runoff();
79     }
80
81     /**
82      * Test that we can read and write and that the messages come back in order
83      */
84     @Ignore
85     @Test
86     public void testWriteRead() {
87         System.out.println("testWriteRead entry...");
88         Producer p = new DmaapProducerImpl(urls, topicWrite,user,password);
89
90         String s1 = UUID.randomUUID().toString();
91         String s2 = UUID.randomUUID().toString();
92         if (p.post("TEST", s1) == false) {
93                 // try again - 2nd attempt may succeed if cambria client failed over
94                 p.post("TEST", s1);
95         }
96         if (p.post("TEST", s2) == false) {
97                 // try again - 2nd attempt may succeed if cambria client failed over
98                 p.post("TEST", s2);
99         }
100
101         Consumer c = new DmaapConsumerImpl(urls, topicRead, group, groupId,user,password);
102         List<String> out = c.fetch();
103         // if fetch is empty, try again - a 2nd attempt may succeed if
104         // cambria client has failed over
105         if ((out == null) || out.isEmpty()) {
106                 out = c.fetch();
107         }
108
109         assertNotNull(out);
110         assertEquals(2, out.size());
111         assertEquals(s1, out.get(0));
112         assertEquals(s2, out.get(1));
113
114     }
115
116     /**
117      * Test that we can read and write and that the messages come back in order
118      */
119     @Test
120     @Ignore // Https Not support on jenkins server
121     public void testWriteReadHttps() {
122         System.out.println("testWriteReadHttps entry...");
123         Producer p = new DmaapProducerImpl(urls, topicWrite,user,password);
124         p.useHttps(true);
125
126         String s1 = UUID.randomUUID().toString();
127         String s2 = UUID.randomUUID().toString();
128         if (p.post("TEST", s1) == false) {
129                 // try again - 2nd attempt may succeed if cambria client failed over
130                 p.post("TEST", s1);
131         }
132         if (p.post("TEST", s2) == false) {
133                 // try again - 2nd attempt may succeed if cambria client failed over
134                 p.post("TEST", s2);
135         }
136
137         Consumer c = new DmaapConsumerImpl(urls, topicRead, group, groupId,user,password);
138         c.useHttps(true);
139
140         List<String> out = c.fetch();
141         // if fetch is empty, try again - a 2nd attempt may succeed if
142         // cambria client has failed over
143         if ((out == null) || out.isEmpty()) {
144                 out = c.fetch();
145         }
146
147         assertNotNull(out);
148         assertEquals(2, out.size());
149         assertEquals(s1, out.get(0));
150         assertEquals(s2, out.get(1));
151
152     }
153
154     @Test
155     @Ignore // requires connection to a live DMaaP server
156     public void testBadUrl() {
157         System.out.println("testBadUrl entry...");
158         urls.clear();
159         urls.add("something.local");
160
161         // Producer p = new DmaapProducerImpl(urls, topicWrite);
162         Consumer c = new DmaapConsumerImpl(urls, topicRead, group, groupId,user,password);
163         List<String> result = c.fetch(1000, 1000);
164
165         assertNotNull(result);
166         assertTrue(result.isEmpty());
167     }
168
169     @Test
170     @Ignore // requires connection to a live DMaaP server
171     public void testAuth() {
172         System.out.println("testAuth entry...");
173         Producer p = new DmaapProducerImpl(urls, topicWrite,user,password);
174         Consumer c = new DmaapConsumerImpl(urls, topicRead, group, groupId,user,password);
175
176         p.updateCredentials("key", "secret");
177         c.updateCredentials("key", "secret");
178
179         // TODO - Do some protected dmaap queries when the apis are updated
180     }
181
182     /**
183      * Test DMaaP client failover to another server when a bad url is encountered
184
185      */
186     @Ignore
187     @Test
188     public void testFailover() {
189         System.out.println("testFailover entry...");
190         urls.clear();
191         urls.add("openecomp2.org");  // bad url
192         urls.add("dmaaphost2");
193         Producer p = new DmaapProducerImpl(urls, topicWrite,user,password);
194
195         String s1 = UUID.randomUUID().toString();
196         if (p.post("TEST", s1) == false) {
197                 // try again - cambria client should have failed over
198                 p.post("TEST", s1);
199         }
200
201         urls.clear();
202         urls.add("openecomp3.org");  // bad url
203         urls.add("dmaaphost3");
204         
205         Consumer c = new DmaapConsumerImpl(urls, topicRead, group, groupId,user,password);
206         List<String> out = c.fetch(1000, 1000);
207         // if fetch is empty, try again - cambria client should have failed over
208         if ((out == null) || out.isEmpty()) {
209                 out = c.fetch();
210         }
211
212         assertNotNull(out);
213         assertEquals(1, out.size());
214         assertEquals(s1, out.get(0));
215     }
216     
217     /**
218      * Reads through the entire topic so it is clean for testing. WARNING - ONLY USE ON TOPICS WHERE YOU ARE THE ONLY
219      * WRITER. Could end in an infinite loop otherwise.
220      */
221     private void runoff() {
222         Consumer c = new DmaapConsumerImpl(urls, topicRead, group, groupId,user,password);
223         List<String> data;
224         do {
225             data = c.fetch(1000, 10000);
226         } while (!data.isEmpty()  && data.size()!=1);
227     }
228
229     @Test
230     @Ignore
231     public void testFilter() {
232         System.out.println("testFilter entry...");
233         List<String> res;
234         String filter = "{\"class\":\"Assigned\",\"field\":\"request\"}";
235         Consumer c = new DmaapConsumerImpl(urls, "DCAE-CLOSED-LOOP-EVENTS-DEV1510SIM", group, groupId,user,password,filter);
236         res = c.fetch(2000, 10);
237         assertFalse(res.isEmpty());
238
239         res.clear();
240         filter = "{\"class\":\"Assigned\",\"field\":\"response\"}";
241         c = new DmaapConsumerImpl(urls, "DCAE-CLOSED-LOOP-EVENTS-DEV1510SIM", group, groupId,user,password, filter);
242         res = c.fetch(2000, 10);
243         assertTrue(res.isEmpty());
244     }
245 }