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