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