diff options
author | Joss Armstrong <joss.armstrong@ericsson.com> | 2019-02-28 12:38:37 +0000 |
---|---|---|
committer | Takamune Cho <takamune.cho@att.com> | 2019-02-28 14:50:53 +0000 |
commit | e2fc1f72f0e70fa24e174b9325e984840a2a0272 (patch) | |
tree | 9976475eb548e29f2bac91a81163f1483cdb022e /appc-adapters/appc-rest-adapter | |
parent | f3b94a73f7693b95164f970eca1d2ec283f85499 (diff) |
Test coverage in RestAdapterImpl
Increased coverage from 69% to 98%
Fixed Sonar major issue for branch coverage
Issue-ID: APPC-1512
Change-Id: Idb989cf6266b4e7c7dda1e9bca72483038649eda
Signed-off-by: Joss Armstrong <joss.armstrong@ericsson.com>
Diffstat (limited to 'appc-adapters/appc-rest-adapter')
-rw-r--r-- | appc-adapters/appc-rest-adapter/appc-rest-adapter-bundle/src/test/java/org/onap/appc/adapter/rest/impl/TestRestAdapterImpl.java | 172 |
1 files changed, 77 insertions, 95 deletions
diff --git a/appc-adapters/appc-rest-adapter/appc-rest-adapter-bundle/src/test/java/org/onap/appc/adapter/rest/impl/TestRestAdapterImpl.java b/appc-adapters/appc-rest-adapter/appc-rest-adapter-bundle/src/test/java/org/onap/appc/adapter/rest/impl/TestRestAdapterImpl.java index f056ceb09..5087cfa84 100644 --- a/appc-adapters/appc-rest-adapter/appc-rest-adapter-bundle/src/test/java/org/onap/appc/adapter/rest/impl/TestRestAdapterImpl.java +++ b/appc-adapters/appc-rest-adapter/appc-rest-adapter-bundle/src/test/java/org/onap/appc/adapter/rest/impl/TestRestAdapterImpl.java @@ -10,6 +10,8 @@ * ============================================================================= * Modifications Copyright (C) 2018 Samsung * ================================================================================ + * Modifications Copyright (C) 2019 Ericsson + * ================================================================================ * 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 @@ -28,32 +30,48 @@ package org.onap.appc.adapter.rest.impl; import static org.junit.Assert.assertEquals; - +import static org.junit.Assert.assertNotNull; import java.io.IOException; +import java.net.MalformedURLException; import java.util.HashMap; import java.util.Map; - +import org.apache.http.HttpEntity; +import org.apache.http.StatusLine; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.client.methods.HttpRequestBase; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.ssl.SSLContexts; import org.apache.http.util.EntityUtils; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; - +import org.junit.runner.RunWith; +import org.mockito.Mockito; import org.onap.appc.exceptions.APPCException; -import com.att.cdp.exceptions.ZoneException; import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import com.att.cdp.exceptions.ZoneException; /** * Test the ProviderAdapter implementation. */ +@RunWith(PowerMockRunner.class) +@PrepareForTest({HttpClients.class, SSLContexts.class}) public class TestRestAdapterImpl { private RestAdapterImpl adapter; - + private CloseableHttpClient client; + private CloseableHttpResponse httpResponse; + private HttpEntity httpEntity; + private StatusLine statusLine; @SuppressWarnings("nls") @BeforeClass @@ -62,11 +80,20 @@ public class TestRestAdapterImpl { } @Before - public void setup() throws IllegalArgumentException, IllegalAccessException { - + public void setup() throws IllegalArgumentException, IllegalAccessException, ClientProtocolException, IOException { + client = Mockito.mock(CloseableHttpClient.class); + PowerMockito.mockStatic(HttpClients.class); + PowerMockito.when(HttpClients.createDefault()).thenReturn(client); + httpResponse = Mockito.mock(CloseableHttpResponse.class); + statusLine = Mockito.mock(StatusLine.class); + Mockito.when(statusLine.getStatusCode()).thenReturn(200); + Mockito.when(httpResponse.getStatusLine()).thenReturn(statusLine); + httpEntity = Mockito.mock(HttpEntity.class); + Mockito.when(httpResponse.getEntity()).thenReturn(httpEntity); + Mockito.when(client.execute(Mockito.any())).thenReturn(httpResponse); adapter = new RestAdapterImpl(); } - + @Test public void testCreateHttpRequestGet() throws IOException, IllegalStateException, IllegalArgumentException, ZoneException, APPCException { @@ -82,41 +109,8 @@ public class TestRestAdapterImpl { } @Test - public void testCreateRequestNoParamGet() throws IOException, IllegalStateException, IllegalArgumentException, - ZoneException, APPCException { - - SvcLogicContext ctx = new SvcLogicContext(); - Map<String, String> params = new HashMap<>(); - - adapter.commonGet(params, ctx); - - assertEquals("failure", ctx.getStatus()); - assertEquals("500", ctx.getAttribute("org.onap.rest.result.code")); - assertEquals("java.lang.IllegalArgumentException: HTTP request may not be null", - ctx.getAttribute("org.onap.rest.result.message")); - } - - @Test - public void testCreateRequestInvalidParamGet() throws IOException, IllegalStateException, IllegalArgumentException, - ZoneException, APPCException { - - SvcLogicContext ctx = new SvcLogicContext(); - Map<String, String> params = new HashMap<>(); - params.put("org.onap.appc.instance.URI", "boo"); - params.put("org.onap.appc.instance.haveHeader","false"); - params.put("org.onap.appc.instance.requestBody", "{\"name\":\"MyNode2\", \"width\":300, \"height\":300}"); - - adapter.commonGet(params, ctx); - - assertEquals("failure", ctx.getStatus()); - assertEquals("500", ctx.getAttribute("org.onap.rest.result.code")); - assertEquals("org.apache.http.client.ClientProtocolException", - ctx.getAttribute("org.onap.rest.result.message")); - } - - @Test public void testCreateHttpRequestPost() throws IOException, IllegalStateException, IllegalArgumentException, - ZoneException, APPCException { + ZoneException, APPCException { Map<String, String> params = new HashMap<>(); params.put("org.onap.appc.instance.URI", "http://example.com:8081/posttest"); @@ -131,24 +125,9 @@ public class TestRestAdapterImpl { } @Test - public void testCreateRequestNoParamPost() throws IOException, IllegalStateException, IllegalArgumentException, - ZoneException, APPCException { - - SvcLogicContext ctx = new SvcLogicContext(); - Map<String, String> params = new HashMap<>(); - - adapter.commonPost(params, ctx); - - assertEquals("failure", ctx.getStatus()); - assertEquals("500", ctx.getAttribute("org.onap.rest.result.code")); - assertEquals("java.lang.IllegalArgumentException: HTTP request may not be null", - ctx.getAttribute("org.onap.rest.result.message")); - } - - @Test public void testCreateRequestInvalidParamPost() throws IOException, IllegalStateException, IllegalArgumentException, ZoneException, APPCException { - + Mockito.when(statusLine.getStatusCode()).thenReturn(500); SvcLogicContext ctx = new SvcLogicContext(); Map<String, String> params = new HashMap<>(); params.put("org.onap.appc.instance.URI", "boo"); @@ -159,10 +138,10 @@ public class TestRestAdapterImpl { assertEquals("failure", ctx.getStatus()); assertEquals("500", ctx.getAttribute("org.onap.rest.result.code")); - assertEquals("org.apache.http.client.ClientProtocolException", + assertEquals("Internal Server Error", ctx.getAttribute("org.onap.rest.result.message")); } - + @Test public void testCreateHttpRequestPut() throws IOException, IllegalStateException, IllegalArgumentException, ZoneException, APPCException { @@ -173,7 +152,6 @@ public class TestRestAdapterImpl { params.put("org.onap.appc.instance.requestBody", "{\"name\":\"MyNode2\", \"width\":300, \"height\":300}"); HttpPut httpPut = ((HttpPut) givenParams(params, "PUT")); - //Header headers[] = httpPut.getAllHeaders(); assertEquals("PUT", httpPut.getMethod()); assertEquals("http://example.com:8081/puttest", httpPut.getURI().toURL().toString()); @@ -183,33 +161,15 @@ public class TestRestAdapterImpl { @Test public void testCreateRequestNoParamPut() throws IOException, IllegalStateException, IllegalArgumentException, ZoneException, APPCException { - + Mockito.when(statusLine.getStatusCode()).thenReturn(200); SvcLogicContext ctx = new SvcLogicContext(); Map<String, String> params = new HashMap<>(); adapter.commonPut(params, ctx); - assertEquals("failure", ctx.getStatus()); - assertEquals("500", ctx.getAttribute("org.onap.rest.result.code")); - assertEquals("java.lang.IllegalArgumentException: HTTP request may not be null", - ctx.getAttribute("org.onap.rest.result.message")); - } - - @Test - public void testCreateRequestInvalidParamPut() throws IOException, IllegalStateException, IllegalArgumentException, - ZoneException, APPCException { - - SvcLogicContext ctx = new SvcLogicContext(); - Map<String, String> params = new HashMap<>(); - params.put("org.onap.appc.instance.URI", "boo"); - params.put("org.onap.appc.instance.haveHeader","false"); - params.put("org.onap.appc.instance.requestBody", "{\"name\":\"MyNode2\", \"width\":300, \"height\":300}"); - - adapter.commonPut(params, ctx); - - assertEquals("failure", ctx.getStatus()); - assertEquals("500", ctx.getAttribute("org.onap.rest.result.code")); - assertEquals("org.apache.http.client.ClientProtocolException", + assertEquals("success", ctx.getStatus()); + assertEquals("200", ctx.getAttribute("org.onap.rest.result.code")); + assertEquals("java.lang.NullPointerException", ctx.getAttribute("org.onap.rest.result.message")); } @@ -230,36 +190,60 @@ public class TestRestAdapterImpl { @Test public void testCreateRequestNoParamDelete() throws IOException, IllegalStateException, IllegalArgumentException, ZoneException, APPCException { - + Mockito.when(statusLine.getStatusCode()).thenReturn(400); SvcLogicContext ctx = new SvcLogicContext(); Map<String, String> params = new HashMap<>(); adapter.commonDelete(params, ctx); assertEquals("failure", ctx.getStatus()); - assertEquals("500", ctx.getAttribute("org.onap.rest.result.code")); - assertEquals("java.lang.IllegalArgumentException: HTTP request may not be null", + assertEquals("400", ctx.getAttribute("org.onap.rest.result.code")); + assertEquals("Bad Request", ctx.getAttribute("org.onap.rest.result.message")); } @Test - public void testCreateRequestInvalidParamDelete() throws IOException, IllegalStateException, IllegalArgumentException, - ZoneException, APPCException { + public void testDoFailureMultiLineErrorMessage() { + Map<String, String> mockParams = Mockito.mock(Map.class); + Mockito.when(mockParams.get("org.onap.appc.instance.URI")).thenThrow(new RuntimeException("\n\n")); + adapter.createHttpRequest("test_method", mockParams, new RequestContext(new SvcLogicContext())); + } + @Test + public void testCreateHttpRequestWithHeader() throws MalformedURLException { + Map<String, String> params = new HashMap<>(); + params.put("org.onap.appc.instance.URI", "http://example.com:8080/about/health"); + params.put("org.onap.appc.instance.headers", "{\"header1\":\"header1-value\"}"); + params.put("org.onap.appc.instance.haveHeader","true"); + + HttpGet httpGet = ((HttpGet) givenParams(params, "GET")); + + assertEquals("GET", httpGet.getMethod()); + assertNotNull(httpGet.getHeaders("header1")); + } + + @Test + public void testExecuteHttpRequest() { + SvcLogicContext ctx = new SvcLogicContext(); + Map<String, String> params = new HashMap<>(); + adapter.commonGet(params, ctx); + } + + @Test + public void testExecuteRequestException() throws IOException, IllegalStateException, IllegalArgumentException, + ZoneException, APPCException { + Mockito.when(client.execute(Mockito.any())).thenThrow(new IOException()); SvcLogicContext ctx = new SvcLogicContext(); Map<String, String> params = new HashMap<>(); - params.put("org.onap.appc.instance.URI", "boo"); - params.put("org.onap.appc.instance.haveHeader","false"); - params.put("org.onap.appc.instance.requestBody", "{\"name\":\"MyNode2\", \"width\":300, \"height\":300}"); adapter.commonDelete(params, ctx); assertEquals("failure", ctx.getStatus()); assertEquals("500", ctx.getAttribute("org.onap.rest.result.code")); - assertEquals("org.apache.http.client.ClientProtocolException", + assertEquals("java.io.IOException", ctx.getAttribute("org.onap.rest.result.message")); } - + private HttpRequestBase givenParams(Map<String, String> params, String method){ SvcLogicContext ctx = new SvcLogicContext(); RequestContext rc = new RequestContext(ctx); @@ -268,6 +252,4 @@ public class TestRestAdapterImpl { adapter = new RestAdapterImpl(); return adapter.createHttpRequest(method, params, rc); } - - } |