Added VFModule count
[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 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.assertNotNull;
25 import static org.junit.Assert.assertNull;
26
27 import java.io.File;
28 import java.nio.file.Files;
29 import java.util.ArrayList;
30 import java.util.List;
31 import org.junit.Test;
32 import org.onap.aai.domain.yang.CloudRegion;
33 import org.onap.aai.domain.yang.GenericVnf;
34 import org.onap.aai.domain.yang.ModelVer;
35 import org.onap.aai.domain.yang.ServiceInstance;
36 import org.onap.aai.domain.yang.Tenant;
37 import org.onap.aai.domain.yang.VfModule;
38 import org.onap.aai.domain.yang.Vserver;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 public class AaiCqResponseTest {
43     private static final String ETE_VFMODULE = "Vfmodule_Ete_vFWCLvFWSNK_7ba1fbde_0";
44     private static final String ETE_VNF = "Ete_vFWCLvFWSNK_7ba1fbde_0";
45     private static final Logger LOGGER = LoggerFactory.getLogger(AaiCqResponseTest.class);
46     private static final String CQ_RESPONSE_SAMPLE =
47         "src/test/resources/org/onap/policy/aai/AaiCqResponseFull.json";
48
49     @Test
50     public void testConstructor() throws Exception {
51         /*
52          * Read JSON String and add all AaiObjects
53          */
54
55         String responseString = "";
56         responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath()));
57
58         AaiCqResponse aaiCqResponse;
59         aaiCqResponse = new AaiCqResponse(responseString);
60         assertNotNull(aaiCqResponse);
61         assertNotNull(aaiCqResponse.getInventoryResponseItems());
62     }
63
64     @Test
65     public void testAaiMalformedCqResponse() throws Exception {
66         /*
67          * Read JSON String and add all AaiObjects
68          */
69
70         String responseString = "";
71         responseString = new String(Files.readAllBytes(
72             new File("src/test/resources/org/onap/policy/aai/AaiMalformedCqResponse.json")
73                 .toPath()));
74
75         AaiCqResponse aaiCqResponse;
76         aaiCqResponse = new AaiCqResponse(responseString);
77         for (Object aaiObj : aaiCqResponse.getInventoryResponseItems()) {
78             assertNull(aaiObj);
79         }
80
81     }
82
83     @Test
84     public void testGetItemByList() throws Exception {
85         /*
86          * Read JSON String and add all AaiObjects
87          */
88
89         String responseString = "";
90         responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath()));
91
92         AaiCqResponse aaiCqResponse;
93         aaiCqResponse = new AaiCqResponse(responseString);
94         ArrayList<Vserver> vs = (ArrayList<Vserver>) aaiCqResponse.getItemListByType(Vserver.class);
95         assertNotNull(vs);
96         assertEquals("f953c499-4b1e-426b-8c6d-e9e9f1fc730f", vs.get(0).getVserverId());
97         LOGGER.info(vs.get(0).getVserverId());
98
99     }
100
101     @Test
102     public void testGetServiceInstance() throws Exception {
103
104         String responseString = "";
105         responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath()));
106
107         AaiCqResponse aaiCqResponse;
108         aaiCqResponse = new AaiCqResponse(responseString);
109         ServiceInstance si = aaiCqResponse.getServiceInstance();
110         assertNotNull(si);
111         assertEquals("Service_Ete_Name7ba1fbde-6187-464a-a62d-d9dd25bdf4e8",
112             si.getServiceInstanceName());
113         LOGGER.info(si.getServiceInstanceName());
114     }
115
116     @Test
117     public void testGetDefaultCloudRegion() throws Exception {
118
119         String responseString = "";
120         responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath()));
121
122         AaiCqResponse aaiCqResponse;
123         aaiCqResponse = new AaiCqResponse(responseString);
124         CloudRegion cloudRegion = aaiCqResponse.getDefaultCloudRegion();
125         assertNotNull(cloudRegion);
126         assertEquals("RegionOne", cloudRegion.getCloudRegionId());
127         LOGGER.info(cloudRegion.getCloudRegionId());
128     }
129
130     @Test
131     public void testGetDefaultTenant() throws Exception {
132
133         String responseString = "";
134         responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath()));
135
136         AaiCqResponse aaiCqResponse;
137         aaiCqResponse = new AaiCqResponse(responseString);
138         Tenant tenant = aaiCqResponse.getDefaultTenant();
139         assertNotNull(tenant);
140         assertEquals("41d6d38489bd40b09ea8a6b6b852dcbd", tenant.getTenantId());
141         LOGGER.info(tenant.getTenantId());
142     }
143
144     @Test
145     public void testGetGenericVnfs() throws Exception {
146
147         String responseString = "";
148         responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath()));
149
150         AaiCqResponse aaiCqResponse;
151         aaiCqResponse = new AaiCqResponse(responseString);
152         List<GenericVnf> genericVnfList = aaiCqResponse.getGenericVnfs();
153         assertNotNull(genericVnfList);
154         for (GenericVnf genVnf : genericVnfList) {
155             LOGGER.info(genVnf.getVnfName());
156         }
157
158     }
159
160     @Test
161     public void testGetDefaultGenericVnf() throws Exception {
162
163         String responseString = "";
164         responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath()));
165
166         AaiCqResponse aaiCqResponse;
167         aaiCqResponse = new AaiCqResponse(responseString);
168         GenericVnf genVnf = aaiCqResponse.getDefaultGenericVnf();
169         assertNotNull(genVnf);
170         assertEquals(ETE_VNF, genVnf.getVnfName());
171         LOGGER.info(genVnf.getVnfName());
172
173     }
174
175     @Test
176     public void testGetGenericVnfByName() throws Exception {
177
178         String responseString = "";
179         responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath()));
180
181         AaiCqResponse aaiCqResponse;
182         aaiCqResponse = new AaiCqResponse(responseString);
183         GenericVnf genVnf = aaiCqResponse.getGenericVnfByVnfName(ETE_VNF);
184         assertNotNull(genVnf);
185         assertEquals("f17face5-69cb-4c88-9e0b-7426db7edddd", genVnf.getVnfId());
186         LOGGER.info(genVnf.getVnfId());
187     }
188
189     @Test
190     public void testGetGenericVnfByModelInvariantId() throws Exception {
191
192         String responseString = "";
193         responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath()));
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
207         String responseString = "";
208         responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath()));
209
210         AaiCqResponse aaiCqResponse;
211         aaiCqResponse = new AaiCqResponse(responseString);
212         GenericVnf genVnf = aaiCqResponse
213             .getGenericVnfByVfModuleModelInvariantId("e6130d03-56f1-4b0a-9a1d-e1b2ebc30e0e");
214         assertNotNull(genVnf);
215         assertEquals(ETE_VNF, genVnf.getVnfName());
216         LOGGER.info(genVnf.getVnfName());
217     }
218
219     @Test
220     public void testGetAllVfModules() throws Exception {
221
222         String responseString = "";
223         responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath()));
224
225         AaiCqResponse aaiCqResponse;
226         aaiCqResponse = new AaiCqResponse(responseString);
227         List<VfModule> vfModuleList = aaiCqResponse.getAllVfModules();
228         assertNotNull(vfModuleList);
229         for (VfModule vfMod : vfModuleList) {
230             LOGGER.info(vfMod.getVfModuleName());
231         }
232
233     }
234
235     @Test
236     public void testGetVfModuleByVfModuleName() throws Exception {
237
238         String responseString = "";
239         responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath()));
240
241         AaiCqResponse aaiCqResponse;
242         aaiCqResponse = new AaiCqResponse(responseString);
243         VfModule vfModule = aaiCqResponse.getVfModuleByVfModuleName(ETE_VFMODULE);
244         assertNotNull(vfModule);
245         assertEquals(ETE_VFMODULE, vfModule.getVfModuleName());
246         LOGGER.info(vfModule.getVfModuleName());
247
248     }
249
250     @Test
251     public void testGetVfModuleByVfModelInvariantId() throws Exception {
252
253         String responseString = "";
254         responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath()));
255
256         AaiCqResponse aaiCqResponse;
257         aaiCqResponse = new AaiCqResponse(responseString);
258         VfModule vfModule =
259             aaiCqResponse.getVfModuleByVfModelInvariantId("e6130d03-56f1-4b0a-9a1d-e1b2ebc30e0e");
260         assertNotNull(vfModule);
261         assertEquals(ETE_VFMODULE, vfModule.getVfModuleName());
262         LOGGER.info(vfModule.getVfModuleName());
263
264     }
265
266     @Test
267     public void testGetDefaultVfModule() throws Exception {
268
269         String responseString = "";
270         responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath()));
271
272         AaiCqResponse aaiCqResponse;
273         aaiCqResponse = new AaiCqResponse(responseString);
274         VfModule vfModule = aaiCqResponse.getDefaultVfModule();
275         assertNotNull(vfModule);
276         assertEquals(ETE_VFMODULE, vfModule.getVfModuleName());
277         LOGGER.info(vfModule.getVfModuleName());
278     }
279
280     @Test
281     public void testGetVserver() throws Exception {
282
283         String responseString = "";
284         responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath()));
285
286         AaiCqResponse aaiCqResponse;
287         aaiCqResponse = new AaiCqResponse(responseString);
288         Vserver vserver = aaiCqResponse.getVserver();
289         assertNotNull(vserver);
290         assertEquals(ETE_VNF, vserver.getVserverName());
291         LOGGER.info(vserver.getVserverName());
292
293     }
294
295     @Test
296     public void testGetAllModelVer() throws Exception {
297
298         String responseString = "";
299         responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath()));
300
301         AaiCqResponse aaiCqResponse;
302         aaiCqResponse = new AaiCqResponse(responseString);
303         List<ModelVer> modelVerList = aaiCqResponse.getAllModelVer();
304         assertNotNull(modelVerList);
305         for (ModelVer modV : modelVerList) {
306             LOGGER.info(modV.getModelName());
307         }
308
309     }
310
311     @Test
312     public void testGetModelVerByVersionId() throws Exception {
313
314         String responseString = "";
315         responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath()));
316
317         AaiCqResponse aaiCqResponse;
318         aaiCqResponse = new AaiCqResponse(responseString);
319         ModelVer modelVer =
320             aaiCqResponse.getModelVerByVersionId("189a5070-3bd5-45ac-8a1d-c84ca40b277b");
321         assertNotNull(modelVer);
322         assertEquals("vFWCL_vFWSNK bbefb8ce-2bde", modelVer.getModelName());
323         LOGGER.info(modelVer.getModelName());
324
325     }
326
327     @Test
328     public void testGetVfModuleCount() throws Exception {
329         String responseString =
330             new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath()));
331         AaiCqResponse aaiCqResponse;
332         aaiCqResponse = new AaiCqResponse(responseString);
333         int count = aaiCqResponse.getVfModuleCount("47958575-138f-452a-8c8d-d89b595f8164",
334             "e6130d03-56f1-4b0a-9a1d-e1b2ebc30e0e", "94b18b1d-cc91-4f43-911a-e6348665f292");
335         assertEquals(1, count);
336     }
337
338     /**
339      * Provides sample CQ response.
340      * 
341      * @return a CQ response
342      * @throws Exception file read exception
343      */
344     public String getAaiCqResponse() throws Exception {
345         String responseString = "";
346         responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath()));
347         return responseString;
348     }
349
350 }