added test case to ConfigResourceNodeTest.java
[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  * Modification 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 should_add_attribute_with_success_if_save_config_files_succeed() throws SvcLogicException {
161         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder().build();
162
163         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
164         configResourceNode.saveConfigFiles(inParams, contextMock);
165
166         verify(contextMock).setAttribute(anyString(), eq(AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS));
167     }
168
169     @Test
170     public void should_add_attribute_with_success_if_update_upload_config_succeed() throws SvcLogicException {
171         when(contextMock.getAttribute(UPLOAD_CONFIG_ID_PARAM)).thenReturn("1234");
172
173         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder().build();
174
175         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
176         configResourceNode.updateUploadConfig(inParams, contextMock);
177
178         verify(contextMock).setAttribute(anyString(), eq(AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS));
179     }
180
181     @Test
182     public void should_add_attribute_with_success_if_get_download_config_template_by_vnf_type_succeed()
183             throws SvcLogicException {
184         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder().build();
185
186         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
187         configResourceNode.getDownloadConfigTemplateByVnf(inParams, contextMock);
188
189         verify(contextMock).setAttribute(anyString(), eq(AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS));
190     }
191
192     @Test
193     public void should_add_attribute_with_success_if_get_ssm_chain_succeed() throws SvcLogicException {
194         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder().build();
195
196         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
197         configResourceNode.getSmmChainKeyFiles(inParams, contextMock);
198
199         verify(contextMock).setAttribute(anyString(), eq(AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS));
200     }
201
202     @Test
203     public void should_add_attribute_with_success_if_save_prepare_relationship_succeed() throws SvcLogicException {
204         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder().build();
205
206         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
207         configResourceNode.savePrepareRelationship(inParams, contextMock);
208
209         verify(contextMock).setAttribute(anyString(), eq(AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS));
210     }
211
212     @Test
213     public void should_throw_exception_on_device_config_missing() throws Exception {
214         DGGeneralDBService dbServiceMock =
215                 new MockDbServiceBuilder().getConfigFileReferenceByFileTypeNVnfType(DEVICE_CONF_PREFIX,
216                         DEVICE_CONF_FILE_TYPE, SvcLogicResource.QueryStatus.NOT_FOUND).build();
217
218         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
219
220         expectedException.expect(SvcLogicException.class);
221         expectedException.expectMessage("Unable to Read ConfigFileReference:device-configuration");
222         configResourceNode.getConfigFileReference(inParams, contextMock);
223     }
224
225     @Test
226     public void should_throw_exception_on_device_config_failure() throws Exception {
227         DGGeneralDBService dbServiceMock =
228                 new MockDbServiceBuilder().getConfigFileReferenceByFileTypeNVnfType(DEVICE_CONF_PREFIX,
229                         DEVICE_CONF_FILE_TYPE, SvcLogicResource.QueryStatus.FAILURE).build();
230
231         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
232
233         expectedException.expect(SvcLogicException.class);
234         expectedException.expectMessage("Unable to Read ConfigFileReference:device-configuration");
235         configResourceNode.getConfigFileReference(inParams, contextMock);
236     }
237
238     @Test
239     public void should_throw_exception_on_success_param_missing() throws Exception {
240
241         DGGeneralDBService dbServiceMock =
242                 new MockDbServiceBuilder().getConfigFileReferenceByFileTypeNVnfType(SUCCESS_PREFIX, SUCCESS_FILE_TYPE,
243                         SvcLogicResource.QueryStatus.NOT_FOUND).build();
244
245         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
246
247         expectedException.expect(SvcLogicException.class);
248         expectedException.expectMessage("Unable to Read ConfigFileReference:configuration_success");
249         configResourceNode.getConfigFileReference(inParams, contextMock);
250     }
251
252     @Test
253     public void should_throw_exception_on_success_param_failure() throws Exception {
254         DGGeneralDBService dbServiceMock =
255                 new MockDbServiceBuilder().getConfigFileReferenceByFileTypeNVnfType(SUCCESS_PREFIX, SUCCESS_FILE_TYPE,
256                         SvcLogicResource.QueryStatus.FAILURE).build();
257
258         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
259
260         expectedException.expect(SvcLogicException.class);
261         expectedException.expectMessage("Unable to Read ConfigFileReference:configuration_success");
262         configResourceNode.getConfigFileReference(inParams, contextMock);
263     }
264
265     @Test
266     public void should_throw_exception_on_failure_param_missing() throws Exception {
267
268         DGGeneralDBService dbServiceMock =
269                 new MockDbServiceBuilder().getConfigFileReferenceByFileTypeNVnfType(FAILURE_PREFIX, FAILURE_FILE_TYPE,
270                         SvcLogicResource.QueryStatus.NOT_FOUND).build();
271
272         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
273
274         expectedException.expect(SvcLogicException.class);
275         expectedException.expectMessage("Unable to Read ConfigFileReference:configuration_error");
276         configResourceNode.getConfigFileReference(inParams, contextMock);
277     }
278
279     @Test
280     public void should_throw_exception_on_failure_param_failure() throws Exception {
281         DGGeneralDBService dbServiceMock =
282                 new MockDbServiceBuilder().getConfigFileReferenceByFileTypeNVnfType(FAILURE_PREFIX, FAILURE_FILE_TYPE,
283                         SvcLogicResource.QueryStatus.FAILURE).build();
284
285         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
286
287         expectedException.expect(SvcLogicException.class);
288         expectedException.expectMessage("Unable to Read ConfigFileReference:configuration_error");
289         configResourceNode.getConfigFileReference(inParams, contextMock);
290     }
291
292     @Test
293     public void should_throw_exception_on_log_param_missing() throws Exception {
294
295         DGGeneralDBService dbServiceMock =
296                 new MockDbServiceBuilder().getConfigFileReferenceByFileTypeNVnfType(LOG_PREFIX, LOG_FILE_TYPE,
297                         SvcLogicResource.QueryStatus.NOT_FOUND).build();
298
299         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
300
301         expectedException.expect(SvcLogicException.class);
302         expectedException.expectMessage("Unable to Read ConfigFileReference:configuration_log");
303         configResourceNode.getConfigFileReference(inParams, contextMock);
304     }
305
306     @Test
307     public void should_throw_exception_on_log_param_failure() throws Exception {
308         DGGeneralDBService dbServiceMock =
309                 new MockDbServiceBuilder().getConfigFileReferenceByFileTypeNVnfType(LOG_PREFIX, LOG_FILE_TYPE,
310                         SvcLogicResource.QueryStatus.FAILURE).build();
311
312         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
313
314         expectedException.expect(SvcLogicException.class);
315         expectedException.expectMessage("Unable to Read ConfigFileReference:configuration_log");
316         configResourceNode.getConfigFileReference(inParams, contextMock);
317     }
318
319     @Test
320     public void should_throw_exception_on_device_protocol_missing() throws SvcLogicException {
321         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
322                 .getDeviceProtocolByVnfType(DEVICE_PROTOCOL_PREFIX, SvcLogicResource.QueryStatus.NOT_FOUND).build();
323
324         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
325
326         expectedException.expect(SvcLogicException.class);
327         expectedException.expectMessage("Unable to Read device_interface_protocol");
328         configResourceNode.getCommonConfigInfo(inParams, contextMock);
329     }
330
331     @Test
332     public void should_throw_exception_on_device_protocol_failure() throws SvcLogicException {
333         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
334                 .getDeviceProtocolByVnfType(DEVICE_PROTOCOL_PREFIX, SvcLogicResource.QueryStatus.FAILURE).build();
335
336         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
337
338         expectedException.expect(SvcLogicException.class);
339         expectedException.expectMessage("Unable to Read device_interface_protocol");
340         configResourceNode.getCommonConfigInfo(inParams, contextMock);
341     }
342
343     @Test
344     public void should_throw_exception_on_conf_action_by_vnf_action_failure() throws SvcLogicException {
345         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
346                 .getConfigureActionDGByVnfTypeNAction(CONF_ACTION_PREFIX, SvcLogicResource.QueryStatus.FAILURE).build();
347
348         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
349
350         expectedException.expect(SvcLogicException.class);
351         expectedException.expectMessage("Unable to Read configure_action_dg");
352         configResourceNode.getCommonConfigInfo(inParams, contextMock);
353     }
354
355     @Test
356     public void should_throw_exception_on_conf_action_missing() throws SvcLogicException {
357         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
358                 .getConfigureActionDGByVnfTypeNAction(CONF_ACTION_PREFIX, SvcLogicResource.QueryStatus.NOT_FOUND)
359                 .getConfigureActionDGByVnfType(CONF_ACTION_PREFIX, SvcLogicResource.QueryStatus.NOT_FOUND).build();
360
361         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
362
363         expectedException.expect(SvcLogicException.class);
364         expectedException.expectMessage("Unable to Read configure_action_dg");
365         configResourceNode.getCommonConfigInfo(inParams, contextMock);
366     }
367
368     @Test
369     public void should_throw_exception_on_conf_action_failure() throws SvcLogicException {
370         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
371                 .getConfigureActionDGByVnfTypeNAction(CONF_ACTION_PREFIX, SvcLogicResource.QueryStatus.NOT_FOUND)
372                 .getConfigureActionDGByVnfType(CONF_ACTION_PREFIX, SvcLogicResource.QueryStatus.FAILURE).build();
373
374         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
375
376         expectedException.expect(SvcLogicException.class);
377         expectedException.expectMessage("Unable to Read configure_action_dg");
378         configResourceNode.getCommonConfigInfo(inParams, contextMock);
379     }
380
381     @Test
382     public void should_throw_exception_on_db_template_failure() throws SvcLogicException {
383         inParams.put(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX, "some prefix");
384         inParams.put(AppcDataServiceConstant.INPUT_PARAM_FILE_CATEGORY, "some file category");
385
386         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
387                 .getTemplate("some prefix", "some file category", SvcLogicResource.QueryStatus.FAILURE).build();
388
389         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
390
391         expectedException.expect(SvcLogicException.class);
392         expectedException.expectMessage("Unable to Read some file category");
393         configResourceNode.getTemplate(inParams, contextMock);
394     }
395
396     @Test
397     public void should_throw_exception_on_db_template_by_action_failure() throws SvcLogicException {
398         inParams.put(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX, "some prefix");
399         inParams.put(AppcDataServiceConstant.INPUT_PARAM_FILE_CATEGORY, "some file category");
400
401         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
402                 .getTemplate("some prefix", "some file category", SvcLogicResource.QueryStatus.NOT_FOUND)
403                 .getTemplateByVnfTypeNAction("some prefix", "some file category", SvcLogicResource.QueryStatus.FAILURE)
404                 .build();
405
406         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
407
408         expectedException.expect(SvcLogicException.class);
409         expectedException.expectMessage("Unable to Read some file category");
410         configResourceNode.getTemplate(inParams, contextMock);
411     }
412
413     @Test
414     public void should_throw_exception_on_db_template_by_action_missing() throws SvcLogicException {
415         inParams.put(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX, "some prefix");
416         inParams.put(AppcDataServiceConstant.INPUT_PARAM_FILE_CATEGORY, "some file category");
417
418         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
419                 .getTemplate("some prefix", "some file category", SvcLogicResource.QueryStatus.NOT_FOUND)
420                 .getTemplateByVnfTypeNAction("some prefix", "some file category",
421                         SvcLogicResource.QueryStatus.NOT_FOUND)
422                 .build();
423
424         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
425
426         expectedException.expect(SvcLogicException.class);
427         expectedException.expectMessage("Unable to Read some file category");
428         configResourceNode.getTemplate(inParams, contextMock);
429     }
430
431     @Test
432     public void should_throw_exception_on_db_template_by_name_missing() throws SvcLogicException {
433         inParams.put(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX, "some prefix");
434         inParams.put(AppcDataServiceConstant.INPUT_PARAM_FILE_CATEGORY, "some file category");
435
436         SvcLogicContext context = new SvcLogicContext();
437         context.setAttribute("template-name", "test template");
438
439         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
440                 .getTemplateByTemplateName("some prefix", "test template", SvcLogicResource.QueryStatus.NOT_FOUND)
441                 .build();
442
443         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
444
445         expectedException.expect(SvcLogicException.class);
446         expectedException.expectMessage("Unable to Read some file category template");
447         configResourceNode.getTemplate(inParams, context);
448     }
449
450     @Test
451     public void should_throw_exception_on_db_template_by_name_failure() throws SvcLogicException {
452         inParams.put(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX, "some prefix");
453         inParams.put(AppcDataServiceConstant.INPUT_PARAM_FILE_CATEGORY, "some file category");
454
455         SvcLogicContext context = new SvcLogicContext();
456         context.setAttribute("template-name", "test template");
457
458         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
459                 .getTemplateByTemplateName("some prefix", "test template", SvcLogicResource.QueryStatus.FAILURE)
460                 .build();
461
462         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
463
464         expectedException.expect(SvcLogicException.class);
465         expectedException.expectMessage("Unable to Read some file category template");
466         configResourceNode.getTemplate(inParams, context);
467     }
468
469     @Test
470     public void should_throw_exception_on_save_config_failure() throws SvcLogicException {
471         SvcLogicContext context = new SvcLogicContext();
472         context.setAttribute(FILE_CATEGORY_PARAM, "some file category");
473
474         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
475                 .saveConfigFiles(CONFIG_FILES_PREFIX, SvcLogicResource.QueryStatus.FAILURE).build();
476
477         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
478
479         expectedException.expect(SvcLogicException.class);
480         expectedException.expectMessage("Unable to Save some file category in configfiles");
481         configResourceNode.saveConfigFiles(inParams, context);
482     }
483
484     @Test
485     public void should_throw_exception_on_get_max_config_id_missing() throws SvcLogicException {
486         SvcLogicContext context = new SvcLogicContext();
487         context.setAttribute(FILE_CATEGORY_PARAM, "some file category");
488
489         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
490                 .getMaxConfigFileId(MAX_CONF_FILE_PREFIX, "some file category", SvcLogicResource.QueryStatus.NOT_FOUND)
491                 .build();
492
493         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
494
495         expectedException.expect(SvcLogicException.class);
496         expectedException.expectMessage("Unable to get some file category from configfiles");
497
498         configResourceNode.saveConfigFiles(inParams, context);
499     }
500
501     @Test
502     public void should_throw_exception_on_save_config_files_failure() throws SvcLogicException {
503         SvcLogicContext context = new SvcLogicContext();
504         context.setAttribute(CONFIG_FILE_ID_PARAM, "some file id");
505
506         DGGeneralDBService dbServiceMock =
507                 new MockDbServiceBuilder().savePrepareRelationship(PREPARE_RELATIONSHIP_PARAM, "some file id", SDC_IND,
508                         SvcLogicResource.QueryStatus.FAILURE).build();
509
510         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
511
512         expectedException.expect(SvcLogicException.class);
513         expectedException.expectMessage("Unable to save prepare_relationship");
514         configResourceNode.saveConfigFiles(inParams, context);
515     }
516
517     @Test
518     public void should_throw_exception_on_save_upload_config_failure() throws SvcLogicException {
519         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
520                 .saveUploadConfig(UPLOAD_CONFIG_PREFIX, SvcLogicResource.QueryStatus.FAILURE).build();
521
522         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
523
524         expectedException.expect(SvcLogicException.class);
525         expectedException.expectMessage("Unable to Save configuration in upload_config");
526         configResourceNode.updateUploadConfig(inParams, contextMock);
527     }
528
529     @Test
530     public void should_throw_exception_on_get_upload_config_failure() throws SvcLogicException {
531         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
532                 .getUploadConfigInfo(UPLOAD_CONFIG_INFO_PREFIX, SvcLogicResource.QueryStatus.FAILURE).build();
533
534         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
535
536         expectedException.expect(SvcLogicException.class);
537         expectedException.expectMessage("Unable to get record from upload_config");
538         configResourceNode.updateUploadConfig(inParams, contextMock);
539     }
540
541     @Test
542     public void should_throw_exception_on_get_upload_config_missing() throws SvcLogicException {
543         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
544                 .getUploadConfigInfo(UPLOAD_CONFIG_INFO_PREFIX, SvcLogicResource.QueryStatus.NOT_FOUND).build();
545
546         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
547
548         expectedException.expect(SvcLogicException.class);
549         expectedException.expectMessage("Unable to get record from upload_config");
550         configResourceNode.updateUploadConfig(inParams, contextMock);
551     }
552
553     @Test
554     public void should_throw_exception_on_update_upload_config_failure() throws SvcLogicException {
555         when(contextMock.getAttribute(UPLOAD_CONFIG_ID_PARAM)).thenReturn("1234");
556
557         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
558                 .updateUploadConfig(UPLOAD_CONFIG_PREFIX, 1234, SvcLogicResource.QueryStatus.FAILURE).build();
559
560         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
561
562         expectedException.expect(SvcLogicException.class);
563         expectedException.expectMessage("Unable to upload upload_config");
564         configResourceNode.updateUploadConfig(inParams, contextMock);
565     }
566
567     @Test
568     public void should_throw_exception_on_get_download_config_failure() throws SvcLogicException {
569         inParams.put(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX, "some prefix");
570
571         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
572                 .getDownloadConfigTemplateByVnf("some prefix", SvcLogicResource.QueryStatus.FAILURE).build();
573
574         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
575
576         expectedException.expect(SvcLogicException.class);
577         expectedException.expectMessage("Unable to get download config template.");
578         configResourceNode.getDownloadConfigTemplateByVnf(inParams, contextMock);
579     }
580
581     @Test
582     public void should_throw_exception_on_get_ssm_chain_failure() throws SvcLogicException {
583         when(contextMock.getAttribute(SITE_LOCATION_PARAM)).thenReturn("some location");
584
585         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
586                 .getTemplateByArtifactType("smm", "smm", "some location", SvcLogicResource.QueryStatus.FAILURE).build();
587
588         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
589
590         expectedException.expect(SvcLogicException.class);
591         expectedException.expectMessage("Unable to Read smm file");
592         configResourceNode.getSmmChainKeyFiles(inParams, contextMock);
593     }
594
595     @Test
596     public void should_throw_exception_on_get_ca_chain_failure() throws SvcLogicException {
597         when(contextMock.getAttribute(SITE_LOCATION_PARAM)).thenReturn("some location");
598
599         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder().getTemplateByArtifactType("intermediate-ca-chain",
600                 "intermediate_ca_chain", "some location", SvcLogicResource.QueryStatus.FAILURE).build();
601
602         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
603
604         expectedException.expect(SvcLogicException.class);
605         expectedException.expectMessage("Unable to Read intermediate_ca_chain file");
606         configResourceNode.getSmmChainKeyFiles(inParams, contextMock);
607     }
608
609     @Test
610     public void should_throw_exception_on_get_server_certificate_and_key_failure() throws SvcLogicException {
611         when(contextMock.getAttribute(SITE_LOCATION_PARAM)).thenReturn("some location");
612
613         DGGeneralDBService dbServiceMock =
614                 new MockDbServiceBuilder().getTemplateByArtifactType("server-certificate-and-key",
615                         "server_certificate_and_key", "some location", SvcLogicResource.QueryStatus.FAILURE).build();
616
617         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
618
619         expectedException.expect(SvcLogicException.class);
620         expectedException.expectMessage("Unable to Read server_certificate_and_key file");
621         configResourceNode.getSmmChainKeyFiles(inParams, contextMock);
622     }
623
624     @Test
625     public void should_throw_exception_on_save_prepare_relationship_failure() throws SvcLogicException {
626         inParams.put(AppcDataServiceConstant.INPUT_PARAM_SDC_ARTIFACT_IND, "some sdnc index");
627         inParams.put(AppcDataServiceConstant.INPUT_PARAM_FILE_ID, "some file id");
628
629         DGGeneralDBService dbServiceMock =
630                 new MockDbServiceBuilder().savePrepareRelationship(PREPARE_RELATIONSHIP_PARAM, "some file id",
631                         "some sdnc index", QueryStatus.FAILURE).build();
632
633         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
634
635         expectedException.expect(SvcLogicException.class);
636         expectedException.expectMessage(UNABLE_TO_SAVE_RELATIONSHIP_STR);
637         configResourceNode.savePrepareRelationship(inParams, contextMock);
638     }
639
640     @Test
641     public void should_save_save_config_files_for_empty_config_params() throws SvcLogicException {
642
643         when(contextMock.getAttribute(TMP_CONVERTCONFIG_ESC_DATA)).thenReturn("some esc data");
644         when(contextMock.getAttribute("configuration")).thenReturn("some configuration");
645         when(contextMock.getAttribute(CONFIG_PARAMS)).thenReturn("");
646
647         DGGeneralDBService dbServiceMock =
648                 new MockDbServiceBuilder().savePrepareRelationship(PREPARE_RELATIONSHIP_PARAM, "some file id",
649                         "some sdnc index", QueryStatus.FAILURE).build();
650
651         ConfigResourceNode configResourceNode = spy(new ConfigResourceNode(dbServiceMock));
652         configResourceNode.saveConfigBlock(inParams, contextMock);
653
654         verify(configResourceNode).saveDeviceConfiguration(inParams, contextMock, "Request", "some esc data",
655                 "some configuration");
656         verify(contextMock, atLeastOnce()).setAttribute(anyString(), eq(AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS));
657     }
658
659     @Test
660     public void should_save_save_config_files_for_non_empty_config_params() throws SvcLogicException {
661
662         when(contextMock.getAttribute(TMP_CONVERTCONFIG_ESC_DATA)).thenReturn("some esc data");
663         when(contextMock.getAttribute(TMP_MERGE_MERGED_DATA)).thenReturn("merged data");
664         when(contextMock.getAttribute(CONFIG_PARAMS)).thenReturn("non empty");
665
666         DGGeneralDBService dbServiceMock =
667                 new MockDbServiceBuilder().savePrepareRelationship(PREPARE_RELATIONSHIP_PARAM, "some file id",
668                         "some sdnc index", QueryStatus.FAILURE).build();
669
670         ConfigResourceNode configResourceNode = spy(new ConfigResourceNode(dbServiceMock));
671         configResourceNode.saveConfigBlock(inParams, contextMock);
672
673         verify(configResourceNode).saveDeviceConfiguration(inParams, contextMock, "Configurator", "some esc data",
674                 "merged data");
675         verify(configResourceNode).saveConfigBlock(inParams, contextMock);
676         verify(contextMock, atLeastOnce()).setAttribute(anyString(), eq(AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS));
677     }
678
679 }