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