Add coverage for ConfigResourceNode-3
[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 com.fasterxml.jackson.databind.JsonNode;
4 import com.fasterxml.jackson.databind.ObjectMapper;
5 import com.fasterxml.jackson.databind.node.JsonNodeFactory;
6 import java.util.HashMap;
7 import static org.junit.Assert.assertEquals;
8 import static org.junit.Assert.assertFalse;
9 import org.junit.Before;
10 import org.junit.Rule;
11 import org.junit.Test;
12 import org.junit.rules.ExpectedException;
13 import static org.mockito.Matchers.anyString;
14 import static org.mockito.Matchers.eq;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.verify;
17 import org.onap.appc.data.services.AppcDataServiceConstant;
18 import org.onap.appc.data.services.db.DGGeneralDBService;
19 import static org.onap.appc.data.services.node.ConfigResourceNode.CONFIG_FILE_ID_PARAM;
20 import static org.onap.appc.data.services.node.ConfigResourceNode.CONF_ACTION_PREFIX;
21 import static org.onap.appc.data.services.node.ConfigResourceNode.DEVICE_CONF_FILE_TYPE;
22 import static org.onap.appc.data.services.node.ConfigResourceNode.DEVICE_CONF_PREFIX;
23 import static org.onap.appc.data.services.node.ConfigResourceNode.DEVICE_PROTOCOL_PREFIX;
24 import static org.onap.appc.data.services.node.ConfigResourceNode.FAILURE_FILE_TYPE;
25 import static org.onap.appc.data.services.node.ConfigResourceNode.FAILURE_PREFIX;
26 import static org.onap.appc.data.services.node.ConfigResourceNode.FILE_CATEGORY_PARAM;
27 import static org.onap.appc.data.services.node.ConfigResourceNode.LOG_FILE_TYPE;
28 import static org.onap.appc.data.services.node.ConfigResourceNode.LOG_PREFIX;
29 import static org.onap.appc.data.services.node.ConfigResourceNode.MAX_CONF_FILE_PREFIX;
30 import static org.onap.appc.data.services.node.ConfigResourceNode.PREPARE_RELATIONSHIP_PARAM;
31 import static org.onap.appc.data.services.node.ConfigResourceNode.SDC_IND;
32 import static org.onap.appc.data.services.node.ConfigResourceNode.SUCCESS_FILE_TYPE;
33 import static org.onap.appc.data.services.node.ConfigResourceNode.SUCCESS_PREFIX;
34 import static org.onap.appc.data.services.node.ConfigResourceNode.CONFIG_FILES_PREFIX;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
38
39 public class ConfigResourceNodeTest {
40
41     private HashMap<String, String> inParams;
42     private SvcLogicContext contextMock;
43
44     @Rule
45     public ExpectedException expectedException = ExpectedException.none();
46
47     @Before
48     public void setUp() {
49         contextMock = mock(SvcLogicContext.class);
50         inParams = new HashMap<>();
51     }
52
53     /**
54      * Example data:
55      * <p>
56      * {"capabilities":{"vnfc":[],"vm":[{"ConfigureTest":["SSC","MMSC"]}],"vf-module":[],"vnf":["ConfigModify","HealthCheck"]}}
57      */
58     @Test
59     public void shouldProcessCapabilitiesForVMLevel() throws Exception {
60         SvcLogicContext ctx = new SvcLogicContext();
61         ConfigResourceNode node = new ConfigResourceNode(DGGeneralDBService.initialise());
62         String findCapability = "Restart";
63         JsonNodeFactory.instance.objectNode();
64         String subCaps = "[{\"Restart\":[\"SSC\",\"MMC\"]},{\"Rebuild\":[\"SSC\"]},{\"Migrate\":[\"SSC\"]},{\"Snapshot\":[\"SSC\"]},{\"Start\":[\"SSC\"]},{\"Stop\":[\"SSC\"]}]";
65         ObjectMapper m = new ObjectMapper();
66         JsonNode subCapabilities = m.readTree(subCaps);
67         String vServerId = "testServer";
68         ctx.setAttribute("tmp.vnfInfo.vm.vnfc.vnfc-function-code", "MMC");
69         ctx.setAttribute("tmp.vnfInfo.vm.vnfc.vnfc-name", "testVnfc");
70         node.processCapabilitiesForVMLevel(vServerId, ctx, findCapability, subCapabilities);
71         String result = ctx.getAttribute("capabilities");
72         assertEquals(result, "Supported");
73     }
74
75     @Test
76     public void shouldCheckIfCapabilityCheckNeeded() {
77         ConfigResourceNode node = new ConfigResourceNode(DGGeneralDBService.initialise());
78         String findCapability = "Start";
79         String capLevel = "vnf";
80         boolean result = node.checkIfCapabilityCheckNeeded(capLevel, findCapability);
81         assertFalse(result);
82     }
83
84     @Test
85     public void should_add_attribute_with_success_if_get_config_file_succeed() throws SvcLogicException {
86         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder().build();
87
88         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
89         configResourceNode.getConfigFileReference(inParams, contextMock);
90
91         verify(contextMock).setAttribute(anyString(), eq(AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS));
92     }
93
94     @Test
95     public void should_add_attribute_with_success_if_get_config_info_succeed() throws SvcLogicException {
96         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder().build();
97
98         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
99         configResourceNode.getCommonConfigInfo(inParams, contextMock);
100
101         verify(contextMock).setAttribute(anyString(), eq(AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS));
102     }
103
104     @Test
105     public void should_add_attribute_with_success_if_get_template_succeed() throws SvcLogicException {
106         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder().build();
107
108         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
109         configResourceNode.getTemplate(inParams, contextMock);
110
111         verify(contextMock).setAttribute(anyString(), eq(AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS));
112     }
113
114     @Test
115     public void should_add_attribute_with_success_if_save_config_files_succeed() throws SvcLogicException {
116         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder().build();
117
118         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
119         configResourceNode.saveConfigFiles(inParams, contextMock);
120
121         verify(contextMock).setAttribute(anyString(), eq(AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS));
122     }
123
124     @Test
125     public void should_throw_exception_on_device_config_missing() throws Exception {
126         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
127             .getConfigFileReferenceByFileTypeNVnfType(DEVICE_CONF_PREFIX, DEVICE_CONF_FILE_TYPE, SvcLogicResource.QueryStatus.NOT_FOUND)
128             .build();
129
130         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
131
132         expectedException.expect(SvcLogicException.class);
133         expectedException.expectMessage("Unable to Read ConfigFileReference:device-configuration");
134         configResourceNode.getConfigFileReference(inParams, contextMock);
135     }
136
137     @Test
138     public void should_throw_exception_on_device_config_failure() throws Exception {
139         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
140             .getConfigFileReferenceByFileTypeNVnfType(DEVICE_CONF_PREFIX, DEVICE_CONF_FILE_TYPE, SvcLogicResource.QueryStatus.FAILURE)
141             .build();
142
143         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
144
145         expectedException.expect(SvcLogicException.class);
146         expectedException.expectMessage("Unable to Read ConfigFileReference:device-configuration");
147         configResourceNode.getConfigFileReference(inParams, contextMock);
148     }
149
150     @Test
151     public void should_throw_exception_on_success_param_missing() throws Exception {
152
153         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
154             .getConfigFileReferenceByFileTypeNVnfType(SUCCESS_PREFIX, SUCCESS_FILE_TYPE, SvcLogicResource.QueryStatus.NOT_FOUND)
155             .build();
156
157         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
158
159         expectedException.expect(SvcLogicException.class);
160         expectedException.expectMessage("Unable to Read ConfigFileReference:configuration_success");
161         configResourceNode.getConfigFileReference(inParams, contextMock);
162     }
163
164     @Test
165     public void should_throw_exception_on_success_param_failure() throws Exception {
166         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
167             .getConfigFileReferenceByFileTypeNVnfType(SUCCESS_PREFIX, SUCCESS_FILE_TYPE, SvcLogicResource.QueryStatus.FAILURE)
168             .build();
169
170         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
171
172         expectedException.expect(SvcLogicException.class);
173         expectedException.expectMessage("Unable to Read ConfigFileReference:configuration_success");
174         configResourceNode.getConfigFileReference(inParams, contextMock);
175     }
176
177     @Test
178     public void should_throw_exception_on_failure_param_missing() throws Exception {
179
180         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
181             .getConfigFileReferenceByFileTypeNVnfType(FAILURE_PREFIX, FAILURE_FILE_TYPE, SvcLogicResource.QueryStatus.NOT_FOUND)
182             .build();
183
184         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
185
186         expectedException.expect(SvcLogicException.class);
187         expectedException.expectMessage("Unable to Read ConfigFileReference:configuration_error");
188         configResourceNode.getConfigFileReference(inParams, contextMock);
189     }
190
191     @Test
192     public void should_throw_exception_on_failure_param_failure() throws Exception {
193         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
194             .getConfigFileReferenceByFileTypeNVnfType(FAILURE_PREFIX, FAILURE_FILE_TYPE, SvcLogicResource.QueryStatus.FAILURE)
195             .build();
196
197         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
198
199         expectedException.expect(SvcLogicException.class);
200         expectedException.expectMessage("Unable to Read ConfigFileReference:configuration_error");
201         configResourceNode.getConfigFileReference(inParams, contextMock);
202     }
203
204     @Test
205     public void should_throw_exception_on_log_param_missing() throws Exception {
206
207         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
208             .getConfigFileReferenceByFileTypeNVnfType(LOG_PREFIX, LOG_FILE_TYPE, SvcLogicResource.QueryStatus.NOT_FOUND)
209             .build();
210
211         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
212
213         expectedException.expect(SvcLogicException.class);
214         expectedException.expectMessage("Unable to Read ConfigFileReference:configuration_log");
215         configResourceNode.getConfigFileReference(inParams, contextMock);
216     }
217
218     @Test
219     public void should_throw_exception_on_log_param_failure() throws Exception {
220         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
221             .getConfigFileReferenceByFileTypeNVnfType(LOG_PREFIX, LOG_FILE_TYPE, SvcLogicResource.QueryStatus.FAILURE)
222             .build();
223
224         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
225
226         expectedException.expect(SvcLogicException.class);
227         expectedException.expectMessage("Unable to Read ConfigFileReference:configuration_log");
228         configResourceNode.getConfigFileReference(inParams, contextMock);
229     }
230
231     @Test
232     public void should_throw_exception_on_device_protocol_missing() throws SvcLogicException {
233         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
234             .getDeviceProtocolByVnfType(DEVICE_PROTOCOL_PREFIX, SvcLogicResource.QueryStatus.NOT_FOUND)
235             .build();
236
237         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
238
239         expectedException.expect(SvcLogicException.class);
240         expectedException.expectMessage("Unable to Read device_interface_protocol");
241         configResourceNode.getCommonConfigInfo(inParams, contextMock);
242     }
243
244     @Test
245     public void should_throw_exception_on_device_protocol_failure() throws SvcLogicException {
246         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
247             .getDeviceProtocolByVnfType(DEVICE_PROTOCOL_PREFIX, SvcLogicResource.QueryStatus.FAILURE)
248             .build();
249
250         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
251
252         expectedException.expect(SvcLogicException.class);
253         expectedException.expectMessage("Unable to Read device_interface_protocol");
254         configResourceNode.getCommonConfigInfo(inParams, contextMock);
255     }
256
257     @Test
258     public void should_throw_exception_on_conf_action_by_vnf_action_failure() throws SvcLogicException {
259         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
260             .getConfigureActionDGByVnfTypeNAction(CONF_ACTION_PREFIX, SvcLogicResource.QueryStatus.FAILURE)
261             .build();
262
263         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
264
265         expectedException.expect(SvcLogicException.class);
266         expectedException.expectMessage("Unable to Read configure_action_dg");
267         configResourceNode.getCommonConfigInfo(inParams, contextMock);
268     }
269
270     @Test
271     public void should_throw_exception_on_conf_action_missing() throws SvcLogicException {
272         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
273             .getConfigureActionDGByVnfTypeNAction(CONF_ACTION_PREFIX, SvcLogicResource.QueryStatus.NOT_FOUND)
274             .getConfigureActionDGByVnfType(CONF_ACTION_PREFIX, SvcLogicResource.QueryStatus.NOT_FOUND)
275             .build();
276
277         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
278
279         expectedException.expect(SvcLogicException.class);
280         expectedException.expectMessage("Unable to Read configure_action_dg");
281         configResourceNode.getCommonConfigInfo(inParams, contextMock);
282     }
283
284     @Test
285     public void should_throw_exception_on_conf_action_failure() throws SvcLogicException {
286         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
287             .getConfigureActionDGByVnfTypeNAction(CONF_ACTION_PREFIX, SvcLogicResource.QueryStatus.NOT_FOUND)
288             .getConfigureActionDGByVnfType(CONF_ACTION_PREFIX, SvcLogicResource.QueryStatus.FAILURE)
289             .build();
290
291         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
292
293         expectedException.expect(SvcLogicException.class);
294         expectedException.expectMessage("Unable to Read configure_action_dg");
295         configResourceNode.getCommonConfigInfo(inParams, contextMock);
296     }
297
298     @Test
299     public void should_throw_exception_on_db_template_failure() throws SvcLogicException {
300         inParams.put(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX, "some prefix");
301         inParams.put(AppcDataServiceConstant.INPUT_PARAM_FILE_CATEGORY, "some file category");
302
303         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
304             .getTemplate("some prefix", "some file category", SvcLogicResource.QueryStatus.FAILURE)
305             .build();
306
307         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
308
309         expectedException.expect(SvcLogicException.class);
310         expectedException.expectMessage("Unable to Read some file category");
311         configResourceNode.getTemplate(inParams, contextMock);
312     }
313
314     @Test
315     public void should_throw_exception_on_db_template_by_action_failure() throws SvcLogicException {
316         inParams.put(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX, "some prefix");
317         inParams.put(AppcDataServiceConstant.INPUT_PARAM_FILE_CATEGORY, "some file category");
318
319         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
320             .getTemplate("some prefix", "some file category", SvcLogicResource.QueryStatus.NOT_FOUND)
321             .getTemplateByVnfTypeNAction("some prefix", "some file category", SvcLogicResource.QueryStatus.FAILURE)
322             .build();
323
324         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
325
326         expectedException.expect(SvcLogicException.class);
327         expectedException.expectMessage("Unable to Read some file category");
328         configResourceNode.getTemplate(inParams, contextMock);
329     }
330
331     @Test
332     public void should_throw_exception_on_db_template_by_action_missing() throws SvcLogicException {
333         inParams.put(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX, "some prefix");
334         inParams.put(AppcDataServiceConstant.INPUT_PARAM_FILE_CATEGORY, "some file category");
335
336         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
337             .getTemplate("some prefix", "some file category", SvcLogicResource.QueryStatus.NOT_FOUND)
338             .getTemplateByVnfTypeNAction("some prefix", "some file category", SvcLogicResource.QueryStatus.NOT_FOUND)
339             .build();
340
341         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
342
343         expectedException.expect(SvcLogicException.class);
344         expectedException.expectMessage("Unable to Read some file category");
345         configResourceNode.getTemplate(inParams, contextMock);
346     }
347     
348     @Test
349     public void should_throw_exception_on_db_template_by_name_missing() throws SvcLogicException {
350         inParams.put(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX, "some prefix");
351         inParams.put(AppcDataServiceConstant.INPUT_PARAM_FILE_CATEGORY, "some file category");
352
353         SvcLogicContext context = new SvcLogicContext();
354         context.setAttribute("template-name", "test template");
355
356         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
357             .getTemplateByTemplateName("some prefix", "test template", SvcLogicResource.QueryStatus.NOT_FOUND)
358             .build();
359
360         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
361
362         expectedException.expect(SvcLogicException.class);
363         expectedException.expectMessage("Unable to Read some file category template");
364         configResourceNode.getTemplate(inParams, context);
365     }
366
367     @Test
368     public void should_throw_exception_on_db_template_by_name_failure() throws SvcLogicException {
369         inParams.put(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX, "some prefix");
370         inParams.put(AppcDataServiceConstant.INPUT_PARAM_FILE_CATEGORY, "some file category");
371
372         SvcLogicContext context = new SvcLogicContext();
373         context.setAttribute("template-name", "test template");
374
375         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
376             .getTemplateByTemplateName("some prefix", "test template", SvcLogicResource.QueryStatus.FAILURE)
377             .build();
378
379         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
380
381         expectedException.expect(SvcLogicException.class);
382         expectedException.expectMessage("Unable to Read some file category template");
383         configResourceNode.getTemplate(inParams, context);
384     }
385
386     @Test
387     public void should_throw_exception_on_save_config_failure() throws SvcLogicException {
388         SvcLogicContext context = new SvcLogicContext();
389         context.setAttribute(FILE_CATEGORY_PARAM, "some file category");
390
391         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
392             .saveConfigFiles(CONFIG_FILES_PREFIX, SvcLogicResource.QueryStatus.FAILURE)
393             .build();
394
395         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
396
397         expectedException.expect(SvcLogicException.class);
398         expectedException.expectMessage("Unable to Save some file category in configfiles");
399         configResourceNode.saveConfigFiles(inParams, context);
400     }
401
402     @Test
403     public void should_throw_exception_on_get_max_config_id_missing() throws SvcLogicException {
404         SvcLogicContext context = new SvcLogicContext();
405         context.setAttribute(FILE_CATEGORY_PARAM, "some file category");
406
407         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
408             .getMaxConfigFileId(MAX_CONF_FILE_PREFIX, "some file category", SvcLogicResource.QueryStatus.NOT_FOUND)
409             .build();
410
411         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
412
413         expectedException.expect(SvcLogicException.class);
414         expectedException.expectMessage("Unable to get some file category from configfiles");
415
416         configResourceNode.saveConfigFiles(inParams, context);
417     }
418
419     @Test
420     public void should_throw_exception_on_save_config_files_failure() throws SvcLogicException {
421         SvcLogicContext context = new SvcLogicContext();
422         context.setAttribute(CONFIG_FILE_ID_PARAM, "some file id");
423
424         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
425             .savePrepareRelationship(PREPARE_RELATIONSHIP_PARAM, "some file id", SDC_IND, SvcLogicResource.QueryStatus.FAILURE)
426             .build();
427
428         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
429
430         expectedException.expect(SvcLogicException.class);
431         expectedException.expectMessage("Unable to save prepare_relationship");
432         configResourceNode.saveConfigFiles(inParams, context);
433     }
434
435 }