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