update the package name
[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 java.io.File;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.util.List;
29 import java.util.Properties;
30 import java.util.concurrent.TimeUnit;
31
32 import org.json.JSONObject;
33 import org.junit.After;
34 import org.junit.Assert;
35 import org.junit.Before;
36 import org.junit.Test;
37
38 import org.onap.dmaap.mr.client.MRClientFactory;
39 import org.onap.dmaap.mr.client.MRPublisher.message;
40 import org.onap.dmaap.mr.client.response.MRPublisherResponse;
41
42 public class MRSimplerBatchPublisherTest {
43         
44         File outFile;
45         @Before
46         public void setUp() throws Exception {
47                 Properties properties = new Properties();
48                 properties.load(MRSimplerBatchPublisherTest.class.getClassLoader().getResourceAsStream("dme2/producer.properties"));
49                 
50                 String routeFilePath="dme2/preferredRoute.txt";
51                 
52                 File file = new File(MRSimplerBatchPublisherTest.class.getClassLoader().getResource(routeFilePath).getFile());
53                 properties.put("DME2preferredRouterFilePath", MRSimplerBatchPublisherTest.class.getClassLoader().getResource(routeFilePath).getFile());
54                 
55                 outFile = new File(file.getParent() + "/producer_tmp.properties");
56                 properties.store(new FileOutputStream(outFile), "");
57         }
58
59         @Test
60         public void testSend() throws IOException, InterruptedException {
61                                 
62                 final MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher)MRClientFactory.createBatchingPublisher(outFile.getPath());        
63                 
64                 //publish some messages
65                 final JSONObject msg1 = new JSONObject ();
66                 pub.send ( "MyPartitionKey", msg1.toString () );
67
68                 final List<message> stuck = pub.close ( 1, TimeUnit.SECONDS );
69                 if ( stuck.size () > 0 ) {
70                         System.out.println( stuck.size() + " messages unsent" );
71                 }
72                 else
73                 {
74                         System.out.println ( "Clean exit; all messages sent." );
75                 }
76                 
77                 
78         }
79
80         @Test
81         public void testSendBatchWithResponse() throws IOException, InterruptedException {
82                                 
83                 final MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher)MRClientFactory.createBatchingPublisher(outFile.getPath(), true);  
84                 
85                 //publish some messages
86                 final JSONObject msg1 = new JSONObject ();
87                 pub.send ( "MyPartitionKey", msg1.toString () );
88                 MRPublisherResponse pubResponse = new MRPublisherResponse();
89                 pub.setPubResponse(pubResponse);
90                 
91                 MRPublisherResponse mrPublisherResponse = pub.sendBatchWithResponse();
92                 Assert.assertEquals(1, mrPublisherResponse.getPendingMsgs());
93                 
94         }
95
96 }