Fix race condition in AaiCqResponse
[policy/models.git] / models-interactions / model-impl / aai / src / test / java / org / onap / policy / aai / AaiCqResponseTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.aai;
22
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;
27
28 import java.io.File;
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;
44
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";
51
52     @Test
53     public void testConstructor() throws Exception {
54         String responseString = getAaiCqResponse();
55
56         AaiCqResponse aaiCqResponse;
57         aaiCqResponse = new AaiCqResponse(responseString);
58         assertNotNull(aaiCqResponse);
59         assertNotNull(aaiCqResponse.getInventoryResponseItems());
60     }
61
62     @Test
63     public void testMultiThreaded() throws Exception {
64         final AtomicInteger success = new AtomicInteger(0);
65         final String json = getAaiCqResponse();
66
67         Thread[] threads = new Thread[5];
68         for (int x = 0; x < threads.length; ++x) {
69             threads[x] = new Thread(() -> runIt(json, success));
70         }
71
72         for (Thread thread : threads) {
73             thread.start();
74         }
75
76         for (Thread thread : threads) {
77             thread.join();
78         }
79
80         assertEquals(threads.length, success.get());
81     }
82
83     @Test
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);
88
89         AaiCqResponse aaiCqResponse;
90         aaiCqResponse = new AaiCqResponse(responseString);
91         for (Object aaiObj : aaiCqResponse.getInventoryResponseItems()) {
92             assertNull(aaiObj);
93         }
94
95     }
96
97     @Test
98     public void testGetItemByList() throws Exception {
99         /*
100          * Read JSON String and add all AaiObjects
101          */
102
103         String responseString = "";
104         responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath()));
105
106         AaiCqResponse aaiCqResponse;
107         aaiCqResponse = new AaiCqResponse(responseString);
108         ArrayList<Vserver> vs = (ArrayList<Vserver>) aaiCqResponse.getItemListByType(Vserver.class);
109         assertNotNull(vs);
110         assertEquals("f953c499-4b1e-426b-8c6d-e9e9f1fc730f", vs.get(0).getVserverId());
111         LOGGER.info(vs.get(0).getVserverId());
112
113     }
114
115     @Test
116     public void testGetServiceInstance() throws Exception {
117         String responseString = getAaiCqResponse();
118
119         AaiCqResponse aaiCqResponse;
120         aaiCqResponse = new AaiCqResponse(responseString);
121         ServiceInstance si = aaiCqResponse.getServiceInstance();
122         assertNotNull(si);
123         assertEquals("Service_Ete_Name7ba1fbde-6187-464a-a62d-d9dd25bdf4e8",
124             si.getServiceInstanceName());
125         LOGGER.info(si.getServiceInstanceName());
126     }
127
128     @Test
129     public void testGetDefaultCloudRegion() throws Exception {
130         String responseString = getAaiCqResponse();
131
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());
138     }
139
140     @Test
141     public void testGetDefaultTenant() throws Exception {
142         String responseString = getAaiCqResponse();
143
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());
150     }
151
152     @Test
153     public void testGetGenericVnfs() throws Exception {
154         String responseString = getAaiCqResponse();
155
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());
162         }
163
164     }
165
166     @Test
167     public void testGetDefaultGenericVnf() throws Exception {
168         String responseString = getAaiCqResponse();
169
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());
176
177     }
178
179     @Test
180     public void testGetGenericVnfByName() throws Exception {
181         String responseString = getAaiCqResponse();
182
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());
189     }
190
191     @Test
192     public void testGetGenericVnfByModelInvariantId() throws Exception {
193         String responseString = getAaiCqResponse();
194
195         AaiCqResponse aaiCqResponse;
196         aaiCqResponse = new AaiCqResponse(responseString);
197         GenericVnf genVnf =
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());
202     }
203
204     @Test
205     public void testGetGenericVnfByVfModuleModelInvariantId() throws Exception {
206         String responseString = getAaiCqResponse();
207
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());
215     }
216
217     @Test
218     public void testGetAllVfModules() throws Exception {
219         String responseString = getAaiCqResponse();
220
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());
227         }
228
229     }
230
231     @Test
232     public void testGetVfModuleByVfModuleName() throws Exception {
233         String responseString = getAaiCqResponse();
234
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());
241
242     }
243
244     @Test
245     public void testGetVfModuleByVfModelInvariantId() throws Exception {
246         String responseString = getAaiCqResponse();
247
248         AaiCqResponse aaiCqResponse;
249         aaiCqResponse = new AaiCqResponse(responseString);
250         VfModule vfModule =
251             aaiCqResponse.getVfModuleByVfModelInvariantId("e6130d03-56f1-4b0a-9a1d-e1b2ebc30e0e");
252         assertNotNull(vfModule);
253         assertEquals(ETE_VFMODULE, vfModule.getVfModuleName());
254         LOGGER.info(vfModule.getVfModuleName());
255
256     }
257
258     @Test
259     public void testGetDefaultVfModule() throws Exception {
260         String responseString = getAaiCqResponse();
261
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());
268     }
269
270     @Test
271     public void testGetVserver() throws Exception {
272         String responseString = getAaiCqResponse();
273
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());
280
281     }
282
283     @Test
284     public void testGetAllModelVer() throws Exception {
285         String responseString = getAaiCqResponse();
286
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());
293         }
294
295     }
296
297     @Test
298     public void testGetModelVerByVersionId() throws Exception {
299         String responseString = getAaiCqResponse();
300
301         AaiCqResponse aaiCqResponse;
302         aaiCqResponse = new AaiCqResponse(responseString);
303         ModelVer modelVer =
304             aaiCqResponse.getModelVerByVersionId("189a5070-3bd5-45ac-8a1d-c84ca40b277b");
305         assertNotNull(modelVer);
306         assertEquals("vFWCL_vFWSNK bbefb8ce-2bde", modelVer.getModelName());
307         LOGGER.info(modelVer.getModelName());
308
309     }
310
311     @Test
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);
318     }
319
320     /**
321      * Provides sample CQ response.
322      *
323      * @return a CQ response
324      * @throws Exception file read exception
325      */
326     public String getAaiCqResponse() throws Exception {
327         return Files.readString(new File(CQ_RESPONSE_SAMPLE).toPath(), StandardCharsets.UTF_8);
328     }
329
330     /**
331      * Creates a response object from the given json.
332      * @param json response
333      * @param success incremented if the test succeeds
334      */
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();
340     }
341
342 }