commiting code for test coverage
[dmaap/messagerouter/dmaapclient.git] / src / test / java / com / att / nsa / mr / client / impl / MRSimplerBatchPublisherTest.java
1 /*******************************************************************************\r
2  *  ============LICENSE_START=======================================================\r
3  *  org.onap.dmaap\r
4  *  ================================================================================\r
5  *  Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  *  ================================================================================\r
7  *  Licensed under the Apache License, Version 2.0 (the "License");\r
8  *  you may not use this file except in compliance with the License.\r
9  *  You may obtain a copy of the License at\r
10  *        http://www.apache.org/licenses/LICENSE-2.0\r
11  *  \r
12  *  Unless required by applicable law or agreed to in writing, software\r
13  *  distributed under the License is distributed on an "AS IS" BASIS,\r
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
15  *  See the License for the specific language governing permissions and\r
16  *  limitations under the License.\r
17  *  ============LICENSE_END=========================================================\r
18  *\r
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
20  *  \r
21  *******************************************************************************/\r
22 \r
23 package com.att.nsa.mr.client.impl;\r
24 \r
25 import java.io.File;\r
26 import java.io.FileOutputStream;\r
27 import java.io.IOException;\r
28 import java.util.List;\r
29 import java.util.Properties;\r
30 import java.util.concurrent.TimeUnit;\r
31 \r
32 import org.json.JSONObject;\r
33 import org.junit.After;\r
34 import org.junit.Assert;\r
35 import org.junit.Before;\r
36 import org.junit.Test;\r
37 \r
38 import com.att.nsa.mr.client.MRClientFactory;\r
39 import com.att.nsa.mr.client.MRPublisher.message;\r
40 import com.att.nsa.mr.client.response.MRPublisherResponse;\r
41 \r
42 public class MRSimplerBatchPublisherTest {\r
43         \r
44         File outFile;\r
45         @Before\r
46         public void setUp() throws Exception {\r
47                 Properties properties = new Properties();\r
48                 properties.load(MRSimplerBatchPublisherTest.class.getClassLoader().getResourceAsStream("dme2/producer.properties"));\r
49                 \r
50                 String routeFilePath="dme2/preferredRoute.txt";\r
51                 \r
52                 File file = new File(MRSimplerBatchPublisherTest.class.getClassLoader().getResource(routeFilePath).getFile());\r
53                 properties.put("DME2preferredRouterFilePath", MRSimplerBatchPublisherTest.class.getClassLoader().getResource(routeFilePath).getFile());\r
54                 \r
55                 outFile = new File(file.getParent() + "/producer_tmp.properties");\r
56                 properties.store(new FileOutputStream(outFile), "");\r
57         }\r
58 \r
59         @Test\r
60         public void testSend() throws IOException, InterruptedException {\r
61                                 \r
62                 final MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher)MRClientFactory.createBatchingPublisher(outFile.getPath());        \r
63                 \r
64                 //publish some messages\r
65                 final JSONObject msg1 = new JSONObject ();\r
66                 pub.send ( "MyPartitionKey", msg1.toString () );\r
67 \r
68                 final List<message> stuck = pub.close ( 1, TimeUnit.SECONDS );\r
69                 if ( stuck.size () > 0 ) {\r
70                         System.out.println( stuck.size() + " messages unsent" );\r
71                 }\r
72                 else\r
73                 {\r
74                         System.out.println ( "Clean exit; all messages sent." );\r
75                 }\r
76                 \r
77                 \r
78         }\r
79 \r
80         @Test\r
81         public void testSendBatchWithResponse() throws IOException, InterruptedException {\r
82                                 \r
83                 final MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher)MRClientFactory.createBatchingPublisher(outFile.getPath(), true);  \r
84                 \r
85                 //publish some messages\r
86                 final JSONObject msg1 = new JSONObject ();\r
87                 pub.send ( "MyPartitionKey", msg1.toString () );\r
88                 MRPublisherResponse pubResponse = new MRPublisherResponse();\r
89                 pub.setPubResponse(pubResponse);\r
90                 \r
91                 MRPublisherResponse mrPublisherResponse = pub.sendBatchWithResponse();\r
92                 Assert.assertEquals(1, mrPublisherResponse.getPendingMsgs());\r
93                 \r
94         }\r
95 \r
96 }\r