2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Ericsson
6 * ================================================================================
7 * Modifications Copyright (C) 2019 AT&T Intellectual Property
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.dg.aai.impl;
26 import static org.hamcrest.CoreMatchers.isA;
27 import java.util.HashMap;
29 import org.junit.Before;
30 import org.junit.Rule;
31 import org.junit.Test;
32 import org.junit.rules.ExpectedException;
33 import org.junit.runner.RunWith;
34 import org.mockito.Matchers;
35 import org.mockito.Mockito;
36 import org.onap.appc.dg.aai.exception.AAIQueryException;
37 import org.onap.appc.dg.aai.objects.AAIQueryResult;
38 import org.onap.appc.dg.aai.objects.Relationship;
39 import org.onap.appc.exceptions.APPCException;
40 import org.onap.appc.i18n.Msg;
41 import org.onap.ccsdk.sli.adaptors.aai.AAIClient;
42 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
43 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
44 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
45 import org.osgi.framework.FrameworkUtil;
46 import org.powermock.core.classloader.annotations.PrepareForTest;
47 import org.powermock.modules.junit4.PowerMockRunner;
48 import com.att.eelf.i18n.EELFResourceManager;
49 import org.hamcrest.CoreMatchers;
50 import org.junit.Assert;
51 import org.osgi.framework.Bundle;
52 import org.osgi.framework.BundleContext;
53 import org.osgi.framework.ServiceReference;
54 import org.powermock.api.mockito.PowerMockito;
56 @RunWith(PowerMockRunner.class)
57 @PrepareForTest(FrameworkUtil.class)
58 public class AAIPluginImplTest {
60 private final BundleContext bundleContext = Mockito.mock(BundleContext.class);
61 private final Bundle bundleService = Mockito.mock(Bundle.class);
62 private final ServiceReference sref = Mockito.mock(ServiceReference.class);
63 private final AAIClient aaiClient = Mockito.mock(AAIClient.class);
64 private SvcLogicContext ctx;
65 private Map<String, String> params;
68 public ExpectedException expectedEx = ExpectedException.none();
71 public void setUp() throws NoSuchFieldException, IllegalAccessException {
72 PowerMockito.mockStatic(FrameworkUtil.class);
73 PowerMockito.when(FrameworkUtil.getBundle(Matchers.any(Class.class))).thenReturn(bundleService);
74 PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext);
75 PowerMockito.when(bundleContext.getServiceReference(Matchers.any(Class.class))).thenReturn(sref);
76 PowerMockito.when(bundleContext.<AAIClient>getService(sref)).thenReturn(aaiClient);
77 params = new HashMap<String, String>();
78 params.put(Constants.AAI_INPUT_DATA + ".suffix", "TEST_DATA");
82 public void testPostGenericVnfDataNotFound() throws APPCException, SvcLogicException {
83 SvcLogicResource.QueryStatus status = SvcLogicResource.QueryStatus.NOT_FOUND;
84 Mockito.doReturn(status).when(aaiClient).update(Mockito.anyString(), Mockito.anyString(),
85 Mockito.anyMap(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
86 ctx = new SvcLogicContext();
87 AAIPluginImpl impl = new AAIPluginImpl();
89 expectedEx.expect(APPCException.class);
90 expectedEx.expectMessage("VNF not found with vnf_id null");
91 impl.postGenericVnfData(params, ctx);
95 public void testPostGenericVnfDataFailure() throws APPCException, SvcLogicException {
96 SvcLogicResource.QueryStatus status = SvcLogicResource.QueryStatus.FAILURE;
97 Mockito.doReturn(status).when(aaiClient).update(Mockito.anyString(), Mockito.anyString(),
98 Mockito.anyMap(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
99 ctx = new SvcLogicContext();
100 AAIPluginImpl impl = new AAIPluginImpl();
102 expectedEx.expect(APPCException.class);
103 expectedEx.expectMessage("Error Querying AAI with vnfID = null");
104 impl.postGenericVnfData(params, ctx);
108 public void testPostGenericVnfDataSucces() throws APPCException, SvcLogicException {
109 SvcLogicResource.QueryStatus status = SvcLogicResource.QueryStatus.SUCCESS;
110 Mockito.doReturn(status).when(aaiClient).update(Mockito.anyString(), Mockito.anyString(),
111 Mockito.anyMap(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
112 ctx = new SvcLogicContext();
113 AAIPluginImpl impl = new AAIPluginImpl();
115 impl.postGenericVnfData(params, ctx);
116 Assert.assertThat(ctx.getAttribute(org.onap.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE),
117 CoreMatchers.containsString("Operation PostGenericVnfData succeed for VNF ID null"));
121 public void testPostGenericVnfDataFailureThrownExeption() throws APPCException, SvcLogicException {
122 Mockito.doThrow(new SvcLogicException()).when(aaiClient).update(Mockito.anyString(), Mockito.anyString(),
123 Mockito.anyMap(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
124 ctx = new SvcLogicContext();
125 AAIPluginImpl impl = new AAIPluginImpl();
127 expectedEx.expect(APPCException.class);
128 expectedEx.expectCause(isA(SvcLogicException.class));
129 impl.postGenericVnfData(params, ctx);
133 public void testGetGenericVnfDataNotFound() throws APPCException, SvcLogicException {
134 SvcLogicResource.QueryStatus notFound = SvcLogicResource.QueryStatus.NOT_FOUND;
135 Mockito.doReturn(notFound).when(aaiClient).query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
136 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
137 ctx = new SvcLogicContext();
138 AAIPluginImpl impl = new AAIPluginImpl();
140 expectedEx.expect(APPCException.class);
141 expectedEx.expectMessage("VNF not found with vnf_id null");
142 impl.getGenericVnfData(params, ctx);
146 public void testGetGenericVnfDataFailure() throws APPCException, SvcLogicException {
147 SvcLogicResource.QueryStatus status = SvcLogicResource.QueryStatus.FAILURE;
148 Mockito.doReturn(status).when(aaiClient).query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
149 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
150 ctx = new SvcLogicContext();
151 AAIPluginImpl impl = new AAIPluginImpl();
153 expectedEx.expect(APPCException.class);
154 expectedEx.expectMessage("Error Querying AAI with vnfID = null");
155 impl.getGenericVnfData(params, ctx);
159 public void testGetGenericVnfDataSucces() throws APPCException, SvcLogicException {
160 SvcLogicResource.QueryStatus status = SvcLogicResource.QueryStatus.SUCCESS;
161 Mockito.doReturn(status).when(aaiClient).query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
162 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
163 ctx = new SvcLogicContext();
164 AAIPluginImpl impl = new AAIPluginImpl();
166 impl.getGenericVnfData(params, ctx);
167 Assert.assertThat(ctx.getAttribute(org.onap.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE),
168 CoreMatchers.containsString("Operation GetGenericVnfData succeed for VNF ID null"));
172 public void testGetGenericVnfDataFailureThrownExeption() throws APPCException, SvcLogicException {
173 Mockito.doThrow(new SvcLogicException()).when(aaiClient).query(Mockito.anyString(), Mockito.anyBoolean(),
174 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),
175 Mockito.any(SvcLogicContext.class));
176 ctx = new SvcLogicContext();
177 AAIPluginImpl impl = new AAIPluginImpl();
179 expectedEx.expect(APPCException.class);
180 expectedEx.expectCause(isA(SvcLogicException.class));
181 impl.getGenericVnfData(params, ctx);
185 public void testGetVnfHierarchyAaiExceptionFlow() throws APPCException, SvcLogicException {
186 SvcLogicResource.QueryStatus status = SvcLogicResource.QueryStatus.FAILURE;
187 Mockito.doReturn(status).when(aaiClient).query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
188 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
189 ctx = new SvcLogicContext();
190 params.put(Constants.RESOURCEKEY, "TEST_RESOURCE_KEY");
191 AAIPluginImpl impl = new AAIPluginImpl();
193 expectedEx.expect(APPCException.class);
194 expectedEx.expectMessage("Error Retrieving VNF hierarchy");
195 impl.getVnfHierarchy(params, ctx);
199 public void testGetVnfHierarchyAaiExceptionFlow2() throws APPCException, SvcLogicException {
200 Mockito.doThrow(new SvcLogicException()).when(aaiClient).query(Mockito.anyString(), Mockito.anyBoolean(),
201 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),
202 Mockito.any(SvcLogicContext.class));
203 ctx = new SvcLogicContext();
204 params.put(Constants.RESOURCEKEY, "TEST_RESOURCE_KEY");
205 AAIPluginImpl impl = new AAIPluginImpl();
207 expectedEx.expect(APPCException.class);
208 expectedEx.expectMessage("Error Retrieving VNF hierarchy");
209 impl.getVnfHierarchy(params, ctx);
213 public void testGetVnfHierarchyNoVMs() throws APPCException, SvcLogicException {
214 SvcLogicResource.QueryStatus status = SvcLogicResource.QueryStatus.SUCCESS;
215 Mockito.doReturn(status).when(aaiClient).query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
216 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
217 ctx = new SvcLogicContext();
218 params.put(Constants.RESOURCEKEY, "TEST_RESOURCE_KEY");
219 AAIPluginImpl impl = new AAIPluginImpl();
221 impl.getVnfHierarchy(params, ctx);
222 Assert.assertEquals("0", ctx.getAttribute("VNF.VMCount"));
226 public void testGetVnfHierarchy() throws APPCException, SvcLogicException, AAIQueryException {
227 String vnfId = "TEST_RESOURCE_KEY";
228 SvcLogicResource.QueryStatus status = SvcLogicResource.QueryStatus.SUCCESS;
229 Mockito.doReturn(status).when(aaiClient).query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
230 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
231 ctx = new SvcLogicContext();
232 params.put(Constants.RESOURCEKEY, "TEST_RESOURCE_KEY");
233 AAIPluginImpl impl = Mockito.spy(new AAIPluginImpl());
234 AAIQueryResult aaiQueryResult = new AAIQueryResult();
235 aaiQueryResult.getAdditionProperties().put("vnf-type", "TEST_VNF_TYPE");
236 Relationship relationship = new Relationship();
237 relationship.setRelatedTo("vserver");
238 aaiQueryResult.getRelationshipList().add(relationship);
239 Mockito.doReturn(aaiQueryResult).when(impl).readVnf(vnfId);
240 AAIQueryResult vmQueryResult = new AAIQueryResult();
241 vmQueryResult.getAdditionProperties().put("vserver-selflink", "TEST_VM_NAME");
242 Relationship vmRelationship = new Relationship();
243 vmRelationship.setRelatedTo("vnfc");
244 vmQueryResult.getRelationshipList().add(vmRelationship);
245 Mockito.doReturn(vmQueryResult).when(impl).readVM(null, null, null, null);
247 impl.getVnfHierarchy(params, ctx);
248 Assert.assertEquals(EELFResourceManager.format(Msg.SUCCESS_EVENT_MESSAGE, "GetVNFHierarchy", "VNF ID " + vnfId),
249 ctx.getAttribute(org.onap.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE));
253 public void testReadVM() throws AAIQueryException {
254 AAIPluginImpl impl = Mockito.spy(new AAIPluginImpl());
255 ctx = new SvcLogicContext();
256 ctx.setAttribute("VM.relationship-list.relationship_length", "1");
257 ctx.setAttribute("VM.relationship-list.relationship[0].relationship-data_length", "1");
258 ctx.setAttribute("VM.relationship-list.relationship[0].related-to-property_length", "1");
259 Mockito.doReturn(ctx).when(impl).readResource("vserver.vserver-id = 'null' AND tenant.tenant_id = 'null' AND "
260 + "cloud-region.cloud-owner = 'null' AND cloud-region.cloud-region-id = 'null'", "VM", "vserver");
262 Assert.assertEquals(null, impl.readVM(null, null, null, null).getAdditionProperties().get("resource-version"));
266 public void testGetResource() throws SvcLogicException, APPCException {
267 SvcLogicResource.QueryStatus status = SvcLogicResource.QueryStatus.SUCCESS;
268 Mockito.doReturn(status).when(aaiClient).query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
269 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
270 ctx = new SvcLogicContext();
271 params.put(Constants.RESOURCEKEY, "TEST_RESOURCE_KEY");
272 AAIPluginImpl impl = new AAIPluginImpl();
274 impl.getResource(params, ctx);
275 Assert.assertEquals("SUCCESS", ctx.getAttribute("getResource_result"));
279 public void testPostResource() throws SvcLogicException, APPCException {
280 SvcLogicResource.QueryStatus status = SvcLogicResource.QueryStatus.SUCCESS;
281 Mockito.doReturn(status).when(aaiClient).update(Mockito.anyString(), Mockito.anyString(),
282 Mockito.anyMap(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
283 ctx = new SvcLogicContext();
284 params.put(Constants.RESOURCEKEY, "TEST_RESOURCE_KEY");
285 AAIPluginImpl impl = new AAIPluginImpl();
287 impl.postResource(params, ctx);
288 Assert.assertEquals("SUCCESS", ctx.getAttribute("postResource_result"));
292 public void testDeleteResource() throws SvcLogicException, APPCException {
293 SvcLogicResource.QueryStatus status = SvcLogicResource.QueryStatus.SUCCESS;
294 Mockito.doReturn(status).when(aaiClient).delete(Mockito.anyString(), Mockito.anyString(),
295 Mockito.any(SvcLogicContext.class));
296 ctx = new SvcLogicContext();
297 params.put(Constants.RESOURCEKEY, "TEST_RESOURCE_KEY");
298 AAIPluginImpl impl = new AAIPluginImpl();
300 impl.deleteResource(params, ctx);
301 Assert.assertEquals("SUCCESS", ctx.getAttribute("deleteResource_result"));