Fix too many constructor param in dmaapclient
[dmaap/messagerouter/dmaapclient.git] / src / test / java / org / onap / dmaap / mr / client / impl / MRConsumerImplTest.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
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
11  *  
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=========================================================
18  *
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  *  
21  *******************************************************************************/
22 package org.onap.dmaap.mr.client.impl;
23
24 import java.io.File;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.util.LinkedList;
28 import java.util.Properties;
29
30 import junit.framework.TestCase;
31
32 import org.junit.Test;
33 import org.onap.dmaap.mr.client.MRClientFactory;
34 import org.onap.dmaap.mr.test.clients.ProtocolTypeConstants;
35
36 public class MRConsumerImplTest extends TestCase {
37         @Test
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"),
46                                 -1, -1);
47                 assertEquals("http://localhost:8080/events/" + "topic/cg/cid", url);
48         }
49
50         @Test
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"),
59                                 -1, -1);
60                 assertEquals("http://localhost:8080/events/" + "topic/cg/cid?filter=filter", url);
61         }
62
63         @Test
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"),
72                                 30000, -1);
73                 assertEquals("http://localhost:8080/events/" + "topic/cg/cid?timeout=30000", url);
74         }
75
76         @Test
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"),
85                                 -1, 100);
86                 assertEquals("http://localhost:8080/events/" + "topic/cg/cid?limit=100", url);
87         }
88
89         @Test
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"),
98                                 1000, 400);
99                 assertEquals("http://localhost:8080/events/" + "topic/cg/cid?timeout=1000&limit=400&filter=f", url);
100         }
101
102         @Test
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"),
111                                 -1, -1);
112                 assertEquals("http://localhost:8080/events/" + "topic/cg/cid?filter=%7B+%22foo%22%3D%22bar%22bar%22+%7D", url);
113         }
114
115         @Test
116         public void testFetchWithReturnConsumerResponse() throws IOException {
117                 final LinkedList<String> hosts = new LinkedList<String>();
118                 hosts.add("localhost:8080");
119                 Properties properties = new Properties();
120                 properties.load(
121                                 MRSimplerBatchPublisherTest.class.getClassLoader().getResourceAsStream("dme2/consumer.properties"));
122
123                 String routeFilePath = "dme2/preferredRoute.txt";
124
125                 File file = new File(MRSimplerBatchPublisherTest.class.getClassLoader().getResource(routeFilePath).getFile());
126                 properties.put("routeFilePath",
127                                 MRSimplerBatchPublisherTest.class.getClassLoader().getResource(routeFilePath).getFile());
128                 
129                 File outFile = new File(file.getParent() + "/consumer_tmp.properties");
130                 properties.store(new FileOutputStream(outFile), "");
131
132                 MRClientFactory.prop=properties;
133
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());
146                 assertTrue(true);
147         }
148
149         @Test
150         public void testFetch() throws Exception {
151                 final LinkedList<String> hosts = new LinkedList<String>();
152                 hosts.add("localhost:8080");
153                 
154                 
155                 Properties properties = new Properties();
156                 properties.load(
157                                 MRSimplerBatchPublisherTest.class.getClassLoader().getResourceAsStream("dme2/consumer.properties"));
158
159                 String routeFilePath = "dme2/preferredRoute.txt";
160
161                 File file = new File(MRSimplerBatchPublisherTest.class.getClassLoader().getResource(routeFilePath).getFile());
162                 properties.put("routeFilePath",
163                                 MRSimplerBatchPublisherTest.class.getClassLoader().getResource(routeFilePath).getFile());
164                 
165                 File outFile = new File(file.getParent() + "/consumer_tmp.properties");
166                 properties.store(new FileOutputStream(outFile), "");
167
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);
174                 try {
175                         c.fetch();
176                 } catch (Exception e) {
177                         assertTrue(true);
178                 }
179                 c.setProtocolFlag(ProtocolTypeConstants.AAF_AUTH.getValue());
180                 try {
181                         c.fetch();
182                 } catch (Exception e) {
183                         assertTrue(true);
184                 }
185                 c.setProtocolFlag(ProtocolTypeConstants.HTTPNOAUTH.getValue());
186                 try {
187                         c.fetch();
188                 } catch (Exception e) {
189                         assertTrue(true);
190                 }
191                 c.setProtocolFlag(ProtocolTypeConstants.AUTH_KEY.getValue());
192                 try {
193                         c.fetch();
194                 } catch (Exception e) {
195                         assertTrue(true);
196                 }
197         }
198 }