2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.aai;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
29 import java.nio.charset.StandardCharsets;
30 import java.nio.file.Files;
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.concurrent.atomic.AtomicInteger;
34 import org.junit.Test;
35 import org.onap.aai.domain.yang.CloudRegion;
36 import org.onap.aai.domain.yang.GenericVnf;
37 import org.onap.aai.domain.yang.ModelVer;
38 import org.onap.aai.domain.yang.ServiceInstance;
39 import org.onap.aai.domain.yang.Tenant;
40 import org.onap.aai.domain.yang.VfModule;
41 import org.onap.aai.domain.yang.Vserver;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 public class AaiCqResponseTest {
46 private static final String ETE_VFMODULE = "Vfmodule_Ete_vFWCLvFWSNK_7ba1fbde_0";
47 private static final String ETE_VNF = "Ete_vFWCLvFWSNK_7ba1fbde_0";
48 private static final Logger LOGGER = LoggerFactory.getLogger(AaiCqResponseTest.class);
49 private static final String CQ_RESPONSE_SAMPLE =
50 "src/test/resources/org/onap/policy/aai/AaiCqResponseFull.json";
53 public void testConstructor() throws Exception {
54 String responseString = getAaiCqResponse();
56 AaiCqResponse aaiCqResponse;
57 aaiCqResponse = new AaiCqResponse(responseString);
58 assertNotNull(aaiCqResponse);
59 assertNotNull(aaiCqResponse.getInventoryResponseItems());
63 public void testMultiThreaded() throws Exception {
64 final AtomicInteger success = new AtomicInteger(0);
65 final String json = getAaiCqResponse();
67 Thread[] threads = new Thread[5];
68 for (int x = 0; x < threads.length; ++x) {
69 threads[x] = new Thread(() -> runIt(json, success));
72 for (Thread thread : threads) {
76 for (Thread thread : threads) {
80 assertEquals(threads.length, success.get());
84 public void testAaiMalformedCqResponse() throws Exception {
85 String responseString = Files.readString(
86 new File("src/test/resources/org/onap/policy/aai/AaiMalformedCqResponse.json").toPath(),
87 StandardCharsets.UTF_8);
89 AaiCqResponse aaiCqResponse;
90 aaiCqResponse = new AaiCqResponse(responseString);
91 for (Object aaiObj : aaiCqResponse.getInventoryResponseItems()) {
98 public void testGetItemByList() throws Exception {
100 * Read JSON String and add all AaiObjects
103 String responseString = "";
104 responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath()));
106 AaiCqResponse aaiCqResponse;
107 aaiCqResponse = new AaiCqResponse(responseString);
108 ArrayList<Vserver> vs = (ArrayList<Vserver>) aaiCqResponse.getItemListByType(Vserver.class);
110 assertEquals("f953c499-4b1e-426b-8c6d-e9e9f1fc730f", vs.get(0).getVserverId());
111 LOGGER.info(vs.get(0).getVserverId());
116 public void testGetServiceInstance() throws Exception {
117 String responseString = getAaiCqResponse();
119 AaiCqResponse aaiCqResponse;
120 aaiCqResponse = new AaiCqResponse(responseString);
121 ServiceInstance si = aaiCqResponse.getServiceInstance();
123 assertEquals("Service_Ete_Name7ba1fbde-6187-464a-a62d-d9dd25bdf4e8",
124 si.getServiceInstanceName());
125 LOGGER.info(si.getServiceInstanceName());
129 public void testGetDefaultCloudRegion() throws Exception {
130 String responseString = getAaiCqResponse();
132 AaiCqResponse aaiCqResponse;
133 aaiCqResponse = new AaiCqResponse(responseString);
134 CloudRegion cloudRegion = aaiCqResponse.getDefaultCloudRegion();
135 assertNotNull(cloudRegion);
136 assertEquals("RegionOne", cloudRegion.getCloudRegionId());
137 LOGGER.info(cloudRegion.getCloudRegionId());
141 public void testGetDefaultTenant() throws Exception {
142 String responseString = getAaiCqResponse();
144 AaiCqResponse aaiCqResponse;
145 aaiCqResponse = new AaiCqResponse(responseString);
146 Tenant tenant = aaiCqResponse.getDefaultTenant();
147 assertNotNull(tenant);
148 assertEquals("41d6d38489bd40b09ea8a6b6b852dcbd", tenant.getTenantId());
149 LOGGER.info(tenant.getTenantId());
153 public void testGetGenericVnfs() throws Exception {
154 String responseString = getAaiCqResponse();
156 AaiCqResponse aaiCqResponse;
157 aaiCqResponse = new AaiCqResponse(responseString);
158 List<GenericVnf> genericVnfList = aaiCqResponse.getGenericVnfs();
159 assertNotNull(genericVnfList);
160 for (GenericVnf genVnf : genericVnfList) {
161 LOGGER.info(genVnf.getVnfName());
167 public void testGetDefaultGenericVnf() throws Exception {
168 String responseString = getAaiCqResponse();
170 AaiCqResponse aaiCqResponse;
171 aaiCqResponse = new AaiCqResponse(responseString);
172 GenericVnf genVnf = aaiCqResponse.getDefaultGenericVnf();
173 assertNotNull(genVnf);
174 assertEquals(ETE_VNF, genVnf.getVnfName());
175 LOGGER.info(genVnf.getVnfName());
180 public void testGetGenericVnfByName() throws Exception {
181 String responseString = getAaiCqResponse();
183 AaiCqResponse aaiCqResponse;
184 aaiCqResponse = new AaiCqResponse(responseString);
185 GenericVnf genVnf = aaiCqResponse.getGenericVnfByVnfName(ETE_VNF);
186 assertNotNull(genVnf);
187 assertEquals("f17face5-69cb-4c88-9e0b-7426db7edddd", genVnf.getVnfId());
188 LOGGER.info(genVnf.getVnfId());
192 public void testGetGenericVnfByModelInvariantId() throws Exception {
193 String responseString = getAaiCqResponse();
195 AaiCqResponse aaiCqResponse;
196 aaiCqResponse = new AaiCqResponse(responseString);
198 aaiCqResponse.getGenericVnfByModelInvariantId("9a243c47-fd5f-43d1-bd2a-f17bd12a61f2");
199 assertNotNull(genVnf);
200 assertEquals("9a243c47-fd5f-43d1-bd2a-f17bd12a61f2", genVnf.getModelInvariantId());
201 LOGGER.info(genVnf.getModelInvariantId());
205 public void testGetGenericVnfByVfModuleModelInvariantId() throws Exception {
206 String responseString = getAaiCqResponse();
208 AaiCqResponse aaiCqResponse;
209 aaiCqResponse = new AaiCqResponse(responseString);
210 GenericVnf genVnf = aaiCqResponse
211 .getGenericVnfByVfModuleModelInvariantId("e6130d03-56f1-4b0a-9a1d-e1b2ebc30e0e");
212 assertNotNull(genVnf);
213 assertEquals(ETE_VNF, genVnf.getVnfName());
214 LOGGER.info(genVnf.getVnfName());
218 public void testGetAllVfModules() throws Exception {
219 String responseString = getAaiCqResponse();
221 AaiCqResponse aaiCqResponse;
222 aaiCqResponse = new AaiCqResponse(responseString);
223 List<VfModule> vfModuleList = aaiCqResponse.getAllVfModules();
224 assertNotNull(vfModuleList);
225 for (VfModule vfMod : vfModuleList) {
226 LOGGER.info(vfMod.getVfModuleName());
232 public void testGetVfModuleByVfModuleName() throws Exception {
233 String responseString = getAaiCqResponse();
235 AaiCqResponse aaiCqResponse;
236 aaiCqResponse = new AaiCqResponse(responseString);
237 VfModule vfModule = aaiCqResponse.getVfModuleByVfModuleName(ETE_VFMODULE);
238 assertNotNull(vfModule);
239 assertEquals(ETE_VFMODULE, vfModule.getVfModuleName());
240 LOGGER.info(vfModule.getVfModuleName());
245 public void testGetVfModuleByVfModelInvariantId() throws Exception {
246 String responseString = getAaiCqResponse();
248 AaiCqResponse aaiCqResponse;
249 aaiCqResponse = new AaiCqResponse(responseString);
251 aaiCqResponse.getVfModuleByVfModelInvariantId("e6130d03-56f1-4b0a-9a1d-e1b2ebc30e0e");
252 assertNotNull(vfModule);
253 assertEquals(ETE_VFMODULE, vfModule.getVfModuleName());
254 LOGGER.info(vfModule.getVfModuleName());
259 public void testGetDefaultVfModule() throws Exception {
260 String responseString = getAaiCqResponse();
262 AaiCqResponse aaiCqResponse;
263 aaiCqResponse = new AaiCqResponse(responseString);
264 VfModule vfModule = aaiCqResponse.getDefaultVfModule();
265 assertNotNull(vfModule);
266 assertEquals(ETE_VFMODULE, vfModule.getVfModuleName());
267 LOGGER.info(vfModule.getVfModuleName());
271 public void testGetVserver() throws Exception {
272 String responseString = getAaiCqResponse();
274 AaiCqResponse aaiCqResponse;
275 aaiCqResponse = new AaiCqResponse(responseString);
276 Vserver vserver = aaiCqResponse.getVserver();
277 assertNotNull(vserver);
278 assertEquals(ETE_VNF, vserver.getVserverName());
279 LOGGER.info(vserver.getVserverName());
284 public void testGetAllModelVer() throws Exception {
285 String responseString = getAaiCqResponse();
287 AaiCqResponse aaiCqResponse;
288 aaiCqResponse = new AaiCqResponse(responseString);
289 List<ModelVer> modelVerList = aaiCqResponse.getAllModelVer();
290 assertNotNull(modelVerList);
291 for (ModelVer modV : modelVerList) {
292 LOGGER.info(modV.getModelName());
298 public void testGetModelVerByVersionId() throws Exception {
299 String responseString = getAaiCqResponse();
301 AaiCqResponse aaiCqResponse;
302 aaiCqResponse = new AaiCqResponse(responseString);
304 aaiCqResponse.getModelVerByVersionId("189a5070-3bd5-45ac-8a1d-c84ca40b277b");
305 assertNotNull(modelVer);
306 assertEquals("vFWCL_vFWSNK bbefb8ce-2bde", modelVer.getModelName());
307 LOGGER.info(modelVer.getModelName());
312 public void testGetVfModuleCount() throws Exception {
313 String responseString = getAaiCqResponse();
314 AaiCqResponse aaiCqResponse = new AaiCqResponse(responseString);
315 int count = aaiCqResponse.getVfModuleCount("47958575-138f-452a-8c8d-d89b595f8164",
316 "e6130d03-56f1-4b0a-9a1d-e1b2ebc30e0e", "94b18b1d-cc91-4f43-911a-e6348665f292");
317 assertEquals(1, count);
321 * Provides sample CQ response.
323 * @return a CQ response
324 * @throws Exception file read exception
326 public String getAaiCqResponse() throws Exception {
327 return Files.readString(new File(CQ_RESPONSE_SAMPLE).toPath(), StandardCharsets.UTF_8);
331 * Creates a response object from the given json.
332 * @param json response
333 * @param success incremented if the test succeeds
335 private void runIt(String json, AtomicInteger success) {
336 AaiCqResponse cq = new AaiCqResponse(json);
337 List<VfModule> list = cq.getAllVfModules();
338 assertFalse(list.isEmpty());
339 success.incrementAndGet();