dc57d246e306e518e61c27aefef072fcd3cdfd79
[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  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.artifact.handler.node;
25
26 import org.apache.commons.io.IOUtils;
27 import org.json.JSONArray;
28 import org.json.JSONObject;
29 import org.junit.Before;
30 import org.junit.Ignore;
31 import org.junit.Test;
32 import org.mockito.Mockito;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
34 import org.powermock.reflect.Whitebox;
35 import org.onap.appc.artifact.handler.dbservices.MockDBService;
36 import org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants;
37 import org.onap.appc.artifact.handler.utils.ArtifactHandlerProviderUtilTest;
38 import java.util.Map;
39 import java.util.HashMap;
40 import java.nio.charset.Charset;
41
42 import static org.junit.Assert.assertFalse;
43 import static org.junit.Assert.assertTrue;
44
45 public class ArtifactHandlerNodeTest {
46
47     private ArtifactHandlerNode artifactHandlerNode;
48
49     @Before
50     public void setUp() throws Exception {
51         artifactHandlerNode = Mockito.spy(ArtifactHandlerNode.class);
52         Mockito.doReturn(true)
53             .when(artifactHandlerNode)
54             .updateStoreArtifacts(Mockito.any(JSONObject.class), Mockito.any(JSONObject.class));
55         Mockito.doReturn(true)
56             .when(artifactHandlerNode)
57             .storeReferenceData(Mockito.any(JSONObject.class), Mockito.any(JSONObject.class));
58     }
59
60     @Test
61     public void testProcessArtifact() throws Exception {
62         SvcLogicContext ctx = new SvcLogicContext();
63         ctx.setAttribute("test", "test");
64         Map<String, String> inParams = new HashMap<>();
65         JSONObject postData = new JSONObject();
66         JSONObject input = new JSONObject();
67         inParams.put("response_prefix", "prefix");
68         JSONObject requestInfo = new JSONObject();
69         JSONObject documentInfo = new JSONObject();
70         String artifactContent = IOUtils.toString(ArtifactHandlerProviderUtilTest.class.getClassLoader()
71                 .getResourceAsStream("templates/reference_template"), Charset.defaultCharset());
72         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_CONTENTS, artifactContent);
73         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_NAME, "reference_Junit.json");
74         requestInfo.put("RequestInfo", "testValue");
75         input.put(SdcArtifactHandlerConstants.DOCUMENT_PARAMETERS, documentInfo);
76         input.put(SdcArtifactHandlerConstants.REQUEST_INFORMATION, requestInfo);
77         postData.put("input", input);
78         inParams.put("postData", postData.toString());
79         artifactHandlerNode.processArtifact(inParams, ctx);
80     }
81
82     @Ignore("Test is taking 60 seconds")
83     @Test(expected = Exception.class)
84     public void testStoreReferenceData() throws Exception {
85         JSONObject documentInfo = new JSONObject();
86         String artifactContent = IOUtils.toString(ArtifactHandlerProviderUtilTest.class.getClassLoader()
87                 .getResourceAsStream("templates/reference_template"), Charset.defaultCharset());
88         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_CONTENTS, artifactContent);
89         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_NAME, "reference_Junit.json");
90         JSONObject requestInfo = new JSONObject();
91         requestInfo.put("RequestInfo", "testStoreReferenceData");
92         artifactHandlerNode.storeReferenceData(requestInfo, documentInfo);
93     }
94
95     @Test
96     public void testPopulateProtocolReference() throws Exception {
97         ArtifactHandlerNode ah = new ArtifactHandlerNode();
98         String contentStr =
99                 "{\"action\": \"TestAction\",\"action-level\": \"vnf\",\"scope\": {\"vnf-type\": \"vDBE-I\",\"vnfc-type\": null},\"template\": \"N\",\"device-protocol\": \"REST\"}";
100         JSONObject content = new JSONObject(contentStr);
101         MockDBService dbService = MockDBService.initialise();
102         Whitebox.invokeMethod(ah, "populateProtocolReference", dbService, content);
103     }
104
105     @Test
106     public void testProcessAndStoreCapablitiesArtifact() throws Exception {
107         ArtifactHandlerNode ah = new ArtifactHandlerNode();
108         JSONObject capabilities = new JSONObject();
109         JSONObject documentInfo = new JSONObject();
110         MockDBService dbService = MockDBService.initialise();
111         documentInfo.put(SdcArtifactHandlerConstants.SERVICE_UUID, "testuid");
112         documentInfo.put(SdcArtifactHandlerConstants.DISTRIBUTION_ID, "testDist");
113         documentInfo.put(SdcArtifactHandlerConstants.SERVICE_NAME, "testName");
114         documentInfo.put(SdcArtifactHandlerConstants.SERVICE_DESCRIPTION, "testDesc");
115         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_UUID, "testRes");
116         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_INSTANCE_NAME, "testResIns");
117         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_VERSION, "testVers");
118         documentInfo.put(SdcArtifactHandlerConstants.RESOURCE_TYPE, "testResType");
119         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_UUID, "testArtifactUuid");
120         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_VERSION, "testArtifactVers");
121         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_DESRIPTION, "testArtifactDesc");
122         Whitebox.invokeMethod(ah, "processAndStoreCapabilitiesArtifact", dbService, documentInfo, capabilities,
123                 "artifactName", "someVnf");
124     }
125
126     @Test
127     public void testCleanVnfcInstance() throws Exception {
128         ArtifactHandlerNode ah = new ArtifactHandlerNode();
129         SvcLogicContext ctx = new SvcLogicContext();
130         Whitebox.invokeMethod(ah, "cleanVnfcInstance", ctx);
131         assertTrue(true);
132     }
133
134     @Ignore("Test is taking 60 seconds")
135     @Test(expected = Exception.class)
136     public void testGetArtifactIDException() throws Exception {
137         ArtifactHandlerNode ah = new ArtifactHandlerNode();
138         String yFileName = "yFileName";
139         Whitebox.invokeMethod(ah, "getArtifactID", yFileName);
140     }
141
142     @Ignore("Test is taking 60 seconds")
143     @Test(expected = Exception.class)
144     public void testStoreUpdateSdcArtifacts() throws Exception {
145         ArtifactHandlerNode ah = new ArtifactHandlerNode();
146         String postDataStr =
147                 "{\"request-information\":{},\"document-parameters\":{\"artifact-name\":\"testArtifact\",\"artifact-contents\":{\"content\":\"TestContent\"}}}";
148         JSONObject postData = new JSONObject(postDataStr);
149         Whitebox.invokeMethod(ah, "storeUpdateSdcArtifacts", postData);
150     }
151
152     @Ignore("Test is taking 60 seconds")
153     @Test(expected = Exception.class)
154     public void testUpdateStoreArtifacts() throws Exception {
155         JSONObject documentInfo = new JSONObject();
156         String artifactContent = IOUtils.toString(ArtifactHandlerProviderUtilTest.class.getClassLoader()
157                 .getResourceAsStream("templates/reference_template"), Charset.defaultCharset());
158         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_CONTENTS, artifactContent);
159         documentInfo.put(SdcArtifactHandlerConstants.ARTIFACT_NAME, "reference_Junit.json");
160         JSONObject requestInfo = new JSONObject();
161         requestInfo.put("RequestInfo", "testupdateStoreArtifacts");
162         artifactHandlerNode.updateStoreArtifacts(requestInfo, documentInfo);
163     }
164
165     @Test
166     public void testCleanArtifactInstanceData() throws Exception {
167         SvcLogicContext ctx = new SvcLogicContext();
168         Whitebox.invokeMethod(artifactHandlerNode, "cleanArtifactInstanceData", ctx);
169     }
170
171     @Ignore("Test is taking 60 seconds")
172     @Test(expected = Exception.class)
173     public void testUpdateYangContents() throws Exception {
174         String artifactId = "1";
175         String yangContents = "SomeContent";
176         Whitebox.invokeMethod(artifactHandlerNode, "updateYangContents", artifactId, yangContents);
177     }
178
179     @Test
180     public void testProcessVmList() throws Exception{
181         String contentStr = "{\r\n\t\"action\": \"ConfigScaleOut\",\r\n\t\"action-level\": \"VNF\",\r\n\t\"scope\": "
182                 + "{\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\": "
183                 + "[\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\": "
184                 + "\"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\": "
185                 + "\"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\": "
186                 + "\"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\": "
187                 + "\"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\": "
188                 + "\"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\": "
189                 + "[{\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\": "
190                 + "\"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\": "
191                 + "\"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\": "
192                 + "\"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\": "
193                 + "\"PD_JunitTESTv_parameter_yang.json\",\r\n\t\t\"artifact-type\": \"PD_definations\"\r\n\t}]\r\n}";
194         JSONObject content=new JSONObject(contentStr);
195         MockDBService dbService = MockDBService.initialise();
196         SvcLogicContext context = new SvcLogicContext();
197         artifactHandlerNode.processVmList(content, context, dbService);
198     }
199
200     @Test
201     public void testProcessConfigTypeActions() throws Exception{
202         String contentStr = "{\"action\": \"ConfigScaleOut\"}";
203         JSONObject content=new JSONObject(contentStr);
204         MockDBService dbService = MockDBService.initialise();
205         SvcLogicContext context = new SvcLogicContext();
206         context.setAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL,"Test");
207         artifactHandlerNode.processConfigTypeActions(content, dbService, context);
208     }
209
210     @Test
211     public void testProcessArtifactLists() throws Exception{
212         String contentStr = "{\r\n\t\"action\": \"ConfigScaleOut\",\r\n\t\"action-level\": \"VNF\",\r\n\t\"scope\": "
213                 + "{\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\": "
214                 + "[\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\": "
215                 + "\"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\": "
216                 + "\"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\": "
217                 + "\"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\": "
218                 + "\"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\": "
219                 + "\"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\": "
220                 + "[{\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\": "
221                 + "\"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\": "
222                 + "\"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\": "
223                 + "\"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\": "
224                 + "\"PD_JunitTESTv_parameter_yang.json\",\r\n\t\t\"artifact-type\": \"PD_definations\"\r\n\t}]\r\n}";
225         JSONObject content=new JSONObject(contentStr);
226         MockDBService dbService = MockDBService.initialise();
227         SvcLogicContext context = new SvcLogicContext();
228         artifactHandlerNode.processArtifactList(content,dbService,context);
229      }
230
231     @Test
232     public void testProcessActionLists() throws Exception {
233         String contentStr = "{\r\n\t\"action\": \"HealthCheck\",\r\n\t\"action-level\": \"vm\",\r\n\t\"scope\":"
234                 + " {\r\n\t\t\"vnf-type\": \"vDBE-I\",\r\n\t\t\"vnfc-type\": null\r\n\t},\r\n\t\"template\": "
235                 + "\"N\",\r\n\t\"device-protocol\": \"REST\",\r\n\t\"vnfc-function-code-list\": [\"SSC\", \"MMSC\"]\r\n}";
236         JSONObject content = new JSONObject(contentStr);
237         JSONArray vmActionVnfcFunctionCodesList = new JSONArray();
238         JSONArray vnfActionList = new JSONArray();
239         JSONArray vfModuleActionList = new JSONArray();
240         JSONArray vnfcActionList = new JSONArray();
241         String[] actionLevels = { "vnf", "vm", "vf-module", "vnfc" };
242         for (String actionLevel : actionLevels) {
243             artifactHandlerNode.processActionLists(content, actionLevel, vnfcActionList, vfModuleActionList,
244                     vnfActionList, vmActionVnfcFunctionCodesList);
245         }
246     }
247
248     @Test
249     public void testIsCapabilityArtifactNeeded() throws Exception {
250         String scopeObjStr1= "{\"vnf-type\":\"someVnf\",\"vnfc-type\":\"somVnfc\"}";
251         String scopeObjStr2= "{\"vnf-type\":\"someVnf\",\"vnfc-type\":\"\"}";
252         JSONObject scope1 = new JSONObject(scopeObjStr1);
253         JSONObject scope2 = new JSONObject(scopeObjStr2);
254         SvcLogicContext context = new SvcLogicContext();
255         assertFalse(artifactHandlerNode.isCapabilityArtifactNeeded(scope1, context));
256         assertTrue(artifactHandlerNode.isCapabilityArtifactNeeded(scope2, context));
257     }
258
259     @Test
260     public void testProcessArtifactListsWithMultipleTemplatesThrowsExceptionOnFailure() throws Exception {
261         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\": "
262                 + "\"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\": "
263                 + "\"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\": "
264                 + "\"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\":"
265                 + " \"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\": "
266                 + "\"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\": "
267                 + "\"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\":"
268                 + " \"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\","
269                 + "\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\","
270                 + "\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\","
271                 + "\r\n\t\t\t\"artifact-type\": \"parameter_definitions\"\r\n\t\t},\r\n\t\t{\r\n\t\t\t\"artifact-name\": "
272                 + "\"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\": "
273                 + "\"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\":"
274                 + " [\"TID-0405-EZ\",\r\n\t\t\"TID-0405-EZ-2\"],\r\n\t\t\"scopeType\": \"vnf-type\"\r\n\t}";
275         JSONObject content = new JSONObject(contentStr);
276         MockDBService dbService = MockDBService.initialise();
277         SvcLogicContext context = new SvcLogicContext();
278         context.setAttribute("vnf-type", "someVnf");
279         context.setAttribute("action", "ConfigScaleOut");
280         artifactHandlerNode.processArtifactList(content, dbService, context);;
281
282     }
283
284 }