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