2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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=========================================================
22 package org.openecomp.appc.adapter.messaging.dmaap.impl;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
31 import org.junit.Before;
32 import org.junit.Ignore;
33 import org.junit.Test;
34 import org.openecomp.appc.adapter.message.Consumer;
35 import org.openecomp.appc.adapter.message.Producer;
36 import org.openecomp.appc.adapter.messaging.dmaap.impl.DmaapConsumerImpl;
37 import org.openecomp.appc.adapter.messaging.dmaap.impl.DmaapProducerImpl;
38 import org.openecomp.appc.configuration.Configuration;
39 import org.openecomp.appc.configuration.ConfigurationFactory;
41 public class TestConsumerProducerImpl {
43 private Collection<String> urls;
44 private String topicRead;
45 private String topicWrite;
47 private String groupId;
49 private String password;
53 System.out.println("setup entry...");
54 // urls = new HashSet<String>();
55 // urls.add("dmaaphost1");
56 // urls.add("dmaaphost2");
57 // //remove unavailable dmaap instance for build
58 // //urls.add("dmaaphost3");
60 // topicRead = "APPC-UNIT-TEST";
61 // topicWrite = "APPC-UNIT-TEST";
62 // group = "APPC-CLIENT";
64 Configuration configuration = ConfigurationFactory.getConfiguration();
65 List<String> hosts = Arrays.asList(configuration.getProperty("poolMembers").split(","));
66 urls = new HashSet<String>(hosts);
67 topicRead = configuration.getProperty("topic.read");
68 topicWrite = configuration.getProperty("topic.write");
69 user = configuration.getProperty("dmaap.appc.username");
70 password = configuration.getProperty("dmaap.appc.password");
71 group = "APPC-CLIENT";
79 * Test that we can read and write and that the messages come back in order
83 public void testWriteRead() {
84 System.out.println("testWriteRead entry...");
85 Producer p = new DmaapProducerImpl(urls, topicWrite,user,password);
87 String s1 = UUID.randomUUID().toString();
88 String s2 = UUID.randomUUID().toString();
89 if (p.post("TEST", s1) == false) {
90 // try again - 2nd attempt may succeed if cambria client failed over
93 if (p.post("TEST", s2) == false) {
94 // try again - 2nd attempt may succeed if cambria client failed over
98 Consumer c = new DmaapConsumerImpl(urls, topicRead, group, groupId,user,password);
99 List<String> out = c.fetch();
100 // if fetch is empty, try again - a 2nd attempt may succeed if
101 // cambria client has failed over
102 if ((out == null) || out.isEmpty()) {
107 assertEquals(2, out.size());
108 assertEquals(s1, out.get(0));
109 assertEquals(s2, out.get(1));
114 * Test that we can read and write and that the messages come back in order
117 @Ignore // Https Not support on jenkins server
118 public void testWriteReadHttps() {
119 System.out.println("testWriteReadHttps entry...");
120 Producer p = new DmaapProducerImpl(urls, topicWrite,user,password);
123 String s1 = UUID.randomUUID().toString();
124 String s2 = UUID.randomUUID().toString();
125 if (p.post("TEST", s1) == false) {
126 // try again - 2nd attempt may succeed if cambria client failed over
129 if (p.post("TEST", s2) == false) {
130 // try again - 2nd attempt may succeed if cambria client failed over
134 Consumer c = new DmaapConsumerImpl(urls, topicRead, group, groupId,user,password);
137 List<String> out = c.fetch();
138 // if fetch is empty, try again - a 2nd attempt may succeed if
139 // cambria client has failed over
140 if ((out == null) || out.isEmpty()) {
145 assertEquals(2, out.size());
146 assertEquals(s1, out.get(0));
147 assertEquals(s2, out.get(1));
152 @Ignore // requires connection to a live DMaaP server
153 public void testBadUrl() {
154 System.out.println("testBadUrl entry...");
156 urls.add("something.local");
158 // Producer p = new DmaapProducerImpl(urls, topicWrite);
159 Consumer c = new DmaapConsumerImpl(urls, topicRead, group, groupId,user,password);
160 List<String> result = c.fetch(1000, 1000);
162 assertNotNull(result);
163 assertTrue(result.isEmpty());
167 @Ignore // requires connection to a live DMaaP server
168 public void testAuth() {
169 System.out.println("testAuth entry...");
170 Producer p = new DmaapProducerImpl(urls, topicWrite,user,password);
171 Consumer c = new DmaapConsumerImpl(urls, topicRead, group, groupId,user,password);
173 p.updateCredentials("key", "secret");
174 c.updateCredentials("key", "secret");
176 // TODO - Do some protected dmaap queries when the apis are updated
180 * Test DMaaP client failover to another server when a bad url is encountered
185 public void testFailover() {
186 System.out.println("testFailover entry...");
188 urls.add("openecomp2.org"); // bad url
189 urls.add("dmaaphost2");
190 Producer p = new DmaapProducerImpl(urls, topicWrite,user,password);
192 String s1 = UUID.randomUUID().toString();
193 if (p.post("TEST", s1) == false) {
194 // try again - cambria client should have failed over
199 urls.add("openecomp3.org"); // bad url
200 urls.add("dmaaphost3");
202 Consumer c = new DmaapConsumerImpl(urls, topicRead, group, groupId,user,password);
203 List<String> out = c.fetch(1000, 1000);
204 // if fetch is empty, try again - cambria client should have failed over
205 if ((out == null) || out.isEmpty()) {
210 assertEquals(1, out.size());
211 assertEquals(s1, out.get(0));
215 * Reads through the entire topic so it is clean for testing. WARNING - ONLY USE ON TOPICS WHERE YOU ARE THE ONLY
216 * WRITER. Could end in an infinite loop otherwise.
218 private void runoff() {
219 Consumer c = new DmaapConsumerImpl(urls, topicRead, group, groupId,user,password);
222 data = c.fetch(1000, 10000);
223 } while (!data.isEmpty() && data.size()!=1);
228 public void testFilter() {
229 System.out.println("testFilter entry...");
231 String filter = "{\"class\":\"Assigned\",\"field\":\"request\"}";
232 Consumer c = new DmaapConsumerImpl(urls, "DCAE-CLOSED-LOOP-EVENTS-DEV1510SIM", group, groupId,user,password,filter);
233 res = c.fetch(2000, 10);
234 assertFalse(res.isEmpty());
237 filter = "{\"class\":\"Assigned\",\"field\":\"response\"}";
238 c = new DmaapConsumerImpl(urls, "DCAE-CLOSED-LOOP-EVENTS-DEV1510SIM", group, groupId,user,password, filter);
239 res = c.fetch(2000, 10);
240 assertTrue(res.isEmpty());