From 567a72bdcc1e653d444d45ebde6b8b749776b27e Mon Sep 17 00:00:00 2001 From: Joss Armstrong Date: Tue, 5 Mar 2019 14:18:59 +0000 Subject: Test coverage in ArtifactHandlerClient Increase coverage from 34% to 83% Fix for Sonar major issue on branch coverage Issue-ID: APPC-1436 Change-Id: Ie26e2cb71c833e4f086da2a238bd6f956bd85f33 Signed-off-by: Joss Armstrong --- appc-inbound/appc-design-services/provider/pom.xml | 6 ++ .../services/util/ArtifactHandlerClient.java | 2 +- .../services/util/DesignServiceConstants.java | 2 +- .../services/util/ArtifactHandlerClientTest.java | 66 ++++++++++++++++---- .../design/validator/TestArifactHandlerClient.java | 72 ---------------------- 5 files changed, 63 insertions(+), 85 deletions(-) delete mode 100644 appc-inbound/appc-design-services/provider/src/test/java/org/onap/appc/design/validator/TestArifactHandlerClient.java (limited to 'appc-inbound') diff --git a/appc-inbound/appc-design-services/provider/pom.xml b/appc-inbound/appc-design-services/provider/pom.xml index e3ecf887a..4593a2fa8 100755 --- a/appc-inbound/appc-design-services/provider/pom.xml +++ b/appc-inbound/appc-design-services/provider/pom.xml @@ -57,6 +57,12 @@ mockito-core test + + org.javassist + javassist + 3.22.0-GA + test + org.onap.appc appc-design-services-model diff --git a/appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/services/util/ArtifactHandlerClient.java b/appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/services/util/ArtifactHandlerClient.java index a75f8d21f..9aeaa1aa5 100644 --- a/appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/services/util/ArtifactHandlerClient.java +++ b/appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/services/util/ArtifactHandlerClient.java @@ -143,7 +143,7 @@ public class ArtifactHandlerClient { } catch (Exception e) { log.debug("failed in RESTCONT Action", e); - throw new IOException("Error While Sending Rest Request" + e.getMessage()); + throw new IOException("Error While Sending Rest Request" + e.getMessage(), e); } finally { // clean up. if (client != null) { diff --git a/appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/services/util/DesignServiceConstants.java b/appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/services/util/DesignServiceConstants.java index ccd5bf8d0..42f3d3f3e 100644 --- a/appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/services/util/DesignServiceConstants.java +++ b/appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/services/util/DesignServiceConstants.java @@ -111,7 +111,7 @@ public class DesignServiceConstants { public static final String CHECKVNF = "checkVNF"; private DesignServiceConstants() { } - + public static String getEnvironmentVariable(String env) { return System.getenv(env); } diff --git a/appc-inbound/appc-design-services/provider/src/test/java/org/onap/appc/design/services/util/ArtifactHandlerClientTest.java b/appc-inbound/appc-design-services/provider/src/test/java/org/onap/appc/design/services/util/ArtifactHandlerClientTest.java index a23956f18..8b3d023cc 100644 --- a/appc-inbound/appc-design-services/provider/src/test/java/org/onap/appc/design/services/util/ArtifactHandlerClientTest.java +++ b/appc-inbound/appc-design-services/provider/src/test/java/org/onap/appc/design/services/util/ArtifactHandlerClientTest.java @@ -21,16 +21,15 @@ package org.onap.appc.design.services.util; +import static org.hamcrest.CoreMatchers.isA; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import java.io.IOException; -import java.security.KeyManagementException; +import java.net.URI; +import java.net.URISyntaxException; import java.security.NoSuchAlgorithmException; -import java.security.SecureRandom; import java.util.Properties; -import javax.net.ssl.KeyManager; import javax.net.ssl.SSLContext; -import javax.net.ssl.TrustManager; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -41,17 +40,35 @@ import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.reflect.Whitebox; +import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.WebResource; +import com.sun.jersey.api.client.WebResource.Builder; + @RunWith(PowerMockRunner.class) -@PrepareForTest({DesignServiceConstants.class}) +@PrepareForTest({DesignServiceConstants.class, SSLContext.class, Client.class}) public class ArtifactHandlerClientTest { + private SSLContext sslContext = PowerMockito.mock(SSLContext.class); + private Client client; + private WebResource webResource; + private Builder builder; + @Rule public ExpectedException expectedEx = ExpectedException.none(); @Before - public void setup() throws NoSuchAlgorithmException, KeyManagementException { + public void setup() throws URISyntaxException { PowerMockito.mockStatic(DesignServiceConstants.class); + PowerMockito.mockStatic(SSLContext.class); + PowerMockito.mockStatic(Client.class); + sslContext = PowerMockito.mock(SSLContext.class); + client = Mockito.mock(Client.class); + builder = PowerMockito.mock(Builder.class); + webResource = PowerMockito.mock(WebResource.class); + PowerMockito.when(Client.create(Mockito.anyObject())).thenReturn(client); + webResource = PowerMockito.mock(WebResource.class); + PowerMockito.when(client.resource(Mockito.any(URI.class))).thenReturn(webResource); } @Test @@ -64,9 +81,9 @@ public class ArtifactHandlerClientTest { public void testConstructor() throws IOException { PowerMockito.when(DesignServiceConstants.getEnvironmentVariable(ArtifactHandlerClient.SDNC_CONFIG_DIR_VAR)) .thenReturn("src/test/resources"); - ArtifactHandlerClient client = new ArtifactHandlerClient(); - assertTrue(client instanceof ArtifactHandlerClient); - Properties props = Whitebox.getInternalState(client, "props"); + ArtifactHandlerClient ahClient = new ArtifactHandlerClient(); + assertTrue(ahClient instanceof ArtifactHandlerClient); + Properties props = Whitebox.getInternalState(ahClient, "props"); assertTrue(props.containsKey("appc.upload.provider.url")); } @@ -74,12 +91,39 @@ public class ArtifactHandlerClientTest { public void testCreateArtifactData() throws IOException { PowerMockito.when(DesignServiceConstants.getEnvironmentVariable(ArtifactHandlerClient.SDNC_CONFIG_DIR_VAR)) .thenReturn("src/test/resources"); - ArtifactHandlerClient client = new ArtifactHandlerClient(); + ArtifactHandlerClient ahClient = new ArtifactHandlerClient(); assertEquals("{\"input\": {\"request-information\":{\"request-id\":\"\",\"request-action\":" + "\"StoreSdcDocumentRequest\",\"source\":\"Design-tool\"},\"document-parameters\":" + "{\"artifact-version\":\"TEST\",\"artifact-name\":\"TEST\",\"artifact-contents\":" - + "\"TEST\"}}}", client.createArtifactData("{\"" + DesignServiceConstants.ARTIFACT_NAME + + "\"TEST\"}}}", ahClient.createArtifactData("{\"" + DesignServiceConstants.ARTIFACT_NAME + "\":\"TEST\", \"" + DesignServiceConstants.ARTIFACT_VERSOIN + "\":\"TEST\", \"" + DesignServiceConstants.ARTIFACT_CONTENTS + "\":\"TEST\"}", "")); } + + @Test + public void testExecuteArtifactHandlerInternalException() throws IOException, NoSuchAlgorithmException { + PowerMockito.when(SSLContext.getInstance("SSL")).thenReturn(sslContext); + PowerMockito.when(DesignServiceConstants.getEnvironmentVariable(ArtifactHandlerClient.SDNC_CONFIG_DIR_VAR)) + .thenReturn("src/test/resources"); + builder = Mockito.mock(Builder.class); + PowerMockito.when(webResource.accept("application/json")).thenReturn(builder); + ArtifactHandlerClient ahClient = new ArtifactHandlerClient(); + Properties properties = new Properties(); + properties.put("appc.upload.user", "TEST_USER"); + properties.put("appc.upload.provider.url", "http://127.0.0.1:8080/path"); + properties.put("appc.upload.pass", "enc:password"); + Whitebox.setInternalState(ahClient, "props", properties); + expectedEx.expectCause(isA(ArtifactHandlerInternalException.class)); + ahClient.execute("", "GET"); + } + + @Test + public void testExecuteIOException() throws IOException, NoSuchAlgorithmException { + PowerMockito.when(SSLContext.getInstance("SSL")).thenThrow(new RuntimeException()); + PowerMockito.when(DesignServiceConstants.getEnvironmentVariable(ArtifactHandlerClient.SDNC_CONFIG_DIR_VAR)) + .thenReturn("src/test/resources"); + ArtifactHandlerClient ahClient = new ArtifactHandlerClient(); + expectedEx.expect(IOException.class); + ahClient.execute("", ""); + } } diff --git a/appc-inbound/appc-design-services/provider/src/test/java/org/onap/appc/design/validator/TestArifactHandlerClient.java b/appc-inbound/appc-design-services/provider/src/test/java/org/onap/appc/design/validator/TestArifactHandlerClient.java deleted file mode 100644 index 941106199..000000000 --- a/appc-inbound/appc-design-services/provider/src/test/java/org/onap/appc/design/validator/TestArifactHandlerClient.java +++ /dev/null @@ -1,72 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Copyright (C) 2017 Amdocs - * ============================================================================= - * 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========================================================= - */ - -package org.onap.appc.design.validator; - -import org.junit.Before; -import org.junit.Test; -import org.onap.appc.design.services.util.ArtifactHandlerClient; -import junit.framework.Assert; -import static org.mockito.Mockito.*; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.util.Enumeration; -import java.util.Properties; -import org.apache.commons.io.FileUtils; - -public class TestArifactHandlerClient { - - @Test - public void testCreateArtifactData(){ - - try{ - String content = FileUtils.readFileToString(new File("src/test/resources/uploadArtifact")); - String payload = " { \"userID\": \"00000\", \"vnf-type\" : \"DesigTest-VNF\", \"action\" : \"Configure\", \"artifact-name\":\"DesignRestArtifact_reference\",\"artifact-version\" :\"0.01\",\"artifact-type\" :\"DESIGNTOOL-TEST\",\"artifact-contents\": " - + content + - " } "; - String requestID ="0000"; - ArtifactHandlerClient ahi = new ArtifactHandlerClient(); - String value = ahi.createArtifactData(payload, requestID); - Assert.assertTrue(!value.isEmpty()); - }catch(Exception e) - { - } - } - - @Test - public void testExecute(){ - - try{ - String content = FileUtils.readFileToString(new File("src/test/resources/uploadArtifact")); - String payload = " { \"userID\": \"00000\", \"vnf-type\" : \"DesigTest-VNF\", \"action\" : \"Configure\", \"artifact-name\":\"DesignRestArtifact_reference\",\"artifact-version\" :\"0.01\",\"artifact-type\" :\"DESIGNTOOL-TEST\",\"artifact-contents\": " - + content + - " } "; - String rpc = "Post"; - ArtifactHandlerClient ahi = new ArtifactHandlerClient(); - ahi.execute(payload, rpc); - }catch(Exception e) - { - } - } -} -- cgit 1.2.3-korg