Junit for code coverage in artifact-handler 91/60391/5
authorDilip kumar Pampana <dilip.kumar.pampana@ibm.com>
Mon, 13 Aug 2018 19:48:47 +0000 (15:48 -0400)
committerTakamune Cho <tc012c@att.com>
Fri, 31 Aug 2018 00:15:14 +0000 (00:15 +0000)
Junit Test cases for checking Exceptions part and also for isUpdate in
DBService.java file

Issue-ID: APPC-1140
Change-Id: Ibbfe9daf5465f4efcc8d42e8ed508d0b5e5129bb
Signed-off-by: Dilip kumar Pampana <dilip.kumar.pampana@ibm.com>
appc-inbound/appc-artifact-handler/provider/src/test/java/org/onap/appc/artifact/handler/dbservices/DBServiceTest.java
appc-inbound/appc-artifact-handler/provider/src/test/java/org/onap/appc/artifact/handler/dbservices/MockSvcLogicResource.java
appc-inbound/appc-artifact-handler/provider/src/test/java/org/onap/appc/artifact/handler/dbservices/TestDBServiceExceptions.java [new file with mode: 0644]

index ede019e..aac7cbe 100644 (file)
@@ -6,6 +6,8 @@
  * ================================================================================
  * Copyright (C) 2017 Amdocs
  * =============================================================================
+ * Modifications Copyright (C) 2018 IBM
+ * ================================================================================
  * 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
 
 package org.onap.appc.artifact.handler.dbservices;
 
-import java.nio.charset.Charset;
-import org.json.JSONObject;
 import org.junit.Ignore;
 import org.junit.Test;
 import org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants;
 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
-import org.powermock.reflect.Whitebox;
 import static org.junit.Assert.assertEquals;
 
 public class DBServiceTest {
@@ -194,13 +193,27 @@ public class DBServiceTest {
         dbService.processVnfcReference(ctx, isUpdate);
     }
 
-    //@Test
+    @Test
     public void testProcessDeviceAuthentication() throws Exception {
         MockDBService dbService = MockDBService.initialise();
         SvcLogicContext ctx = new SvcLogicContext();
         ctx.setAttribute("test", "test");
+        ctx.setAttribute("url", "");
+        String expectedKey ="update DEVICE_AUTHENTICATION set USER_NAME = '' , PORT_NUMBER = 0, URL = ''  where VNF_TYPE = $vnf-type  AND PROTOCOL = $device-protocol AND  ACTION = $action";
         boolean isUpdate = true;
         dbService.processDeviceAuthentication(ctx, isUpdate);
+        assertEquals(expectedKey,ctx.getAttribute("keys"));
+    }
+
+    @Test
+    public void testProcessDeviceAuthenticationforFalse() throws Exception {
+        MockDBService dbService = MockDBService.initialise();
+        SvcLogicContext ctx = new SvcLogicContext();
+        ctx.setAttribute("test", "test");
+        ctx.setAttribute("url", "");
+        boolean isUpdate = false;
+        dbService.processDeviceAuthentication(ctx, isUpdate);
+        assertEquals(true,ctx.getAttribute("keys").contains("DEVICE_AUTHENTICATION"));
     }
 
     //@Test
@@ -221,6 +234,16 @@ public class DBServiceTest {
         dbService.processDeviceInterfaceProtocol(ctx, isUpdate);
     }
 
+    @Test
+    public void testProcessDeviceInterfaceProtocolForFalse() throws Exception {
+        MockDBService dbService = MockDBService.initialise();
+        SvcLogicContext ctx = new SvcLogicContext();
+        ctx.setAttribute("test", "test");
+        boolean isUpdate = false;
+        dbService.processDeviceInterfaceProtocol(ctx, isUpdate);
+        assertEquals(true,ctx.getAttribute("keys").contains("DEVICE_INTERFACE_PROTOCOL"));
+    }
+
     @Test
     public void testProcessDeviceInterfaceProtocolException() throws Exception {
         MockDBService dbService = MockDBService.initialise();
index df5b329..6513e6a 100644 (file)
@@ -6,6 +6,8 @@
  * ================================================================================
  * Copyright (C) 2017 Amdocs
  * =============================================================================
+ * Modifications Copyright (C) 2018 IBM
+ * ================================================================================
  * 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
@@ -25,6 +27,8 @@ package org.onap.appc.artifact.handler.dbservices;
 
 import java.util.Map;
 
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
 import org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource;
@@ -35,6 +39,7 @@ public class MockSvcLogicResource extends SqlResource {
     public QueryStatus query(String resource, boolean localOnly, String select, String key, String prefix,
             String orderBy, SvcLogicContext ctx) throws SvcLogicException {
         QueryStatus status = QueryStatus.SUCCESS;
+        ctx.setAttribute("keys",key);
         ctx.setAttribute("id", "testId");
         ctx.setAttribute("VNF_TYPE", "testvnf");
         ctx.setAttribute("maximum", "1");
@@ -47,8 +52,7 @@ public class MockSvcLogicResource extends SqlResource {
     @Override
     public QueryStatus save(String resource, boolean force, boolean localOnly, String key, Map<String, String> parms,
             String prefix, SvcLogicContext ctx) throws SvcLogicException {
+        ctx.setAttribute("keys", key);
         return QueryStatus.SUCCESS;
     }
-
-
 }
diff --git a/appc-inbound/appc-artifact-handler/provider/src/test/java/org/onap/appc/artifact/handler/dbservices/TestDBServiceExceptions.java b/appc-inbound/appc-artifact-handler/provider/src/test/java/org/onap/appc/artifact/handler/dbservices/TestDBServiceExceptions.java
new file mode 100644 (file)
index 0000000..3c42985
--- /dev/null
@@ -0,0 +1,153 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP : APPC
+ * ================================================================================
+ * Copyright (C) 2018 IBM
+ * =============================================================================
+ * 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.artifact.handler.dbservices;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants;
+import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
+import org.mockito.Mockito;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.powermock.reflect.Whitebox;
+
+@RunWith(MockitoJUnitRunner.class)
+public class TestDBServiceExceptions {
+
+    private MockDBService dbService;
+
+    private MockSvcLogicResource mockSVCLogicResource;
+
+    private SvcLogicContext ctx ;
+
+    @Before
+    public void setup(){
+           dbService =  MockDBService.initialise();
+           mockSVCLogicResource = Mockito.spy(MockSvcLogicResource.class);
+           ctx = new SvcLogicContext();
+        }
+
+    @Test
+    public void testSaveArtifacts() throws Exception {              
+        String artifactName = "TestArtifact";
+        String prefix ="";
+        ctx.setAttribute("test", "test");
+        String result= dbService.getInternalVersionNumber(ctx, artifactName, prefix);
+        assertEquals("1",result);
+       
+    }
+
+    @Test
+    public void testProcessSdcReferencesForCapability() throws Exception {        
+        ctx.setAttribute("test", "test");
+        ctx.setAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY, "capability");
+        boolean isUpdate = false;
+        String expectedKey = "insert into ASDC_REFERENCE set VNFC_TYPE = null  , FILE_CATEGORY = $file-category , " +
+                "VNF_TYPE = $vnf-type , ACTION = null  , ARTIFACT_TYPE = null  , ARTIFACT_NAME = $artifact-name";
+        dbService.processSdcReferences(ctx, isUpdate);
+        assertEquals(expectedKey,ctx.getAttribute("keys"));
+        }
+
+    @Test
+    public void testProcessSdcReferences() throws Exception {        
+        ctx.setAttribute("test", "test");
+        ctx.setAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY, "Test");
+        String expectedKey = "insert into ASDC_REFERENCE set VNFC_TYPE = $vnfc-type , FILE_CATEGORY = $file-category , VNF_TYPE = $vnf-type , ACTION = $action , ARTIFACT_TYPE = $artifact-type , ARTIFACT_NAME = $artifact-name";
+        boolean isUpdate = false;
+        dbService.processSdcReferences(ctx, isUpdate);
+        assertEquals(expectedKey,ctx.getAttribute("keys"));
+    }
+
+    @Test(expected = Exception.class)
+    public void testGetDownLoadDGReference() throws Exception {       
+        ctx.setAttribute("test", "test");
+        ctx.setAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL, "");
+        dbService.getDownLoadDGReference(ctx);
+    }
+
+    @Test
+    public void testProcessConfigActionDg() throws Exception {      
+        ctx.setAttribute("test", "test");
+        boolean isUpdate = false;
+        ctx.setAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE, "Reference");
+        String expectedKey = "insert into CONFIGURE_ACTION_DG set DOWNLOAD_CONFIG_DG = $download-dg-reference , ACTION = $action , VNF_TYPE = $vnf-type";
+        dbService.processConfigActionDg(ctx, isUpdate);
+        assertEquals(expectedKey,ctx.getAttribute("keys"));
+    }
+
+    @Test
+    public void testProcessConfigActionDgForElse() throws Exception {       
+        ctx.setAttribute("test", "test");
+        boolean isUpdate = false;
+        String expectedKey = null;
+        dbService.processConfigActionDg(ctx, isUpdate);
+        assertEquals(expectedKey,ctx.getAttribute("keys"));
+    }
+
+    @Test
+    public void testprocessDpwnloadDGReference() throws Exception {        
+        ctx.setAttribute("test", "test");
+        boolean isUpdate = false;
+        String expectedKey = "insert into DOWNLOAD_DG_REFERENCE set DOWNLOAD_CONFIG_DG = $download-dg-reference , PROTOCOL = $device-protocol";
+        dbService.processDownloadDgReference(ctx, isUpdate);
+        assertEquals(expectedKey,ctx.getAttribute("keys"));
+    }
+
+    @Test
+    public void testResolveWhereClause() throws Exception {       
+        ctx.setAttribute("test", "test");
+        String db="DOWNLOAD_DG_REFERENCE";
+        String whereClause="";
+        String result =  Whitebox.invokeMethod(dbService, "resolveWhereClause", ctx, db, whereClause);
+        assertEquals(true, result.contains("PROTOCOL"));
+    }
+
+    @Test
+    public void testResolveWhereClauseForDevice_Authentication() throws Exception {       
+        ctx.setAttribute("test", "test");
+        String db="DEVICE_AUTHENTICATION";
+        String whereClause="Test";
+        String result =  Whitebox.invokeMethod(dbService, "resolveWhereClause", ctx, db, whereClause);
+        assertEquals(true, result.contains("Test"));
+    }
+
+    @Test
+    public void testResolveWhereClauseCONFIGURE_ACTION_DG() throws Exception {        
+        ctx.setAttribute("test", "test");
+        String db="CONFIGURE_ACTION_DG";
+        String whereClause="Test";
+        String result =  Whitebox.invokeMethod(dbService, "resolveWhereClause", ctx, db, whereClause);
+        assertEquals(true, result.contains("Test"));
+    }
+
+    @Test
+    public void testResolveWhereClauseVNFC_REFERENCE() throws Exception {      
+        ctx.setAttribute("test", "test");
+        String db="VNFC_REFERENCE";
+        String whereClause="TestVNFC_REFERENCE";
+        String result =  Whitebox.invokeMethod(dbService, "resolveWhereClause", ctx, db, whereClause);
+        assertEquals(true, result.contains("TestVNFC_REFERENCE"));
+    }
+
+}