1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
22 package org.onap.dmaap.mr.client.impl;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.util.LinkedList;
28 import java.util.Properties;
30 import junit.framework.TestCase;
32 import org.junit.Test;
33 import org.onap.dmaap.mr.client.MRClientFactory;
34 import org.onap.dmaap.mr.test.clients.ProtocolTypeConstants;
36 public class MRConsumerImplTest extends TestCase {
38 public void testNullFilter() throws IOException {
39 final LinkedList<String> hosts = new LinkedList<String>();
40 hosts.add("localhost:8080");
41 final MRConsumerImpl c = new MRConsumerImpl.MRConsumerImplBuilder().setHostPart(hosts)
42 .setTopic("topic").setConsumerGroup("cg").setConsumerId("cid").setTimeoutMs(-1)
43 .setLimit(-1).setFilter(null).setApiKey_username(null).setApiSecret_password(null)
44 .createMRConsumerImpl();
45 final String url = c.createUrlPath(MRConstants.makeConsumerUrl("localhost:8080", "topic", "cg", "cid", "http"),
47 assertEquals("http://localhost:8080/events/" + "topic/cg/cid", url);
51 public void testFilterWithNoTimeoutOrLimit() throws IOException {
52 final LinkedList<String> hosts = new LinkedList<String>();
53 hosts.add("localhost:8080");
54 final MRConsumerImpl c = new MRConsumerImpl.MRConsumerImplBuilder().setHostPart(hosts)
55 .setTopic("topic").setConsumerGroup("cg").setConsumerId("cid").setTimeoutMs(-1)
56 .setLimit(-1).setFilter("filter").setApiKey_username(null)
57 .setApiSecret_password(null).createMRConsumerImpl();
58 final String url = c.createUrlPath(MRConstants.makeConsumerUrl("localhost:8080", "topic", "cg", "cid", "http"),
60 assertEquals("http://localhost:8080/events/" + "topic/cg/cid?filter=filter", url);
64 public void testTimeoutNoLimitNoFilter() throws IOException {
65 final LinkedList<String> hosts = new LinkedList<String>();
66 hosts.add("localhost:8080");
67 final MRConsumerImpl c = new MRConsumerImpl.MRConsumerImplBuilder().setHostPart(hosts)
68 .setTopic("topic").setConsumerGroup("cg").setConsumerId("cid").setTimeoutMs(30000)
69 .setLimit(-1).setFilter(null).setApiKey_username(null).setApiSecret_password(null)
70 .createMRConsumerImpl();
71 final String url = c.createUrlPath(MRConstants.makeConsumerUrl("localhost:8080", "topic", "cg", "cid", "http"),
73 assertEquals("http://localhost:8080/events/" + "topic/cg/cid?timeout=30000", url);
77 public void testNoTimeoutWithLimitNoFilter() throws IOException {
78 final LinkedList<String> hosts = new LinkedList<String>();
79 hosts.add("localhost:8080");
80 final MRConsumerImpl c = new MRConsumerImpl.MRConsumerImplBuilder().setHostPart(hosts)
81 .setTopic("topic").setConsumerGroup("cg").setConsumerId("cid").setTimeoutMs(-1)
82 .setLimit(100).setFilter(null).setApiKey_username(null).setApiSecret_password(null)
83 .createMRConsumerImpl();
84 final String url = c.createUrlPath(MRConstants.makeConsumerUrl("localhost:8080", "topic", "cg", "cid", "http"),
86 assertEquals("http://localhost:8080/events/" + "topic/cg/cid?limit=100", url);
90 public void testWithTimeoutWithLimitWithFilter() throws IOException {
91 final LinkedList<String> hosts = new LinkedList<String>();
92 hosts.add("localhost:8080");
93 final MRConsumerImpl c = new MRConsumerImpl.MRConsumerImplBuilder().setHostPart(hosts)
94 .setTopic("topic").setConsumerGroup("cg").setConsumerId("cid").setTimeoutMs(1000)
95 .setLimit(400).setFilter("f").setApiKey_username(null).setApiSecret_password(null)
96 .createMRConsumerImpl();
97 final String url = c.createUrlPath(MRConstants.makeConsumerUrl("localhost:8080", "topic", "cg", "cid", "http"),
99 assertEquals("http://localhost:8080/events/" + "topic/cg/cid?timeout=1000&limit=400&filter=f", url);
103 public void testFilterEncoding() throws IOException {
104 final LinkedList<String> hosts = new LinkedList<String>();
105 hosts.add("localhost:8080");
106 final MRConsumerImpl c = new MRConsumerImpl.MRConsumerImplBuilder().setHostPart(hosts)
107 .setTopic("topic").setConsumerGroup("cg").setConsumerId("cid").setTimeoutMs(-1)
108 .setLimit(-1).setFilter("{ \"foo\"=\"bar\"bar\" }").setApiKey_username(null)
109 .setApiSecret_password(null).createMRConsumerImpl();
110 final String url = c.createUrlPath(MRConstants.makeConsumerUrl("localhost:8080", "topic", "cg", "cid", "http"),
112 assertEquals("http://localhost:8080/events/" + "topic/cg/cid?filter=%7B+%22foo%22%3D%22bar%22bar%22+%7D", url);
116 public void testFetchWithReturnConsumerResponse() throws IOException {
117 final LinkedList<String> hosts = new LinkedList<String>();
118 hosts.add("localhost:8080");
119 Properties properties = new Properties();
121 MRSimplerBatchPublisherTest.class.getClassLoader().getResourceAsStream("dme2/consumer.properties"));
123 String routeFilePath = "dme2/preferredRoute.txt";
125 File file = new File(MRSimplerBatchPublisherTest.class.getClassLoader().getResource(routeFilePath).getFile());
126 properties.put("routeFilePath",
127 MRSimplerBatchPublisherTest.class.getClassLoader().getResource(routeFilePath).getFile());
129 File outFile = new File(file.getParent() + "/consumer_tmp.properties");
130 properties.store(new FileOutputStream(outFile), "");
132 MRClientFactory.prop=properties;
134 final MRConsumerImpl c = new MRConsumerImpl.MRConsumerImplBuilder().setHostPart(hosts)
135 .setTopic("topic").setConsumerGroup("cg").setConsumerId("cid").setTimeoutMs(-1)
136 .setLimit(-1).setFilter("{ \"foo\"=\"bar\"bar\" }").setApiKey_username(null)
137 .setApiSecret_password(null).createMRConsumerImpl();
138 c.setProps(properties);
139 assertNotNull(c.fetchWithReturnConsumerResponse());
140 c.setProtocolFlag(ProtocolTypeConstants.AAF_AUTH.getValue());
141 assertNotNull(c.fetchWithReturnConsumerResponse());
142 c.setProtocolFlag(ProtocolTypeConstants.HTTPNOAUTH.getValue());
143 assertNotNull(c.fetchWithReturnConsumerResponse());
144 c.setProtocolFlag(ProtocolTypeConstants.AUTH_KEY.getValue());
145 assertNotNull(c.fetchWithReturnConsumerResponse());
150 public void testFetch() throws Exception {
151 final LinkedList<String> hosts = new LinkedList<String>();
152 hosts.add("localhost:8080");
155 Properties properties = new Properties();
157 MRSimplerBatchPublisherTest.class.getClassLoader().getResourceAsStream("dme2/consumer.properties"));
159 String routeFilePath = "dme2/preferredRoute.txt";
161 File file = new File(MRSimplerBatchPublisherTest.class.getClassLoader().getResource(routeFilePath).getFile());
162 properties.put("routeFilePath",
163 MRSimplerBatchPublisherTest.class.getClassLoader().getResource(routeFilePath).getFile());
165 File outFile = new File(file.getParent() + "/consumer_tmp.properties");
166 properties.store(new FileOutputStream(outFile), "");
168 MRClientFactory.prop=properties;
169 final MRConsumerImpl c = new MRConsumerImpl.MRConsumerImplBuilder().setHostPart(hosts)
170 .setTopic("topic").setConsumerGroup("cg").setConsumerId("cid").setTimeoutMs(-1)
171 .setLimit(-1).setFilter("{ \"foo\"=\"bar\"bar\" }").setApiKey_username(null)
172 .setApiSecret_password(null).createMRConsumerImpl();
173 c.setProps(properties);
176 } catch (Exception e) {
179 c.setProtocolFlag(ProtocolTypeConstants.AAF_AUTH.getValue());
182 } catch (Exception e) {
185 c.setProtocolFlag(ProtocolTypeConstants.HTTPNOAUTH.getValue());
188 } catch (Exception e) {
191 c.setProtocolFlag(ProtocolTypeConstants.AUTH_KEY.getValue());
194 } catch (Exception e) {