2 * ============LICENSE_START=======================================================
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
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 * ============LICENSE_END=========================================================
24 package org.onap.appc.adapter.messaging.dmaap.impl;
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;
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;
43 public class TestConsumerProducerImpl {
45 private Collection<String> urls;
46 private String topicRead;
47 private String topicWrite;
49 private String groupId;
51 private String password;
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");
62 // topicRead = "APPC-UNIT-TEST";
63 // topicWrite = "APPC-UNIT-TEST";
64 // group = "APPC-CLIENT";
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";
81 * Test that we can read and write and that the messages come back in order
85 public void testWriteRead() {
86 System.out.println("testWriteRead entry...");
87 Producer p = new DmaapProducerImpl(urls, topicWrite,user,password);
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
95 if (p.post("TEST", s2) == false) {
96 // try again - 2nd attempt may succeed if cambria client failed over
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()) {
109 assertEquals(2, out.size());
110 assertEquals(s1, out.get(0));
111 assertEquals(s2, out.get(1));
116 * Test that we can read and write and that the messages come back in order
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);
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
131 if (p.post("TEST", s2) == false) {
132 // try again - 2nd attempt may succeed if cambria client failed over
136 Consumer c = new DmaapConsumerImpl(urls, topicRead, group, groupId,user,password);
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()) {
147 assertEquals(2, out.size());
148 assertEquals(s1, out.get(0));
149 assertEquals(s2, out.get(1));
154 @Ignore // requires connection to a live DMaaP server
155 public void testBadUrl() {
156 System.out.println("testBadUrl entry...");
158 urls.add("something.local");
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);
164 assertNotNull(result);
165 assertTrue(result.isEmpty());
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);
175 p.updateCredentials("key", "secret");
176 c.updateCredentials("key", "secret");
178 // TODO - Do some protected dmaap queries when the apis are updated
182 * Test DMaaP client failover to another server when a bad url is encountered
187 public void testFailover() {
188 System.out.println("testFailover entry...");
190 urls.add("openecomp2.org"); // bad url
191 urls.add("dmaaphost2");
192 Producer p = new DmaapProducerImpl(urls, topicWrite,user,password);
194 String s1 = UUID.randomUUID().toString();
195 if (p.post("TEST", s1) == false) {
196 // try again - cambria client should have failed over
201 urls.add("openecomp3.org"); // bad url
202 urls.add("dmaaphost3");
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()) {
212 assertEquals(1, out.size());
213 assertEquals(s1, out.get(0));
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.
220 private void runoff() {
221 Consumer c = new DmaapConsumerImpl(urls, topicRead, group, groupId,user,password);
224 data = c.fetch(1000, 10000);
225 } while (!data.isEmpty() && data.size()!=1);
230 public void testFilter() {
231 System.out.println("testFilter entry...");
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());
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());