2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Amdocs
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.appc.adapter.messaging.dmaap.impl;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
32 import org.junit.Before;
33 import org.junit.Ignore;
34 import org.junit.Test;
35 import org.openecomp.appc.adapter.message.Consumer;
36 import org.openecomp.appc.adapter.message.Producer;
37 import org.openecomp.appc.adapter.messaging.dmaap.impl.DmaapConsumerImpl;
38 import org.openecomp.appc.adapter.messaging.dmaap.impl.DmaapProducerImpl;
39 import org.openecomp.appc.configuration.Configuration;
40 import org.openecomp.appc.configuration.ConfigurationFactory;
42 public class TestConsumerProducerImpl {
44 private Collection<String> urls;
45 private String topicRead;
46 private String topicWrite;
48 private String groupId;
50 private String password;
54 System.out.println("setup entry...");
55 // urls = new HashSet<String>();
56 // urls.add("dmaaphost1");
57 // urls.add("dmaaphost2");
58 // //remove unavailable dmaap instance for build
59 // //urls.add("dmaaphost3");
61 // topicRead = "APPC-UNIT-TEST";
62 // topicWrite = "APPC-UNIT-TEST";
63 // group = "APPC-CLIENT";
65 Configuration configuration = ConfigurationFactory.getConfiguration();
66 List<String> hosts = Arrays.asList(configuration.getProperty("poolMembers").split(","));
67 urls = new HashSet<String>(hosts);
68 topicRead = configuration.getProperty("topic.read");
69 topicWrite = configuration.getProperty("topic.write");
70 user = configuration.getProperty("dmaap.appc.username");
71 password = configuration.getProperty("dmaap.appc.password");
72 group = "APPC-CLIENT";
80 * Test that we can read and write and that the messages come back in order
84 public void testWriteRead() {
85 System.out.println("testWriteRead entry...");
86 Producer p = new DmaapProducerImpl(urls, topicWrite,user,password);
88 String s1 = UUID.randomUUID().toString();
89 String s2 = UUID.randomUUID().toString();
90 if (p.post("TEST", s1) == false) {
91 // try again - 2nd attempt may succeed if cambria client failed over
94 if (p.post("TEST", s2) == false) {
95 // try again - 2nd attempt may succeed if cambria client failed over
99 Consumer c = new DmaapConsumerImpl(urls, topicRead, group, groupId,user,password);
100 List<String> out = c.fetch();
101 // if fetch is empty, try again - a 2nd attempt may succeed if
102 // cambria client has failed over
103 if ((out == null) || out.isEmpty()) {
108 assertEquals(2, out.size());
109 assertEquals(s1, out.get(0));
110 assertEquals(s2, out.get(1));
115 * Test that we can read and write and that the messages come back in order
118 @Ignore // Https Not support on jenkins server
119 public void testWriteReadHttps() {
120 System.out.println("testWriteReadHttps entry...");
121 Producer p = new DmaapProducerImpl(urls, topicWrite,user,password);
124 String s1 = UUID.randomUUID().toString();
125 String s2 = UUID.randomUUID().toString();
126 if (p.post("TEST", s1) == false) {
127 // try again - 2nd attempt may succeed if cambria client failed over
130 if (p.post("TEST", s2) == false) {
131 // try again - 2nd attempt may succeed if cambria client failed over
135 Consumer c = new DmaapConsumerImpl(urls, topicRead, group, groupId,user,password);
138 List<String> out = c.fetch();
139 // if fetch is empty, try again - a 2nd attempt may succeed if
140 // cambria client has failed over
141 if ((out == null) || out.isEmpty()) {
146 assertEquals(2, out.size());
147 assertEquals(s1, out.get(0));
148 assertEquals(s2, out.get(1));
153 @Ignore // requires connection to a live DMaaP server
154 public void testBadUrl() {
155 System.out.println("testBadUrl entry...");
157 urls.add("something.local");
159 // Producer p = new DmaapProducerImpl(urls, topicWrite);
160 Consumer c = new DmaapConsumerImpl(urls, topicRead, group, groupId,user,password);
161 List<String> result = c.fetch(1000, 1000);
163 assertNotNull(result);
164 assertTrue(result.isEmpty());
168 @Ignore // requires connection to a live DMaaP server
169 public void testAuth() {
170 System.out.println("testAuth entry...");
171 Producer p = new DmaapProducerImpl(urls, topicWrite,user,password);
172 Consumer c = new DmaapConsumerImpl(urls, topicRead, group, groupId,user,password);
174 p.updateCredentials("key", "secret");
175 c.updateCredentials("key", "secret");
177 // TODO - Do some protected dmaap queries when the apis are updated
181 * Test DMaaP client failover to another server when a bad url is encountered
186 public void testFailover() {
187 System.out.println("testFailover entry...");
189 urls.add("openecomp2.org"); // bad url
190 urls.add("dmaaphost2");
191 Producer p = new DmaapProducerImpl(urls, topicWrite,user,password);
193 String s1 = UUID.randomUUID().toString();
194 if (p.post("TEST", s1) == false) {
195 // try again - cambria client should have failed over
200 urls.add("openecomp3.org"); // bad url
201 urls.add("dmaaphost3");
203 Consumer c = new DmaapConsumerImpl(urls, topicRead, group, groupId,user,password);
204 List<String> out = c.fetch(1000, 1000);
205 // if fetch is empty, try again - cambria client should have failed over
206 if ((out == null) || out.isEmpty()) {
211 assertEquals(1, out.size());
212 assertEquals(s1, out.get(0));
216 * Reads through the entire topic so it is clean for testing. WARNING - ONLY USE ON TOPICS WHERE YOU ARE THE ONLY
217 * WRITER. Could end in an infinite loop otherwise.
219 private void runoff() {
220 Consumer c = new DmaapConsumerImpl(urls, topicRead, group, groupId,user,password);
223 data = c.fetch(1000, 10000);
224 } while (!data.isEmpty() && data.size()!=1);
229 public void testFilter() {
230 System.out.println("testFilter entry...");
232 String filter = "{\"class\":\"Assigned\",\"field\":\"request\"}";
233 Consumer c = new DmaapConsumerImpl(urls, "DCAE-CLOSED-LOOP-EVENTS-DEV1510SIM", group, groupId,user,password,filter);
234 res = c.fetch(2000, 10);
235 assertFalse(res.isEmpty());
238 filter = "{\"class\":\"Assigned\",\"field\":\"response\"}";
239 c = new DmaapConsumerImpl(urls, "DCAE-CLOSED-LOOP-EVENTS-DEV1510SIM", group, groupId,user,password, filter);
240 res = c.fetch(2000, 10);
241 assertTrue(res.isEmpty());