Changed to unmaintained
[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-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
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")
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();
87     }
88
89     @Test
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));
108     }
109
110     @Test
111     public void testPopulateProtocolReference() throws Exception {
112         ArtifactHandlerNode ah = new ArtifactHandlerNode();
113         String contentStr =
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);
118         assertNotNull(ah);
119     }
120
121     @Test
122     public void testCleanVnfcInstance() throws Exception {
123         ArtifactHandlerNode ah = new ArtifactHandlerNode();
124         SvcLogicContext ctx = new SvcLogicContext();
125         Whitebox.invokeMethod(ah, "cleanVnfcInstance", ctx);
126         assertTrue(true);
127     }
128
129     @Test
130     public void testStoreUpdateSdcArtifacts() throws Exception {
131         ArtifactHandlerNode ah = new ArtifactHandlerNode();
132         String postDataStr =
133                 "{\"request-information\":{\"request-id\": \"12345\"},\"document-parameters\":{\"artifact-name\":\"testArtifact\",\"artifact-contents\":{\"content\":\"TestContent\"}}}";
134         JSONObject postData = new JSONObject(postDataStr);
135         expectedEx.expect(ArtifactHandlerInternalException.class);
136         Whitebox.invokeMethod(ah, "storeUpdateSdcArtifacts", postData);
137     }
138
139     @Test
140     public void testUpdateYangContents() throws Exception {
141         String artifactId = "1";
142         String yangContents = "SomeContent";
143         Whitebox.invokeMethod(artifactHandlerNode, "updateYangContents", artifactId, yangContents);
144         Mockito.verify(dbServiceMock)
145                 .updateYangContents(Mockito.any(SvcLogicContext.class), Mockito.anyString(), Mockito.anyString());
146     }
147
148     @Test
149     public void testProcessVmList() throws Exception{
150         String contentStr = "{\r\n\t\"action\": \"ConfigScaleOut\",\r\n\t\"action-level\": \"VNF\",\r\n\t\"scope\": "
151                 + "{\r\n\t\t\"vnf-type\": \"ScaleOutVNF\",\r\n\t\t\"vnfc-type\": \"\"\r\n\t},\r\n"
152                 + "\t\"template\": \"Y\",\r\n\t\"vm\": "
153                 + "[\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\": "
154                 + "[{\r\n\t\t\t\"vnfc-instance\": 1,\r\n\t\t\t\"vnfc-type\": \"t1\",\r\n\t\t\t\"vnfc-function-code\": "
155                 + "\"Testdbg\",\r\n\t\t\t\"group-notation-type\": \"GNType\",\r\n"
156                 + "\t\t\t\"ipaddress-v4-oam-vip\": \"N\",\r\n"
157                 + "\t\t\t\"group-notation-value\": \"GNValue\"\r\n\t\t}]\r\n\t},\r\n"
158                 + "\t{ \r\n\t\t\"vm-instance\": 1,\r\n\t\t\"template-id\":\"id2\",\r\n"
159                 + "\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"
160                 + "\t\t\t\"vnfc-function-code\": \"Testdbg\",\r\n\t\t\t\"group-notation-type\": \"GNType\",\r\n"
161                 + "\t\t\t\"ipaddress-v4-oam-vip\": \"N\",\r\n\t\t\t\"group-notation-value\": \"GNValue\"\r\n\t\t},\r\n"
162                 + "\t\t{\r\n\t\t\t\"vnfc-instance\": 2,\r\n\t\t\t\"vnfc-type\": \"t2\",\r\n"
163                 + "\t\t\t\"vnfc-function-code\": \"Testdbg\",\r\n\t\t\t\"group-notation-type\": \"GNType\",\r\n"
164                 + "\t\t\t\"ipaddress-v4-oam-vip\": \"Y\",\r\n\t\t\t\"group-notation-value\": \"GNValue\"\r\n\t\t}]\r\n"
165                 + "\t},\r\n\t{\r\n\t\t\"vm-instance\": 2,\r\n\t\t\"template-id\":\"id3\",\r\n"
166                 + "\t\t\"vnfc\": [{\r\n\t\t\t\"vnfc-instance\": 1,\r\n\t\t\t\"vnfc-type\": \"t3\",\r\n"
167                 + "\t\t\t\"vnfc-function-code\": \"Testdbg\",\r\n\t\t\t\"group-notation-type\": \"GNType\",\r\n"
168                 + "\t\t\t\"ipaddress-v4-oam-vip\": \"Y\",\r\n\t\t\t\"group-notation-value\": \"GNValue\"\r\n\t\t}]\r\n"
169                 + "\t}],\r\n\t\"device-protocol\": \"TEST-PROTOCOL\",\r\n\t\"user-name\": \"Testnetconf\",\r\n"
170                 + "\t\"port-number\": \"22\",\r\n\t\"artifact-list\": [{\r\n"
171                 + "\t\t\"artifact-name\": \"Testv_template.json\",\r\n"
172                 + "\t\t\"artifact-type\": \"Testconfig_template\"\r\n\t},\r\n"
173                 + "\t{\r\n\t\t\"artifact-name\": \"TESTv_parameter_definitions.json\",\r\n"
174                 + "\t\t\"artifact-type\": \"Testparameter_definitions\"\r\n\t},\r\n\t{\r\n"
175                 + "\t\t\"artifact-name\": \"PD_JunitTESTv_parameter_yang.json\",\r\n"
176                 + "\t\t\"artifact-type\": \"PD_definations\"\r\n\t}]\r\n}";
177         JSONObject content=new JSONObject(contentStr);
178         MockDBService dbService = MockDBService.initialise();
179         SvcLogicContext context = new SvcLogicContext();
180         artifactHandlerNode.processVmList(content, context, dbService);
181         assertNotNull(contentStr);
182     }
183
184     @Test
185     public void testProcessConfigTypeActions() throws Exception {
186         String contentStr = "{\"action\": \"ConfigScaleOut\"}";
187         JSONObject content = new JSONObject(contentStr);
188         MockDBService dbService = MockDBService.initialise();
189         SvcLogicContext context = new SvcLogicContext();
190         context.setAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL, "Test");
191         artifactHandlerNode.processConfigTypeActions(content, dbService, context);
192         assertNotNull(contentStr);
193     }
194
195     @Test
196     public void testProcessActionLists() throws Exception {
197         String contentStr = "{\r\n\t\"action\": \"HealthCheck\",\r\n\t\"action-level\": \"vm\",\r\n\t\"scope\":"
198                 + " {\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"
199                 + "\t\"device-protocol\": \"REST\",\r\n\t\"vnfc-function-code-list\": [\"SSC\", \"MMSC\"]\r\n}";
200         JSONObject content = new JSONObject(contentStr);
201         JSONArray vmActionVnfcFunctionCodesList = new JSONArray();
202         JSONArray vnfActionList = new JSONArray();
203         JSONArray vfModuleActionList = new JSONArray();
204         JSONArray vnfcActionList = new JSONArray();
205         String[] actionLevels = { "vnf", "vm", "vf-module", "vnfc" };
206         for (String actionLevel : actionLevels) {
207             artifactHandlerNode.processActionLists(content, actionLevel, vnfcActionList, vfModuleActionList,
208                     vnfActionList, vmActionVnfcFunctionCodesList);
209         }
210         assertNotNull(contentStr);
211     }
212
213     @Test
214     public void testIsCapabilityArtifactNeeded() throws Exception {
215         String scopeObjStr1 = "{\"vnf-type\":\"someVnf\",\"vnfc-type\":\"somVnfc\"}";
216         String scopeObjStr2 = "{\"vnf-type\":\"someVnf\",\"vnfc-type\":\"\"}";
217         JSONObject scope1 = new JSONObject(scopeObjStr1);
218         JSONObject scope2 = new JSONObject(scopeObjStr2);
219         SvcLogicContext context = new SvcLogicContext();
220         artifactHandlerNode.setVnfcTypeInformation(scope1, context);
221         assertFalse(artifactHandlerNode.isCapabilityArtifactNeeded(context));
222         artifactHandlerNode.setVnfcTypeInformation(scope2, context);
223         assertTrue(artifactHandlerNode.isCapabilityArtifactNeeded(context));
224     }
225
226     @Test
227     public void testProcessArtifactListsWithMultipleTemplates() throws Exception {
228         String contentStr = "{\r\n\t\t\"action\": \"ConfigScaleOut\",\r\n\t\t\"action-level\": \"vnf\",\r\n"
229                 + "\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"
230                 + "\t\t\"template\": \"Y\",\r\n\t\t\"vm\": [{\r\n\t\t\t\"template-id\": \"TID-0405-EZ\",\r\n"
231                 + "\t\t\t\"vm-instance\": 1,\r\n\t\t\t\"vnfc\": [{\r\n\t\t\t\t\"vnfc-instance\": \"1\",\r\n"
232                 + "\t\t\t\t\"vnfc-function-code\": \"Cfg-ez\",\r\n\t\t\t\t\"ipaddress-v4-oam-vip\": \"Y\",\r\n"
233                 + "\t\t\t\t\"group-notation-type\": \"first-vnfc-name\",\r\n"
234                 + "\t\t\t\t\"group-notation-value\": \"pair\",\r\n\t\t\t\t\"vnfc-type\": \"vCfg-0405-ez\"\r\n"
235                 + "\t\t\t}]\r\n\t\t},\r\n\t\t{\r\n\t\t\t\"template-id\": \"TID-0405-EZ\",\r\n"
236                 + "\t\t\t\"vm-instance\": 2,\r\n\t\t\t\"vnfc\": [{\r\n\t\t\t\t\"vnfc-instance\": \"1\",\r\n"
237                 + "\t\t\t\t\"vnfc-function-code\": \"Cfg-ez\",\r\n\t\t\t\t\"ipaddress-v4-oam-vip\": \"Y\",\r\n"
238                 + "\t\t\t\t\"group-notation-type\": \"first-vnfc-name\",\r\n"
239                 + "\t\t\t\t\"group-notation-value\": \"pair\",\r\n\t\t\t\t\"vnfc-type\": \"vCfg-0405-ez\"\r\n"
240                 + "\t\t\t}]\r\n\t\t}],\r\n\t\t\"device-protocol\": \"ANSIBLE\",\r\n\t\t\"user-name\": \"root\",\r\n"
241                 + "\t\t\"port-number\": \"22\",\r\n\t\t\"artifact-list\": [{\r\n"
242                 + "\t\t\t\"artifact-name\": \"template_ConfigScaleOut_vCfgSO-0405_0.0.1V_TID-0405-EZ.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.yaml\",\r\n"
245                 + "\t\t\t\"artifact-type\": \"parameter_definitions\"\r\n\t\t},\r\n\t\t{\r\n"
246                 + "\t\t\t\"artifact-name\": \"template_ConfigScaleOut_vCfgSO-0405_0.0.1V_TID-0405-EZ-2.json\",\r\n"
247                 + "\t\t\t\"artifact-type\": \"config_template\"\r\n\t\t},\r\n\t\t{\r\n"
248                 + "\t\t\t\"artifact-name\": \"pd_ConfigScaleOut_vCfgSO-0405_0.0.1V_TID-0405-EZ-2.yaml\",\r\n"
249                 + "\t\t\t\"artifact-type\": \"parameter_definitions\"\r\n\t\t}],\r\n"
250                 + "\t\t\"template-id-list\": [\"TID-0405-EZ\",\r\n\t\t\"TID-0405-EZ-2\"],\r\n"
251                 + "\t\t\"scopeType\": \"vnf-type\"\r\n\t}";
252         JSONObject content = new JSONObject(contentStr);
253         MockDBService dbService = MockDBService.initialise();
254         SvcLogicContext context = new SvcLogicContext();
255         context.setAttribute("vnf-type", "someVnf");
256         context.setAttribute("action", "ConfigScaleOut");
257         artifactHandlerNode.processArtifactList(content, dbService, context, null);
258         assertNotNull(content);
259     }
260
261     @Test
262     public void testProcessArtifactListsWithVnfcTypeList() throws Exception {
263         String contentStr = "{\r\n\t\"action\": \"Configure\",\r\n\t\"action-level\": \"vnf\",\r\n\t\"scope\": {\r\n"
264                 + "\t\t\"vnf-type\": \"newtypeofvnf\",\r\n\t\t\"vnfc-type-list\": [\"vnfctype1\",\"vnfctype2\"]\r\n"
265                 + "\t},\r\n\t\"template\": \"Y\",\r\n\t\"vm\": [{\r\n\t\t\t\"vm-instance\": 1,\r\n"
266                 + "\t\t\t\"template-id\": \"vnfctype1\",\r\n\t\t\t\"vnfc\": [{\r\n"
267                 + "\t\t\t\t\"vnfc-instance\": \"1\",\r\n\t\t\t\t\"vnfc-function-code\": \"fcx\",\r\n"
268                 + "\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\": 1,\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\t{\r\n\t\t\t\"vm-instance\": 2,\r\n\t\t\t\"template-id\": \"vnfctype2\",\r\n"
277                 + "\t\t\t\"vnfc\": [{\r\n\t\t\t\t\"vnfc-instance\": \"1\",\r\n"
278                 + "\t\t\t\t\"vnfc-function-code\": \"fcx\",\r\n\t\t\t\t\"ipaddress-v4-oam-vip\": \"Y\",\r\n"
279                 + "\t\t\t\t\"group-notation-type\": \"first-vnfc-name\",\r\n"
280                 + "\t\t\t\t\"group-notation-value\": \"pair\",\r\n\t\t\t\t\"vnfc-type\": \"vDBE\"\r\n\t\t\t}]\r\n"
281                 + "\t\t}\r\n\t],\r\n\t\"device-protocol\": \"NETCONF-XML\",\r\n\t\"user-name\": \"netconf\",\r\n"
282                 + "\t\"port-number\": \"20\",\r\n\t\"artifact-list\": [{\r\n"
283                 + "\t\t\t\"artifact-name\": \"template_ConfigScaleOut_newtypeofvnf_0.0.1V_vnfctype1.xml\",\r\n"
284                 + "\t\t\t\"artifact-type\": \"config_template\"\r\n\t\t},\r\n\t\t{\r\n"
285                 + "\t\t\t\"artifact-name\": \"pd_ConfigScaleOut_newtypeofvnf_0.0.1V_vnfctype1.yaml\",\r\n"
286                 + "\t\t\t\"artifact-type\": \"parameter_definitions\"\r\n\t\t},\r\n"
287                 + "\t\t{\r\n\t\t\t\"artifact-name\": \"template_ConfigScaleOut_newtypeofvnf_0.0.1V_vnfctype2.xml\",\r\n"
288                 + "\t\t\t\"artifact-type\": \"config_template\"\r\n\t\t},\r\n"
289                 + "\t\t{\r\n\t\t\t\"artifact-name\": \"pd_ConfigScaleOut_newtypeofvnf_0.0.1V_vnfctype2.yaml\",\r\n"
290                 + "\t\t\t\"artifact-type\": \"parameter_definitions\"\r\n\t\t}\r\n\t],\r\n"
291                 + "\t\"scopeType\": \"vnf-type\"\r\n}";
292         JSONObject content = new JSONObject(contentStr);
293         MockDBService dbService = MockDBService.initialise();
294         SvcLogicContext context = new SvcLogicContext();
295         context.setAttribute("vnf-type", "someVnf");
296         context.setAttribute("action", "Configure");
297         JSONObject scope = (JSONObject)content.get("scope");
298         JSONArray vnfcTypeList = artifactHandlerNode.setVnfcTypeInformation(scope, context);
299         artifactHandlerNode.processArtifactList(content, dbService, context, vnfcTypeList);
300         JSONArray vnfcLists = scope.getJSONArray("vnfc-type-list");
301         assertEquals(vnfcLists.toString(), "[\"vnfctype1\",\"vnfctype2\"]");
302         assertEquals(context.getAttribute("vnfc-type"), "vnfctype2");
303         assertNotNull (vnfcTypeList);
304     }
305
306     @Test
307     public void testProcessArtifactPdArtifactName() throws IOException, ArtifactProcessorException {
308         SvcLogicContext ctx = new SvcLogicContext();
309         ctx.setAttribute("test", "test");
310         Map<String, String> inParams = new HashMap<>();
311         JSONObject postData = new JSONObject();
312         JSONObject input = new JSONObject();
313         inParams.put("response_prefix", "prefix");
314         JSONObject requestInfo = new JSONObject();
315         JSONObject documentInfo = getDocumentInfo("templates/pd_template");
316         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_NAME, "pd_Junit.json");
317         requestInfo.put("RequestInfo", "testValue");
318         requestInfo.put("request-id", "testREQUEST_ID");
319         input.put(SdcArtifactHandlerConstants.DOCUMENT_PARAMETERS, documentInfo);
320         input.put(SdcArtifactHandlerConstants.REQUEST_INFORMATION, requestInfo);
321         postData.put("input", input);
322         inParams.put("postData", postData.toString());
323         artifactHandlerNode.processArtifact(inParams, ctx);
324         Mockito.verify(dbServiceMock, Mockito.times(2)).initialise();
325     }
326
327     private JSONObject getDocumentInfo(String filename) throws IOException {
328         JSONObject documentInfo = new JSONObject();
329         String artifactContent = IOUtils.toString(
330                 ArtifactHandlerProviderUtilTest.class.getClassLoader().getResourceAsStream(filename),
331                 Charset.defaultCharset());
332         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_CONTENTS, artifactContent);
333         documentInfo.put(SdcArtifactHandlerConstants.SERVICE_UUID, "12345");
334         documentInfo.put(SdcArtifactHandlerConstants.DISTRIBUTION_ID, "12345");
335         documentInfo.put(SdcArtifactHandlerConstants.SERVICE_NAME, "12345");
336         documentInfo.put(SdcArtifactHandlerConstants.SERVICE_DESCRIPTION, "12345");
337         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_UUID, "12345");
338         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_INSTANCE_NAME, "12345");
339         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_NAME, "12345");
340         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_VERSION, "12345");
341         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_TYPE, "12345");
342         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_UUID, "12345");
343         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_TYPE, "12345");
344         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_VERSION, "12345");
345         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_DESRIPTION, "12345");
346         return documentInfo;
347     }
348
349
350     @Test
351     public void testValidateAnsibleAdminArtifact() throws Exception {
352         String contentStr = "{\"fqdn-list\":[{\"vnf-management-server-fqdn\":\"fqdn-value1 url:port\","
353                 + "\"cloud-owner-list\":[{\"cloud-owner\":\"aic3.0\",\"region-id-list\":[{\"region-id\":\"san4a\","
354                 + "\"tenant-id-list\":[\"tenantuuid1\",\"tenantuuid2\"]},{\"region-id\":\"san4b\","
355                 + "\"tenant-id-list\":[\"tenantuuid1\",\"tenantuuid2\"]}]},{\"cloud-owner\":\"nc1.0\","
356                 + "\"region-id-list\":[{\"region-id\":\"san4a\",\"tenant-id-list\":[\"tenantuuid3\","
357                 + "\"tenantuuid4\"]}]}],\"description\":\"fqdn for east zone Production\",\"username\":\"attuid\","
358                 + "\"create-date\":\"\",\"modify-username\":\"\",\"modify-date\":\"\"},"
359                 + "{\"vnf-management-server-fqdn\":\"fqdn-value2 url:port\","
360                 + "\"cloud-owner-list\":[{\"cloud-owner\":\"aic3.0\",\"region-id-list\":[{\"region-id\":\"san4a\","
361                 + "\"tenant-id-list\":[\"tenantuuid5\",\"tenantuuid6\"]},{\"region-id\":\"san4b\","
362                 + "\"tenant-id-list\":[\"tenantuuid5\",\"tenantuuid6\"]}]},{\"cloud-owner\":\"nc1.0\","
363                 + "\"region-id-list\":[{\"region-id\":\"san4a\","
364                 + "\"tenant-id-list\":[\"tenantuuid7\",\"tenantuuid8\"]}]}],"
365                 + "\"description\":\"fqdn for east zone Test\",\"username\":\"attuid\",\"create-date\":\"\","
366                 + "\"modify-username\":\"\",\"modify-date\":\"\"}]}";
367
368         JSONObject documentInfo = new JSONObject();
369         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_CONTENTS, contentStr);
370         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_NAME, "ansible_admin_FQDN_Artifact_0.0.1V.json");
371         artifactHandlerNode.validateAnsibleAdminArtifact(documentInfo);
372     }
373
374     @Test
375     public void testValidateAnsibleAdminArtifactWithException() throws Exception {
376         String contentStrOne = "{\"fqdn-list\":[{\"vnf-management-server-fqdn\":\"fqdn-value1 url:port\","
377                 + "\"cloud-owner-list\":[{\"cloud-owner\":\"aic3.0\",\"region-id-list\":[{\"region-id\":\"san4a\","
378                 + "\"tenant-id-list\":[\"tenantuuid1\",\"tenantuuid2\"]},{\"region-id\":\"san4b\","
379                 + "\"tenant-id-list\":[\"tenantuuid1\",\"tenantuuid2\"]}]},{\"cloud-owner\":\"nc1.0\","
380                 + "\"region-id-list\":[{\"region-id\":\"san4a\","
381                 + "\"tenant-id-list\":[\"tenantuuid3\",\"tenantuuid4\"]}]}],"
382                 + "\"description\":\"fqdn for east zone Production\",\"username\":\"attuid\",\"create-date\":\"\","
383                 + "\"modify-username\":\"\",\"modify-date\":\"\"},"
384                 + "{\"vnf-management-server-fqdn\":\"fqdn-value2 url:port\","
385                 + "\"cloud-owner-list\":[{\"cloud-owner\":\"aic3.0\",\"region-id-list\":[{\"region-id\":\"san4a\","
386                 + "\"tenant-id-list\":[\"tenantuuid1\",\"tenantuuid6\"]},{\"region-id\":\"san4b\","
387                 + "\"tenant-id-list\":[\"tenantuuid5\",\"tenantuuid6\"]}]},{\"cloud-owner\":\"nc1.0\","
388                 + "\"region-id-list\":[{\"region-id\":\"san4a\","
389                 + "\"tenant-id-list\":[\"tenantuuid7\",\"tenantuuid8\"]}]}],"
390                 + "\"description\":\"fqdn for east zone Test\",\"username\":\"attuid\",\"create-date\":\"\","
391                 + "\"modify-username\":\"\",\"modify-date\":\"\"}]}";
392         JSONObject documentInfoOne = new JSONObject();
393         documentInfoOne.put(SdcArtifactHandlerConstants.ARTIFACT_CONTENTS, contentStrOne);
394         documentInfoOne.put(SdcArtifactHandlerConstants.ARTIFACT_NAME, "ansible_admin_FQDN_Artifact_0.0.1V.json");
395
396         try {
397             artifactHandlerNode.validateAnsibleAdminArtifact(documentInfoOne);
398             fail("Missing exception");
399         } catch (ArtifactHandlerInternalException e) {
400             assertTrue(e.getMessage().contains("Validation Failure"));
401         }
402
403     }
404
405     @Test
406     public void testValidateAnsibleAdminArtifactWithJSONException() throws Exception {
407         String contentStrOne =
408                 "{\"fqdn-list\":[{\"vnf-management-server-fqdn\":\"fqdn-value1 url:port\",\"cloud-owner-list\":[{\"cloud-owner\":\"aic3.0\"}";
409
410         JSONObject documentInfoOne = new JSONObject();
411         documentInfoOne.put(SdcArtifactHandlerConstants.ARTIFACT_CONTENTS, contentStrOne);
412         documentInfoOne.put(SdcArtifactHandlerConstants.ARTIFACT_NAME, "ansible_admin_FQDN_Artifact_0.0.1V.json");
413
414         try {
415             artifactHandlerNode.validateAnsibleAdminArtifact(documentInfoOne);
416             fail("Missing exception");
417         } catch (ArtifactHandlerInternalException je) {
418             assertTrue(je.getMessage().contains("JSON Exception"));
419         }
420
421     }
422
423     @Test
424     public void testProcessArtifactWithException() throws Exception {
425         SvcLogicContext ctx = new SvcLogicContext();
426         ctx.setAttribute("test", "test");
427         Map<String, String> inParams = new HashMap<>();
428         JSONObject postData = new JSONObject();
429         JSONObject input = new JSONObject();
430         inParams.put("response_prefix", "prefix");
431         JSONObject requestInfo = new JSONObject();
432         JSONObject documentInfo = new JSONObject();
433         String artifactContent = IOUtils.toString(
434                 ArtifactHandlerProviderUtilTest.class.getClassLoader().getResourceAsStream("templates/reference_template"),
435                 Charset.defaultCharset());
436         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_CONTENTS, artifactContent);
437         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_NAME, "");
438         requestInfo.put("RequestInfo", "testValue");
439         requestInfo.put("request-id", "testREQUEST_ID");
440         input.put(SdcArtifactHandlerConstants.DOCUMENT_PARAMETERS, documentInfo);
441         input.put(SdcArtifactHandlerConstants.REQUEST_INFORMATION, requestInfo);
442         postData.put("input", input);
443         inParams.put("postData", postData.toString());
444         try {
445             artifactHandlerNode.processArtifact(inParams, ctx);
446             fail("Missing exception");
447         } catch (Exception e) {
448             assertTrue(e.getMessage().contains("Missing Artifact Name"));
449         }
450
451     }
452
453     @Test
454     public void testProcessArtifactWithExceptionforAnsible() throws Exception {
455         SvcLogicContext ctx = new SvcLogicContext();
456         ctx.setAttribute("test", "test");
457         Map<String, String> inParams = new HashMap<>();
458         JSONObject postData = new JSONObject();
459         JSONObject input = new JSONObject();
460         inParams.put("response_prefix", "prefix");
461         JSONObject requestInfo = new JSONObject();
462         JSONObject documentInfo = new JSONObject();
463         String artifactContent = IOUtils.toString(
464                 ArtifactHandlerProviderUtilTest.class.getClassLoader().getResourceAsStream("templates/reference_template"),
465                 Charset.defaultCharset());
466         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_CONTENTS, artifactContent);
467         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_NAME, "ansible_admin_FQDN_Artifact_0.0.2V.json");
468         requestInfo.put("RequestInfo", "testValue");
469         requestInfo.put("request-id", "testREQUEST_ID");
470         input.put(SdcArtifactHandlerConstants.DOCUMENT_PARAMETERS, documentInfo);
471         input.put(SdcArtifactHandlerConstants.REQUEST_INFORMATION, requestInfo);
472         postData.put("input", input);
473         inParams.put("postData", postData.toString());
474
475         try {
476             artifactHandlerNode.processArtifact(inParams, ctx);
477             fail("Missing exception");
478         } catch (Exception e) {
479             assertTrue(e.getMessage().contains("JSON Exception:ansible admin"));
480         }
481     }
482
483     @Test
484     public void testProcessAndStoreCapablitiesArtifact() throws Exception {
485         ArtifactHandlerNode ah = new ArtifactHandlerNode();
486         JSONObject capabilities = new JSONObject();
487         JSONObject documentInfo = new JSONObject();
488         MockDBService dbService = MockDBService.initialise();
489         documentInfo.put(SdcArtifactHandlerConstants.SERVICE_UUID, "testuid");
490         documentInfo.put(SdcArtifactHandlerConstants.DISTRIBUTION_ID, "testDist");
491         documentInfo.put(SdcArtifactHandlerConstants.SERVICE_NAME, "testName");
492         documentInfo.put(SdcArtifactHandlerConstants.SERVICE_DESCRIPTION, "testDesc");
493         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_UUID, "testRes");
494         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_INSTANCE_NAME, "testResIns");
495         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_NAME, "testResName");
496         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_VERSION, "testVers");
497         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_TYPE, "testResType");
498         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_UUID, "testArtifactUuid");
499         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_VERSION, "testArtifactVers");
500         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_DESRIPTION, "testArtifactDesc");
501         Whitebox.invokeMethod(ah, "processAndStoreCapabilitiesArtifact", dbService, documentInfo, capabilities,
502                 "artifactName", "someVnf");
503         assertNotNull(ah);
504     }
505
506
507 }