add testcases
[dmaap/messagerouter/dmaapclient.git] / src / test / java / org / onap / dmaap / mr / client / impl / MRSimplerBatchPublisherTest.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
23 package org.onap.dmaap.mr.client.impl;
24
25 import static org.junit.Assert.assertEquals;
26
27 import java.io.File;
28 import java.io.FileNotFoundException;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 import java.util.List;
32 import java.util.Properties;
33 import java.util.concurrent.TimeUnit;
34
35 import org.json.JSONObject;
36 import org.junit.Assert;
37 import org.junit.Before;
38 import org.junit.Test;
39
40 import org.onap.dmaap.mr.client.MRClientFactory;
41 import org.onap.dmaap.mr.client.MRPublisher.message;
42 import org.onap.dmaap.mr.client.response.MRPublisherResponse;
43 import org.onap.dmaap.mr.test.clients.ProtocolTypeConstants;
44
45 public class MRSimplerBatchPublisherTest {
46
47         File outFile;
48
49         public void setUp(String contentType) throws Exception {
50                 Properties properties = new Properties();
51                 properties.load(
52                                 MRSimplerBatchPublisherTest.class.getClassLoader().getResourceAsStream("dme2/producer.properties"));
53
54                 String routeFilePath = "dme2/preferredRoute.txt";
55
56                 File file = new File(MRSimplerBatchPublisherTest.class.getClassLoader().getResource(routeFilePath).getFile());
57                 properties.put("DME2preferredRouterFilePath",
58                                 MRSimplerBatchPublisherTest.class.getClassLoader().getResource(routeFilePath).getFile());
59                 if (contentType != null) {
60                         properties.put("contenttype", contentType);
61                 }
62                 outFile = new File(file.getParent() + "/producer_tmp.properties");
63                 properties.store(new FileOutputStream(outFile), "");
64         }
65
66         @Test
67         public void testSend() throws Exception {
68
69                 setUp(null);
70
71                 final MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher) MRClientFactory
72                                 .createBatchingPublisher(outFile.getPath());
73
74                 // publish some messages
75                 final JSONObject msg1 = new JSONObject();
76                 pub.send("MyPartitionKey", msg1.toString());
77
78                 final List<message> stuck = pub.close(1, TimeUnit.SECONDS);
79                 Assert.assertEquals(1, stuck.size());
80
81         }
82
83         @Test
84         public void testSendBatchWithResponse() throws Exception {
85
86                 setUp(null);
87
88                 final MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher) MRClientFactory
89                                 .createBatchingPublisher(outFile.getPath(), true);
90
91                 // publish some messages
92                 final JSONObject msg1 = new JSONObject();
93                 pub.send("MyPartitionKey", msg1.toString());
94                 MRPublisherResponse pubResponse = new MRPublisherResponse();
95                 pub.setPubResponse(pubResponse);
96
97                 MRPublisherResponse mrPublisherResponse = pub.sendBatchWithResponse();
98                 Assert.assertEquals(1, mrPublisherResponse.getPendingMsgs());
99
100         }
101
102         @Test
103         public void testSendBatchWithResponseConText() throws Exception {
104
105                 setUp("text/plain");
106
107                 final MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher) MRClientFactory
108                                 .createBatchingPublisher(outFile.getPath());
109
110                 // publish some messages
111                 final JSONObject msg1 = new JSONObject();
112                 pub.send("MyPartitionKey", msg1.toString());
113
114                 final List<message> stuck = pub.close(1, TimeUnit.SECONDS);
115                 Assert.assertEquals(1, stuck.size());
116
117         }
118
119         @Test
120         public void testSendBatchWithResponseContCambria() throws Exception {
121
122                 setUp("application/cambria-zip");
123
124                 final MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher) MRClientFactory
125                                 .createBatchingPublisher(outFile.getPath());
126
127                 // publish some messages
128                 final JSONObject msg1 = new JSONObject();
129                 pub.send("MyPartitionKey", msg1.toString());
130
131                 final List<message> stuck = pub.close(1, TimeUnit.SECONDS);
132                 Assert.assertEquals(1, stuck.size());
133
134         }
135
136         @Test
137         public void testSendBatchWithResponseProtKey() throws Exception {
138
139                 setUp(null);
140
141                 final MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher) MRClientFactory
142                                 .createBatchingPublisher(outFile.getPath());
143                 pub.setProtocolFlag(ProtocolTypeConstants.AUTH_KEY.getValue());
144                 // publish some messages
145                 final JSONObject msg1 = new JSONObject();
146                 pub.send("MyPartitionKey", msg1.toString());
147
148                 final List<message> stuck = pub.close(1, TimeUnit.SECONDS);
149                 Assert.assertEquals(1, stuck.size());
150
151         }
152
153         @Test
154         public void testSendBatchWithResponseProtAaf() throws Exception {
155
156                 setUp(null);
157
158                 final MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher) MRClientFactory
159                                 .createBatchingPublisher(outFile.getPath());
160                 pub.setProtocolFlag(ProtocolTypeConstants.AAF_AUTH.getValue());
161                 // publish some messages
162                 final JSONObject msg1 = new JSONObject();
163                 pub.send("MyPartitionKey", msg1.toString());
164
165                 final List<message> stuck = pub.close(1, TimeUnit.SECONDS);
166                 Assert.assertEquals(1, stuck.size());
167
168         }
169
170         @Test
171         public void testSendBatchWithResponseProtNoAuth() throws Exception {
172
173                 setUp(null);
174
175                 final MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher) MRClientFactory
176                                 .createBatchingPublisher(outFile.getPath());
177                 pub.setProtocolFlag(ProtocolTypeConstants.HTTPNOAUTH.getValue());
178                 // publish some messages
179                 final JSONObject msg1 = new JSONObject();
180                 pub.send("MyPartitionKey", msg1.toString());
181
182                 final List<message> stuck = pub.close(1, TimeUnit.SECONDS);
183                 Assert.assertEquals(1, stuck.size());
184
185         }
186
187         @Test
188         public void testSendBatchWithResponsecontypeText() throws Exception {
189
190                 setUp("text/plain");
191
192                 final MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher) MRClientFactory
193                                 .createBatchingPublisher(outFile.getPath(), true);
194
195                 // publish some messages
196                 final JSONObject msg1 = new JSONObject();
197                 pub.send("MyPartitionKey", "payload");
198                 MRPublisherResponse pubResponse = new MRPublisherResponse();
199                 pub.setPubResponse(pubResponse);
200
201                 MRPublisherResponse mrPublisherResponse = pub.sendBatchWithResponse();
202                 Assert.assertEquals(1, mrPublisherResponse.getPendingMsgs());
203
204         }
205
206         @Test
207         public void testSendBatchWithResponsecontypeCambria() throws Exception {
208
209                 setUp("application/cambria-zip");
210
211                 final MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher) MRClientFactory
212                                 .createBatchingPublisher(outFile.getPath(), true);
213
214                 // publish some messages
215                 final JSONObject msg1 = new JSONObject();
216                 pub.send("MyPartitionKey", "payload");
217                 MRPublisherResponse pubResponse = new MRPublisherResponse();
218                 pub.setPubResponse(pubResponse);
219
220                 MRPublisherResponse mrPublisherResponse = pub.sendBatchWithResponse();
221                 Assert.assertEquals(1, mrPublisherResponse.getPendingMsgs());
222
223         }
224
225         @Test
226         public void testSendBatchWithResponsePrAuthKey() throws Exception {
227
228                 setUp(null);
229
230                 final MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher) MRClientFactory
231                                 .createBatchingPublisher(outFile.getPath(), true);
232                 pub.setProtocolFlag(ProtocolTypeConstants.AUTH_KEY.getValue());
233
234                 // publish some messages
235                 final JSONObject msg1 = new JSONObject();
236                 pub.send("MyPartitionKey", msg1.toString());
237                 MRPublisherResponse pubResponse = new MRPublisherResponse();
238                 pub.setPubResponse(pubResponse);
239
240                 MRPublisherResponse mrPublisherResponse = pub.sendBatchWithResponse();
241                 Assert.assertEquals(1, mrPublisherResponse.getPendingMsgs());
242
243         }
244
245         @Test
246         public void testSendBatchWithResponsePrAaf() throws Exception {
247
248                 setUp(null);
249
250                 final MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher) MRClientFactory
251                                 .createBatchingPublisher(outFile.getPath(), true);
252                 pub.setProtocolFlag(ProtocolTypeConstants.AAF_AUTH.getValue());
253
254                 // publish some messages
255                 final JSONObject msg1 = new JSONObject();
256                 pub.send("MyPartitionKey", msg1.toString());
257                 MRPublisherResponse pubResponse = new MRPublisherResponse();
258                 pub.setPubResponse(pubResponse);
259
260                 MRPublisherResponse mrPublisherResponse = pub.sendBatchWithResponse();
261                 Assert.assertEquals(1, mrPublisherResponse.getPendingMsgs());
262
263         }
264
265         @Test
266         public void testSendBatchWithResponsePrNoauth() throws Exception {
267
268                 setUp(null);
269
270                 final MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher) MRClientFactory
271                                 .createBatchingPublisher(outFile.getPath(), true);
272                 pub.setProtocolFlag(ProtocolTypeConstants.HTTPNOAUTH.getValue());
273
274                 // publish some messages
275                 final JSONObject msg1 = new JSONObject();
276                 pub.send("MyPartitionKey", msg1.toString());
277                 MRPublisherResponse pubResponse = new MRPublisherResponse();
278                 pub.setPubResponse(pubResponse);
279
280                 MRPublisherResponse mrPublisherResponse = pub.sendBatchWithResponse();
281                 Assert.assertEquals(1, mrPublisherResponse.getPendingMsgs());
282
283         }
284         
285         @Test
286         public void createPublisherResponse() throws Exception{
287                 setUp(null);
288                 MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher) MRClientFactory
289                                 .createBatchingPublisher(outFile.getPath(), true);
290                 
291                 MRPublisherResponse response=pub.createMRPublisherResponse("{\"message\": \"published the message\", \"status\": \"200\"}", new MRPublisherResponse());
292                 assertEquals("200", response.getResponseCode());
293                 
294         }
295         
296         @Test
297         public void createPublisherResponseSucc() throws Exception{
298                 setUp(null);
299                 MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher) MRClientFactory
300                                 .createBatchingPublisher(outFile.getPath(), true);
301                 
302                 MRPublisherResponse response=pub.createMRPublisherResponse("{\"fakemessage\": \"published the message\", \"fakestatus\": \"200\"}", new MRPublisherResponse());
303                 assertEquals("200", response.getResponseCode());
304                 
305         }
306         
307         @Test
308         public void createPublisherResponseError() throws Exception{
309                 setUp(null);
310                 MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher) MRClientFactory
311                                 .createBatchingPublisher(outFile.getPath(), true);
312                 
313                 MRPublisherResponse response=pub.createMRPublisherResponse("", new MRPublisherResponse());
314                 assertEquals("400", response.getResponseCode());
315                 
316         }
317
318 }