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