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