Support multiple Ansible servers
[appc.git] / appc-inbound / appc-artifact-handler / provider / src / test / java / org / onap / appc / artifact / handler / node / ArtifactHandlerNodeTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 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
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
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.
22  *
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.artifact.handler.node;
27
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;
51 import java.util.Map;
52 import java.util.HashMap;
53 import java.io.IOException;
54 import java.nio.charset.Charset;
55
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;
62
63
64 @RunWith(PowerMockRunner.class)
65 @PrepareForTest({DBService.class, YANGGeneratorFactory.class})
66 public class ArtifactHandlerNodeTest {
67
68     private ArtifactHandlerNode artifactHandlerNode;
69     private DBService dbServiceMock;
70
71     @Rule
72     public ExpectedException expectedEx = ExpectedException.none();
73
74     @Before
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").when(dbServiceMock).getInternalVersionNumber(Mockito.any(), Mockito.anyString(), Mockito.anyString());
80         PowerMockito.when(DBService.initialise()).thenReturn(dbServiceMock);
81         PowerMockito.mockStatic(YANGGeneratorFactory.class);
82         YANGGenerator yangGeneratorMock = Mockito.mock(YANGGenerator.class);
83         PowerMockito.when(YANGGeneratorFactory.getYANGGenerator()).thenReturn(yangGeneratorMock);
84         ArtifactProcessorImpl artifactProcessorMock = Mockito.mock(ArtifactProcessorImpl.class);
85         Mockito.doReturn(artifactProcessorMock).when(artifactHandlerNode).getArtifactProcessorImpl();
86     }
87
88     @Test
89     public void testProcessArtifact() throws Exception {
90         SvcLogicContext ctx = new SvcLogicContext();
91         ctx.setAttribute("test", "test");
92         Map<String, String> inParams = new HashMap<>();
93         JSONObject postData = new JSONObject();
94         JSONObject input = new JSONObject();
95         inParams.put("response_prefix", "prefix");
96         JSONObject requestInfo = new JSONObject();
97         JSONObject documentInfo = getDocumentInfo("templates/reference_template");
98         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_NAME, "reference_Junit.json");
99         requestInfo.put("RequestInfo", "testValue");
100         input.put(SdcArtifactHandlerConstants.DOCUMENT_PARAMETERS, documentInfo);
101         input.put(SdcArtifactHandlerConstants.REQUEST_INFORMATION, requestInfo);
102         postData.put("input", input);
103         inParams.put("postData", postData.toString());
104         artifactHandlerNode.processArtifact(inParams, ctx);
105         assertNull(ctx.getAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY));
106     }
107
108     @Test
109     public void testPopulateProtocolReference() throws Exception {
110         ArtifactHandlerNode ah = new ArtifactHandlerNode();
111         String contentStr =
112                 "{\"action\": \"TestAction\",\"action-level\": \"vnf\",\"scope\": {\"vnf-type\": \"vDBE-I\",\"vnfc-type\": null},\"template\": \"N\",\"device-protocol\": \"REST\"}";
113         JSONObject content = new JSONObject(contentStr);
114         MockDBService dbService = MockDBService.initialise();
115         Whitebox.invokeMethod(ah, "populateProtocolReference", dbService, content);
116     }
117
118     @Test
119     public void testCleanVnfcInstance() throws Exception {
120         ArtifactHandlerNode ah = new ArtifactHandlerNode();
121         SvcLogicContext ctx = new SvcLogicContext();
122         Whitebox.invokeMethod(ah, "cleanVnfcInstance", ctx);
123         assertTrue(true);
124     }
125
126     @Test
127     public void testStoreUpdateSdcArtifacts() throws Exception {
128         ArtifactHandlerNode ah = new ArtifactHandlerNode();
129         String postDataStr =
130                 "{\"request-information\":{\"request-id\": \"12345\"},\"document-parameters\":{\"artifact-name\":\"testArtifact\",\"artifact-contents\":{\"content\":\"TestContent\"}}}";
131         JSONObject postData = new JSONObject(postDataStr);
132         expectedEx.expect(ArtifactHandlerInternalException.class);
133         Whitebox.invokeMethod(ah, "storeUpdateSdcArtifacts", postData);
134     }
135
136     @Test
137     public void testUpdateYangContents() throws Exception {
138         String artifactId = "1";
139         String yangContents = "SomeContent";
140         Whitebox.invokeMethod(artifactHandlerNode, "updateYangContents", artifactId, yangContents);
141         Mockito.verify(dbServiceMock).updateYangContents(Mockito.any(SvcLogicContext.class),
142                 Mockito.anyString(), Mockito.anyString());
143     }
144
145     @Test
146     public void testProcessVmList() throws Exception{
147         String contentStr = "{\r\n\t\"action\": \"ConfigScaleOut\",\r\n\t\"action-level\": \"VNF\",\r\n\t\"scope\": "
148                 + "{\r\n\t\t\"vnf-type\": \"ScaleOutVNF\",\r\n\t\t\"vnfc-type\": \"\"\r\n\t},\r\n\t\"template\": \"Y\",\r\n\t\"vm\": "
149                 + "[\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\": [{\r\n\t\t\t\"vnfc-instance\": 1,\r\n\t\t\t\"vnfc-type\": \"t1\",\r\n\t\t\t\"vnfc-function-code\": "
150                 + "\"Testdbg\",\r\n\t\t\t\"group-notation-type\": \"GNType\",\r\n\t\t\t\"ipaddress-v4-oam-vip\": \"N\",\r\n\t\t\t\"group-notation-value\": "
151                 + "\"GNValue\"\r\n\t\t}]\r\n\t},\r\n\t{ \r\n\t\t\"vm-instance\": 1,\r\n\t\t\"template-id\":\"id2\",\r\n\t\t\r\n\t\t\"vnfc\": [{\r\n\t\t\t\"vnfc-instance\": 1,\r\n\t\t\t\"vnfc-type\": "
152                 + "\"t1\",\r\n\t\t\t\"vnfc-function-code\": \"Testdbg\",\r\n\t\t\t\"group-notation-type\": \"GNType\",\r\n\t\t\t\"ipaddress-v4-oam-vip\": \"N\",\r\n\t\t\t\"group-notation-value\": "
153                 + "\"GNValue\"\r\n\t\t},\r\n\t\t{\r\n\t\t\t\"vnfc-instance\": 2,\r\n\t\t\t\"vnfc-type\": \"t2\",\r\n\t\t\t\"vnfc-function-code\": \"Testdbg\",\r\n\t\t\t\"group-notation-type\": "
154                 + "\"GNType\",\r\n\t\t\t\"ipaddress-v4-oam-vip\": \"Y\",\r\n\t\t\t\"group-notation-value\": \"GNValue\"\r\n\t\t}]\r\n\t},\r\n\t{\r\n\t\t\"vm-instance\": 2,\r\n\t\t\"template-id\":\"id3\",\r\n\t\t\"vnfc\": "
155                 + "[{\r\n\t\t\t\"vnfc-instance\": 1,\r\n\t\t\t\"vnfc-type\": \"t3\",\r\n\t\t\t\"vnfc-function-code\": \"Testdbg\",\r\n\t\t\t\"group-notation-type\": "
156                 + "\"GNType\",\r\n\t\t\t\"ipaddress-v4-oam-vip\": \"Y\",\r\n\t\t\t\"group-notation-value\": \"GNValue\"\r\n\t\t}]\r\n\t}],\r\n\t\"device-protocol\": "
157                 + "\"TEST-PROTOCOL\",\r\n\t\"user-name\": \"Testnetconf\",\r\n\t\"port-number\": \"22\",\r\n\t\"artifact-list\": [{\r\n\t\t\"artifact-name\": \"Testv_template.json\",\r\n\t\t\"artifact-type\": "
158                 + "\"Testconfig_template\"\r\n\t},\r\n\t{\r\n\t\t\"artifact-name\": \"TESTv_parameter_definitions.json\",\r\n\t\t\"artifact-type\": \"Testparameter_definitions\"\r\n\t},\r\n\t{\r\n\t\t\"artifact-name\": "
159                 + "\"PD_JunitTESTv_parameter_yang.json\",\r\n\t\t\"artifact-type\": \"PD_definations\"\r\n\t}]\r\n}";
160         JSONObject content=new JSONObject(contentStr);
161         MockDBService dbService = MockDBService.initialise();
162         SvcLogicContext context = new SvcLogicContext();
163         artifactHandlerNode.processVmList(content, context, dbService);
164     }
165
166     @Test
167     public void testProcessConfigTypeActions() throws Exception{
168         String contentStr = "{\"action\": \"ConfigScaleOut\"}";
169         JSONObject content=new JSONObject(contentStr);
170         MockDBService dbService = MockDBService.initialise();
171         SvcLogicContext context = new SvcLogicContext();
172         context.setAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL,"Test");
173         artifactHandlerNode.processConfigTypeActions(content, dbService, context);
174     }
175
176     @Test
177     public void testProcessActionLists() throws Exception {
178         String contentStr = "{\r\n\t\"action\": \"HealthCheck\",\r\n\t\"action-level\": \"vm\",\r\n\t\"scope\":"
179                 + " {\r\n\t\t\"vnf-type\": \"vDBE-I\",\r\n\t\t\"vnfc-type\": null\r\n\t},\r\n\t\"template\": "
180                 + "\"N\",\r\n\t\"device-protocol\": \"REST\",\r\n\t\"vnfc-function-code-list\": [\"SSC\", \"MMSC\"]\r\n}";
181         JSONObject content = new JSONObject(contentStr);
182         JSONArray vmActionVnfcFunctionCodesList = new JSONArray();
183         JSONArray vnfActionList = new JSONArray();
184         JSONArray vfModuleActionList = new JSONArray();
185         JSONArray vnfcActionList = new JSONArray();
186         String[] actionLevels = { "vnf", "vm", "vf-module", "vnfc" };
187         for (String actionLevel : actionLevels) {
188             artifactHandlerNode.processActionLists(content, actionLevel, vnfcActionList, vfModuleActionList,
189                     vnfActionList, vmActionVnfcFunctionCodesList);
190         }
191     }
192
193     @Test
194     public void testIsCapabilityArtifactNeeded() throws Exception {
195         String scopeObjStr1= "{\"vnf-type\":\"someVnf\",\"vnfc-type\":\"somVnfc\"}";
196         String scopeObjStr2= "{\"vnf-type\":\"someVnf\",\"vnfc-type\":\"\"}";
197         JSONObject scope1 = new JSONObject(scopeObjStr1);
198         JSONObject scope2 = new JSONObject(scopeObjStr2);
199         SvcLogicContext context = new SvcLogicContext();
200         artifactHandlerNode.setVnfcTypeInformation(scope1, context);
201         assertFalse(artifactHandlerNode.isCapabilityArtifactNeeded(context));
202         artifactHandlerNode.setVnfcTypeInformation(scope2, context);
203         assertTrue(artifactHandlerNode.isCapabilityArtifactNeeded(context));
204     }
205
206     @Test
207     public void testProcessArtifactListsWithMultipleTemplates() throws Exception {
208         String contentStr = "{\r\n\t\t\"action\": \"ConfigScaleOut\",\r\n\t\t\"action-level\": \"vnf\",\r\n\t\t\"scope\": {\r\n\t\t\t\"vnf-type\": "
209                 + "\"vCfgSO-0405\",\r\n\t\t\t\"vnfc-type\": \"\"\r\n\t\t},\r\n\t\t\"template\": \"Y\",\r\n\t\t\"vm\": [{\r\n\t\t\t\"template-id\": "
210                 + "\"TID-0405-EZ\",\r\n\t\t\t\"vm-instance\": 1,\r\n\t\t\t\"vnfc\": [{\r\n\t\t\t\t\"vnfc-instance\": \"1\",\r\n\t\t\t\t\"vnfc-function-code\": "
211                 + "\"Cfg-ez\",\r\n\t\t\t\t\"ipaddress-v4-oam-vip\": \"Y\",\r\n\t\t\t\t\"group-notation-type\": \"first-vnfc-name\",\r\n\t\t\t\t\"group-notation-value\":"
212                 + " \"pair\",\r\n\t\t\t\t\"vnfc-type\": \"vCfg-0405-ez\"\r\n\t\t\t}]\r\n\t\t},\r\n\t\t{\r\n\t\t\t\"template-id\": "
213                 + "\"TID-0405-EZ\",\r\n\t\t\t\"vm-instance\": 2,\r\n\t\t\t\"vnfc\": [{\r\n\t\t\t\t\"vnfc-instance\": \"1\",\r\n\t\t\t\t\"vnfc-function-code\": "
214                 + "\"Cfg-ez\",\r\n\t\t\t\t\"ipaddress-v4-oam-vip\": \"Y\",\r\n\t\t\t\t\"group-notation-type\": \"first-vnfc-name\",\r\n\t\t\t\t\"group-notation-value\":"
215                 + " \"pair\",\r\n\t\t\t\t\"vnfc-type\": \"vCfg-0405-ez\"\r\n\t\t\t}]\r\n\t\t}],\r\n\t\t\"device-protocol\": \"ANSIBLE\",\r\n\t\t\"user-name\": \"root\","
216                 + "\r\n\t\t\"port-number\": \"22\",\r\n\t\t\"artifact-list\": [{\r\n\t\t\t\"artifact-name\": \"template_ConfigScaleOut_vCfgSO-0405_0.0.1V_TID-0405-EZ.json\","
217                 + "\r\n\t\t\t\"artifact-type\": \"config_template\"\r\n\t\t},\r\n\t\t{\r\n\t\t\t\"artifact-name\": \"pd_ConfigScaleOut_vCfgSO-0405_0.0.1V_TID-0405-EZ.yaml\","
218                 + "\r\n\t\t\t\"artifact-type\": \"parameter_definitions\"\r\n\t\t},\r\n\t\t{\r\n\t\t\t\"artifact-name\": "
219                 + "\"template_ConfigScaleOut_vCfgSO-0405_0.0.1V_TID-0405-EZ-2.json\",\r\n\t\t\t\"artifact-type\": \"config_template\"\r\n\t\t},\r\n\t\t{\r\n\t\t\t\"artifact-name\": "
220                 + "\"pd_ConfigScaleOut_vCfgSO-0405_0.0.1V_TID-0405-EZ-2.yaml\",\r\n\t\t\t\"artifact-type\": \"parameter_definitions\"\r\n\t\t}],\r\n\t\t\"template-id-list\":"
221                 + " [\"TID-0405-EZ\",\r\n\t\t\"TID-0405-EZ-2\"],\r\n\t\t\"scopeType\": \"vnf-type\"\r\n\t}";
222         JSONObject content = new JSONObject(contentStr);
223         MockDBService dbService = MockDBService.initialise();
224         SvcLogicContext context = new SvcLogicContext();
225         context.setAttribute("vnf-type", "someVnf");
226         context.setAttribute("action", "ConfigScaleOut");
227         artifactHandlerNode.processArtifactList(content, dbService, context, null);
228     }
229
230     @Test
231     public void testProcessArtifactListsWithVnfcTypeList() throws Exception {
232         String contentStr = "{\r\n\t\"action\": \"Configure\",\r\n\t\"action-level\": \"vnf\",\r\n\t\"scope\": {\r\n\t\t\"vnf-type\": "
233                 + "\"newtypeofvnf\",\r\n\t\t\"vnfc-type-list\": [\"vnfctype1\",\"vnfctype2\"]\r\n\t},\r\n\t\"template\": \"Y\",\r\n\t\"vm\":"
234                 + " [{\r\n\t\t\t\"vm-instance\": 1,\r\n\t\t\t\"template-id\": \"vnfctype1\",\r\n\t\t\t\"vnfc\": [{\r\n\t\t\t\t\"vnfc-instance\": "
235                 + "\"1\",\r\n\t\t\t\t\"vnfc-function-code\": \"fcx\",\r\n\t\t\t\t\"ipaddress-v4-oam-vip\": \"Y\",\r\n\t\t\t\t\"group-notation-type\": "
236                 + "\"first-vnfc-name\",\r\n\t\t\t\t\"group-notation-value\": \"pair\",\r\n\t\t\t\t\"vnfc-type\": \"vDBE\"\r\n\t\t\t}]\r\n\t\t},"
237                 + "\r\n\t\t{\r\n\t\t\t\"vm-instance\": 1,\r\n\t\t\t\"template-id\": \"vnfctype2\",\r\n\t\t\t\"vnfc\": [{\r\n\t\t\t\t\"vnfc-instance\": "
238                 + "\"1\",\r\n\t\t\t\t\"vnfc-function-code\": \"fcx\",\r\n\t\t\t\t\"ipaddress-v4-oam-vip\": \"Y\",\r\n\t\t\t\t\"group-notation-type\": "
239                 + "\"first-vnfc-name\",\r\n\t\t\t\t\"group-notation-value\": \"pair\",\r\n\t\t\t\t\"vnfc-type\": \"vDBE\"\r\n\t\t\t}]\r\n\t\t},"
240                 + "\r\n\t\t{\r\n\t\t\t\"vm-instance\": 2,\r\n\t\t\t\"template-id\": \"vnfctype2\",\r\n\t\t\t\"vnfc\": [{\r\n\t\t\t\t\"vnfc-instance\": "
241                 + "\"1\",\r\n\t\t\t\t\"vnfc-function-code\": \"fcx\",\r\n\t\t\t\t\"ipaddress-v4-oam-vip\": \"Y\",\r\n\t\t\t\t\"group-notation-type\": "
242                 + "\"first-vnfc-name\",\r\n\t\t\t\t\"group-notation-value\": \"pair\",\r\n\t\t\t\t\"vnfc-type\": \"vDBE\"\r\n\t\t\t}]\r\n\t\t}\r\n\t],"
243                 + "\r\n\t\"device-protocol\": \"NETCONF-XML\",\r\n\t\"user-name\": \"netconf\",\r\n\t\"port-number\": \"20\",\r\n\t\"artifact-list\": "
244                 + "[{\r\n\t\t\t\"artifact-name\": \"template_ConfigScaleOut_newtypeofvnf_0.0.1V_vnfctype1.xml\",\r\n\t\t\t\"artifact-type\": "
245                 + "\"config_template\"\r\n\t\t},\r\n\t\t{\r\n\t\t\t\"artifact-name\": \"pd_ConfigScaleOut_newtypeofvnf_0.0.1V_vnfctype1.yaml\","
246                 + "\r\n\t\t\t\"artifact-type\": \"parameter_definitions\"\r\n\t\t},\r\n\t\t{\r\n\t\t\t\"artifact-name\": "
247                 + "\"template_ConfigScaleOut_newtypeofvnf_0.0.1V_vnfctype2.xml\",\r\n\t\t\t\"artifact-type\": "
248                 + "\"config_template\"\r\n\t\t},\r\n\t\t{\r\n\t\t\t\"artifact-name\": \"pd_ConfigScaleOut_newtypeofvnf_0.0.1V_vnfctype2.yaml\","
249                 + "\r\n\t\t\t\"artifact-type\": \"parameter_definitions\"\r\n\t\t}\r\n\t],\r\n\t\"scopeType\": \"vnf-type\"\r\n}";
250         JSONObject content = new JSONObject(contentStr);
251         MockDBService dbService = MockDBService.initialise();
252         SvcLogicContext context = new SvcLogicContext();
253         context.setAttribute("vnf-type", "someVnf");
254         context.setAttribute("action", "Configure");
255         JSONObject scope = (JSONObject)content.get("scope");
256         JSONArray vnfcTypeList = artifactHandlerNode.setVnfcTypeInformation(scope, context);
257         artifactHandlerNode.processArtifactList(content, dbService, context, vnfcTypeList);
258         JSONArray vnfcLists = scope.getJSONArray("vnfc-type-list");        
259         assertEquals(vnfcLists.toString(), "[\"vnfctype1\",\"vnfctype2\"]");
260         assertEquals(context.getAttribute("vnfc-type"),"vnfctype2");
261         assertNotNull (vnfcTypeList);
262     }
263
264     @Test
265     public void testProcessArtifactPdArtifactName() throws IOException, ArtifactProcessorException {
266         SvcLogicContext ctx = new SvcLogicContext();
267         ctx.setAttribute("test", "test");
268         Map<String, String> inParams = new HashMap<>();
269         JSONObject postData = new JSONObject();
270         JSONObject input = new JSONObject();
271         inParams.put("response_prefix", "prefix");
272         JSONObject requestInfo = new JSONObject();
273         JSONObject documentInfo = getDocumentInfo("templates/pd_template");
274         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_NAME, "pd_Junit.json");
275         requestInfo.put("RequestInfo", "testValue");
276         input.put(SdcArtifactHandlerConstants.DOCUMENT_PARAMETERS, documentInfo);
277         input.put(SdcArtifactHandlerConstants.REQUEST_INFORMATION, requestInfo);
278         postData.put("input", input);
279         inParams.put("postData", postData.toString());
280         artifactHandlerNode.processArtifact(inParams, ctx);
281         Mockito.verify(dbServiceMock, Mockito.times(2)).initialise();
282     }
283
284     private JSONObject getDocumentInfo(String filename) throws IOException {
285         JSONObject documentInfo = new JSONObject();
286         String artifactContent = IOUtils.toString(ArtifactHandlerProviderUtilTest.class.getClassLoader()
287                 .getResourceAsStream(filename), Charset.defaultCharset());
288         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_CONTENTS, artifactContent);
289         documentInfo.put(SdcArtifactHandlerConstants.SERVICE_UUID, "12345");
290         documentInfo.put(SdcArtifactHandlerConstants.DISTRIBUTION_ID, "12345");
291         documentInfo.put(SdcArtifactHandlerConstants.SERVICE_NAME, "12345");
292         documentInfo.put(SdcArtifactHandlerConstants.SERVICE_DESCRIPTION, "12345");
293         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_UUID, "12345");        
294         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_INSTANCE_NAME, "12345");
295         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_VERSION, "12345");
296         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_TYPE, "12345");
297         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_UUID, "12345");
298         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_TYPE, "12345");
299         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_VERSION, "12345");
300         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_DESRIPTION, "12345");
301         return documentInfo;
302     }
303     
304     
305     @Test
306     public void testValidateAnsibleAdminArtifact() throws Exception {
307         String contentStr = "{\"fqdn-list\":[{\"vnf-management-server-fqdn\":\"fqdn-value1 url:port\",\"cloud-owner-list\":[{\"cloud-owner\":\"aic3.0\","
308           + "\"region-id-list\":[{\"region-id\":\"san4a\",\"tenant-id-list\":[\"tenantuuid1\",\"tenantuuid2\"]},{\"region-id\":\"san4b\","
309           + "\"tenant-id-list\":[\"tenantuuid1\",\"tenantuuid2\"]}]},{\"cloud-owner\":\"nc1.0\",\"region-id-list\":[{\"region-id\":\"san4a\","
310           + "\"tenant-id-list\":[\"tenantuuid3\",\"tenantuuid4\"]}]}],\"description\":\"fqdn for east zone Production\",\"username\":\"attuid\","
311           + "\"create-date\":\"\",\"modify-username\":\"\",\"modify-date\":\"\"},{\"vnf-management-server-fqdn\":\"fqdn-value2 url:port\","
312           + "\"cloud-owner-list\":[{\"cloud-owner\":\"aic3.0\",\"region-id-list\":[{\"region-id\":\"san4a\",\"tenant-id-list\":[\"tenantuuid5\","
313           + "\"tenantuuid6\"]},{\"region-id\":\"san4b\",\"tenant-id-list\":[\"tenantuuid5\",\"tenantuuid6\"]}]},{\"cloud-owner\":\"nc1.0\","
314           + "\"region-id-list\":[{\"region-id\":\"san4a\",\"tenant-id-list\":[\"tenantuuid7\",\"tenantuuid8\"]}]}],"
315           + "\"description\":\"fqdn for east zone Test\",\"username\":\"attuid\",\"create-date\":\"\","
316           + "\"modify-username\":\"\",\"modify-date\":\"\"}]}";
317
318         JSONObject documentInfo = new JSONObject();       
319         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_CONTENTS, contentStr);
320         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_NAME, "ansible_admin_FQDN_Artifact_0.0.1V.json");
321         artifactHandlerNode.validateAnsibleAdminArtifact(documentInfo);
322     }
323     
324     @Test
325     public void testValidateAnsibleAdminArtifactWithException() throws Exception {
326        String contentStrOne = "{\"fqdn-list\":[{\"vnf-management-server-fqdn\":\"fqdn-value1 url:port\",\"cloud-owner-list\":[{\"cloud-owner\":\"aic3.0\","
327          + "\"region-id-list\":[{\"region-id\":\"san4a\",\"tenant-id-list\":[\"tenantuuid1\",\"tenantuuid2\"]},{\"region-id\":\"san4b\","
328          + "\"tenant-id-list\":[\"tenantuuid1\",\"tenantuuid2\"]}]},{\"cloud-owner\":\"nc1.0\",\"region-id-list\":[{\"region-id\":\"san4a\","
329          + "\"tenant-id-list\":[\"tenantuuid3\",\"tenantuuid4\"]}]}],\"description\":\"fqdn for east zone Production\",\"username\":\"attuid\","
330          + "\"create-date\":\"\",\"modify-username\":\"\",\"modify-date\":\"\"},{\"vnf-management-server-fqdn\":\"fqdn-value2 url:port\","
331          + "\"cloud-owner-list\":[{\"cloud-owner\":\"aic3.0\",\"region-id-list\":[{\"region-id\":\"san4a\",\"tenant-id-list\":[\"tenantuuid1\","
332          + "\"tenantuuid6\"]},{\"region-id\":\"san4b\",\"tenant-id-list\":[\"tenantuuid5\",\"tenantuuid6\"]}]},{\"cloud-owner\":\"nc1.0\","
333          + "\"region-id-list\":[{\"region-id\":\"san4a\",\"tenant-id-list\":[\"tenantuuid7\",\"tenantuuid8\"]}]}],"
334          + "\"description\":\"fqdn for east zone Test\",\"username\":\"attuid\",\"create-date\":\"\","
335          + "\"modify-username\":\"\",\"modify-date\":\"\"}]}";
336        JSONObject documentInfoOne = new JSONObject();       
337        documentInfoOne.put(SdcArtifactHandlerConstants.ARTIFACT_CONTENTS, contentStrOne);
338        documentInfoOne.put(SdcArtifactHandlerConstants.ARTIFACT_NAME, "ansible_admin_FQDN_Artifact_0.0.1V.json");
339        
340        try {
341            artifactHandlerNode.validateAnsibleAdminArtifact(documentInfoOne);
342            fail("Missing exception");
343            }catch(ArtifactHandlerInternalException e) {
344             assertTrue(e.getMessage().contains("Validation Failure"));
345             }
346        
347     }
348     
349     @Test
350     public void testValidateAnsibleAdminArtifactWithJSONException() throws Exception {
351         String contentStrOne = "{\"fqdn-list\":[{\"vnf-management-server-fqdn\":\"fqdn-value1 url:port\",\"cloud-owner-list\":[{\"cloud-owner\":\"aic3.0\"}";           
352        
353         JSONObject documentInfoOne = new JSONObject();       
354         documentInfoOne.put(SdcArtifactHandlerConstants.ARTIFACT_CONTENTS, contentStrOne);
355         documentInfoOne.put(SdcArtifactHandlerConstants.ARTIFACT_NAME, "ansible_admin_FQDN_Artifact_0.0.1V.json");
356                
357         try {
358             artifactHandlerNode.validateAnsibleAdminArtifact(documentInfoOne);
359             fail("Missing exception");
360         }catch(ArtifactHandlerInternalException je) {
361           assertTrue(je.getMessage().contains("JSON Exception"));
362           
363         }           
364        
365      }
366     
367     @Test
368     public void testProcessArtifactWithException() throws Exception {
369         SvcLogicContext ctx = new SvcLogicContext();
370         ctx.setAttribute("test", "test");
371         Map<String, String> inParams = new HashMap<>();
372         JSONObject postData = new JSONObject();
373         JSONObject input = new JSONObject();
374         inParams.put("response_prefix", "prefix");
375         JSONObject requestInfo = new JSONObject();
376         JSONObject documentInfo = new JSONObject();
377         String artifactContent = IOUtils.toString(ArtifactHandlerProviderUtilTest.class.getClassLoader()
378                 .getResourceAsStream("templates/reference_template"), Charset.defaultCharset());
379         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_CONTENTS, artifactContent);
380         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_NAME, "");
381         requestInfo.put("RequestInfo", "testValue");
382         requestInfo.put("request-id","testREQUEST_ID");
383         input.put(SdcArtifactHandlerConstants.DOCUMENT_PARAMETERS, documentInfo);
384         input.put(SdcArtifactHandlerConstants.REQUEST_INFORMATION, requestInfo);
385         postData.put("input", input);
386         inParams.put("postData", postData.toString());
387         try {
388             artifactHandlerNode.processArtifact(inParams, ctx);
389             fail("Missing exception");
390         }catch(Exception e) {
391           assertTrue(e.getMessage().contains("Missing Artifact Name"));
392         }
393         
394     }
395     @Test
396     public void testProcessArtifactWithExceptionforAnsible() throws Exception {
397         SvcLogicContext ctx = new SvcLogicContext();
398         ctx.setAttribute("test", "test");
399         Map<String, String> inParams = new HashMap<>();
400         JSONObject postData = new JSONObject();
401         JSONObject input = new JSONObject();
402         inParams.put("response_prefix", "prefix");
403         JSONObject requestInfo = new JSONObject();
404         JSONObject documentInfo = new JSONObject();
405         String artifactContent = IOUtils.toString(ArtifactHandlerProviderUtilTest.class.getClassLoader()
406                 .getResourceAsStream("templates/reference_template"), Charset.defaultCharset());
407         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_CONTENTS, artifactContent);
408         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_NAME, "ansible_admin_FQDN_Artifact_0.0.2V.json");
409         requestInfo.put("RequestInfo", "testValue");
410         requestInfo.put("request-id","testREQUEST_ID");
411         input.put(SdcArtifactHandlerConstants.DOCUMENT_PARAMETERS, documentInfo);
412         input.put(SdcArtifactHandlerConstants.REQUEST_INFORMATION, requestInfo);
413         postData.put("input", input);
414         inParams.put("postData", postData.toString());
415         
416         try {
417              artifactHandlerNode.processArtifact(inParams, ctx);
418              fail("Missing exception");
419         }catch(Exception e) {
420            assertTrue(e.getMessage().contains("JSON Exception:ansible admin"));
421         }
422      }
423     
424     @Test
425     public void testProcessAndStoreCapablitiesArtifact() throws Exception {
426         ArtifactHandlerNode ah = new ArtifactHandlerNode();
427         JSONObject capabilities = new JSONObject();
428         JSONObject documentInfo = new JSONObject();
429         MockDBService dbService = MockDBService.initialise();
430         documentInfo.put(SdcArtifactHandlerConstants.SERVICE_UUID, "testuid");
431         documentInfo.put(SdcArtifactHandlerConstants.DISTRIBUTION_ID, "testDist");
432         documentInfo.put(SdcArtifactHandlerConstants.SERVICE_NAME, "testName");
433         documentInfo.put(SdcArtifactHandlerConstants.SERVICE_DESCRIPTION, "testDesc");
434         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_UUID, "testRes");
435         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_INSTANCE_NAME, "testResIns");
436         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_VERSION, "testVers");
437         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_TYPE, "testResType");
438         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_UUID, "testArtifactUuid");
439         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_VERSION, "testArtifactVers");
440         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_DESRIPTION, "testArtifactDesc");
441         Whitebox.invokeMethod(ah, "processAndStoreCapabilitiesArtifact", dbService, documentInfo, capabilities,
442                 "artifactName", "someVnf");
443     }
444     
445
446 }