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