1 package org.onap.appc.data.services.node;
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;
39 public class ConfigResourceNodeTest {
41 private HashMap<String, String> inParams;
42 private SvcLogicContext contextMock;
45 public ExpectedException expectedException = ExpectedException.none();
49 contextMock = mock(SvcLogicContext.class);
50 inParams = new HashMap<>();
56 * {"capabilities":{"vnfc":[],"vm":[{"ConfigureTest":["SSC","MMSC"]}],"vf-module":[],"vnf":["ConfigModify","HealthCheck"]}}
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");
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);
85 public void should_add_attribute_with_success_if_get_config_file_succeed() throws SvcLogicException {
86 DGGeneralDBService dbServiceMock = new MockDbServiceBuilder().build();
88 ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
89 configResourceNode.getConfigFileReference(inParams, contextMock);
91 verify(contextMock).setAttribute(anyString(), eq(AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS));
95 public void should_add_attribute_with_success_if_get_config_info_succeed() throws SvcLogicException {
96 DGGeneralDBService dbServiceMock = new MockDbServiceBuilder().build();
98 ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
99 configResourceNode.getCommonConfigInfo(inParams, contextMock);
101 verify(contextMock).setAttribute(anyString(), eq(AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS));
105 public void should_add_attribute_with_success_if_get_template_succeed() throws SvcLogicException {
106 DGGeneralDBService dbServiceMock = new MockDbServiceBuilder().build();
108 ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
109 configResourceNode.getTemplate(inParams, contextMock);
111 verify(contextMock).setAttribute(anyString(), eq(AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS));
115 public void should_add_attribute_with_success_if_save_config_files_succeed() throws SvcLogicException {
116 DGGeneralDBService dbServiceMock = new MockDbServiceBuilder().build();
118 ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
119 configResourceNode.saveConfigFiles(inParams, contextMock);
121 verify(contextMock).setAttribute(anyString(), eq(AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS));
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)
130 ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
132 expectedException.expect(SvcLogicException.class);
133 expectedException.expectMessage("Unable to Read ConfigFileReference:device-configuration");
134 configResourceNode.getConfigFileReference(inParams, contextMock);
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)
143 ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
145 expectedException.expect(SvcLogicException.class);
146 expectedException.expectMessage("Unable to Read ConfigFileReference:device-configuration");
147 configResourceNode.getConfigFileReference(inParams, contextMock);
151 public void should_throw_exception_on_success_param_missing() throws Exception {
153 DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
154 .getConfigFileReferenceByFileTypeNVnfType(SUCCESS_PREFIX, SUCCESS_FILE_TYPE, SvcLogicResource.QueryStatus.NOT_FOUND)
157 ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
159 expectedException.expect(SvcLogicException.class);
160 expectedException.expectMessage("Unable to Read ConfigFileReference:configuration_success");
161 configResourceNode.getConfigFileReference(inParams, contextMock);
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)
170 ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
172 expectedException.expect(SvcLogicException.class);
173 expectedException.expectMessage("Unable to Read ConfigFileReference:configuration_success");
174 configResourceNode.getConfigFileReference(inParams, contextMock);
178 public void should_throw_exception_on_failure_param_missing() throws Exception {
180 DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
181 .getConfigFileReferenceByFileTypeNVnfType(FAILURE_PREFIX, FAILURE_FILE_TYPE, SvcLogicResource.QueryStatus.NOT_FOUND)
184 ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
186 expectedException.expect(SvcLogicException.class);
187 expectedException.expectMessage("Unable to Read ConfigFileReference:configuration_error");
188 configResourceNode.getConfigFileReference(inParams, contextMock);
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)
197 ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
199 expectedException.expect(SvcLogicException.class);
200 expectedException.expectMessage("Unable to Read ConfigFileReference:configuration_error");
201 configResourceNode.getConfigFileReference(inParams, contextMock);
205 public void should_throw_exception_on_log_param_missing() throws Exception {
207 DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
208 .getConfigFileReferenceByFileTypeNVnfType(LOG_PREFIX, LOG_FILE_TYPE, SvcLogicResource.QueryStatus.NOT_FOUND)
211 ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
213 expectedException.expect(SvcLogicException.class);
214 expectedException.expectMessage("Unable to Read ConfigFileReference:configuration_log");
215 configResourceNode.getConfigFileReference(inParams, contextMock);
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)
224 ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
226 expectedException.expect(SvcLogicException.class);
227 expectedException.expectMessage("Unable to Read ConfigFileReference:configuration_log");
228 configResourceNode.getConfigFileReference(inParams, contextMock);
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)
237 ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
239 expectedException.expect(SvcLogicException.class);
240 expectedException.expectMessage("Unable to Read device_interface_protocol");
241 configResourceNode.getCommonConfigInfo(inParams, contextMock);
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)
250 ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
252 expectedException.expect(SvcLogicException.class);
253 expectedException.expectMessage("Unable to Read device_interface_protocol");
254 configResourceNode.getCommonConfigInfo(inParams, contextMock);
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)
263 ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
265 expectedException.expect(SvcLogicException.class);
266 expectedException.expectMessage("Unable to Read configure_action_dg");
267 configResourceNode.getCommonConfigInfo(inParams, contextMock);
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)
277 ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
279 expectedException.expect(SvcLogicException.class);
280 expectedException.expectMessage("Unable to Read configure_action_dg");
281 configResourceNode.getCommonConfigInfo(inParams, contextMock);
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)
291 ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
293 expectedException.expect(SvcLogicException.class);
294 expectedException.expectMessage("Unable to Read configure_action_dg");
295 configResourceNode.getCommonConfigInfo(inParams, contextMock);
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");
303 DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
304 .getTemplate("some prefix", "some file category", SvcLogicResource.QueryStatus.FAILURE)
307 ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
309 expectedException.expect(SvcLogicException.class);
310 expectedException.expectMessage("Unable to Read some file category");
311 configResourceNode.getTemplate(inParams, contextMock);
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");
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)
324 ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
326 expectedException.expect(SvcLogicException.class);
327 expectedException.expectMessage("Unable to Read some file category");
328 configResourceNode.getTemplate(inParams, contextMock);
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");
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)
341 ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
343 expectedException.expect(SvcLogicException.class);
344 expectedException.expectMessage("Unable to Read some file category");
345 configResourceNode.getTemplate(inParams, contextMock);
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");
353 SvcLogicContext context = new SvcLogicContext();
354 context.setAttribute("template-name", "test template");
356 DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
357 .getTemplateByTemplateName("some prefix", "test template", SvcLogicResource.QueryStatus.NOT_FOUND)
360 ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
362 expectedException.expect(SvcLogicException.class);
363 expectedException.expectMessage("Unable to Read some file category template");
364 configResourceNode.getTemplate(inParams, context);
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");
372 SvcLogicContext context = new SvcLogicContext();
373 context.setAttribute("template-name", "test template");
375 DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
376 .getTemplateByTemplateName("some prefix", "test template", SvcLogicResource.QueryStatus.FAILURE)
379 ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
381 expectedException.expect(SvcLogicException.class);
382 expectedException.expectMessage("Unable to Read some file category template");
383 configResourceNode.getTemplate(inParams, context);
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");
391 DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
392 .saveConfigFiles(CONFIG_FILES_PREFIX, SvcLogicResource.QueryStatus.FAILURE)
395 ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
397 expectedException.expect(SvcLogicException.class);
398 expectedException.expectMessage("Unable to Save some file category in configfiles");
399 configResourceNode.saveConfigFiles(inParams, context);
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");
407 DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
408 .getMaxConfigFileId(MAX_CONF_FILE_PREFIX, "some file category", SvcLogicResource.QueryStatus.NOT_FOUND)
411 ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
413 expectedException.expect(SvcLogicException.class);
414 expectedException.expectMessage("Unable to get some file category from configfiles");
416 configResourceNode.saveConfigFiles(inParams, context);
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");
424 DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
425 .savePrepareRelationship(PREPARE_RELATIONSHIP_PARAM, "some file id", SDC_IND, SvcLogicResource.QueryStatus.FAILURE)
428 ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
430 expectedException.expect(SvcLogicException.class);
431 expectedException.expectMessage("Unable to save prepare_relationship");
432 configResourceNode.saveConfigFiles(inParams, context);