From 173a07c146746f85c17f2d58f2ded84b99818f9a Mon Sep 17 00:00:00 2001 From: Sunil Unnava Date: Tue, 6 Mar 2018 11:41:18 -0500 Subject: testcases for code coverage Issue-ID: DMAAP-271 Change-Id: I9b11a61d1098598f8dc6c687a10ebf765128d977 Signed-off-by: Sunil Unnava --- .../att/nsa/mr/client/impl/MRBaseClientTest.java | 412 +++++++++++++++++++++ .../nsa/mr/client/impl/MRBatchPublisherTest.java | 54 +++ .../att/nsa/mr/client/impl/MRConsumerImplTest.java | 17 + .../att/nsa/mr/client/impl/MRMetaClientTest.java | 22 ++ .../mr/client/impl/MRSimplerBatchConsumerTest.java | 1 + 5 files changed, 506 insertions(+) create mode 100644 src/test/java/com/att/nsa/mr/client/impl/MRBaseClientTest.java create mode 100644 src/test/java/com/att/nsa/mr/client/impl/MRBatchPublisherTest.java (limited to 'src/test/java/com/att/nsa/mr/client/impl') diff --git a/src/test/java/com/att/nsa/mr/client/impl/MRBaseClientTest.java b/src/test/java/com/att/nsa/mr/client/impl/MRBaseClientTest.java new file mode 100644 index 0000000..1c291b9 --- /dev/null +++ b/src/test/java/com/att/nsa/mr/client/impl/MRBaseClientTest.java @@ -0,0 +1,412 @@ +/******************************************************************************* + * ============LICENSE_START======================================================= + * org.onap.dmaap + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * + *******************************************************************************/ +package com.att.nsa.mr.client.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.net.MalformedURLException; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; + +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.ResponseBuilder; + +import org.apache.http.HttpException; +import org.glassfish.jersey.internal.util.Base64; +import org.json.JSONException; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +@PowerMockIgnore("org.apache.http.conn.ssl.*") +@PrepareForTest({ DmaapClientUtil.class }) +public class MRBaseClientTest { + + //@InjectMocks + private MRBaseClient mrBaseClient; + private Collection hosts = new HashSet<>(Arrays.asList("localhost:8080")); + private String clientSignature = "topic" + "::" + "cg" + "::" + "cid"; + + @Before + public void setup() throws MalformedURLException { + mrBaseClient=new MRBaseClient(hosts, clientSignature); + PowerMockito.mockStatic(DmaapClientUtil.class); + } + + @Test + public void testGet() throws JSONException, HttpException { + + ResponseBuilder responseBuilder = Response.ok(); + PowerMockito + .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", + "password")) + .thenReturn( + responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); + + mrBaseClient.get("/path", "username", "password", "HTTPAUTH"); + assertTrue(true); + + } + + @Test + public void testGet_403() throws JSONException, HttpException { + ResponseBuilder responseBuilder = Response.status(403); + PowerMockito + .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", + "password")) + .thenReturn( + responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); + mrBaseClient.get("/path", "username", "password", "HTTPAUTH"); + assertTrue(true); + + } + + @Test + public void testGet_basicauth() throws JSONException, HttpException { + + ResponseBuilder responseBuilder = Response.ok(); + PowerMockito + .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"), + Base64.encodeAsString("username:password"))) + .thenReturn( + responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); + + mrBaseClient.get("/path", "username", "password", "HTTPAAF"); + assertTrue(true); + + } + + @Test(expected = HttpException.class) + public void testGet_error() throws JSONException, HttpException { + + ResponseBuilder responseBuilder = Response.ok(); + PowerMockito + .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", + "password")) + .thenReturn( + responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); + + mrBaseClient.get("/path", null, null, "HTTPAUTH"); + assertTrue(true); + + } + + @Test + public void testGet_wrongjson() throws JSONException, HttpException { + + ResponseBuilder responseBuilder = Response.ok(); + PowerMockito + .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", + "password")) + .thenReturn(responseBuilder.header("transactionid", "transactionid").entity("[[").build()); + + mrBaseClient.get("/path", "username", "password", "HTTPAUTH"); + assertTrue(true); + } + + @Test + public void testGetResponse() throws JSONException, HttpException { + + ResponseBuilder responseBuilder = Response.ok(); + PowerMockito + .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", + "password")) + .thenReturn( + responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); + + mrBaseClient.getResponse("/path", "username", "password", "HTTPAUTH"); + assertTrue(true); + + } + + @Test + public void testGetResponse_aaf() throws JSONException, HttpException { + + ResponseBuilder responseBuilder = Response.ok(); + PowerMockito + .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"), + Base64.encodeAsString("username:password"))) + .thenReturn( + responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); + + mrBaseClient.getResponse("/path", "username", "password", "HTTPAAF"); + assertTrue(true); + + } + + @Test(expected = HttpException.class) + public void testGetResponse_error() throws JSONException, HttpException { + + ResponseBuilder responseBuilder = Response.ok(); + PowerMockito + .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", + "password")) + .thenReturn( + responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); + + mrBaseClient.getResponse("/path", null, null, "HTTPAUTH"); + + } + + @Test + public void testAuthResponse() throws JSONException, HttpException { + + ResponseBuilder responseBuilder = Response.ok(); + PowerMockito + .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", + "password")) + .thenReturn( + responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); + + mrBaseClient.getAuthResponse("/path", "username", "password", "username", "password", "HTTPAUTH"); + assertTrue(true); + + } + + @Test(expected = HttpException.class) + public void testAuthResponsee_error() throws JSONException, HttpException { + + ResponseBuilder responseBuilder = Response.ok(); + PowerMockito + .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", + "password")) + .thenReturn( + responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); + + mrBaseClient.getAuthResponse("/path", null, null, null, null, "HTTPAUTH"); + + } + + @Test + public void testPostAuth() throws JSONException, HttpException { + + ResponseBuilder responseBuilder = Response.ok(); + PowerMockito + .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", + "password", new String("{\"test\":\"test\"}").getBytes(), "application/json")) + .thenReturn( + responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); + + mrBaseClient.postAuth("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", "username", + "password", "username", "password", "HTTPAUTH"); + assertTrue(true); + + } + + @Test(expected = HttpException.class) + public void testPostAuth_error() throws JSONException, HttpException { + + ResponseBuilder responseBuilder = Response.ok(); + PowerMockito + .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", + "password", new String("{\"test\":\"test\"}").getBytes(), "application/json")) + .thenReturn( + responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); + + mrBaseClient.postAuth("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", null, null, + null, null, "HTTPAUTH"); + assertTrue(true); + + } + + @Test + public void testGetNoAuthResponse() throws JSONException, HttpException { + + ResponseBuilder responseBuilder = Response.ok(); + PowerMockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget("/path"))).thenReturn( + responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); + + mrBaseClient.getNoAuthResponse("/path", "username", "password", "HTTPAUTH"); + assertTrue(true); + + } + + @Test + public void testPost() throws JSONException, HttpException { + + ResponseBuilder responseBuilder = Response.ok(); + PowerMockito + .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"), + Base64.encodeAsString("username:password"))) + .thenReturn( + responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); + + mrBaseClient.post("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", "username", + "password", "HTTPAUTH"); + assertTrue(true); + + } + + @Test(expected = HttpException.class) + public void testPost_error() throws JSONException, HttpException { + + ResponseBuilder responseBuilder = Response.ok(); + PowerMockito + .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"), + Base64.encodeAsString("username:password"))) + .thenReturn( + responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); + + mrBaseClient.post("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", null, null, + "HTTPAUTH"); + + } + + @Test + public void testPostAuthwithResponse() throws JSONException, HttpException { + + ResponseBuilder responseBuilder = Response.ok(); + PowerMockito + .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", + "password", new String("{\"test\":\"test\"}").getBytes(), "application/json")) + .thenReturn( + responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); + + mrBaseClient.postAuthwithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", + "username", "password", "username", "password", "HTTPAUTH"); + assertTrue(true); + + } + + @Test(expected = HttpException.class) + public void testPostAuthwithResponse_error() throws JSONException, HttpException { + + ResponseBuilder responseBuilder = Response.ok(); + PowerMockito + .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", + "password", new String("{\"test\":\"test\"}").getBytes(), "application/json")) + .thenReturn( + responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); + + mrBaseClient.postAuthwithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", + null, null, null, null, "HTTPAUTH"); + assertTrue(true); + + } + + @Test + public void testPostWithResponse() throws JSONException, HttpException { + + ResponseBuilder responseBuilder = Response.ok(); + PowerMockito + .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"), + Base64.encodeAsString("username:password"))) + .thenReturn( + responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); + + mrBaseClient.postWithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", + "username", "password", "HTTPAUTH"); + assertTrue(true); + + } + + @Test(expected = HttpException.class) + public void testPostWithResponse_error() throws JSONException, HttpException { + + ResponseBuilder responseBuilder = Response.ok(); + PowerMockito + .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"), + Base64.encodeAsString("username:password"))) + .thenReturn( + responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); + + mrBaseClient.postWithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", null, + null, "HTTPAUTH"); + + } + + @Test + public void testGetAuth() throws JSONException, HttpException { + + ResponseBuilder responseBuilder = Response.ok(); + PowerMockito + .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", + "password")) + .thenReturn( + responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); + mrBaseClient.getAuth("/path", "username", "password", "username", "password", "HTTPAUTH"); + assertTrue(true); + + } + + @Test(expected = HttpException.class) + public void testGetAuth_error() throws JSONException, HttpException { + + ResponseBuilder responseBuilder = Response.ok(); + PowerMockito + .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", + "password", new String("{\"test\":\"test\"}").getBytes(), "application/json")) + .thenReturn( + responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); + + mrBaseClient.getAuth("/path", null, null, null, null, "HTTPAUTH"); + assertTrue(true); + + } + + @Test + public void testGetNoAuth() throws JSONException, HttpException { + + ResponseBuilder responseBuilder = Response.ok(); + PowerMockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget("/path"))).thenReturn( + responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); + mrBaseClient.getNoAuth("/path", "username", "password", "HTTPAUTH"); + assertTrue(true); + + } + + @Test(expected = HttpException.class) + public void testGetNoAuth_error() throws JSONException, HttpException { + + ResponseBuilder responseBuilder = Response.ok(); + PowerMockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget("/path"))).thenReturn( + responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); + mrBaseClient.getNoAuth("/path", null, null, "HTTPAUTH"); + assertTrue(true); + + } + + @Test + public void testGetHTTPErrorResponseMessage() { + + assertEquals(mrBaseClient.getHTTPErrorResponseMessage("testtest"), "testtest"); + + } + + @Test + public void getGTTPErrorResponseCode() { + + assertEquals(mrBaseClient.getHTTPErrorResponseMessage("testtest"), "testtest"); + + } + +} diff --git a/src/test/java/com/att/nsa/mr/client/impl/MRBatchPublisherTest.java b/src/test/java/com/att/nsa/mr/client/impl/MRBatchPublisherTest.java new file mode 100644 index 0000000..19b58e9 --- /dev/null +++ b/src/test/java/com/att/nsa/mr/client/impl/MRBatchPublisherTest.java @@ -0,0 +1,54 @@ +/******************************************************************************* + * ============LICENSE_START======================================================= + * org.onap.dmaap + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * + *******************************************************************************/ +package com.att.nsa.mr.client.impl; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; + +import org.junit.Before; +import org.junit.Test; + +public class MRBatchPublisherTest { + + private Collection hosts=new HashSet<>(Arrays.asList("/test")); + private MRBatchPublisher mrBatchPublisher=new MRBatchPublisher(hosts, "topic", 2, 20, true); + + + @Before + public void setup(){ + + + } + + @Test + public void testSend() throws IOException{ + mrBatchPublisher.send("testmessage"); + } + + @Test + public void testClose() throws IOException{ + mrBatchPublisher.close(); + } + +} diff --git a/src/test/java/com/att/nsa/mr/client/impl/MRConsumerImplTest.java b/src/test/java/com/att/nsa/mr/client/impl/MRConsumerImplTest.java index 86057c4..bfac947 100644 --- a/src/test/java/com/att/nsa/mr/client/impl/MRConsumerImplTest.java +++ b/src/test/java/com/att/nsa/mr/client/impl/MRConsumerImplTest.java @@ -23,11 +23,13 @@ package com.att.nsa.mr.client.impl; import java.io.IOException; import java.util.LinkedList; +import java.util.Properties; import junit.framework.TestCase; import org.junit.Test; +import com.att.nsa.mr.client.MRClientFactory; import com.att.nsa.mr.client.impl.MRConstants; import com.att.nsa.mr.client.impl.MRConsumerImpl; @@ -92,4 +94,19 @@ public class MRConsumerImplTest extends TestCase final String url = c.createUrlPath (MRConstants.makeConsumerUrl ( "localhost:8080", "topic", "cg", "cid","http" ), -1, -1 ); assertEquals ( "http://localhost:8080/events/" + "topic/cg/cid?filter=%7B+%22foo%22%3D%22bar%22bar%22+%7D", url ); } + + @Test + public void testFetchWithReturnConsumerResponse () throws IOException + { + final LinkedList hosts = new LinkedList (); + hosts.add ( "localhost:8080" ); + Properties properties = new Properties(); + properties.load(MRSimplerBatchConsumerTest.class.getClassLoader().getResourceAsStream("dme2/consumer.properties")); + + final MRConsumerImpl c = new MRConsumerImpl ( hosts, "topic", "cg", "cid", -1, -1, "{ \"foo\"=\"bar\"bar\" }", null, null ); + c.fetchWithReturnConsumerResponse(); + c.setProtocolFlag("HTTPAAF"); + c.fetchWithReturnConsumerResponse(); + assertTrue(true); + } } diff --git a/src/test/java/com/att/nsa/mr/client/impl/MRMetaClientTest.java b/src/test/java/com/att/nsa/mr/client/impl/MRMetaClientTest.java index dc2214f..4a3650f 100644 --- a/src/test/java/com/att/nsa/mr/client/impl/MRMetaClientTest.java +++ b/src/test/java/com/att/nsa/mr/client/impl/MRMetaClientTest.java @@ -32,9 +32,11 @@ import org.junit.Rule; import org.junit.Test; import static com.github.tomakehurst.wiremock.client.WireMock.*; +import static org.junit.Assert.assertTrue; import com.att.nsa.apiClient.http.HttpException; import com.att.nsa.apiClient.http.HttpObjectNotFoundException; +import com.att.nsa.mr.client.MRClient.MRApiException; import com.att.nsa.mr.client.MRTopicManager.TopicInfo; import com.github.tomakehurst.wiremock.junit.WireMockRule; @@ -102,6 +104,26 @@ public class MRMetaClientTest { e.printStackTrace(); } } + @Test + public void testupdateApiKey(){ + final Collection hosts = new LinkedList (); + hosts.add ( "localhost:" + wireMock.port() ); + + MRMetaClient c; + try { + c = new MRMetaClient(hosts); + c.updateCurrentApiKey("test@onap.com", "test email"); + }catch (HttpException e) { + + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + catch (NullPointerException e) { + assertTrue(true); + } + + } } diff --git a/src/test/java/com/att/nsa/mr/client/impl/MRSimplerBatchConsumerTest.java b/src/test/java/com/att/nsa/mr/client/impl/MRSimplerBatchConsumerTest.java index ba4bb12..506d277 100644 --- a/src/test/java/com/att/nsa/mr/client/impl/MRSimplerBatchConsumerTest.java +++ b/src/test/java/com/att/nsa/mr/client/impl/MRSimplerBatchConsumerTest.java @@ -71,5 +71,6 @@ public class MRSimplerBatchConsumerTest { } } + } -- cgit 1.2.3-korg