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