2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.adapter.messaging.dmaap.impl;
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;
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;
44 public class TestConsumerProducerImpl {
46 private Collection<String> urls;
47 private String topicRead;
48 private String topicWrite;
50 private String groupId;
52 private String password;
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");
63 // topicRead = "APPC-UNIT-TEST";
64 // topicWrite = "APPC-UNIT-TEST";
65 // group = "APPC-CLIENT";
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";
82 * Test that we can read and write and that the messages come back in order
86 public void testWriteRead() {
87 System.out.println("testWriteRead entry...");
88 Producer p = new DmaapProducerImpl(urls, topicWrite,user,password);
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
96 if (p.post("TEST", s2) == false) {
97 // try again - 2nd attempt may succeed if cambria client failed over
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()) {
110 assertEquals(2, out.size());
111 assertEquals(s1, out.get(0));
112 assertEquals(s2, out.get(1));
117 * Test that we can read and write and that the messages come back in order
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);
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
132 if (p.post("TEST", s2) == false) {
133 // try again - 2nd attempt may succeed if cambria client failed over
137 Consumer c = new DmaapConsumerImpl(urls, topicRead, group, groupId,user,password);
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()) {
148 assertEquals(2, out.size());
149 assertEquals(s1, out.get(0));
150 assertEquals(s2, out.get(1));
155 @Ignore // requires connection to a live DMaaP server
156 public void testBadUrl() {
157 System.out.println("testBadUrl entry...");
159 urls.add("something.local");
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);
165 assertNotNull(result);
166 assertTrue(result.isEmpty());
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);
176 p.updateCredentials("key", "secret");
177 c.updateCredentials("key", "secret");
179 // TODO - Do some protected dmaap queries when the apis are updated
183 * Test DMaaP client failover to another server when a bad url is encountered
188 public void testFailover() {
189 System.out.println("testFailover entry...");
191 urls.add("openecomp2.org"); // bad url
192 urls.add("dmaaphost2");
193 Producer p = new DmaapProducerImpl(urls, topicWrite,user,password);
195 String s1 = UUID.randomUUID().toString();
196 if (p.post("TEST", s1) == false) {
197 // try again - cambria client should have failed over
202 urls.add("openecomp3.org"); // bad url
203 urls.add("dmaaphost3");
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()) {
213 assertEquals(1, out.size());
214 assertEquals(s1, out.get(0));
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.
221 private void runoff() {
222 Consumer c = new DmaapConsumerImpl(urls, topicRead, group, groupId,user,password);
225 data = c.fetch(1000, 10000);
226 } while (!data.isEmpty() && data.size()!=1);
231 public void testFilter() {
232 System.out.println("testFilter entry...");
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());
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());