5707aaa8df6e6c8355f67c8f499427e7e0f10892
[appc.git] / appc-config / appc-data-services / provider / src / test / java / org / onap / appc / data / services / node / ConfigResourceNodeTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2018 IBM.
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.data.services.node;
25
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.mockito.Matchers.anyString;
29 import static org.mockito.Matchers.eq;
30 import static org.mockito.Mockito.atLeastOnce;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.spy;
33 import static org.mockito.Mockito.verify;
34 import static org.mockito.Mockito.when;
35 import static org.onap.appc.data.services.node.ConfigResourceNode.CONFIG_FILES_PREFIX;
36 import static org.onap.appc.data.services.node.ConfigResourceNode.CONFIG_FILE_ID_PARAM;
37 import static org.onap.appc.data.services.node.ConfigResourceNode.CONFIG_PARAMS;
38 import static org.onap.appc.data.services.node.ConfigResourceNode.CONF_ACTION_PREFIX;
39 import static org.onap.appc.data.services.node.ConfigResourceNode.DEVICE_CONF_FILE_TYPE;
40 import static org.onap.appc.data.services.node.ConfigResourceNode.DEVICE_CONF_PREFIX;
41 import static org.onap.appc.data.services.node.ConfigResourceNode.DEVICE_PROTOCOL_PREFIX;
42 import static org.onap.appc.data.services.node.ConfigResourceNode.FAILURE_FILE_TYPE;
43 import static org.onap.appc.data.services.node.ConfigResourceNode.FAILURE_PREFIX;
44 import static org.onap.appc.data.services.node.ConfigResourceNode.FILE_CATEGORY_PARAM;
45 import static org.onap.appc.data.services.node.ConfigResourceNode.LOG_FILE_TYPE;
46 import static org.onap.appc.data.services.node.ConfigResourceNode.LOG_PREFIX;
47 import static org.onap.appc.data.services.node.ConfigResourceNode.MAX_CONF_FILE_PREFIX;
48 import static org.onap.appc.data.services.node.ConfigResourceNode.PREPARE_RELATIONSHIP_PARAM;
49 import static org.onap.appc.data.services.node.ConfigResourceNode.SDC_IND;
50 import static org.onap.appc.data.services.node.ConfigResourceNode.SITE_LOCATION_PARAM;
51 import static org.onap.appc.data.services.node.ConfigResourceNode.SUCCESS_FILE_TYPE;
52 import static org.onap.appc.data.services.node.ConfigResourceNode.SUCCESS_PREFIX;
53 import static org.onap.appc.data.services.node.ConfigResourceNode.TMP_CONVERTCONFIG_ESC_DATA;
54 import static org.onap.appc.data.services.node.ConfigResourceNode.TMP_MERGE_MERGED_DATA;
55 import static org.onap.appc.data.services.node.ConfigResourceNode.UNABLE_TO_SAVE_RELATIONSHIP_STR;
56 import static org.onap.appc.data.services.node.ConfigResourceNode.UPLOAD_CONFIG_ID_PARAM;
57 import static org.onap.appc.data.services.node.ConfigResourceNode.UPLOAD_CONFIG_INFO_PREFIX;
58 import static org.onap.appc.data.services.node.ConfigResourceNode.UPLOAD_CONFIG_PREFIX;
59 import com.fasterxml.jackson.databind.JsonNode;
60 import com.fasterxml.jackson.databind.ObjectMapper;
61 import com.fasterxml.jackson.databind.node.JsonNodeFactory;
62 import java.util.HashMap;
63 import org.junit.Before;
64 import org.junit.Rule;
65 import org.junit.Test;
66 import org.junit.rules.ExpectedException;
67 import org.onap.appc.data.services.AppcDataServiceConstant;
68 import org.onap.appc.data.services.db.DGGeneralDBService;
69 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
70 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
71 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
72 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
73
74 public class ConfigResourceNodeTest {
75
76     private HashMap<String, String> inParams;
77     private SvcLogicContext contextMock;
78
79     @Rule
80     public ExpectedException expectedException = ExpectedException.none();
81
82     @Before
83     public void setUp() {
84         contextMock = mock(SvcLogicContext.class);
85         inParams = new HashMap<>();
86     }
87
88     /**
89      * Example data:
90      * <p>
91      * {"capabilities":{"vnfc":[],"vm":[{"ConfigureTest":["SSC","MMSC"]}],"vf-module":[],"vnf":["ConfigModify","HealthCheck"]}}
92      */
93     @Test
94     public void shouldProcessCapabilitiesForVMLevel() throws Exception {
95         SvcLogicContext ctx = new SvcLogicContext();
96         ConfigResourceNode node = new ConfigResourceNode(DGGeneralDBService.initialise());
97         String findCapability = "Restart";
98         JsonNodeFactory.instance.objectNode();
99         String subCaps =
100                 "[{\"Restart\":[\"SSC\",\"MMC\"]},{\"Rebuild\":[\"SSC\"]},{\"Migrate\":[\"SSC\"]},{\"Snapshot\":[\"SSC\"]},{\"Start\":[\"SSC\"]},{\"Stop\":[\"SSC\"]}]";
101         ObjectMapper m = new ObjectMapper();
102         JsonNode subCapabilities = m.readTree(subCaps);
103         String vServerId = "testServer";
104         ctx.setAttribute("tmp.vnfInfo.vm.vnfc.vnfc-function-code", "MMC");
105         ctx.setAttribute("tmp.vnfInfo.vm.vnfc.vnfc-name", "testVnfc");
106         node.processCapabilitiesForVMLevel(vServerId, ctx, findCapability, subCapabilities);
107         String result = ctx.getAttribute("capabilities");
108         assertEquals(result, "Supported");
109     }
110
111     @Test
112     public void shouldCheckIfCapabilityCheckNeeded() {
113         ConfigResourceNode node = new ConfigResourceNode(DGGeneralDBService.initialise());
114         String findCapability = "Start";
115         String capLevel = "vnf";
116         boolean result = node.checkIfCapabilityCheckNeeded(capLevel, findCapability);
117         assertFalse(result);
118     }
119
120     @Test
121     public void should_add_attribute_with_success_if_get_config_file_succeed() throws SvcLogicException {
122         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder().build();
123
124         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
125         configResourceNode.getConfigFileReference(inParams, contextMock);
126
127         verify(contextMock).setAttribute(anyString(), eq(AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS));
128     }
129
130     @Test
131     public void should_add_attribute_with_success_if_get_config_info_succeed() throws SvcLogicException {
132         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder().build();
133
134         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
135         configResourceNode.getCommonConfigInfo(inParams, contextMock);
136
137         verify(contextMock).setAttribute(anyString(), eq(AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS));
138     }
139
140     @Test
141     public void should_add_attribute_with_success_if_get_template_succeed() throws SvcLogicException {
142         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder().build();
143
144         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
145         configResourceNode.getTemplate(inParams, contextMock);
146         verify(contextMock).setAttribute(anyString(), eq(AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS));
147     }
148     
149     @Test
150     public void testGetVnfcReference() throws Exception {
151         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder().build();
152         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
153         contextMock.setAttribute("template-model-id", "testModelId");
154         contextMock.setAttribute("vnfc-type", "testVnfcType");
155         configResourceNode.getVnfcReference(inParams, contextMock);
156         verify(contextMock).setAttribute(anyString(), eq(AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS));
157     }
158     
159     @Test
160     public void testGetCapability() throws Exception {
161         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder().build();
162         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
163         contextMock.setAttribute("responsePrefix", "testResponsePrefix");
164         contextMock.setAttribute("caplevel", "testCapLevel");
165         contextMock.setAttribute("checkCapability", "testCheckCapability");
166         contextMock.setAttribute("vServerId", "testVServerId");
167         contextMock.setAttribute("vnf-type", "testVnfType");
168         configResourceNode.getCapability(inParams, contextMock);
169         verify(contextMock).setAttribute(anyString(), eq(AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS));
170     }
171     
172     @Test
173     public void testGetConfigFilesByVnfVmNCategory() throws Exception {
174         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder().build();
175         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
176         contextMock.setAttribute("responsePrefix", "testResponsePrefix");
177         contextMock.setAttribute("fileCategory", "testFileCategory");
178         contextMock.setAttribute("vnfId", "testVnfId");
179         contextMock.setAttribute("vmName", "testVmName");
180         configResourceNode.getConfigFilesByVnfVmNCategory(inParams, contextMock);
181         verify(contextMock).setAttribute(anyString(), eq(AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS));
182     }
183
184     @Test
185     public void should_add_attribute_with_success_if_save_config_files_succeed() throws SvcLogicException {
186         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder().build();
187
188         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
189         configResourceNode.saveConfigFiles(inParams, contextMock);
190
191         verify(contextMock).setAttribute(anyString(), eq(AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS));
192     }
193
194     @Test
195     public void should_add_attribute_with_success_if_update_upload_config_succeed() throws SvcLogicException {
196         when(contextMock.getAttribute(UPLOAD_CONFIG_ID_PARAM)).thenReturn("1234");
197
198         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder().build();
199
200         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
201         configResourceNode.updateUploadConfig(inParams, contextMock);
202
203         verify(contextMock).setAttribute(anyString(), eq(AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS));
204     }
205
206     @Test
207     public void should_add_attribute_with_success_if_get_download_config_template_by_vnf_type_succeed()
208             throws SvcLogicException {
209         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder().build();
210
211         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
212         configResourceNode.getDownloadConfigTemplateByVnf(inParams, contextMock);
213
214         verify(contextMock).setAttribute(anyString(), eq(AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS));
215     }
216
217     @Test
218     public void should_add_attribute_with_success_if_get_ssm_chain_succeed() throws SvcLogicException {
219         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder().build();
220
221         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
222         configResourceNode.getSmmChainKeyFiles(inParams, contextMock);
223
224         verify(contextMock).setAttribute(anyString(), eq(AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS));
225     }
226
227     @Test
228     public void should_add_attribute_with_success_if_save_prepare_relationship_succeed() throws SvcLogicException {
229         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder().build();
230
231         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
232         configResourceNode.savePrepareRelationship(inParams, contextMock);
233
234         verify(contextMock).setAttribute(anyString(), eq(AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS));
235     }
236
237     @Test
238     public void should_throw_exception_on_device_config_missing() throws Exception {
239         DGGeneralDBService dbServiceMock =
240                 new MockDbServiceBuilder().getConfigFileReferenceByFileTypeNVnfType(DEVICE_CONF_PREFIX,
241                         DEVICE_CONF_FILE_TYPE, SvcLogicResource.QueryStatus.NOT_FOUND).build();
242
243         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
244
245         expectedException.expect(SvcLogicException.class);
246         expectedException.expectMessage("Unable to Read ConfigFileReference:device-configuration");
247         configResourceNode.getConfigFileReference(inParams, contextMock);
248     }
249
250     @Test
251     public void should_throw_exception_on_device_config_failure() throws Exception {
252         DGGeneralDBService dbServiceMock =
253                 new MockDbServiceBuilder().getConfigFileReferenceByFileTypeNVnfType(DEVICE_CONF_PREFIX,
254                         DEVICE_CONF_FILE_TYPE, SvcLogicResource.QueryStatus.FAILURE).build();
255
256         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
257
258         expectedException.expect(SvcLogicException.class);
259         expectedException.expectMessage("Unable to Read ConfigFileReference:device-configuration");
260         configResourceNode.getConfigFileReference(inParams, contextMock);
261     }
262
263     @Test
264     public void should_throw_exception_on_success_param_missing() throws Exception {
265
266         DGGeneralDBService dbServiceMock =
267                 new MockDbServiceBuilder().getConfigFileReferenceByFileTypeNVnfType(SUCCESS_PREFIX, SUCCESS_FILE_TYPE,
268                         SvcLogicResource.QueryStatus.NOT_FOUND).build();
269
270         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
271
272         expectedException.expect(SvcLogicException.class);
273         expectedException.expectMessage("Unable to Read ConfigFileReference:configuration_success");
274         configResourceNode.getConfigFileReference(inParams, contextMock);
275     }
276
277     @Test
278     public void should_throw_exception_on_success_param_failure() throws Exception {
279         DGGeneralDBService dbServiceMock =
280                 new MockDbServiceBuilder().getConfigFileReferenceByFileTypeNVnfType(SUCCESS_PREFIX, SUCCESS_FILE_TYPE,
281                         SvcLogicResource.QueryStatus.FAILURE).build();
282
283         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
284
285         expectedException.expect(SvcLogicException.class);
286         expectedException.expectMessage("Unable to Read ConfigFileReference:configuration_success");
287         configResourceNode.getConfigFileReference(inParams, contextMock);
288     }
289
290     @Test
291     public void should_throw_exception_on_failure_param_missing() throws Exception {
292
293         DGGeneralDBService dbServiceMock =
294                 new MockDbServiceBuilder().getConfigFileReferenceByFileTypeNVnfType(FAILURE_PREFIX, FAILURE_FILE_TYPE,
295                         SvcLogicResource.QueryStatus.NOT_FOUND).build();
296
297         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
298
299         expectedException.expect(SvcLogicException.class);
300         expectedException.expectMessage("Unable to Read ConfigFileReference:configuration_error");
301         configResourceNode.getConfigFileReference(inParams, contextMock);
302     }
303
304     @Test
305     public void should_throw_exception_on_failure_param_failure() throws Exception {
306         DGGeneralDBService dbServiceMock =
307                 new MockDbServiceBuilder().getConfigFileReferenceByFileTypeNVnfType(FAILURE_PREFIX, FAILURE_FILE_TYPE,
308                         SvcLogicResource.QueryStatus.FAILURE).build();
309
310         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
311
312         expectedException.expect(SvcLogicException.class);
313         expectedException.expectMessage("Unable to Read ConfigFileReference:configuration_error");
314         configResourceNode.getConfigFileReference(inParams, contextMock);
315     }
316
317     @Test
318     public void should_throw_exception_on_log_param_missing() throws Exception {
319
320         DGGeneralDBService dbServiceMock =
321                 new MockDbServiceBuilder().getConfigFileReferenceByFileTypeNVnfType(LOG_PREFIX, LOG_FILE_TYPE,
322                         SvcLogicResource.QueryStatus.NOT_FOUND).build();
323
324         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
325
326         expectedException.expect(SvcLogicException.class);
327         expectedException.expectMessage("Unable to Read ConfigFileReference:configuration_log");
328         configResourceNode.getConfigFileReference(inParams, contextMock);
329     }
330
331     @Test
332     public void should_throw_exception_on_log_param_failure() throws Exception {
333         DGGeneralDBService dbServiceMock =
334                 new MockDbServiceBuilder().getConfigFileReferenceByFileTypeNVnfType(LOG_PREFIX, LOG_FILE_TYPE,
335                         SvcLogicResource.QueryStatus.FAILURE).build();
336
337         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
338
339         expectedException.expect(SvcLogicException.class);
340         expectedException.expectMessage("Unable to Read ConfigFileReference:configuration_log");
341         configResourceNode.getConfigFileReference(inParams, contextMock);
342     }
343
344     @Test
345     public void should_throw_exception_on_device_protocol_missing() throws SvcLogicException {
346         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
347                 .getDeviceProtocolByVnfType(DEVICE_PROTOCOL_PREFIX, SvcLogicResource.QueryStatus.NOT_FOUND).build();
348
349         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
350
351         expectedException.expect(SvcLogicException.class);
352         expectedException.expectMessage("Unable to Read device_interface_protocol");
353         configResourceNode.getCommonConfigInfo(inParams, contextMock);
354     }
355
356     @Test
357     public void should_throw_exception_on_device_protocol_failure() throws SvcLogicException {
358         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
359                 .getDeviceProtocolByVnfType(DEVICE_PROTOCOL_PREFIX, SvcLogicResource.QueryStatus.FAILURE).build();
360
361         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
362
363         expectedException.expect(SvcLogicException.class);
364         expectedException.expectMessage("Unable to Read device_interface_protocol");
365         configResourceNode.getCommonConfigInfo(inParams, contextMock);
366     }
367
368     @Test
369     public void should_throw_exception_on_conf_action_by_vnf_action_failure() throws SvcLogicException {
370         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
371                 .getConfigureActionDGByVnfTypeNAction(CONF_ACTION_PREFIX, SvcLogicResource.QueryStatus.FAILURE).build();
372
373         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
374
375         expectedException.expect(SvcLogicException.class);
376         expectedException.expectMessage("Unable to Read configure_action_dg");
377         configResourceNode.getCommonConfigInfo(inParams, contextMock);
378     }
379
380     @Test
381     public void should_throw_exception_on_conf_action_missing() throws SvcLogicException {
382         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
383                 .getConfigureActionDGByVnfTypeNAction(CONF_ACTION_PREFIX, SvcLogicResource.QueryStatus.NOT_FOUND)
384                 .getConfigureActionDGByVnfType(CONF_ACTION_PREFIX, SvcLogicResource.QueryStatus.NOT_FOUND).build();
385
386         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
387
388         expectedException.expect(SvcLogicException.class);
389         expectedException.expectMessage("Unable to Read configure_action_dg");
390         configResourceNode.getCommonConfigInfo(inParams, contextMock);
391     }
392
393     @Test
394     public void should_throw_exception_on_conf_action_failure() throws SvcLogicException {
395         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
396                 .getConfigureActionDGByVnfTypeNAction(CONF_ACTION_PREFIX, SvcLogicResource.QueryStatus.NOT_FOUND)
397                 .getConfigureActionDGByVnfType(CONF_ACTION_PREFIX, SvcLogicResource.QueryStatus.FAILURE).build();
398
399         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
400
401         expectedException.expect(SvcLogicException.class);
402         expectedException.expectMessage("Unable to Read configure_action_dg");
403         configResourceNode.getCommonConfigInfo(inParams, contextMock);
404     }
405
406     @Test
407     public void should_throw_exception_on_db_template_failure() throws SvcLogicException {
408         inParams.put(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX, "some prefix");
409         inParams.put(AppcDataServiceConstant.INPUT_PARAM_FILE_CATEGORY, "some file category");
410
411         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
412                 .getTemplate("some prefix", "some file category", SvcLogicResource.QueryStatus.FAILURE).build();
413
414         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
415
416         expectedException.expect(SvcLogicException.class);
417         expectedException.expectMessage("Unable to Read some file category");
418         configResourceNode.getTemplate(inParams, contextMock);
419     }
420
421     @Test
422     public void should_throw_exception_on_db_template_by_action_failure() throws SvcLogicException {
423         inParams.put(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX, "some prefix");
424         inParams.put(AppcDataServiceConstant.INPUT_PARAM_FILE_CATEGORY, "some file category");
425
426         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
427                 .getTemplate("some prefix", "some file category", SvcLogicResource.QueryStatus.NOT_FOUND)
428                 .getTemplateByVnfTypeNAction("some prefix", "some file category", SvcLogicResource.QueryStatus.FAILURE)
429                 .build();
430
431         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
432
433         expectedException.expect(SvcLogicException.class);
434         expectedException.expectMessage("Unable to Read some file category");
435         configResourceNode.getTemplate(inParams, contextMock);
436     }
437
438     @Test
439     public void should_throw_exception_on_db_template_by_action_missing() throws SvcLogicException {
440         inParams.put(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX, "some prefix");
441         inParams.put(AppcDataServiceConstant.INPUT_PARAM_FILE_CATEGORY, "some file category");
442
443         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
444                 .getTemplate("some prefix", "some file category", SvcLogicResource.QueryStatus.NOT_FOUND)
445                 .getTemplateByVnfTypeNAction("some prefix", "some file category",
446                         SvcLogicResource.QueryStatus.NOT_FOUND)
447                 .build();
448
449         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
450
451         expectedException.expect(SvcLogicException.class);
452         expectedException.expectMessage("Unable to Read some file category");
453         configResourceNode.getTemplate(inParams, contextMock);
454     }
455
456     @Test
457     public void should_throw_exception_on_db_template_by_name_missing() throws SvcLogicException {
458         inParams.put(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX, "some prefix");
459         inParams.put(AppcDataServiceConstant.INPUT_PARAM_FILE_CATEGORY, "some file category");
460
461         SvcLogicContext context = new SvcLogicContext();
462         context.setAttribute("template-name", "test template");
463
464         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
465                 .getTemplateByTemplateName("some prefix", "test template", SvcLogicResource.QueryStatus.NOT_FOUND)
466                 .build();
467
468         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
469
470         expectedException.expect(SvcLogicException.class);
471         expectedException.expectMessage("Unable to Read some file category template");
472         configResourceNode.getTemplate(inParams, context);
473     }
474
475     @Test
476     public void should_throw_exception_on_db_template_by_name_failure() throws SvcLogicException {
477         inParams.put(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX, "some prefix");
478         inParams.put(AppcDataServiceConstant.INPUT_PARAM_FILE_CATEGORY, "some file category");
479
480         SvcLogicContext context = new SvcLogicContext();
481         context.setAttribute("template-name", "test template");
482
483         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
484                 .getTemplateByTemplateName("some prefix", "test template", SvcLogicResource.QueryStatus.FAILURE)
485                 .build();
486
487         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
488
489         expectedException.expect(SvcLogicException.class);
490         expectedException.expectMessage("Unable to Read some file category template");
491         configResourceNode.getTemplate(inParams, context);
492     }
493
494     @Test
495     public void should_throw_exception_on_save_config_failure() throws SvcLogicException {
496         SvcLogicContext context = new SvcLogicContext();
497         context.setAttribute(FILE_CATEGORY_PARAM, "some file category");
498
499         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
500                 .saveConfigFiles(CONFIG_FILES_PREFIX, SvcLogicResource.QueryStatus.FAILURE).build();
501
502         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
503
504         expectedException.expect(SvcLogicException.class);
505         expectedException.expectMessage("Unable to Save some file category in configfiles");
506         configResourceNode.saveConfigFiles(inParams, context);
507     }
508
509     @Test
510     public void should_throw_exception_on_get_max_config_id_missing() throws SvcLogicException {
511         SvcLogicContext context = new SvcLogicContext();
512         context.setAttribute(FILE_CATEGORY_PARAM, "some file category");
513
514         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
515                 .getMaxConfigFileId(MAX_CONF_FILE_PREFIX, "some file category", SvcLogicResource.QueryStatus.NOT_FOUND)
516                 .build();
517
518         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
519
520         expectedException.expect(SvcLogicException.class);
521         expectedException.expectMessage("Unable to get some file category from configfiles");
522
523         configResourceNode.saveConfigFiles(inParams, context);
524     }
525
526     @Test
527     public void should_throw_exception_on_save_config_files_failure() throws SvcLogicException {
528         SvcLogicContext context = new SvcLogicContext();
529         context.setAttribute(CONFIG_FILE_ID_PARAM, "some file id");
530
531         DGGeneralDBService dbServiceMock =
532                 new MockDbServiceBuilder().savePrepareRelationship(PREPARE_RELATIONSHIP_PARAM, "some file id", SDC_IND,
533                         SvcLogicResource.QueryStatus.FAILURE).build();
534
535         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
536
537         expectedException.expect(SvcLogicException.class);
538         expectedException.expectMessage("Unable to save prepare_relationship");
539         configResourceNode.saveConfigFiles(inParams, context);
540     }
541
542     @Test
543     public void should_throw_exception_on_save_upload_config_failure() throws SvcLogicException {
544         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
545                 .saveUploadConfig(UPLOAD_CONFIG_PREFIX, SvcLogicResource.QueryStatus.FAILURE).build();
546
547         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
548
549         expectedException.expect(SvcLogicException.class);
550         expectedException.expectMessage("Unable to Save configuration in upload_config");
551         configResourceNode.updateUploadConfig(inParams, contextMock);
552     }
553
554     @Test
555     public void should_throw_exception_on_get_upload_config_failure() throws SvcLogicException {
556         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
557                 .getUploadConfigInfo(UPLOAD_CONFIG_INFO_PREFIX, SvcLogicResource.QueryStatus.FAILURE).build();
558
559         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
560
561         expectedException.expect(SvcLogicException.class);
562         expectedException.expectMessage("Unable to get record from upload_config");
563         configResourceNode.updateUploadConfig(inParams, contextMock);
564     }
565
566     @Test
567     public void should_throw_exception_on_get_upload_config_missing() throws SvcLogicException {
568         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
569                 .getUploadConfigInfo(UPLOAD_CONFIG_INFO_PREFIX, SvcLogicResource.QueryStatus.NOT_FOUND).build();
570
571         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
572
573         expectedException.expect(SvcLogicException.class);
574         expectedException.expectMessage("Unable to get record from upload_config");
575         configResourceNode.updateUploadConfig(inParams, contextMock);
576     }
577
578     @Test
579     public void should_throw_exception_on_update_upload_config_failure() throws SvcLogicException {
580         when(contextMock.getAttribute(UPLOAD_CONFIG_ID_PARAM)).thenReturn("1234");
581
582         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
583                 .updateUploadConfig(UPLOAD_CONFIG_PREFIX, 1234, SvcLogicResource.QueryStatus.FAILURE).build();
584
585         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
586
587         expectedException.expect(SvcLogicException.class);
588         expectedException.expectMessage("Unable to upload upload_config");
589         configResourceNode.updateUploadConfig(inParams, contextMock);
590     }
591
592     @Test
593     public void should_throw_exception_on_get_download_config_failure() throws SvcLogicException {
594         inParams.put(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX, "some prefix");
595
596         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
597                 .getDownloadConfigTemplateByVnf("some prefix", SvcLogicResource.QueryStatus.FAILURE).build();
598
599         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
600
601         expectedException.expect(SvcLogicException.class);
602         expectedException.expectMessage("Unable to get download config template.");
603         configResourceNode.getDownloadConfigTemplateByVnf(inParams, contextMock);
604     }
605
606     @Test
607     public void should_throw_exception_on_get_ssm_chain_failure() throws SvcLogicException {
608         when(contextMock.getAttribute(SITE_LOCATION_PARAM)).thenReturn("some location");
609
610         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
611                 .getTemplateByArtifactType("smm", "smm", "some location", SvcLogicResource.QueryStatus.FAILURE).build();
612
613         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
614
615         expectedException.expect(SvcLogicException.class);
616         expectedException.expectMessage("Unable to Read smm file");
617         configResourceNode.getSmmChainKeyFiles(inParams, contextMock);
618     }
619
620     @Test
621     public void should_throw_exception_on_get_ca_chain_failure() throws SvcLogicException {
622         when(contextMock.getAttribute(SITE_LOCATION_PARAM)).thenReturn("some location");
623
624         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder().getTemplateByArtifactType("intermediate-ca-chain",
625                 "intermediate_ca_chain", "some location", SvcLogicResource.QueryStatus.FAILURE).build();
626
627         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
628
629         expectedException.expect(SvcLogicException.class);
630         expectedException.expectMessage("Unable to Read intermediate_ca_chain file");
631         configResourceNode.getSmmChainKeyFiles(inParams, contextMock);
632     }
633
634     @Test
635     public void should_throw_exception_on_get_server_certificate_and_key_failure() throws SvcLogicException {
636         when(contextMock.getAttribute(SITE_LOCATION_PARAM)).thenReturn("some location");
637
638         DGGeneralDBService dbServiceMock =
639                 new MockDbServiceBuilder().getTemplateByArtifactType("server-certificate-and-key",
640                         "server_certificate_and_key", "some location", SvcLogicResource.QueryStatus.FAILURE).build();
641
642         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
643
644         expectedException.expect(SvcLogicException.class);
645         expectedException.expectMessage("Unable to Read server_certificate_and_key file");
646         configResourceNode.getSmmChainKeyFiles(inParams, contextMock);
647     }
648
649     @Test
650     public void should_throw_exception_on_save_prepare_relationship_failure() throws SvcLogicException {
651         inParams.put(AppcDataServiceConstant.INPUT_PARAM_SDC_ARTIFACT_IND, "some sdnc index");
652         inParams.put(AppcDataServiceConstant.INPUT_PARAM_FILE_ID, "some file id");
653
654         DGGeneralDBService dbServiceMock =
655                 new MockDbServiceBuilder().savePrepareRelationship(PREPARE_RELATIONSHIP_PARAM, "some file id",
656                         "some sdnc index", QueryStatus.FAILURE).build();
657
658         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
659
660         expectedException.expect(SvcLogicException.class);
661         expectedException.expectMessage(UNABLE_TO_SAVE_RELATIONSHIP_STR);
662         configResourceNode.savePrepareRelationship(inParams, contextMock);
663     }
664
665     @Test
666     public void should_save_save_config_files_for_empty_config_params() throws SvcLogicException {
667
668         when(contextMock.getAttribute(TMP_CONVERTCONFIG_ESC_DATA)).thenReturn("some esc data");
669         when(contextMock.getAttribute("configuration")).thenReturn("some configuration");
670         when(contextMock.getAttribute(CONFIG_PARAMS)).thenReturn("");
671
672         DGGeneralDBService dbServiceMock =
673                 new MockDbServiceBuilder().savePrepareRelationship(PREPARE_RELATIONSHIP_PARAM, "some file id",
674                         "some sdnc index", QueryStatus.FAILURE).build();
675
676         ConfigResourceNode configResourceNode = spy(new ConfigResourceNode(dbServiceMock));
677         configResourceNode.saveConfigBlock(inParams, contextMock);
678
679         verify(configResourceNode).saveDeviceConfiguration(inParams, contextMock, "Request", "some esc data",
680                 "some configuration");
681         verify(contextMock, atLeastOnce()).setAttribute(anyString(), eq(AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS));
682     }
683
684     @Test
685     public void should_save_save_config_files_for_non_empty_config_params() throws SvcLogicException {
686
687         when(contextMock.getAttribute(TMP_CONVERTCONFIG_ESC_DATA)).thenReturn("some esc data");
688         when(contextMock.getAttribute(TMP_MERGE_MERGED_DATA)).thenReturn("merged data");
689         when(contextMock.getAttribute(CONFIG_PARAMS)).thenReturn("non empty");
690
691         DGGeneralDBService dbServiceMock =
692                 new MockDbServiceBuilder().savePrepareRelationship(PREPARE_RELATIONSHIP_PARAM, "some file id",
693                         "some sdnc index", QueryStatus.FAILURE).build();
694
695         ConfigResourceNode configResourceNode = spy(new ConfigResourceNode(dbServiceMock));
696         configResourceNode.saveConfigBlock(inParams, contextMock);
697
698         verify(configResourceNode).saveDeviceConfiguration(inParams, contextMock, "Configurator", "some esc data",
699                 "merged data");
700         verify(configResourceNode).saveConfigBlock(inParams, contextMock);
701         verify(contextMock, atLeastOnce()).setAttribute(anyString(), eq(AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS));
702     }
703     
704     @Test
705     public void testSaveTemplateConfig() throws SvcLogicException
706     {
707         SvcLogicContext context = new SvcLogicContext();
708         context.setAttribute(CONFIG_FILE_ID_PARAM, "some file id");
709
710         DGGeneralDBService dbServiceMock =
711                 new MockDbServiceBuilder().savePrepareRelationship(PREPARE_RELATIONSHIP_PARAM, "some file id", SDC_IND,
712                         SvcLogicResource.QueryStatus.FAILURE).build();
713
714         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
715         configResourceNode.saveTemplateConfig(inParams, contextMock);
716
717         verify(contextMock, atLeastOnce()).setAttribute(anyString(), eq(AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS));
718     }
719 }