Test coverage in ArtifactHandlerClient 11/79711/5
authorJoss Armstrong <joss.armstrong@ericsson.com>
Tue, 5 Mar 2019 14:18:59 +0000 (14:18 +0000)
committerTakamune Cho <takamune.cho@att.com>
Wed, 6 Mar 2019 15:31:13 +0000 (15:31 +0000)
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 <joss.armstrong@ericsson.com>
appc-inbound/appc-design-services/provider/pom.xml
appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/services/util/ArtifactHandlerClient.java
appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/services/util/DesignServiceConstants.java
appc-inbound/appc-design-services/provider/src/test/java/org/onap/appc/design/services/util/ArtifactHandlerClientTest.java
appc-inbound/appc-design-services/provider/src/test/java/org/onap/appc/design/validator/TestArifactHandlerClient.java [deleted file]

index e3ecf88..4593a2f 100755 (executable)
             <artifactId>mockito-core</artifactId>            
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.javassist</groupId>
+            <artifactId>javassist</artifactId>
+            <version>3.22.0-GA</version>
+            <scope>test</scope>
+        </dependency>
         <dependency>
             <groupId>org.onap.appc</groupId>
             <artifactId>appc-design-services-model</artifactId>
index a75f8d2..9aeaa1a 100644 (file)
@@ -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) {
index ccd5bf8..42f3d3f 100644 (file)
@@ -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);
     }
index a23956f..8b3d023 100644 (file)
 
 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 (file)
index 9411061..0000000
+++ /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)
-        {
-        }
-    }
-}