2 * ============LICENSE_START=======================================================
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
15 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.adapter.messaging.dmaap.impl;
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;
46 public class TestConsumerProducerImpl {
48 private Collection<String> urls;
49 private String topicRead;
50 private String topicWrite;
52 private String groupId;
54 private String password;
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");
65 // topicRead = "APPC-UNIT-TEST";
66 // topicWrite = "APPC-UNIT-TEST";
67 // group = "APPC-CLIENT";
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";
84 * Test that we can read and write and that the messages come back in order
88 public void testWriteRead() {
89 System.out.println("testWriteRead entry...");
90 Producer p = new DmaapProducerImpl(urls, topicWrite,user,password);
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
98 if (p.post("TEST", s2) == false) {
99 // try again - 2nd attempt may succeed if cambria client failed over
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()) {
112 assertEquals(2, out.size());
113 assertEquals(s1, out.get(0));
114 assertEquals(s2, out.get(1));
119 * Test that we can read and write and that the messages come back in order
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);
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
134 if (p.post("TEST", s2) == false) {
135 // try again - 2nd attempt may succeed if cambria client failed over
139 Consumer c = new DmaapConsumerImpl(urls, topicRead, group, groupId,user,password);
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()) {
150 assertEquals(2, out.size());
151 assertEquals(s1, out.get(0));
152 assertEquals(s2, out.get(1));
157 @Ignore // requires connection to a live DMaaP server
158 public void testBadUrl() {
159 System.out.println("testBadUrl entry...");
161 urls.add("something.local");
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);
167 assertNotNull(result);
168 assertTrue(result.isEmpty());
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);
178 p.updateCredentials("key", "secret");
179 c.updateCredentials("key", "secret");
181 // TODO - Do some protected dmaap queries when the apis are updated
185 * Test DMaaP client failover to another server when a bad url is encountered
190 public void testFailover() {
191 System.out.println("testFailover entry...");
193 urls.add("openecomp2.org"); // bad url
194 urls.add("dmaaphost2");
195 Producer p = new DmaapProducerImpl(urls, topicWrite,user,password);
197 String s1 = UUID.randomUUID().toString();
198 if (p.post("TEST", s1) == false) {
199 // try again - cambria client should have failed over
204 urls.add("openecomp3.org"); // bad url
205 urls.add("dmaaphost3");
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()) {
215 assertEquals(1, out.size());
216 assertEquals(s1, out.get(0));
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.
223 private void runoff() {
224 Consumer c = new DmaapConsumerImpl(urls, topicRead, group, groupId,user,password);
227 data = c.fetch(1000, 10000);
228 } while (!data.isEmpty() && data.size()!=1);
233 public void testFilter() {
234 System.out.println("testFilter entry...");
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());
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());