1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
/*
* ============LICENSE_START=======================================================
* ONAP : APPC
* ================================================================================
* Copyright (C) 2018 Ericsson
* =============================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ============LICENSE_END=========================================================
*/
package org.onap.appc.dg.aai.impl;
import static org.hamcrest.CoreMatchers.isA;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.Mockito;
import org.onap.appc.dg.aai.exception.AAIQueryException;
import org.onap.appc.dg.aai.objects.AAIQueryResult;
import org.onap.appc.dg.aai.objects.Relationship;
import org.onap.appc.exceptions.APPCException;
import org.onap.appc.i18n.Msg;
import org.onap.ccsdk.sli.adaptors.aai.AAIClient;
import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
import org.onap.ccsdk.sli.core.sli.SvcLogicException;
import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
import org.osgi.framework.FrameworkUtil;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import com.att.eelf.i18n.EELFResourceManager;
import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.powermock.api.mockito.PowerMockito;
@RunWith(PowerMockRunner.class)
@PrepareForTest(FrameworkUtil.class)
public class AAIPluginImplTest {
private final BundleContext bundleContext = Mockito.mock(BundleContext.class);
private final Bundle bundleService = Mockito.mock(Bundle.class);
private final ServiceReference sref = Mockito.mock(ServiceReference.class);
private final AAIClient aaiClient = Mockito.mock(AAIClient.class);
private SvcLogicContext ctx;
private Map<String, String> params;
@Rule
public ExpectedException expectedEx = ExpectedException.none();
@Before
public void setUp() throws NoSuchFieldException, IllegalAccessException {
PowerMockito.mockStatic(FrameworkUtil.class);
PowerMockito.when(FrameworkUtil.getBundle(Matchers.any(Class.class))).thenReturn(bundleService);
PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext);
PowerMockito.when(bundleContext.getServiceReference(Matchers.any(Class.class))).thenReturn(sref);
PowerMockito.when(bundleContext.<AAIClient>getService(sref)).thenReturn(aaiClient);
params = new HashMap<String, String>();
params.put(Constants.AAI_INPUT_DATA + ".suffix", "TEST_DATA");
}
@Test
public void testPostGenericVnfDataNotFound() throws APPCException, SvcLogicException {
SvcLogicResource.QueryStatus status = SvcLogicResource.QueryStatus.NOT_FOUND;
Mockito.doReturn(status).when(aaiClient).update(Mockito.anyString(), Mockito.anyString(),
Mockito.anyMap(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
ctx = new SvcLogicContext();
AAIPluginImpl impl = new AAIPluginImpl();
impl.initialize();
expectedEx.expect(APPCException.class);
expectedEx.expectMessage("VNF not found with vnf_id null");
impl.postGenericVnfData(params, ctx);
}
@Test
public void testPostGenericVnfDataFailure() throws APPCException, SvcLogicException {
SvcLogicResource.QueryStatus status = SvcLogicResource.QueryStatus.FAILURE;
Mockito.doReturn(status).when(aaiClient).update(Mockito.anyString(), Mockito.anyString(),
Mockito.anyMap(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
ctx = new SvcLogicContext();
AAIPluginImpl impl = new AAIPluginImpl();
impl.initialize();
expectedEx.expect(APPCException.class);
expectedEx.expectMessage("Error Querying AAI with vnfID = null");
impl.postGenericVnfData(params, ctx);
}
@Test
public void testPostGenericVnfDataSucces() throws APPCException, SvcLogicException {
SvcLogicResource.QueryStatus status = SvcLogicResource.QueryStatus.SUCCESS;
Mockito.doReturn(status).when(aaiClient).update(Mockito.anyString(), Mockito.anyString(),
Mockito.anyMap(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
ctx = new SvcLogicContext();
AAIPluginImpl impl = new AAIPluginImpl();
impl.initialize();
impl.postGenericVnfData(params, ctx);
Assert.assertThat(ctx.getAttribute(org.onap.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE), CoreMatchers.containsString(
"Operation PostGenericVnfData succeed for VNF ID null"));
}
@Test
public void testPostGenericVnfDataFailureThrownExeption() throws APPCException, SvcLogicException {
Mockito.doThrow(new SvcLogicException()).when(aaiClient).update(Mockito.anyString(), Mockito.anyString(),
Mockito.anyMap(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
ctx = new SvcLogicContext();
AAIPluginImpl impl = new AAIPluginImpl();
impl.initialize();
expectedEx.expect(APPCException.class);
expectedEx.expectCause(isA(SvcLogicException.class));
impl.postGenericVnfData(params, ctx);
}
@Test
public void testGetGenericVnfDataNotFound() throws APPCException, SvcLogicException {
SvcLogicResource.QueryStatus notFound = SvcLogicResource.QueryStatus.NOT_FOUND;
Mockito.doReturn(notFound).when(aaiClient).query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
ctx = new SvcLogicContext();
AAIPluginImpl impl = new AAIPluginImpl();
impl.initialize();
expectedEx.expect(APPCException.class);
expectedEx.expectMessage("VNF not found with vnf_id null");
impl.getGenericVnfData(params, ctx);
}
@Test
public void testGetGenericVnfDataFailure() throws APPCException, SvcLogicException {
SvcLogicResource.QueryStatus status = SvcLogicResource.QueryStatus.FAILURE;
Mockito.doReturn(status).when(aaiClient).query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
ctx = new SvcLogicContext();
AAIPluginImpl impl = new AAIPluginImpl();
impl.initialize();
expectedEx.expect(APPCException.class);
expectedEx.expectMessage("Error Querying AAI with vnfID = null");
impl.getGenericVnfData(params, ctx);
}
@Test
public void testGetGenericVnfDataSucces() throws APPCException, SvcLogicException {
SvcLogicResource.QueryStatus status = SvcLogicResource.QueryStatus.SUCCESS;
Mockito.doReturn(status).when(aaiClient).query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
ctx = new SvcLogicContext();
AAIPluginImpl impl = new AAIPluginImpl();
impl.initialize();
impl.getGenericVnfData(params, ctx);
Assert.assertThat(ctx.getAttribute(org.onap.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE), CoreMatchers.containsString(
"Operation GetGenericVnfData succeed for VNF ID null"));
}
@Test
public void testGetGenericVnfDataFailureThrownExeption() throws APPCException, SvcLogicException {
Mockito.doThrow(new SvcLogicException()).when(aaiClient).query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
ctx = new SvcLogicContext();
AAIPluginImpl impl = new AAIPluginImpl();
impl.initialize();
expectedEx.expect(APPCException.class);
expectedEx.expectCause(isA(SvcLogicException.class));
impl.getGenericVnfData(params, ctx);
}
@Test
public void testGetVnfHierarchyAaiExceptionFlow() throws APPCException, SvcLogicException {
SvcLogicResource.QueryStatus status = SvcLogicResource.QueryStatus.FAILURE;
Mockito.doReturn(status).when(aaiClient).query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
ctx = new SvcLogicContext();
params.put(Constants.RESOURCEKEY, "TEST_RESOURCE_KEY");
AAIPluginImpl impl = new AAIPluginImpl();
impl.initialize();
expectedEx.expect(APPCException.class);
expectedEx.expectMessage("Error Retrieving VNF hierarchy");
impl.getVnfHierarchy(params, ctx);
}
@Test
public void testGetVnfHierarchyAaiExceptionFlow2() throws APPCException, SvcLogicException {
Mockito.doThrow(new SvcLogicException()).when(aaiClient).query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
ctx = new SvcLogicContext();
params.put(Constants.RESOURCEKEY, "TEST_RESOURCE_KEY");
AAIPluginImpl impl = new AAIPluginImpl();
impl.initialize();
expectedEx.expect(APPCException.class);
expectedEx.expectMessage("Error Retrieving VNF hierarchy");
impl.getVnfHierarchy(params, ctx);
}
@Test
public void testGetVnfHierarchyNoVMs() throws APPCException, SvcLogicException {
SvcLogicResource.QueryStatus status = SvcLogicResource.QueryStatus.SUCCESS;
Mockito.doReturn(status).when(aaiClient).query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
ctx = new SvcLogicContext();
params.put(Constants.RESOURCEKEY, "TEST_RESOURCE_KEY");
AAIPluginImpl impl = new AAIPluginImpl();
impl.initialize();
impl.getVnfHierarchy(params, ctx);
Assert.assertEquals("0", ctx.getAttribute("VNF.VMCount"));
}
@Test
public void testGetVnfHierarchy() throws APPCException, SvcLogicException, AAIQueryException {
String vnfId = "TEST_RESOURCE_KEY";
SvcLogicResource.QueryStatus status = SvcLogicResource.QueryStatus.SUCCESS;
Mockito.doReturn(status).when(aaiClient).query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
ctx = new SvcLogicContext();
params.put(Constants.RESOURCEKEY, "TEST_RESOURCE_KEY");
AAIPluginImpl impl = Mockito.spy(new AAIPluginImpl());
AAIQueryResult aaiQueryResult = new AAIQueryResult();
aaiQueryResult.getAdditionProperties().put("vnf-type", "TEST_VNF_TYPE");
Relationship relationship = new Relationship();
relationship.setRelatedTo("vserver");
aaiQueryResult.getRelationshipList().add(relationship);
Mockito.doReturn(aaiQueryResult).when(impl).readVnf(vnfId);
AAIQueryResult vmQueryResult = new AAIQueryResult();
vmQueryResult.getAdditionProperties().put("vserver-selflink", "TEST_VM_NAME");
Relationship vmRelationship = new Relationship();
vmRelationship.setRelatedTo("vnfc");
vmQueryResult.getRelationshipList().add(vmRelationship);
Mockito.doReturn(vmQueryResult).when(impl).readVM(null, null, null, null);
impl.initialize();
impl.getVnfHierarchy(params, ctx);
Assert.assertEquals(EELFResourceManager.format(Msg.SUCCESS_EVENT_MESSAGE, "GetVNFHierarchy", "VNF ID " + vnfId),
ctx.getAttribute(org.onap.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE));
}
@Test
public void testReadVM() throws AAIQueryException {
AAIPluginImpl impl = Mockito.spy(new AAIPluginImpl());
ctx = new SvcLogicContext();
ctx.setAttribute("VM.relationship-list.relationship_length", "1");
ctx.setAttribute("VM.relationship-list.relationship[0].relationship-data_length", "1");
ctx.setAttribute("VM.relationship-list.relationship[0].related-to-property_length", "1");
Mockito.doReturn(ctx).when(impl).readResource("vserver.vserver-id = 'null' AND tenant.tenant_id = 'null' AND "
+ "cloud-region.cloud-owner = 'null' AND cloud-region.cloud-region-id = 'null'", "VM", "vserver");
impl.initialize();
Assert.assertEquals(null, impl.readVM(null, null, null, null).getAdditionProperties().get("resource-version"));
}
@Test
public void testGetResource() throws SvcLogicException, APPCException {
SvcLogicResource.QueryStatus status = SvcLogicResource.QueryStatus.SUCCESS;
Mockito.doReturn(status).when(aaiClient).query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
ctx = new SvcLogicContext();
params.put(Constants.RESOURCEKEY, "TEST_RESOURCE_KEY");
AAIPluginImpl impl = new AAIPluginImpl();
impl.initialize();
impl.getResource(params, ctx);
Assert.assertEquals("SUCCESS",ctx.getAttribute("getResource_result"));
}
@Test
public void testPostResource() throws SvcLogicException, APPCException {
SvcLogicResource.QueryStatus status = SvcLogicResource.QueryStatus.SUCCESS;
Mockito.doReturn(status).when(aaiClient).update(Mockito.anyString(), Mockito.anyString(),
Mockito.anyMap(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
ctx = new SvcLogicContext();
params.put(Constants.RESOURCEKEY, "TEST_RESOURCE_KEY");
AAIPluginImpl impl = new AAIPluginImpl();
impl.initialize();
impl.postResource(params, ctx);
Assert.assertEquals("SUCCESS",ctx.getAttribute("postResource_result"));
}
@Test
public void testDeleteResource() throws SvcLogicException, APPCException {
SvcLogicResource.QueryStatus status = SvcLogicResource.QueryStatus.SUCCESS;
Mockito.doReturn(status).when(aaiClient).delete(Mockito.anyString(), Mockito.anyString(),
Mockito.any(SvcLogicContext.class));
ctx = new SvcLogicContext();
params.put(Constants.RESOURCEKEY, "TEST_RESOURCE_KEY");
AAIPluginImpl impl = new AAIPluginImpl();
impl.initialize();
impl.deleteResource(params, ctx);
Assert.assertEquals("SUCCESS",ctx.getAttribute("deleteResource_result"));
}
}
|