1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
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
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=========================================================
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
23 package org.onap.dmaap.mr.client.impl;
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;
32 import org.json.JSONObject;
33 import org.junit.Assert;
34 import org.junit.Before;
35 import org.junit.Test;
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;
41 public class MRSimplerBatchPublisherTest {
45 public void setUp() throws Exception {
46 Properties properties = new Properties();
47 properties.load(MRSimplerBatchPublisherTest.class.getClassLoader().getResourceAsStream("dme2/producer.properties"));
49 String routeFilePath="dme2/preferredRoute.txt";
51 File file = new File(MRSimplerBatchPublisherTest.class.getClassLoader().getResource(routeFilePath).getFile());
52 properties.put("DME2preferredRouterFilePath", MRSimplerBatchPublisherTest.class.getClassLoader().getResource(routeFilePath).getFile());
54 outFile = new File(file.getParent() + "/producer_tmp.properties");
55 properties.store(new FileOutputStream(outFile), "");
59 public void testSend() throws IOException, InterruptedException {
61 final MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher)MRClientFactory.createBatchingPublisher(outFile.getPath());
63 //publish some messages
64 final JSONObject msg1 = new JSONObject ();
65 pub.send ( "MyPartitionKey", msg1.toString () );
67 final List<message> stuck = pub.close ( 1, TimeUnit.SECONDS );
68 if ( stuck.size () > 0 ) {
69 System.out.println( stuck.size() + " messages unsent" );
73 System.out.println ( "Clean exit; all messages sent." );
80 public void testSendBatchWithResponse() throws IOException, InterruptedException {
82 final MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher)MRClientFactory.createBatchingPublisher(outFile.getPath(), true);
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);
90 MRPublisherResponse mrPublisherResponse = pub.sendBatchWithResponse();
91 Assert.assertEquals(1, mrPublisherResponse.getPendingMsgs());