diff options
author | Joss Armstrong <joss.armstrong@ericsson.com> | 2019-02-05 09:39:45 +0000 |
---|---|---|
committer | Patrick Brady <patrick.brady@att.com> | 2019-02-05 21:11:04 +0000 |
commit | 3a3d8e4ff51b849335946ec1cc06aedaa133730b (patch) | |
tree | 96b901a9d3547f2e60348aa865aa510f3a359898 /appc-adapters/appc-rest-healthcheck-adapter/appc-rest-healthcheck-adapter-bundle/src | |
parent | 8f725f853e52d00d5c40a9b78a1c2b6381d10e70 (diff) |
Test coverage in rest-healthcheck-adapter
Increased coverage
Issue-ID: APPC-1387
Change-Id: Idafa0d7dbd5cb740728197bc3b4c232bdbef0821
Signed-off-by: Joss Armstrong <joss.armstrong@ericsson.com>
Diffstat (limited to 'appc-adapters/appc-rest-healthcheck-adapter/appc-rest-healthcheck-adapter-bundle/src')
2 files changed, 92 insertions, 94 deletions
diff --git a/appc-adapters/appc-rest-healthcheck-adapter/appc-rest-healthcheck-adapter-bundle/src/main/java/org/onap/appc/adapter/restHealthcheck/impl/RestHealthcheckAdapterImpl.java b/appc-adapters/appc-rest-healthcheck-adapter/appc-rest-healthcheck-adapter-bundle/src/main/java/org/onap/appc/adapter/restHealthcheck/impl/RestHealthcheckAdapterImpl.java index aa4cb7d07..9188a2e5f 100644 --- a/appc-adapters/appc-rest-healthcheck-adapter/appc-rest-healthcheck-adapter-bundle/src/main/java/org/onap/appc/adapter/restHealthcheck/impl/RestHealthcheckAdapterImpl.java +++ b/appc-adapters/appc-rest-healthcheck-adapter/appc-rest-healthcheck-adapter-bundle/src/main/java/org/onap/appc/adapter/restHealthcheck/impl/RestHealthcheckAdapterImpl.java @@ -5,6 +5,8 @@ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2017 Amdocs + * ================================================================================ + * 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. @@ -24,34 +26,19 @@ package org.onap.appc.adapter.restHealthcheck.impl; import java.util.Map; -import java.util.Properties; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; - +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import org.glassfish.grizzly.http.util.HttpStatus; import org.onap.appc.Constants; import org.onap.appc.adapter.restHealthcheck.RestHealthcheckAdapter; import org.onap.appc.configuration.Configuration; -import org.onap.appc.pool.PoolExtensionException; -import org.onap.appc.util.StructuredPropertyHelper; - - -import com.att.cdp.zones.ImageService; -import com.att.cdp.zones.Provider; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; -import com.att.eelf.i18n.EELFResourceManager; -import org.onap.ccsdk.sli.core.sli.SvcLogicContext; - -import org.glassfish.grizzly.http.util.HttpStatus; - -import static com.att.eelf.configuration.Configuration.*; - -import org.apache.http.*; -import org.apache.http.client.*; -import org.apache.http.client.methods.*; -import org.apache.http.impl.client.*; -import org.apache.http.util.EntityUtils; -import java.io.IOException; -import java.net.InetAddress; public class RestHealthcheckAdapterImpl implements RestHealthcheckAdapter { @@ -81,9 +68,9 @@ public class RestHealthcheckAdapterImpl implements RestHealthcheckAdapter { } public void checkHealth(Map<String, String> params, SvcLogicContext ctx) { logger.info("VNF rest health check"); - String uri=params.get("VNF.URI"); - String endPoint=params.get("VNF.endpoint"); - String tUrl=uri+"/"+endPoint; + String uri = params.get("VNF.URI"); + String endPoint = params.get("VNF.endpoint"); + String tUrl = uri + "/" + endPoint; RequestContext rc = new RequestContext(ctx); rc.isAlive(); try(CloseableHttpClient httpClient = HttpClients.createDefault()) { @@ -92,14 +79,14 @@ public class RestHealthcheckAdapterImpl implements RestHealthcheckAdapter { response = httpClient.execute(httpGet); int responseCode=response.getStatusLine().getStatusCode(); HttpEntity entity = response.getEntity(); - String responseOutput=EntityUtils.toString(entity); - if(responseCode==200) + String responseOutput = EntityUtils.toString(entity); + if(responseCode == 200) { - doSuccess(rc,responseCode,responseOutput); + doSuccess(rc, responseCode, responseOutput); } else { - doHealthCheckFailure(rc,responseCode,responseOutput); + doHealthCheckFailure(rc, responseCode, responseOutput); } } catch (Exception ex) { doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, ex.toString()); @@ -120,7 +107,7 @@ public class RestHealthcheckAdapterImpl implements RestHealthcheckAdapter { } svcLogic.setStatus(OUTCOME_FAILURE); svcLogic.setAttribute("healthcheck.result.code", "200"); - svcLogic.setAttribute("healthcheck.result.message", status+" "+msg); + svcLogic.setAttribute("healthcheck.result.message", status + " " + msg); } /** * @param rc @@ -130,14 +117,14 @@ public class RestHealthcheckAdapterImpl implements RestHealthcheckAdapter { @SuppressWarnings("static-method") private void doHealthCheckFailure(RequestContext rc, int code, String message) { SvcLogicContext svcLogic = rc.getSvcLogicContext(); - String msg = Integer.toString(code)+" "+message; + String msg = Integer.toString(code) + " " + message; svcLogic.setAttribute("healthcheck.result.code", "200"); svcLogic.setAttribute("healthcheck.result.message", msg); } @SuppressWarnings("static-method") private void doSuccess(RequestContext rc, int code, String message) { SvcLogicContext svcLogic = rc.getSvcLogicContext(); - String msg = Integer.toString(code)+" "+message; + String msg = Integer.toString(code) + " " + message; svcLogic.setAttribute("healthcheck.result.code", "400"); svcLogic.setAttribute("healthcheck.result.message", msg); } diff --git a/appc-adapters/appc-rest-healthcheck-adapter/appc-rest-healthcheck-adapter-bundle/src/test/java/org/onap/appc/adapter/restHealthcheck/impl/TestRestHealthcheckAdapterImpl.java b/appc-adapters/appc-rest-healthcheck-adapter/appc-rest-healthcheck-adapter-bundle/src/test/java/org/onap/appc/adapter/restHealthcheck/impl/TestRestHealthcheckAdapterImpl.java index b2aabb846..0535854d4 100644 --- a/appc-adapters/appc-rest-healthcheck-adapter/appc-rest-healthcheck-adapter-bundle/src/test/java/org/onap/appc/adapter/restHealthcheck/impl/TestRestHealthcheckAdapterImpl.java +++ b/appc-adapters/appc-rest-healthcheck-adapter/appc-rest-healthcheck-adapter-bundle/src/test/java/org/onap/appc/adapter/restHealthcheck/impl/TestRestHealthcheckAdapterImpl.java @@ -5,6 +5,8 @@ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2017 Amdocs + * ================================================================================ + * 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. @@ -23,94 +25,103 @@ package org.onap.appc.adapter.restHealthcheck.impl; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - import java.io.IOException; -import java.lang.reflect.Field; import java.util.HashMap; -import java.util.List; import java.util.Map; -import java.util.Properties; -import java.util.Set; - +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.ParseException; +import org.apache.http.StatusLine; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Ignore; import org.junit.Test; -import org.slf4j.MDC; - -import org.onap.appc.Constants; -import org.onap.appc.adapter.restHealthcheck.*; -import org.onap.appc.configuration.ConfigurationFactory; +import org.junit.runner.RunWith; +import org.mockito.Mockito; import org.onap.appc.exceptions.APPCException; -import org.onap.appc.exceptions.UnknownProviderException; -import com.att.cdp.exceptions.ZoneException; -import com.att.cdp.zones.ComputeService; -import com.att.cdp.zones.Context; -import com.att.cdp.zones.ContextFactory; -import com.att.cdp.zones.model.Server; -import com.att.cdp.zones.model.Server.Status; 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; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +@RunWith(PowerMockRunner.class) +@PrepareForTest({HttpClients.class, EntityUtils.class}) public class TestRestHealthcheckAdapterImpl { - @SuppressWarnings("nls") - private static final String PROVIDER_NAME = "ILAB"; - - @SuppressWarnings("nls") - private static final String PROVIDER_TYPE = "OpenStackProvider"; - - private static String IDENTITY_URL; - - private static String PRINCIPAL; - - private static String CREDENTIAL; - - private static String TENANT_NAME; - - private static String TENANT_ID; - - private static String USER_ID; - - private static String REGION_NAME; - - private static String SERVER_URL; - - private static Class<?> providerAdapterImplClass; - private static Class<?> configurationFactoryClass; - private static Field providerCacheField; - private static Field configField; - private RestHealthcheckAdapterImpl adapter; - - - @SuppressWarnings("nls") - @BeforeClass - public static void once() throws NoSuchFieldException, SecurityException, NoSuchMethodException { - - } - + private CloseableHttpClient client; + @Before - public void setup() throws IllegalArgumentException, IllegalAccessException { - + public void setup() throws IllegalArgumentException, IllegalAccessException, ParseException, IOException { + PowerMockito.mockStatic(HttpClients.class); + PowerMockito.mockStatic(EntityUtils.class); + client = Mockito.mock(CloseableHttpClient.class); + PowerMockito.when(HttpClients.createDefault()).thenReturn(client); + PowerMockito.when(EntityUtils.toString(Mockito.any(HttpEntity.class))).thenReturn("TEST"); adapter = new RestHealthcheckAdapterImpl(); } @Test public void testCheckHealth() throws IOException, IllegalStateException, IllegalArgumentException, ZoneException, APPCException { + Map<String, String> params = new HashMap<>(); + params.put("VNF.URI", "http://restHalthCheck.test"); + params.put("VNF.endpoint", "health"); + HttpResponse response = Mockito.mock(CloseableHttpResponse.class); + HttpEntity entity = Mockito.mock(HttpEntity.class); + Mockito.doReturn(entity).when(response).getEntity(); + StatusLine statusLine = Mockito.mock(StatusLine.class); + Mockito.doReturn(200).when(statusLine).getStatusCode(); + Mockito.doReturn(statusLine).when(response).getStatusLine(); + Mockito.doReturn(response).when(client).execute(Mockito.any(HttpGet.class)); + SvcLogicContext svcContext = new SvcLogicContext(); + adapter.checkHealth(params, svcContext); + String statusCode = svcContext.getAttribute("healthcheck.result.code"); + assertEquals("400", statusCode); + } + + @Test + public void testCheckHealthFailure() throws IOException, IllegalStateException, IllegalArgumentException, + ZoneException, APPCException { + Map<String, String> params = new HashMap<>(); + params.put("VNF.URI", "http://restHalthCheck.test"); + params.put("VNF.endpoint", "health"); + HttpResponse response = Mockito.mock(CloseableHttpResponse.class); + HttpEntity entity = Mockito.mock(HttpEntity.class); + Mockito.doReturn(entity).when(response).getEntity(); + StatusLine statusLine = Mockito.mock(StatusLine.class); + Mockito.doReturn(400).when(statusLine).getStatusCode(); + Mockito.doReturn(statusLine).when(response).getStatusLine(); + Mockito.doReturn(response).when(client).execute(Mockito.any(HttpGet.class)); + SvcLogicContext svcContext = new SvcLogicContext(); + adapter.checkHealth(params, svcContext); + String statusCode = svcContext.getAttribute("healthcheck.result.code"); + assertEquals("200", statusCode); + } + @Test + public void testCheckHealthException() throws IOException, IllegalStateException, IllegalArgumentException, + ZoneException, APPCException { Map<String, String> params = new HashMap<>(); params.put("VNF.URI", "http://restHalthCheck.test"); params.put("VNF.endpoint", "health"); + HttpResponse response = Mockito.mock(CloseableHttpResponse.class); + HttpEntity entity = Mockito.mock(HttpEntity.class); + Mockito.doReturn(entity).when(response).getEntity(); + StatusLine statusLine = Mockito.mock(StatusLine.class); + Mockito.doReturn(400).when(statusLine).getStatusCode(); + Mockito.doReturn(statusLine).when(response).getStatusLine(); + Mockito.doThrow(new IOException()).when(client).execute(Mockito.any(HttpGet.class)); SvcLogicContext svcContext = new SvcLogicContext(); adapter.checkHealth(params, svcContext); - String statusCode=svcContext.getAttribute("healthcheck.result.code"); - assertEquals("200",statusCode); + String statusCode = svcContext.getAttribute("healthcheck.result.code"); + assertEquals(RestHealthcheckAdapterImpl.OUTCOME_FAILURE, svcContext.getStatus()); } |