Custom Query Code
[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.ServiceInstance;
35 import org.onap.aai.domain.yang.Tenant;
36 import org.onap.aai.domain.yang.VfModule;
37 import org.onap.aai.domain.yang.Vserver;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 public class AaiCqResponseTest {
42     private static final Logger LOGGER = LoggerFactory.getLogger(AaiCqResponseTest.class);
43     private static final String CQ_RESPONSE_SAMPLE = "src/test/resources/org/onap/policy/aai/AaiCqResponse.json";
44
45
46     @Test
47     public void testConstructor() throws Exception {
48         /*
49          * Read JSON String and add all AaiObjects
50          */
51
52         String responseString = "";
53         responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath()));
54
55         AaiCqResponse aaiCqResponse;
56         aaiCqResponse = new AaiCqResponse(responseString);
57         assertNotNull(aaiCqResponse);
58         assertNotNull(aaiCqResponse.getInventoryResponseItems());
59     }
60
61     @Test
62     public void testAaiMalformedCqResponse() throws Exception {
63         /*
64          * Read JSON String and add all AaiObjects
65          */
66
67         String responseString = "";
68         responseString = new String(Files
69                 .readAllBytes(new File("src/test/resources/org/onap/policy/aai/AaiMalformedCqResponse.json").toPath()));
70
71         AaiCqResponse aaiCqResponse;
72         aaiCqResponse = new AaiCqResponse(responseString);
73         for (Object aaiObj : aaiCqResponse.getInventoryResponseItems()) {
74             assertNull(aaiObj);
75         }
76
77
78     }
79
80     @Test
81     public void testGetItemByList() throws Exception {
82         /*
83          * Read JSON String and add all AaiObjects
84          */
85
86         String responseString = "";
87         responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath()));
88
89         AaiCqResponse aaiCqResponse;
90         aaiCqResponse = new AaiCqResponse(responseString);
91         ArrayList<Vserver> vs = (ArrayList<Vserver>) aaiCqResponse.getItemListByType(Vserver.class);
92         assertNotNull(vs);
93         assertEquals("e7f1db09-ff78-44fc-b256-69095c5556fb", vs.get(0).getVserverId());
94         LOGGER.info(vs.get(0).getVserverId());
95
96     }
97
98     @Test
99     public void testGetServiceInstance() throws Exception {
100
101         String responseString = "";
102         responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath()));
103
104         AaiCqResponse aaiCqResponse;
105         aaiCqResponse = new AaiCqResponse(responseString);
106         ServiceInstance si = aaiCqResponse.getServiceInstance();
107         assertNotNull(si);
108         assertEquals("vLoadBalancerMS-0211-1", si.getServiceInstanceName());
109         LOGGER.info(si.getServiceInstanceName());
110     }
111
112     @Test
113     public void testGetDefaultCloudRegion() throws Exception {
114
115         String responseString = "";
116         responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath()));
117
118         AaiCqResponse aaiCqResponse;
119         aaiCqResponse = new AaiCqResponse(responseString);
120         CloudRegion cloudRegion = aaiCqResponse.getDefaultCloudRegion();
121         assertNotNull(cloudRegion);
122         assertEquals("cr-16197-01-as988q", cloudRegion.getCloudRegionId());
123         LOGGER.info(cloudRegion.getCloudRegionId());
124     }
125
126     @Test
127     public void testGetDefaultTenant() throws Exception {
128
129         String responseString = "";
130         responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath()));
131
132         AaiCqResponse aaiCqResponse;
133         aaiCqResponse = new AaiCqResponse(responseString);
134         Tenant tenant = aaiCqResponse.getDefaultTenant();
135         assertNotNull(tenant);
136         assertEquals("tenant1-16197-as988q", tenant.getTenantId());
137         LOGGER.info(tenant.getTenantId());
138     }
139
140
141
142     @Test
143     public void testGetGenericVnfs() throws Exception {
144
145         String responseString = "";
146         responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath()));
147
148         AaiCqResponse aaiCqResponse;
149         aaiCqResponse = new AaiCqResponse(responseString);
150         List<GenericVnf> genericVnfList = aaiCqResponse.getGenericVnfs();
151         assertNotNull(genericVnfList);
152         for (GenericVnf genVnf : genericVnfList) {
153             LOGGER.info(genVnf.getVnfName());
154         }
155
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("TestVM-Vnf-0201-1", 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("TestVM-Vnf-0201-1");
184         assertNotNull(genVnf);
185         assertEquals("17044ef4-e7f3-46a1-af03-e2aa562f23ac", genVnf.getVnfId());
186         LOGGER.info(genVnf.getVnfId());
187     }
188
189
190     @Test
191     public void testGetGenericVnfByModelInvariantId() throws Exception {
192
193         String responseString = "";
194         responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath()));
195
196         AaiCqResponse aaiCqResponse;
197         aaiCqResponse = new AaiCqResponse(responseString);
198         GenericVnf genVnf = aaiCqResponse.getGenericVnfByModelInvariantId("724ab1cf-6120-49e8-b909-849963bed1d6");
199         assertNotNull(genVnf);
200         assertEquals("724ab1cf-6120-49e8-b909-849963bed1d6", genVnf.getModelInvariantId());
201         LOGGER.info(genVnf.getModelInvariantId());
202     }
203
204     @Test
205     public void testGetAllVfModules() 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         List<VfModule> vfModuleList = aaiCqResponse.getAllVfModules();
213         assertNotNull(vfModuleList);
214         for (VfModule vfMod : vfModuleList) {
215             LOGGER.info(vfMod.getVfModuleName());
216         }
217
218     }
219
220
221     @Test
222     public void testGetVfModuleByVfModuleName() throws Exception {
223
224         String responseString = "";
225         responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath()));
226
227         AaiCqResponse aaiCqResponse;
228         aaiCqResponse = new AaiCqResponse(responseString);
229         VfModule vfModule = aaiCqResponse.getVfModuleByVfModuleName("vLoadBalancerMS-0211-1");
230         assertNotNull(vfModule);
231         assertEquals("vLoadBalancerMS-0211-1", vfModule.getVfModuleName());
232         LOGGER.info(vfModule.getVfModuleName());
233
234
235     }
236
237     @Test
238     public void testGetDefaultVfModule() throws Exception {
239
240         String responseString = "";
241         responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath()));
242
243         AaiCqResponse aaiCqResponse;
244         aaiCqResponse = new AaiCqResponse(responseString);
245         VfModule vfModule = aaiCqResponse.getDefaultVfModule();
246         assertNotNull(vfModule);
247         assertEquals("TestVM-0201-2", vfModule.getVfModuleName());
248         LOGGER.info(vfModule.getVfModuleName());
249     }
250
251     @Test
252     public void testGetVserver() throws Exception {
253
254         String responseString = "";
255         responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath()));
256
257         AaiCqResponse aaiCqResponse;
258         aaiCqResponse = new AaiCqResponse(responseString);
259         Vserver vserver = aaiCqResponse.getVserver();
260         assertNotNull(vserver);
261         assertEquals("vfw-vm-0201-2", vserver.getVserverName());
262         LOGGER.info(vserver.getVserverName());
263
264     }
265
266     /**
267      * Aai Cq sample response.
268      *
269      * @return String return response
270      * @throws Exception file read exception
271      */
272     public String getAaiCqResponse() throws Exception {
273         String responseString = "";
274         responseString = new String(Files.readAllBytes(new File(CQ_RESPONSE_SAMPLE).toPath()));
275         return responseString;
276     }
277
278 }