2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
 
   6  * ================================================================================
 
   7  * Copyright (C) 2017 Amdocs
 
   8  * ================================================================================
 
   9  * Modifications Copyright (C) 2019 Ericsson
 
  10  * =============================================================================
 
  11  * Licensed under the Apache License, Version 2.0 (the "License");
 
  12  * you may not use this file except in compliance with the License.
 
  13  * You may obtain a copy of the License at
 
  15  *      http://www.apache.org/licenses/LICENSE-2.0
 
  17  * Unless required by applicable law or agreed to in writing, software
 
  18  * distributed under the License is distributed on an "AS IS" BASIS,
 
  19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  20  * See the License for the specific language governing permissions and
 
  21  * limitations under the License.
 
  23  * ============LICENSE_END=========================================================
 
  26 package org.onap.appc.artifact.handler.node;
 
  28 import org.apache.commons.io.IOUtils;
 
  29 import org.json.JSONArray;
 
  30 import org.json.JSONObject;
 
  31 import org.junit.Before;
 
  32 import org.junit.Ignore;
 
  33 import org.junit.Rule;
 
  34 import org.junit.Test;
 
  35 import org.junit.rules.ExpectedException;
 
  36 import org.junit.runner.RunWith;
 
  37 import org.mockito.Mockito;
 
  38 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 
  39 import org.onap.sdnc.config.params.transformer.tosca.ArtifactProcessorImpl;
 
  40 import org.onap.sdnc.config.params.transformer.tosca.exceptions.ArtifactProcessorException;
 
  41 import org.powermock.api.mockito.PowerMockito;
 
  42 import org.powermock.core.classloader.annotations.PrepareForTest;
 
  43 import org.powermock.modules.junit4.PowerMockRunner;
 
  44 import org.powermock.reflect.Whitebox;
 
  45 import org.onap.appc.artifact.handler.dbservices.DBService;
 
  46 import org.onap.appc.artifact.handler.dbservices.MockDBService;
 
  47 import org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants;
 
  48 import org.onap.appc.yang.YANGGenerator;
 
  49 import org.onap.appc.yang.impl.YANGGeneratorFactory;
 
  50 import org.onap.appc.artifact.handler.utils.ArtifactHandlerProviderUtilTest;
 
  52 import java.util.HashMap;
 
  53 import java.io.IOException;
 
  54 import java.nio.charset.Charset;
 
  56 import static org.junit.Assert.assertEquals;
 
  57 import static org.junit.Assert.assertFalse;
 
  58 import static org.junit.Assert.assertNotNull;
 
  59 import static org.junit.Assert.assertNull;
 
  60 import static org.junit.Assert.assertTrue;
 
  61 import static org.junit.Assert.fail;
 
  64 @RunWith(PowerMockRunner.class)
 
  65 @PrepareForTest({DBService.class, YANGGeneratorFactory.class})
 
  66 public class ArtifactHandlerNodeTest {
 
  68     private ArtifactHandlerNode artifactHandlerNode;
 
  69     private DBService dbServiceMock;
 
  72     public ExpectedException expectedEx = ExpectedException.none();
 
  75     public void setUp() throws Exception {
 
  76         artifactHandlerNode = Mockito.spy(new ArtifactHandlerNode());
 
  77         PowerMockito.mockStatic(DBService.class);
 
  78         dbServiceMock = Mockito.mock(DBService.class);
 
  79         Mockito.doReturn("12345")
 
  80                 .when(dbServiceMock).getInternalVersionNumber(Mockito.any(), Mockito.anyString(), Mockito.anyString());
 
  81         PowerMockito.when(DBService.initialise()).thenReturn(dbServiceMock);
 
  82         PowerMockito.mockStatic(YANGGeneratorFactory.class);
 
  83         YANGGenerator yangGeneratorMock = Mockito.mock(YANGGenerator.class);
 
  84         PowerMockito.when(YANGGeneratorFactory.getYANGGenerator()).thenReturn(yangGeneratorMock);
 
  85         ArtifactProcessorImpl artifactProcessorMock = Mockito.mock(ArtifactProcessorImpl.class);
 
  86         Mockito.doReturn(artifactProcessorMock).when(artifactHandlerNode).getArtifactProcessorImpl();
 
  90     public void testProcessArtifact() throws Exception {
 
  91         SvcLogicContext ctx = new SvcLogicContext();
 
  92         ctx.setAttribute("test", "test");
 
  93         Map<String, String> inParams = new HashMap<>();
 
  94         JSONObject postData = new JSONObject();
 
  95         JSONObject input = new JSONObject();
 
  96         inParams.put("response_prefix", "prefix");
 
  97         JSONObject requestInfo = new JSONObject();
 
  98         JSONObject documentInfo = getDocumentInfo("templates/reference_template");
 
  99         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_NAME, "reference_Junit.json");
 
 100         requestInfo.put("RequestInfo", "testValue");
 
 101         requestInfo.put("request-id", "testREQUEST_ID");
 
 102         input.put(SdcArtifactHandlerConstants.DOCUMENT_PARAMETERS, documentInfo);
 
 103         input.put(SdcArtifactHandlerConstants.REQUEST_INFORMATION, requestInfo);
 
 104         postData.put("input", input);
 
 105         inParams.put("postData", postData.toString());
 
 106         artifactHandlerNode.processArtifact(inParams, ctx);
 
 107         assertNull(ctx.getAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY));
 
 111     public void testPopulateProtocolReference() throws Exception {
 
 112         ArtifactHandlerNode ah = new ArtifactHandlerNode();
 
 114                 "{\"action\": \"TestAction\",\"action-level\": \"vnf\",\"scope\": {\"vnf-type\": \"vDBE-I\",\"vnfc-type\": null},\"template\": \"N\",\"device-protocol\": \"REST\"}";
 
 115         JSONObject content = new JSONObject(contentStr);
 
 116         MockDBService dbService = MockDBService.initialise();
 
 117         Whitebox.invokeMethod(ah, "populateProtocolReference", dbService, content);
 
 121     public void testCleanVnfcInstance() throws Exception {
 
 122         ArtifactHandlerNode ah = new ArtifactHandlerNode();
 
 123         SvcLogicContext ctx = new SvcLogicContext();
 
 124         Whitebox.invokeMethod(ah, "cleanVnfcInstance", ctx);
 
 129     public void testStoreUpdateSdcArtifacts() throws Exception {
 
 130         ArtifactHandlerNode ah = new ArtifactHandlerNode();
 
 132                 "{\"request-information\":{\"request-id\": \"12345\"},\"document-parameters\":{\"artifact-name\":\"testArtifact\",\"artifact-contents\":{\"content\":\"TestContent\"}}}";
 
 133         JSONObject postData = new JSONObject(postDataStr);
 
 134         expectedEx.expect(ArtifactHandlerInternalException.class);
 
 135         Whitebox.invokeMethod(ah, "storeUpdateSdcArtifacts", postData);
 
 139     public void testUpdateYangContents() throws Exception {
 
 140         String artifactId = "1";
 
 141         String yangContents = "SomeContent";
 
 142         Whitebox.invokeMethod(artifactHandlerNode, "updateYangContents", artifactId, yangContents);
 
 143         Mockito.verify(dbServiceMock)
 
 144                 .updateYangContents(Mockito.any(SvcLogicContext.class), Mockito.anyString(), Mockito.anyString());
 
 148     public void testProcessVmList() throws Exception{
 
 149         String contentStr = "{\r\n\t\"action\": \"ConfigScaleOut\",\r\n\t\"action-level\": \"VNF\",\r\n\t\"scope\": "
 
 150                 + "{\r\n\t\t\"vnf-type\": \"ScaleOutVNF\",\r\n\t\t\"vnfc-type\": \"\"\r\n\t},\r\n"
 
 151                 + "\t\"template\": \"Y\",\r\n\t\"vm\": "
 
 152                 + "[\r\n\t{ \r\n\t\t\"vm-instance\": 1,\r\n\t\t\"template-id\":\"id1\",\r\n\t\t\r\n\t\t\"vnfc\": "
 
 153                 + "[{\r\n\t\t\t\"vnfc-instance\": 1,\r\n\t\t\t\"vnfc-type\": \"t1\",\r\n\t\t\t\"vnfc-function-code\": "
 
 154                 + "\"Testdbg\",\r\n\t\t\t\"group-notation-type\": \"GNType\",\r\n"
 
 155                 + "\t\t\t\"ipaddress-v4-oam-vip\": \"N\",\r\n"
 
 156                 + "\t\t\t\"group-notation-value\": \"GNValue\"\r\n\t\t}]\r\n\t},\r\n"
 
 157                 + "\t{ \r\n\t\t\"vm-instance\": 1,\r\n\t\t\"template-id\":\"id2\",\r\n"
 
 158                 + "\t\t\r\n\t\t\"vnfc\": [{\r\n\t\t\t\"vnfc-instance\": 1,\r\n\t\t\t\"vnfc-type\": \"t1\",\r\n"
 
 159                 + "\t\t\t\"vnfc-function-code\": \"Testdbg\",\r\n\t\t\t\"group-notation-type\": \"GNType\",\r\n"
 
 160                 + "\t\t\t\"ipaddress-v4-oam-vip\": \"N\",\r\n\t\t\t\"group-notation-value\": \"GNValue\"\r\n\t\t},\r\n"
 
 161                 + "\t\t{\r\n\t\t\t\"vnfc-instance\": 2,\r\n\t\t\t\"vnfc-type\": \"t2\",\r\n"
 
 162                 + "\t\t\t\"vnfc-function-code\": \"Testdbg\",\r\n\t\t\t\"group-notation-type\": \"GNType\",\r\n"
 
 163                 + "\t\t\t\"ipaddress-v4-oam-vip\": \"Y\",\r\n\t\t\t\"group-notation-value\": \"GNValue\"\r\n\t\t}]\r\n"
 
 164                 + "\t},\r\n\t{\r\n\t\t\"vm-instance\": 2,\r\n\t\t\"template-id\":\"id3\",\r\n"
 
 165                 + "\t\t\"vnfc\": [{\r\n\t\t\t\"vnfc-instance\": 1,\r\n\t\t\t\"vnfc-type\": \"t3\",\r\n"
 
 166                 + "\t\t\t\"vnfc-function-code\": \"Testdbg\",\r\n\t\t\t\"group-notation-type\": \"GNType\",\r\n"
 
 167                 + "\t\t\t\"ipaddress-v4-oam-vip\": \"Y\",\r\n\t\t\t\"group-notation-value\": \"GNValue\"\r\n\t\t}]\r\n"
 
 168                 + "\t}],\r\n\t\"device-protocol\": \"TEST-PROTOCOL\",\r\n\t\"user-name\": \"Testnetconf\",\r\n"
 
 169                 + "\t\"port-number\": \"22\",\r\n\t\"artifact-list\": [{\r\n"
 
 170                 + "\t\t\"artifact-name\": \"Testv_template.json\",\r\n"
 
 171                 + "\t\t\"artifact-type\": \"Testconfig_template\"\r\n\t},\r\n"
 
 172                 + "\t{\r\n\t\t\"artifact-name\": \"TESTv_parameter_definitions.json\",\r\n"
 
 173                 + "\t\t\"artifact-type\": \"Testparameter_definitions\"\r\n\t},\r\n\t{\r\n"
 
 174                 + "\t\t\"artifact-name\": \"PD_JunitTESTv_parameter_yang.json\",\r\n"
 
 175                 + "\t\t\"artifact-type\": \"PD_definations\"\r\n\t}]\r\n}";
 
 176         JSONObject content=new JSONObject(contentStr);
 
 177         MockDBService dbService = MockDBService.initialise();
 
 178         SvcLogicContext context = new SvcLogicContext();
 
 179         artifactHandlerNode.processVmList(content, context, dbService);
 
 183     public void testProcessConfigTypeActions() throws Exception {
 
 184         String contentStr = "{\"action\": \"ConfigScaleOut\"}";
 
 185         JSONObject content = new JSONObject(contentStr);
 
 186         MockDBService dbService = MockDBService.initialise();
 
 187         SvcLogicContext context = new SvcLogicContext();
 
 188         context.setAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL, "Test");
 
 189         artifactHandlerNode.processConfigTypeActions(content, dbService, context);
 
 193     public void testProcessActionLists() throws Exception {
 
 194         String contentStr = "{\r\n\t\"action\": \"HealthCheck\",\r\n\t\"action-level\": \"vm\",\r\n\t\"scope\":"
 
 195                 + " {\r\n\t\t\"vnf-type\": \"vDBE-I\",\r\n\t\t\"vnfc-type\": null\r\n\t},\r\n\t\"template\": \"N\",\r\n"
 
 196                 + "\t\"device-protocol\": \"REST\",\r\n\t\"vnfc-function-code-list\": [\"SSC\", \"MMSC\"]\r\n}";
 
 197         JSONObject content = new JSONObject(contentStr);
 
 198         JSONArray vmActionVnfcFunctionCodesList = new JSONArray();
 
 199         JSONArray vnfActionList = new JSONArray();
 
 200         JSONArray vfModuleActionList = new JSONArray();
 
 201         JSONArray vnfcActionList = new JSONArray();
 
 202         String[] actionLevels = { "vnf", "vm", "vf-module", "vnfc" };
 
 203         for (String actionLevel : actionLevels) {
 
 204             artifactHandlerNode.processActionLists(content, actionLevel, vnfcActionList, vfModuleActionList,
 
 205                     vnfActionList, vmActionVnfcFunctionCodesList);
 
 210     public void testIsCapabilityArtifactNeeded() throws Exception {
 
 211         String scopeObjStr1 = "{\"vnf-type\":\"someVnf\",\"vnfc-type\":\"somVnfc\"}";
 
 212         String scopeObjStr2 = "{\"vnf-type\":\"someVnf\",\"vnfc-type\":\"\"}";
 
 213         JSONObject scope1 = new JSONObject(scopeObjStr1);
 
 214         JSONObject scope2 = new JSONObject(scopeObjStr2);
 
 215         SvcLogicContext context = new SvcLogicContext();
 
 216         artifactHandlerNode.setVnfcTypeInformation(scope1, context);
 
 217         assertFalse(artifactHandlerNode.isCapabilityArtifactNeeded(context));
 
 218         artifactHandlerNode.setVnfcTypeInformation(scope2, context);
 
 219         assertTrue(artifactHandlerNode.isCapabilityArtifactNeeded(context));
 
 223     public void testProcessArtifactListsWithMultipleTemplates() throws Exception {
 
 224         String contentStr = "{\r\n\t\t\"action\": \"ConfigScaleOut\",\r\n\t\t\"action-level\": \"vnf\",\r\n"
 
 225                 + "\t\t\"scope\": {\r\n\t\t\t\"vnf-type\": \"vCfgSO-0405\",\r\n\t\t\t\"vnfc-type\": \"\"\r\n\t\t},\r\n"
 
 226                 + "\t\t\"template\": \"Y\",\r\n\t\t\"vm\": [{\r\n\t\t\t\"template-id\": \"TID-0405-EZ\",\r\n"
 
 227                 + "\t\t\t\"vm-instance\": 1,\r\n\t\t\t\"vnfc\": [{\r\n\t\t\t\t\"vnfc-instance\": \"1\",\r\n"
 
 228                 + "\t\t\t\t\"vnfc-function-code\": \"Cfg-ez\",\r\n\t\t\t\t\"ipaddress-v4-oam-vip\": \"Y\",\r\n"
 
 229                 + "\t\t\t\t\"group-notation-type\": \"first-vnfc-name\",\r\n"
 
 230                 + "\t\t\t\t\"group-notation-value\": \"pair\",\r\n\t\t\t\t\"vnfc-type\": \"vCfg-0405-ez\"\r\n"
 
 231                 + "\t\t\t}]\r\n\t\t},\r\n\t\t{\r\n\t\t\t\"template-id\": \"TID-0405-EZ\",\r\n"
 
 232                 + "\t\t\t\"vm-instance\": 2,\r\n\t\t\t\"vnfc\": [{\r\n\t\t\t\t\"vnfc-instance\": \"1\",\r\n"
 
 233                 + "\t\t\t\t\"vnfc-function-code\": \"Cfg-ez\",\r\n\t\t\t\t\"ipaddress-v4-oam-vip\": \"Y\",\r\n"
 
 234                 + "\t\t\t\t\"group-notation-type\": \"first-vnfc-name\",\r\n"
 
 235                 + "\t\t\t\t\"group-notation-value\": \"pair\",\r\n\t\t\t\t\"vnfc-type\": \"vCfg-0405-ez\"\r\n"
 
 236                 + "\t\t\t}]\r\n\t\t}],\r\n\t\t\"device-protocol\": \"ANSIBLE\",\r\n\t\t\"user-name\": \"root\",\r\n"
 
 237                 + "\t\t\"port-number\": \"22\",\r\n\t\t\"artifact-list\": [{\r\n"
 
 238                 + "\t\t\t\"artifact-name\": \"template_ConfigScaleOut_vCfgSO-0405_0.0.1V_TID-0405-EZ.json\",\r\n"
 
 239                 + "\t\t\t\"artifact-type\": \"config_template\"\r\n\t\t},\r\n\t\t{\r\n"
 
 240                 + "\t\t\t\"artifact-name\": \"pd_ConfigScaleOut_vCfgSO-0405_0.0.1V_TID-0405-EZ.yaml\",\r\n"
 
 241                 + "\t\t\t\"artifact-type\": \"parameter_definitions\"\r\n\t\t},\r\n\t\t{\r\n"
 
 242                 + "\t\t\t\"artifact-name\": \"template_ConfigScaleOut_vCfgSO-0405_0.0.1V_TID-0405-EZ-2.json\",\r\n"
 
 243                 + "\t\t\t\"artifact-type\": \"config_template\"\r\n\t\t},\r\n\t\t{\r\n"
 
 244                 + "\t\t\t\"artifact-name\": \"pd_ConfigScaleOut_vCfgSO-0405_0.0.1V_TID-0405-EZ-2.yaml\",\r\n"
 
 245                 + "\t\t\t\"artifact-type\": \"parameter_definitions\"\r\n\t\t}],\r\n"
 
 246                 + "\t\t\"template-id-list\": [\"TID-0405-EZ\",\r\n\t\t\"TID-0405-EZ-2\"],\r\n"
 
 247                 + "\t\t\"scopeType\": \"vnf-type\"\r\n\t}";
 
 248         JSONObject content = new JSONObject(contentStr);
 
 249         MockDBService dbService = MockDBService.initialise();
 
 250         SvcLogicContext context = new SvcLogicContext();
 
 251         context.setAttribute("vnf-type", "someVnf");
 
 252         context.setAttribute("action", "ConfigScaleOut");
 
 253         artifactHandlerNode.processArtifactList(content, dbService, context, null);
 
 257     public void testProcessArtifactListsWithVnfcTypeList() throws Exception {
 
 258         String contentStr = "{\r\n\t\"action\": \"Configure\",\r\n\t\"action-level\": \"vnf\",\r\n\t\"scope\": {\r\n"
 
 259                 + "\t\t\"vnf-type\": \"newtypeofvnf\",\r\n\t\t\"vnfc-type-list\": [\"vnfctype1\",\"vnfctype2\"]\r\n"
 
 260                 + "\t},\r\n\t\"template\": \"Y\",\r\n\t\"vm\": [{\r\n\t\t\t\"vm-instance\": 1,\r\n"
 
 261                 + "\t\t\t\"template-id\": \"vnfctype1\",\r\n\t\t\t\"vnfc\": [{\r\n"
 
 262                 + "\t\t\t\t\"vnfc-instance\": \"1\",\r\n\t\t\t\t\"vnfc-function-code\": \"fcx\",\r\n"
 
 263                 + "\t\t\t\t\"ipaddress-v4-oam-vip\": \"Y\",\r\n"
 
 264                 + "\t\t\t\t\"group-notation-type\": \"first-vnfc-name\",\r\n"
 
 265                 + "\t\t\t\t\"group-notation-value\": \"pair\",\r\n\t\t\t\t\"vnfc-type\": \"vDBE\"\r\n\t\t\t}]\r\n"
 
 266                 + "\t\t},\r\n\t\t{\r\n\t\t\t\"vm-instance\": 1,\r\n\t\t\t\"template-id\": \"vnfctype2\",\r\n"
 
 267                 + "\t\t\t\"vnfc\": [{\r\n\t\t\t\t\"vnfc-instance\": \"1\",\r\n"
 
 268                 + "\t\t\t\t\"vnfc-function-code\": \"fcx\",\r\n\t\t\t\t\"ipaddress-v4-oam-vip\": \"Y\",\r\n"
 
 269                 + "\t\t\t\t\"group-notation-type\": \"first-vnfc-name\",\r\n"
 
 270                 + "\t\t\t\t\"group-notation-value\": \"pair\",\r\n\t\t\t\t\"vnfc-type\": \"vDBE\"\r\n\t\t\t}]\r\n"
 
 271                 + "\t\t},\r\n\t\t{\r\n\t\t\t\"vm-instance\": 2,\r\n\t\t\t\"template-id\": \"vnfctype2\",\r\n"
 
 272                 + "\t\t\t\"vnfc\": [{\r\n\t\t\t\t\"vnfc-instance\": \"1\",\r\n"
 
 273                 + "\t\t\t\t\"vnfc-function-code\": \"fcx\",\r\n\t\t\t\t\"ipaddress-v4-oam-vip\": \"Y\",\r\n"
 
 274                 + "\t\t\t\t\"group-notation-type\": \"first-vnfc-name\",\r\n"
 
 275                 + "\t\t\t\t\"group-notation-value\": \"pair\",\r\n\t\t\t\t\"vnfc-type\": \"vDBE\"\r\n\t\t\t}]\r\n"
 
 276                 + "\t\t}\r\n\t],\r\n\t\"device-protocol\": \"NETCONF-XML\",\r\n\t\"user-name\": \"netconf\",\r\n"
 
 277                 + "\t\"port-number\": \"20\",\r\n\t\"artifact-list\": [{\r\n"
 
 278                 + "\t\t\t\"artifact-name\": \"template_ConfigScaleOut_newtypeofvnf_0.0.1V_vnfctype1.xml\",\r\n"
 
 279                 + "\t\t\t\"artifact-type\": \"config_template\"\r\n\t\t},\r\n\t\t{\r\n"
 
 280                 + "\t\t\t\"artifact-name\": \"pd_ConfigScaleOut_newtypeofvnf_0.0.1V_vnfctype1.yaml\",\r\n"
 
 281                 + "\t\t\t\"artifact-type\": \"parameter_definitions\"\r\n\t\t},\r\n"
 
 282                 + "\t\t{\r\n\t\t\t\"artifact-name\": \"template_ConfigScaleOut_newtypeofvnf_0.0.1V_vnfctype2.xml\",\r\n"
 
 283                 + "\t\t\t\"artifact-type\": \"config_template\"\r\n\t\t},\r\n"
 
 284                 + "\t\t{\r\n\t\t\t\"artifact-name\": \"pd_ConfigScaleOut_newtypeofvnf_0.0.1V_vnfctype2.yaml\",\r\n"
 
 285                 + "\t\t\t\"artifact-type\": \"parameter_definitions\"\r\n\t\t}\r\n\t],\r\n"
 
 286                 + "\t\"scopeType\": \"vnf-type\"\r\n}";
 
 287         JSONObject content = new JSONObject(contentStr);
 
 288         MockDBService dbService = MockDBService.initialise();
 
 289         SvcLogicContext context = new SvcLogicContext();
 
 290         context.setAttribute("vnf-type", "someVnf");
 
 291         context.setAttribute("action", "Configure");
 
 292         JSONObject scope = (JSONObject)content.get("scope");
 
 293         JSONArray vnfcTypeList = artifactHandlerNode.setVnfcTypeInformation(scope, context);
 
 294         artifactHandlerNode.processArtifactList(content, dbService, context, vnfcTypeList);
 
 295         JSONArray vnfcLists = scope.getJSONArray("vnfc-type-list");
 
 296         assertEquals(vnfcLists.toString(), "[\"vnfctype1\",\"vnfctype2\"]");
 
 297         assertEquals(context.getAttribute("vnfc-type"), "vnfctype2");
 
 298         assertNotNull (vnfcTypeList);
 
 302     public void testProcessArtifactPdArtifactName() throws IOException, ArtifactProcessorException {
 
 303         SvcLogicContext ctx = new SvcLogicContext();
 
 304         ctx.setAttribute("test", "test");
 
 305         Map<String, String> inParams = new HashMap<>();
 
 306         JSONObject postData = new JSONObject();
 
 307         JSONObject input = new JSONObject();
 
 308         inParams.put("response_prefix", "prefix");
 
 309         JSONObject requestInfo = new JSONObject();
 
 310         JSONObject documentInfo = getDocumentInfo("templates/pd_template");
 
 311         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_NAME, "pd_Junit.json");
 
 312         requestInfo.put("RequestInfo", "testValue");
 
 313         requestInfo.put("request-id", "testREQUEST_ID");
 
 314         input.put(SdcArtifactHandlerConstants.DOCUMENT_PARAMETERS, documentInfo);
 
 315         input.put(SdcArtifactHandlerConstants.REQUEST_INFORMATION, requestInfo);
 
 316         postData.put("input", input);
 
 317         inParams.put("postData", postData.toString());
 
 318         artifactHandlerNode.processArtifact(inParams, ctx);
 
 319         Mockito.verify(dbServiceMock, Mockito.times(2)).initialise();
 
 322     private JSONObject getDocumentInfo(String filename) throws IOException {
 
 323         JSONObject documentInfo = new JSONObject();
 
 324         String artifactContent = IOUtils.toString(
 
 325                 ArtifactHandlerProviderUtilTest.class.getClassLoader().getResourceAsStream(filename),
 
 326                 Charset.defaultCharset());
 
 327         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_CONTENTS, artifactContent);
 
 328         documentInfo.put(SdcArtifactHandlerConstants.SERVICE_UUID, "12345");
 
 329         documentInfo.put(SdcArtifactHandlerConstants.DISTRIBUTION_ID, "12345");
 
 330         documentInfo.put(SdcArtifactHandlerConstants.SERVICE_NAME, "12345");
 
 331         documentInfo.put(SdcArtifactHandlerConstants.SERVICE_DESCRIPTION, "12345");
 
 332         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_UUID, "12345");
 
 333         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_INSTANCE_NAME, "12345");
 
 334         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_NAME, "12345");
 
 335         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_VERSION, "12345");
 
 336         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_TYPE, "12345");
 
 337         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_UUID, "12345");
 
 338         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_TYPE, "12345");
 
 339         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_VERSION, "12345");
 
 340         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_DESRIPTION, "12345");
 
 346     public void testValidateAnsibleAdminArtifact() throws Exception {
 
 347         String contentStr = "{\"fqdn-list\":[{\"vnf-management-server-fqdn\":\"fqdn-value1 url:port\","
 
 348                 + "\"cloud-owner-list\":[{\"cloud-owner\":\"aic3.0\",\"region-id-list\":[{\"region-id\":\"san4a\","
 
 349                 + "\"tenant-id-list\":[\"tenantuuid1\",\"tenantuuid2\"]},{\"region-id\":\"san4b\","
 
 350                 + "\"tenant-id-list\":[\"tenantuuid1\",\"tenantuuid2\"]}]},{\"cloud-owner\":\"nc1.0\","
 
 351                 + "\"region-id-list\":[{\"region-id\":\"san4a\",\"tenant-id-list\":[\"tenantuuid3\","
 
 352                 + "\"tenantuuid4\"]}]}],\"description\":\"fqdn for east zone Production\",\"username\":\"attuid\","
 
 353                 + "\"create-date\":\"\",\"modify-username\":\"\",\"modify-date\":\"\"},"
 
 354                 + "{\"vnf-management-server-fqdn\":\"fqdn-value2 url:port\","
 
 355                 + "\"cloud-owner-list\":[{\"cloud-owner\":\"aic3.0\",\"region-id-list\":[{\"region-id\":\"san4a\","
 
 356                 + "\"tenant-id-list\":[\"tenantuuid5\",\"tenantuuid6\"]},{\"region-id\":\"san4b\","
 
 357                 + "\"tenant-id-list\":[\"tenantuuid5\",\"tenantuuid6\"]}]},{\"cloud-owner\":\"nc1.0\","
 
 358                 + "\"region-id-list\":[{\"region-id\":\"san4a\","
 
 359                 + "\"tenant-id-list\":[\"tenantuuid7\",\"tenantuuid8\"]}]}],"
 
 360                 + "\"description\":\"fqdn for east zone Test\",\"username\":\"attuid\",\"create-date\":\"\","
 
 361                 + "\"modify-username\":\"\",\"modify-date\":\"\"}]}";
 
 363         JSONObject documentInfo = new JSONObject();
 
 364         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_CONTENTS, contentStr);
 
 365         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_NAME, "ansible_admin_FQDN_Artifact_0.0.1V.json");
 
 366         artifactHandlerNode.validateAnsibleAdminArtifact(documentInfo);
 
 370     public void testValidateAnsibleAdminArtifactWithException() throws Exception {
 
 371         String contentStrOne = "{\"fqdn-list\":[{\"vnf-management-server-fqdn\":\"fqdn-value1 url:port\","
 
 372                 + "\"cloud-owner-list\":[{\"cloud-owner\":\"aic3.0\",\"region-id-list\":[{\"region-id\":\"san4a\","
 
 373                 + "\"tenant-id-list\":[\"tenantuuid1\",\"tenantuuid2\"]},{\"region-id\":\"san4b\","
 
 374                 + "\"tenant-id-list\":[\"tenantuuid1\",\"tenantuuid2\"]}]},{\"cloud-owner\":\"nc1.0\","
 
 375                 + "\"region-id-list\":[{\"region-id\":\"san4a\","
 
 376                 + "\"tenant-id-list\":[\"tenantuuid3\",\"tenantuuid4\"]}]}],"
 
 377                 + "\"description\":\"fqdn for east zone Production\",\"username\":\"attuid\",\"create-date\":\"\","
 
 378                 + "\"modify-username\":\"\",\"modify-date\":\"\"},"
 
 379                 + "{\"vnf-management-server-fqdn\":\"fqdn-value2 url:port\","
 
 380                 + "\"cloud-owner-list\":[{\"cloud-owner\":\"aic3.0\",\"region-id-list\":[{\"region-id\":\"san4a\","
 
 381                 + "\"tenant-id-list\":[\"tenantuuid1\",\"tenantuuid6\"]},{\"region-id\":\"san4b\","
 
 382                 + "\"tenant-id-list\":[\"tenantuuid5\",\"tenantuuid6\"]}]},{\"cloud-owner\":\"nc1.0\","
 
 383                 + "\"region-id-list\":[{\"region-id\":\"san4a\","
 
 384                 + "\"tenant-id-list\":[\"tenantuuid7\",\"tenantuuid8\"]}]}],"
 
 385                 + "\"description\":\"fqdn for east zone Test\",\"username\":\"attuid\",\"create-date\":\"\","
 
 386                 + "\"modify-username\":\"\",\"modify-date\":\"\"}]}";
 
 387         JSONObject documentInfoOne = new JSONObject();
 
 388         documentInfoOne.put(SdcArtifactHandlerConstants.ARTIFACT_CONTENTS, contentStrOne);
 
 389         documentInfoOne.put(SdcArtifactHandlerConstants.ARTIFACT_NAME, "ansible_admin_FQDN_Artifact_0.0.1V.json");
 
 392             artifactHandlerNode.validateAnsibleAdminArtifact(documentInfoOne);
 
 393             fail("Missing exception");
 
 394         } catch (ArtifactHandlerInternalException e) {
 
 395             assertTrue(e.getMessage().contains("Validation Failure"));
 
 401     public void testValidateAnsibleAdminArtifactWithJSONException() throws Exception {
 
 402         String contentStrOne =
 
 403                 "{\"fqdn-list\":[{\"vnf-management-server-fqdn\":\"fqdn-value1 url:port\",\"cloud-owner-list\":[{\"cloud-owner\":\"aic3.0\"}";
 
 405         JSONObject documentInfoOne = new JSONObject();
 
 406         documentInfoOne.put(SdcArtifactHandlerConstants.ARTIFACT_CONTENTS, contentStrOne);
 
 407         documentInfoOne.put(SdcArtifactHandlerConstants.ARTIFACT_NAME, "ansible_admin_FQDN_Artifact_0.0.1V.json");
 
 410             artifactHandlerNode.validateAnsibleAdminArtifact(documentInfoOne);
 
 411             fail("Missing exception");
 
 412         } catch (ArtifactHandlerInternalException je) {
 
 413             assertTrue(je.getMessage().contains("JSON Exception"));
 
 419     public void testProcessArtifactWithException() throws Exception {
 
 420         SvcLogicContext ctx = new SvcLogicContext();
 
 421         ctx.setAttribute("test", "test");
 
 422         Map<String, String> inParams = new HashMap<>();
 
 423         JSONObject postData = new JSONObject();
 
 424         JSONObject input = new JSONObject();
 
 425         inParams.put("response_prefix", "prefix");
 
 426         JSONObject requestInfo = new JSONObject();
 
 427         JSONObject documentInfo = new JSONObject();
 
 428         String artifactContent = IOUtils.toString(
 
 429                 ArtifactHandlerProviderUtilTest.class.getClassLoader().getResourceAsStream("templates/reference_template"),
 
 430                 Charset.defaultCharset());
 
 431         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_CONTENTS, artifactContent);
 
 432         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_NAME, "");
 
 433         requestInfo.put("RequestInfo", "testValue");
 
 434         requestInfo.put("request-id", "testREQUEST_ID");
 
 435         input.put(SdcArtifactHandlerConstants.DOCUMENT_PARAMETERS, documentInfo);
 
 436         input.put(SdcArtifactHandlerConstants.REQUEST_INFORMATION, requestInfo);
 
 437         postData.put("input", input);
 
 438         inParams.put("postData", postData.toString());
 
 440             artifactHandlerNode.processArtifact(inParams, ctx);
 
 441             fail("Missing exception");
 
 442         } catch (Exception e) {
 
 443             assertTrue(e.getMessage().contains("Missing Artifact Name"));
 
 449     public void testProcessArtifactWithExceptionforAnsible() throws Exception {
 
 450         SvcLogicContext ctx = new SvcLogicContext();
 
 451         ctx.setAttribute("test", "test");
 
 452         Map<String, String> inParams = new HashMap<>();
 
 453         JSONObject postData = new JSONObject();
 
 454         JSONObject input = new JSONObject();
 
 455         inParams.put("response_prefix", "prefix");
 
 456         JSONObject requestInfo = new JSONObject();
 
 457         JSONObject documentInfo = new JSONObject();
 
 458         String artifactContent = IOUtils.toString(
 
 459                 ArtifactHandlerProviderUtilTest.class.getClassLoader().getResourceAsStream("templates/reference_template"),
 
 460                 Charset.defaultCharset());
 
 461         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_CONTENTS, artifactContent);
 
 462         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_NAME, "ansible_admin_FQDN_Artifact_0.0.2V.json");
 
 463         requestInfo.put("RequestInfo", "testValue");
 
 464         requestInfo.put("request-id", "testREQUEST_ID");
 
 465         input.put(SdcArtifactHandlerConstants.DOCUMENT_PARAMETERS, documentInfo);
 
 466         input.put(SdcArtifactHandlerConstants.REQUEST_INFORMATION, requestInfo);
 
 467         postData.put("input", input);
 
 468         inParams.put("postData", postData.toString());
 
 471             artifactHandlerNode.processArtifact(inParams, ctx);
 
 472             fail("Missing exception");
 
 473         } catch (Exception e) {
 
 474             assertTrue(e.getMessage().contains("JSON Exception:ansible admin"));
 
 479     public void testProcessAndStoreCapablitiesArtifact() throws Exception {
 
 480         ArtifactHandlerNode ah = new ArtifactHandlerNode();
 
 481         JSONObject capabilities = new JSONObject();
 
 482         JSONObject documentInfo = new JSONObject();
 
 483         MockDBService dbService = MockDBService.initialise();
 
 484         documentInfo.put(SdcArtifactHandlerConstants.SERVICE_UUID, "testuid");
 
 485         documentInfo.put(SdcArtifactHandlerConstants.DISTRIBUTION_ID, "testDist");
 
 486         documentInfo.put(SdcArtifactHandlerConstants.SERVICE_NAME, "testName");
 
 487         documentInfo.put(SdcArtifactHandlerConstants.SERVICE_DESCRIPTION, "testDesc");
 
 488         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_UUID, "testRes");
 
 489         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_INSTANCE_NAME, "testResIns");
 
 490         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_NAME, "testResName");
 
 491         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_VERSION, "testVers");
 
 492         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_TYPE, "testResType");
 
 493         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_UUID, "testArtifactUuid");
 
 494         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_VERSION, "testArtifactVers");
 
 495         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_DESRIPTION, "testArtifactDesc");
 
 496         Whitebox.invokeMethod(ah, "processAndStoreCapabilitiesArtifact", dbService, documentInfo, capabilities,
 
 497                 "artifactName", "someVnf");