1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright © 2021 Orange.
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
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=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 *******************************************************************************/
24 package org.onap.dmaap.mr.client.impl;
26 import junit.framework.TestCase;
27 import org.junit.Test;
28 import org.onap.dmaap.mr.client.MRClientFactory;
29 import org.onap.dmaap.mr.client.ProtocolType;
32 import java.io.FileOutputStream;
33 import java.io.IOException;
34 import java.util.LinkedList;
35 import java.util.Properties;
37 public class MRConsumerImplTest extends TestCase {
39 public void testNullFilter() throws IOException {
40 final LinkedList<String> hosts = new LinkedList<String>();
41 hosts.add("localhost:8080");
42 final MRConsumerImpl c = new MRConsumerImpl.MRConsumerImplBuilder().setHostPart(hosts)
43 .setTopic("topic").setConsumerGroup("cg").setConsumerId("cid").setTimeoutMs(-1)
44 .setLimit(-1).setFilter(null).setApiKey_username(null).setApiSecret_password(null)
45 .createMRConsumerImpl();
46 final String url = c.createUrlPath(MRConstants.makeConsumerUrl("localhost:8080", "topic", "cg", "cid", "http"),
48 assertEquals("http://localhost:8080/events/" + "topic/cg/cid", url);
52 public void testFilterWithNoTimeoutOrLimit() throws IOException {
53 final LinkedList<String> hosts = new LinkedList<String>();
54 hosts.add("localhost:8080");
55 final MRConsumerImpl c = new MRConsumerImpl.MRConsumerImplBuilder().setHostPart(hosts)
56 .setTopic("topic").setConsumerGroup("cg").setConsumerId("cid").setTimeoutMs(-1)
57 .setLimit(-1).setFilter("filter").setApiKey_username(null)
58 .setApiSecret_password(null).createMRConsumerImpl();
59 final String url = c.createUrlPath(MRConstants.makeConsumerUrl("localhost:8080", "topic", "cg", "cid", "http"),
61 assertEquals("http://localhost:8080/events/" + "topic/cg/cid?filter=filter", url);
65 public void testTimeoutNoLimitNoFilter() throws IOException {
66 final LinkedList<String> hosts = new LinkedList<String>();
67 hosts.add("localhost:8080");
68 final MRConsumerImpl c = new MRConsumerImpl.MRConsumerImplBuilder().setHostPart(hosts)
69 .setTopic("topic").setConsumerGroup("cg").setConsumerId("cid").setTimeoutMs(30000)
70 .setLimit(-1).setFilter(null).setApiKey_username(null).setApiSecret_password(null)
71 .createMRConsumerImpl();
72 final String url = c.createUrlPath(MRConstants.makeConsumerUrl("localhost:8080", "topic", "cg", "cid", "http"),
74 assertEquals("http://localhost:8080/events/" + "topic/cg/cid?timeout=30000", url);
78 public void testNoTimeoutWithLimitNoFilter() throws IOException {
79 final LinkedList<String> hosts = new LinkedList<String>();
80 hosts.add("localhost:8080");
81 final MRConsumerImpl c = new MRConsumerImpl.MRConsumerImplBuilder().setHostPart(hosts)
82 .setTopic("topic").setConsumerGroup("cg").setConsumerId("cid").setTimeoutMs(-1)
83 .setLimit(100).setFilter(null).setApiKey_username(null).setApiSecret_password(null)
84 .createMRConsumerImpl();
85 final String url = c.createUrlPath(MRConstants.makeConsumerUrl("localhost:8080", "topic", "cg", "cid", "http"),
87 assertEquals("http://localhost:8080/events/" + "topic/cg/cid?limit=100", url);
91 public void testWithTimeoutWithLimitWithFilter() throws IOException {
92 final LinkedList<String> hosts = new LinkedList<String>();
93 hosts.add("localhost:8080");
94 final MRConsumerImpl c = new MRConsumerImpl.MRConsumerImplBuilder().setHostPart(hosts)
95 .setTopic("topic").setConsumerGroup("cg").setConsumerId("cid").setTimeoutMs(1000)
96 .setLimit(400).setFilter("f").setApiKey_username(null).setApiSecret_password(null)
97 .createMRConsumerImpl();
98 final String url = c.createUrlPath(MRConstants.makeConsumerUrl("localhost:8080", "topic", "cg", "cid", "http"),
100 assertEquals("http://localhost:8080/events/" + "topic/cg/cid?timeout=1000&limit=400&filter=f", url);
104 public void testFilterEncoding() throws IOException {
105 final LinkedList<String> hosts = new LinkedList<String>();
106 hosts.add("localhost:8080");
107 final MRConsumerImpl c = new MRConsumerImpl.MRConsumerImplBuilder().setHostPart(hosts)
108 .setTopic("topic").setConsumerGroup("cg").setConsumerId("cid").setTimeoutMs(-1)
109 .setLimit(-1).setFilter("{ \"foo\"=\"bar\"bar\" }").setApiKey_username(null)
110 .setApiSecret_password(null).createMRConsumerImpl();
111 final String url = c.createUrlPath(MRConstants.makeConsumerUrl("localhost:8080", "topic", "cg", "cid", "http"),
113 assertEquals("http://localhost:8080/events/" + "topic/cg/cid?filter=%7B+%22foo%22%3D%22bar%22bar%22+%7D", url);
117 public void testFetchWithReturnConsumerResponse() throws IOException {
118 final LinkedList<String> hosts = new LinkedList<String>();
119 hosts.add("localhost:8080");
120 Properties properties = new Properties();
122 MRSimplerBatchPublisherTest.class.getClassLoader().getResourceAsStream("dme2/consumer.properties"));
124 String routeFilePath = "dme2/preferredRoute.txt";
126 File file = new File(MRSimplerBatchPublisherTest.class.getClassLoader().getResource(routeFilePath).getFile());
127 properties.put("routeFilePath",
128 MRSimplerBatchPublisherTest.class.getClassLoader().getResource(routeFilePath).getFile());
130 File outFile = new File(file.getParent() + "/consumer_tmp.properties");
131 properties.store(new FileOutputStream(outFile), "");
133 MRClientFactory.prop = properties;
135 final MRConsumerImpl c = new MRConsumerImpl.MRConsumerImplBuilder().setHostPart(hosts)
136 .setTopic("topic").setConsumerGroup("cg").setConsumerId("cid").setTimeoutMs(-1)
137 .setLimit(-1).setFilter("{ \"foo\"=\"bar\"bar\" }").setApiKey_username(null)
138 .setApiSecret_password(null).createMRConsumerImpl();
139 c.setProps(properties);
140 assertNotNull(c.fetchWithReturnConsumerResponse());
141 c.setProtocolFlag(ProtocolType.AAF_AUTH.getValue());
142 assertNotNull(c.fetchWithReturnConsumerResponse());
143 c.setProtocolFlag(ProtocolType.HTTPNOAUTH.getValue());
144 assertNotNull(c.fetchWithReturnConsumerResponse());
145 c.setProtocolFlag(ProtocolType.AUTH_KEY.getValue());
146 assertNotNull(c.fetchWithReturnConsumerResponse());
151 public void testFetch() throws Exception {
152 final LinkedList<String> hosts = new LinkedList<String>();
153 hosts.add("localhost:8080");
156 Properties properties = new Properties();
158 MRSimplerBatchPublisherTest.class.getClassLoader().getResourceAsStream("dme2/consumer.properties"));
160 String routeFilePath = "dme2/preferredRoute.txt";
162 File file = new File(MRSimplerBatchPublisherTest.class.getClassLoader().getResource(routeFilePath).getFile());
163 properties.put("routeFilePath",
164 MRSimplerBatchPublisherTest.class.getClassLoader().getResource(routeFilePath).getFile());
166 File outFile = new File(file.getParent() + "/consumer_tmp.properties");
167 properties.store(new FileOutputStream(outFile), "");
169 MRClientFactory.prop = properties;
170 final MRConsumerImpl c = new MRConsumerImpl.MRConsumerImplBuilder().setHostPart(hosts)
171 .setTopic("topic").setConsumerGroup("cg").setConsumerId("cid").setTimeoutMs(-1)
172 .setLimit(-1).setFilter("{ \"foo\"=\"bar\"bar\" }").setApiKey_username(null)
173 .setApiSecret_password(null).createMRConsumerImpl();
174 c.setProps(properties);
177 } catch (Exception e) {
180 c.setProtocolFlag(ProtocolType.AAF_AUTH.getValue());
183 } catch (Exception e) {
186 c.setProtocolFlag(ProtocolType.HTTPNOAUTH.getValue());
189 } catch (Exception e) {
192 c.setProtocolFlag(ProtocolType.AUTH_KEY.getValue());
195 } catch (Exception e) {