X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Forg%2Fonap%2Fdmaap%2Fmr%2Fclient%2Fimpl%2FMRSimplerBatchPublisherTest.java;h=b2f8817bec1268cf0c6f572782242fc1d897d2d7;hb=e709bb9923625f1f36cd5890d86b8cfa01d05739;hp=aeda89186c1aa604ba585033534560d6d028e2b1;hpb=e3da91b4013e3c4fea1d2a2c6f0e4a1677fb3ba4;p=dmaap%2Fmessagerouter%2Fdmaapclient.git diff --git a/src/test/java/org/onap/dmaap/mr/client/impl/MRSimplerBatchPublisherTest.java b/src/test/java/org/onap/dmaap/mr/client/impl/MRSimplerBatchPublisherTest.java index aeda891..b2f8817 100644 --- a/src/test/java/org/onap/dmaap/mr/client/impl/MRSimplerBatchPublisherTest.java +++ b/src/test/java/org/onap/dmaap/mr/client/impl/MRSimplerBatchPublisherTest.java @@ -22,7 +22,10 @@ package org.onap.dmaap.mr.client.impl; +import static org.junit.Assert.assertEquals; + import java.io.File; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; @@ -37,58 +40,278 @@ import org.junit.Test; import org.onap.dmaap.mr.client.MRClientFactory; import org.onap.dmaap.mr.client.MRPublisher.message; import org.onap.dmaap.mr.client.response.MRPublisherResponse; +import org.onap.dmaap.mr.test.clients.ProtocolTypeConstants; public class MRSimplerBatchPublisherTest { - + File outFile; - @Before - public void setUp() throws Exception { + + public void setUp(String contentType) throws Exception { Properties properties = new Properties(); - properties.load(MRSimplerBatchPublisherTest.class.getClassLoader().getResourceAsStream("dme2/producer.properties")); - - String routeFilePath="dme2/preferredRoute.txt"; - + properties.load( + MRSimplerBatchPublisherTest.class.getClassLoader().getResourceAsStream("dme2/producer.properties")); + + String routeFilePath = "dme2/preferredRoute.txt"; + File file = new File(MRSimplerBatchPublisherTest.class.getClassLoader().getResource(routeFilePath).getFile()); - properties.put("DME2preferredRouterFilePath", MRSimplerBatchPublisherTest.class.getClassLoader().getResource(routeFilePath).getFile()); - + properties.put("DME2preferredRouterFilePath", + MRSimplerBatchPublisherTest.class.getClassLoader().getResource(routeFilePath).getFile()); + if (contentType != null) { + properties.put("contenttype", contentType); + } outFile = new File(file.getParent() + "/producer_tmp.properties"); properties.store(new FileOutputStream(outFile), ""); } @Test - public void testSend() throws IOException, InterruptedException { - - final MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher)MRClientFactory.createBatchingPublisher(outFile.getPath()); - - //publish some messages - final JSONObject msg1 = new JSONObject (); - pub.send ( "MyPartitionKey", msg1.toString () ); + public void testSend() throws Exception { + + setUp(null); + + final MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher) MRClientFactory + .createBatchingPublisher(outFile.getPath()); + + // publish some messages + final JSONObject msg1 = new JSONObject(); + pub.send("MyPartitionKey", msg1.toString()); + + final List stuck = pub.close(1, TimeUnit.SECONDS); + Assert.assertEquals(1, stuck.size()); - final List stuck = pub.close ( 1, TimeUnit.SECONDS ); - if ( stuck.size () > 0 ) { - System.out.println( stuck.size() + " messages unsent" ); - } - else - { - System.out.println ( "Clean exit; all messages sent." ); - } - - } @Test - public void testSendBatchWithResponse() throws IOException, InterruptedException { - - final MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher)MRClientFactory.createBatchingPublisher(outFile.getPath(), true); - - //publish some messages - final JSONObject msg1 = new JSONObject (); - pub.send ( "MyPartitionKey", msg1.toString () ); + public void testSendBatchWithResponse() throws Exception { + + setUp(null); + + final MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher) MRClientFactory + .createBatchingPublisher(outFile.getPath(), true); + + // publish some messages + final JSONObject msg1 = new JSONObject(); + pub.send("MyPartitionKey", msg1.toString()); MRPublisherResponse pubResponse = new MRPublisherResponse(); pub.setPubResponse(pubResponse); - + MRPublisherResponse mrPublisherResponse = pub.sendBatchWithResponse(); Assert.assertEquals(1, mrPublisherResponse.getPendingMsgs()); + + } + + @Test + public void testSendBatchWithResponseConText() throws Exception { + + setUp("text/plain"); + + final MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher) MRClientFactory + .createBatchingPublisher(outFile.getPath()); + + // publish some messages + final JSONObject msg1 = new JSONObject(); + pub.send("MyPartitionKey", msg1.toString()); + + final List stuck = pub.close(1, TimeUnit.SECONDS); + Assert.assertEquals(1, stuck.size()); + + } + + @Test + public void testSendBatchWithResponseContCambria() throws Exception { + + setUp("application/cambria-zip"); + + final MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher) MRClientFactory + .createBatchingPublisher(outFile.getPath()); + + // publish some messages + final JSONObject msg1 = new JSONObject(); + pub.send("MyPartitionKey", msg1.toString()); + + final List stuck = pub.close(1, TimeUnit.SECONDS); + Assert.assertEquals(1, stuck.size()); + + } + + @Test + public void testSendBatchWithResponseProtKey() throws Exception { + + setUp(null); + + final MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher) MRClientFactory + .createBatchingPublisher(outFile.getPath()); + pub.setProtocolFlag(ProtocolTypeConstants.AUTH_KEY.getValue()); + // publish some messages + final JSONObject msg1 = new JSONObject(); + pub.send("MyPartitionKey", msg1.toString()); + + final List stuck = pub.close(1, TimeUnit.SECONDS); + Assert.assertEquals(1, stuck.size()); + + } + + @Test + public void testSendBatchWithResponseProtAaf() throws Exception { + + setUp(null); + + final MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher) MRClientFactory + .createBatchingPublisher(outFile.getPath()); + pub.setProtocolFlag(ProtocolTypeConstants.AAF_AUTH.getValue()); + // publish some messages + final JSONObject msg1 = new JSONObject(); + pub.send("MyPartitionKey", msg1.toString()); + + final List stuck = pub.close(1, TimeUnit.SECONDS); + Assert.assertEquals(1, stuck.size()); + + } + + @Test + public void testSendBatchWithResponseProtNoAuth() throws Exception { + + setUp(null); + + final MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher) MRClientFactory + .createBatchingPublisher(outFile.getPath()); + pub.setProtocolFlag(ProtocolTypeConstants.HTTPNOAUTH.getValue()); + // publish some messages + final JSONObject msg1 = new JSONObject(); + pub.send("MyPartitionKey", msg1.toString()); + + final List stuck = pub.close(1, TimeUnit.SECONDS); + Assert.assertEquals(1, stuck.size()); + + } + + @Test + public void testSendBatchWithResponsecontypeText() throws Exception { + + setUp("text/plain"); + + final MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher) MRClientFactory + .createBatchingPublisher(outFile.getPath(), true); + + // publish some messages + final JSONObject msg1 = new JSONObject(); + pub.send("MyPartitionKey", "payload"); + MRPublisherResponse pubResponse = new MRPublisherResponse(); + pub.setPubResponse(pubResponse); + + MRPublisherResponse mrPublisherResponse = pub.sendBatchWithResponse(); + Assert.assertEquals(1, mrPublisherResponse.getPendingMsgs()); + + } + + @Test + public void testSendBatchWithResponsecontypeCambria() throws Exception { + + setUp("application/cambria-zip"); + + final MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher) MRClientFactory + .createBatchingPublisher(outFile.getPath(), true); + + // publish some messages + final JSONObject msg1 = new JSONObject(); + pub.send("MyPartitionKey", "payload"); + MRPublisherResponse pubResponse = new MRPublisherResponse(); + pub.setPubResponse(pubResponse); + + MRPublisherResponse mrPublisherResponse = pub.sendBatchWithResponse(); + Assert.assertEquals(1, mrPublisherResponse.getPendingMsgs()); + + } + + @Test + public void testSendBatchWithResponsePrAuthKey() throws Exception { + + setUp(null); + + final MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher) MRClientFactory + .createBatchingPublisher(outFile.getPath(), true); + pub.setProtocolFlag(ProtocolTypeConstants.AUTH_KEY.getValue()); + + // publish some messages + final JSONObject msg1 = new JSONObject(); + pub.send("MyPartitionKey", msg1.toString()); + MRPublisherResponse pubResponse = new MRPublisherResponse(); + pub.setPubResponse(pubResponse); + + MRPublisherResponse mrPublisherResponse = pub.sendBatchWithResponse(); + Assert.assertEquals(1, mrPublisherResponse.getPendingMsgs()); + + } + + @Test + public void testSendBatchWithResponsePrAaf() throws Exception { + + setUp(null); + + final MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher) MRClientFactory + .createBatchingPublisher(outFile.getPath(), true); + pub.setProtocolFlag(ProtocolTypeConstants.AAF_AUTH.getValue()); + + // publish some messages + final JSONObject msg1 = new JSONObject(); + pub.send("MyPartitionKey", msg1.toString()); + MRPublisherResponse pubResponse = new MRPublisherResponse(); + pub.setPubResponse(pubResponse); + + MRPublisherResponse mrPublisherResponse = pub.sendBatchWithResponse(); + Assert.assertEquals(1, mrPublisherResponse.getPendingMsgs()); + + } + + @Test + public void testSendBatchWithResponsePrNoauth() throws Exception { + + setUp(null); + + final MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher) MRClientFactory + .createBatchingPublisher(outFile.getPath(), true); + pub.setProtocolFlag(ProtocolTypeConstants.HTTPNOAUTH.getValue()); + + // publish some messages + final JSONObject msg1 = new JSONObject(); + pub.send("MyPartitionKey", msg1.toString()); + MRPublisherResponse pubResponse = new MRPublisherResponse(); + pub.setPubResponse(pubResponse); + + MRPublisherResponse mrPublisherResponse = pub.sendBatchWithResponse(); + Assert.assertEquals(1, mrPublisherResponse.getPendingMsgs()); + + } + + @Test + public void createPublisherResponse() throws Exception{ + setUp(null); + MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher) MRClientFactory + .createBatchingPublisher(outFile.getPath(), true); + + MRPublisherResponse response=pub.createMRPublisherResponse("{\"message\": \"published the message\", \"status\": \"200\"}", new MRPublisherResponse()); + assertEquals("200", response.getResponseCode()); + + } + + @Test + public void createPublisherResponseSucc() throws Exception{ + setUp(null); + MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher) MRClientFactory + .createBatchingPublisher(outFile.getPath(), true); + + MRPublisherResponse response=pub.createMRPublisherResponse("{\"fakemessage\": \"published the message\", \"fakestatus\": \"200\"}", new MRPublisherResponse()); + assertEquals("200", response.getResponseCode()); + + } + + @Test + public void createPublisherResponseError() throws Exception{ + setUp(null); + MRSimplerBatchPublisher pub = (MRSimplerBatchPublisher) MRClientFactory + .createBatchingPublisher(outFile.getPath(), true); + + MRPublisherResponse response=pub.createMRPublisherResponse("", new MRPublisherResponse()); + assertEquals("400", response.getResponseCode()); }