add testcases
[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.client.impl.MRConstants;
35 import org.onap.dmaap.mr.client.impl.MRConsumerImpl;
36 import org.onap.dmaap.mr.test.clients.ProtocolTypeConstants;
37
38 public class MRConsumerImplTest extends TestCase {
39         @Test
40         public void testNullFilter() throws IOException {
41                 final LinkedList<String> hosts = new LinkedList<String>();
42                 hosts.add("localhost:8080");
43                 final MRConsumerImpl c = new MRConsumerImpl(hosts, "topic", "cg", "cid", -1, -1, null, null, null);
44                 final String url = c.createUrlPath(MRConstants.makeConsumerUrl("localhost:8080", "topic", "cg", "cid", "http"),
45                                 -1, -1);
46                 assertEquals("http://localhost:8080/events/" + "topic/cg/cid", url);
47         }
48
49         @Test
50         public void testFilterWithNoTimeoutOrLimit() throws IOException {
51                 final LinkedList<String> hosts = new LinkedList<String>();
52                 hosts.add("localhost:8080");
53                 final MRConsumerImpl c = new MRConsumerImpl(hosts, "topic", "cg", "cid", -1, -1, "filter", null, null);
54                 final String url = c.createUrlPath(MRConstants.makeConsumerUrl("localhost:8080", "topic", "cg", "cid", "http"),
55                                 -1, -1);
56                 assertEquals("http://localhost:8080/events/" + "topic/cg/cid?filter=filter", url);
57         }
58
59         @Test
60         public void testTimeoutNoLimitNoFilter() throws IOException {
61                 final LinkedList<String> hosts = new LinkedList<String>();
62                 hosts.add("localhost:8080");
63                 final MRConsumerImpl c = new MRConsumerImpl(hosts, "topic", "cg", "cid", 30000, -1, null, null, null);
64                 final String url = c.createUrlPath(MRConstants.makeConsumerUrl("localhost:8080", "topic", "cg", "cid", "http"),
65                                 30000, -1);
66                 assertEquals("http://localhost:8080/events/" + "topic/cg/cid?timeout=30000", url);
67         }
68
69         @Test
70         public void testNoTimeoutWithLimitNoFilter() throws IOException {
71                 final LinkedList<String> hosts = new LinkedList<String>();
72                 hosts.add("localhost:8080");
73                 final MRConsumerImpl c = new MRConsumerImpl(hosts, "topic", "cg", "cid", -1, 100, null, null, null);
74                 final String url = c.createUrlPath(MRConstants.makeConsumerUrl("localhost:8080", "topic", "cg", "cid", "http"),
75                                 -1, 100);
76                 assertEquals("http://localhost:8080/events/" + "topic/cg/cid?limit=100", url);
77         }
78
79         @Test
80         public void testWithTimeoutWithLimitWithFilter() throws IOException {
81                 final LinkedList<String> hosts = new LinkedList<String>();
82                 hosts.add("localhost:8080");
83                 final MRConsumerImpl c = new MRConsumerImpl(hosts, "topic", "cg", "cid", 1000, 400, "f", null, null);
84                 final String url = c.createUrlPath(MRConstants.makeConsumerUrl("localhost:8080", "topic", "cg", "cid", "http"),
85                                 1000, 400);
86                 assertEquals("http://localhost:8080/events/" + "topic/cg/cid?timeout=1000&limit=400&filter=f", url);
87         }
88
89         @Test
90         public void testFilterEncoding() throws IOException {
91                 final LinkedList<String> hosts = new LinkedList<String>();
92                 hosts.add("localhost:8080");
93                 final MRConsumerImpl c = new MRConsumerImpl(hosts, "topic", "cg", "cid", -1, -1, "{ \"foo\"=\"bar\"bar\" }",
94                                 null, null);
95                 final String url = c.createUrlPath(MRConstants.makeConsumerUrl("localhost:8080", "topic", "cg", "cid", "http"),
96                                 -1, -1);
97                 assertEquals("http://localhost:8080/events/" + "topic/cg/cid?filter=%7B+%22foo%22%3D%22bar%22bar%22+%7D", url);
98         }
99
100         @Test
101         public void testFetchWithReturnConsumerResponse() throws IOException {
102                 final LinkedList<String> hosts = new LinkedList<String>();
103                 hosts.add("localhost:8080");
104                 Properties properties = new Properties();
105                 properties.load(
106                                 MRSimplerBatchPublisherTest.class.getClassLoader().getResourceAsStream("dme2/consumer.properties"));
107
108                 String routeFilePath = "dme2/preferredRoute.txt";
109
110                 File file = new File(MRSimplerBatchPublisherTest.class.getClassLoader().getResource(routeFilePath).getFile());
111                 properties.put("routeFilePath",
112                                 MRSimplerBatchPublisherTest.class.getClassLoader().getResource(routeFilePath).getFile());
113                 
114                 File outFile = new File(file.getParent() + "/consumer_tmp.properties");
115                 properties.store(new FileOutputStream(outFile), "");
116
117                 MRClientFactory.prop=properties;
118
119                 final MRConsumerImpl c = new MRConsumerImpl(hosts, "topic", "cg", "cid", -1, -1, "{ \"foo\"=\"bar\"bar\" }",
120                                 null, null);
121                 c.setProps(properties);
122                 assertNotNull(c.fetchWithReturnConsumerResponse());
123                 c.setProtocolFlag(ProtocolTypeConstants.AAF_AUTH.getValue());
124                 assertNotNull(c.fetchWithReturnConsumerResponse());
125                 c.setProtocolFlag(ProtocolTypeConstants.HTTPNOAUTH.getValue());
126                 assertNotNull(c.fetchWithReturnConsumerResponse());
127                 c.setProtocolFlag(ProtocolTypeConstants.AUTH_KEY.getValue());
128                 assertNotNull(c.fetchWithReturnConsumerResponse());
129                 assertTrue(true);
130         }
131
132         @Test
133         public void testFetch() throws Exception {
134                 final LinkedList<String> hosts = new LinkedList<String>();
135                 hosts.add("localhost:8080");
136                 
137                 
138                 Properties properties = new Properties();
139                 properties.load(
140                                 MRSimplerBatchPublisherTest.class.getClassLoader().getResourceAsStream("dme2/consumer.properties"));
141
142                 String routeFilePath = "dme2/preferredRoute.txt";
143
144                 File file = new File(MRSimplerBatchPublisherTest.class.getClassLoader().getResource(routeFilePath).getFile());
145                 properties.put("routeFilePath",
146                                 MRSimplerBatchPublisherTest.class.getClassLoader().getResource(routeFilePath).getFile());
147                 
148                 File outFile = new File(file.getParent() + "/consumer_tmp.properties");
149                 properties.store(new FileOutputStream(outFile), "");
150
151                 MRClientFactory.prop=properties;
152                 final MRConsumerImpl c = new MRConsumerImpl(hosts, "topic", "cg", "cid", -1, -1, "{ \"foo\"=\"bar\"bar\" }",
153                                 null, null);
154                 c.setProps(properties);
155                 try {
156                         c.fetch();
157                 } catch (Exception e) {
158                         assertTrue(true);
159                 }
160                 c.setProtocolFlag(ProtocolTypeConstants.AAF_AUTH.getValue());
161                 try {
162                         c.fetch();
163                 } catch (Exception e) {
164                         assertTrue(true);
165                 }
166                 c.setProtocolFlag(ProtocolTypeConstants.HTTPNOAUTH.getValue());
167                 try {
168                         c.fetch();
169                 } catch (Exception e) {
170                         assertTrue(true);
171                 }
172                 c.setProtocolFlag(ProtocolTypeConstants.AUTH_KEY.getValue());
173                 try {
174                         c.fetch();
175                 } catch (Exception e) {
176                         assertTrue(true);
177                 }
178         }
179 }