6ee7a9da78cdd8b8ab8e82188c6c6092d7d35441
[appc.git] /
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.Mockito.mock;
14 import org.onap.appc.data.services.db.DGGeneralDBService;
15 import static org.onap.appc.data.services.node.ConfigResourceNode.DEVICE_CONF_FILE_TYPE;
16 import static org.onap.appc.data.services.node.ConfigResourceNode.DEVICE_CONF_PREFIX;
17 import static org.onap.appc.data.services.node.ConfigResourceNode.FAILURE_FILE_TYPE;
18 import static org.onap.appc.data.services.node.ConfigResourceNode.FAILURE_PREFIX;
19 import static org.onap.appc.data.services.node.ConfigResourceNode.LOG_FILE_TYPE;
20 import static org.onap.appc.data.services.node.ConfigResourceNode.LOG_PREFIX;
21 import static org.onap.appc.data.services.node.ConfigResourceNode.SUCCESS_FILE_TYPE;
22 import static org.onap.appc.data.services.node.ConfigResourceNode.SUCCESS_PREFIX;
23 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
24 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
25 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
26
27 public class ConfigResourceNodeTest {
28
29     private HashMap<String, String> inParams;
30     private SvcLogicContext contextMock;
31
32     @Rule
33     public ExpectedException expectedException = ExpectedException.none();
34
35     @Before
36     public void setUp() {
37         contextMock = mock(SvcLogicContext.class);
38         inParams = new HashMap<>();
39     }
40
41     /**
42      * Example data:
43      *
44      * {"capabilities":{"vnfc":[],"vm":[{"ConfigureTest":["SSC","MMSC"]}],"vf-module":[],"vnf":["ConfigModify","HealthCheck"]}}
45      *
46      */
47     @Test
48     public void shouldProcessCapabilitiesForVMLevel() throws Exception {
49         SvcLogicContext ctx = new SvcLogicContext();
50         ConfigResourceNode node = new ConfigResourceNode(DGGeneralDBService.initialise());
51         String findCapability = "Restart";
52         JsonNodeFactory.instance.objectNode();
53         String subCaps = "[{\"Restart\":[\"SSC\",\"MMC\"]},{\"Rebuild\":[\"SSC\"]},{\"Migrate\":[\"SSC\"]},{\"Snapshot\":[\"SSC\"]},{\"Start\":[\"SSC\"]},{\"Stop\":[\"SSC\"]}]";
54         ObjectMapper m = new ObjectMapper();
55         JsonNode subCapabilities = m.readTree(subCaps);
56         String vServerId = "testServer";
57         ctx.setAttribute("tmp.vnfInfo.vm.vnfc.vnfc-function-code", "MMC");
58         ctx.setAttribute("tmp.vnfInfo.vm.vnfc.vnfc-name", "testVnfc");
59         node.processCapabilitiesForVMLevel(vServerId, ctx, findCapability, subCapabilities);
60         String result = ctx.getAttribute("capabilities");
61         assertEquals(result, "Supported");
62     }
63
64     @Test
65     public void shouldCheckIfCapabilityCheckNeeded() {
66         ConfigResourceNode node = new ConfigResourceNode(DGGeneralDBService.initialise());
67         String findCapability = "Start";
68         String capLevel = "vnf";
69         boolean result = node.checkIfCapabilityCheckNeeded(capLevel, findCapability);
70         assertFalse(result);
71     }
72
73     @Test
74     public void should_not_throw_if_all_db_params_return_success() throws SvcLogicException {
75         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder().build();
76
77         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
78         configResourceNode.getConfigFileReference(inParams, contextMock);
79     }
80
81     @Test
82     public void should_throw_exception_on_device_config_missing() throws Exception {
83         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
84             .configFileReference(DEVICE_CONF_PREFIX, DEVICE_CONF_FILE_TYPE, SvcLogicResource.QueryStatus.NOT_FOUND)
85             .build();
86
87         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
88
89         expectedException.expect(SvcLogicException.class);
90         expectedException.expectMessage("Unable to Read ConfigFileReference:device-configuration");
91         configResourceNode.getConfigFileReference(inParams, contextMock);
92     }
93
94     @Test
95     public void should_throw_exception_on_device_config_failure() throws Exception {
96         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
97             .configFileReference(DEVICE_CONF_PREFIX, DEVICE_CONF_FILE_TYPE, SvcLogicResource.QueryStatus.FAILURE)
98             .build();
99
100         ConfigResourceNode configResourceNode = new ConfigResourceNode(dbServiceMock);
101
102         expectedException.expect(SvcLogicException.class);
103         expectedException.expectMessage("Unable to Read ConfigFileReference:device-configuration");
104         configResourceNode.getConfigFileReference(inParams, contextMock);
105     }
106
107     @Test
108     public void should_throw_exception_on_success_param_missing() throws Exception {
109
110         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
111             .configFileReference(SUCCESS_PREFIX, SUCCESS_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:configuration_success");
118         configResourceNode.getConfigFileReference(inParams, contextMock);
119     }
120
121     @Test
122     public void should_throw_exception_on_success_param_failure() throws Exception {
123         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
124             .configFileReference(SUCCESS_PREFIX, SUCCESS_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:configuration_success");
131         configResourceNode.getConfigFileReference(inParams, contextMock);
132     }
133
134     @Test
135     public void should_throw_exception_on_failure_param_missing() throws Exception {
136
137         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
138             .configFileReference(FAILURE_PREFIX, FAILURE_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_error");
145         configResourceNode.getConfigFileReference(inParams, contextMock);
146     }
147
148     @Test
149     public void should_throw_exception_on_failure_param_failure() throws Exception {
150         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
151             .configFileReference(FAILURE_PREFIX, FAILURE_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_error");
158         configResourceNode.getConfigFileReference(inParams, contextMock);
159     }
160
161     @Test
162     public void should_throw_exception_on_log_param_missing() throws Exception {
163
164         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
165             .configFileReference(LOG_PREFIX, LOG_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_log");
172         configResourceNode.getConfigFileReference(inParams, contextMock);
173     }
174
175     @Test
176     public void should_throw_exception_on_log_param_failure() throws Exception {
177         DGGeneralDBService dbServiceMock = new MockDbServiceBuilder()
178             .configFileReference(LOG_PREFIX, LOG_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_log");
185         configResourceNode.getConfigFileReference(inParams, contextMock);
186     }
187
188 }