Update the license for 2017-2018 license
[aai/traversal.git] / aai-traversal / src / test / java / org / onap / aai / dbgraphmap / SearchGraphNamedQueryTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 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 package org.onap.aai.dbgraphmap;
21
22 import com.google.gson.JsonObject;
23 import com.google.gson.JsonParser;
24
25 import org.apache.commons.io.IOUtils;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.mockito.Mockito;
29 import org.onap.aai.HttpTestUtil;
30 import org.onap.aai.PayloadUtil;
31 import org.onap.aai.dbmap.DBConnectionType;
32 import org.onap.aai.exceptions.AAIException;
33 import org.onap.aai.extensions.AAIExtensionMap;
34 import org.onap.aai.introspection.*;
35 import org.onap.aai.serialization.engines.QueryStyle;
36 import org.onap.aai.util.AAIApiVersion;
37 import org.onap.aai.util.AAIConstants;
38
39 import javax.servlet.http.HttpServletRequest;
40 import javax.ws.rs.core.*;
41
42 import java.io.File;
43 import java.io.IOException;
44 import java.io.InputStream;
45 import java.nio.file.Files;
46 import java.nio.file.Path;
47 import java.nio.file.Paths;
48 import java.util.*;
49
50 import static org.junit.Assert.assertEquals;
51 import static org.junit.Assert.assertNotNull;
52 import static org.junit.Assert.assertTrue;
53 import static org.mockito.Matchers.anyObject;
54 import static org.mockito.Mockito.*;
55
56 public class SearchGraphNamedQueryTest {
57
58     private SearchGraph searchGraph;
59
60     protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json");
61
62     private static final Set<Integer> VALID_HTTP_STATUS_CODES = new HashSet<>();
63
64     private final static Version version = Version.getLatest();
65     private final static ModelType introspectorFactoryType = ModelType.MOXY;
66     private final static QueryStyle queryStyle = QueryStyle.TRAVERSAL;
67     private final static DBConnectionType type = DBConnectionType.REALTIME;
68
69     static {
70         VALID_HTTP_STATUS_CODES.add(200);
71         VALID_HTTP_STATUS_CODES.add(201);
72         VALID_HTTP_STATUS_CODES.add(204);
73     }
74
75     private HttpHeaders httpHeaders;
76
77     private MultivaluedMap<String, String> headersMultiMap;
78     private MultivaluedMap<String, String> queryParameters;
79
80     private List<String> aaiRequestContextList;
81
82     private List<MediaType> outputMediaTypes;
83
84     private static boolean ranOnce = false;
85     
86     private HttpTestUtil httpTestUtil;
87
88     
89     private String getJsonValue(String json, String key ) {
90         JsonObject jsonObj = new JsonParser().parse(json).getAsJsonObject();
91         String strValue = "";
92         if ( jsonObj.isJsonObject()) {
93                 strValue = jsonObj.get(key).getAsString();
94         }
95         return strValue;
96     }
97     
98     private void addWidgets() {
99         String widgetPath = "." + AAIConstants.AAI_FILESEP + "src/main/resources" + AAIConstants.AAI_FILESEP + "etc" +
100                         AAIConstants.AAI_FILESEP + "scriptdata"+ AAIConstants.AAI_FILESEP + "widget-model-json";
101         
102         File dir = new File(widgetPath);
103         File[] files = dir.listFiles();
104         for ( File file : files) {
105                 try {
106                         Path path = Paths.get(widgetPath + AAIConstants.AAI_FILESEP + file.getName());
107                 String widgetPayload = new String(Files.readAllBytes(path));
108                 String modelInvariantId = getJsonValue(widgetPayload, "model-invariant-id");
109                 String widgetUri = "/aai/v12/service-design-and-creation/models/model/" + modelInvariantId;
110                 Response response     = httpTestUtil.doPut(widgetUri, widgetPayload);
111                 assertEquals("Expected the named-query to be created", 201, response.getStatus());
112                         } catch ( AAIException | IOException e) {
113                                 // TODO Auto-generated catch block
114                                 e.printStackTrace();
115                         }
116                 
117         }       
118     }
119     
120     private void addNamedQueries() {
121         String namedQueryPath = "." + AAIConstants.AAI_FILESEP + "src/main/resources" + AAIConstants.AAI_FILESEP + "etc" +
122                         AAIConstants.AAI_FILESEP + "scriptdata"+ AAIConstants.AAI_FILESEP + "named-query-json";
123         
124         File dir = new File(namedQueryPath);
125         File[] files = dir.listFiles();
126         for ( File file : files) {
127                 try {
128                         Path path = Paths.get(namedQueryPath + AAIConstants.AAI_FILESEP + file.getName());
129                 String namedQueryPayload = new String(Files.readAllBytes(path));
130                 String namedQueryUuid = getJsonValue(namedQueryPayload, "named-query-uuid");
131                 String namedQueryUri = "/aai/v12/service-design-and-creation/named-queries/named-query/" + namedQueryUuid;
132
133                 Response response     = httpTestUtil.doPut(namedQueryUri, namedQueryPayload);
134                 assertEquals("Expected the named-query to be created", 201, response.getStatus());
135                         } catch ( AAIException | IOException e) {
136                                 // TODO Auto-generated catch block
137                                 e.printStackTrace();
138                         }
139                 
140         }       
141     }
142     
143     private String addVersionToUri(String uri ) {
144         return "/aai/" + Version.getLatest() + "/" + uri;
145     }
146
147     @Before
148     public void setup(){
149         
150          httpTestUtil = new HttpTestUtil();
151
152         System.setProperty("AJSC_HOME", ".");
153         System.setProperty("BUNDLECONFIG_DIR", "src/main/resources");
154         
155
156         searchGraph = new SearchGraph();
157
158         httpHeaders         = mock(HttpHeaders.class);
159
160         headersMultiMap     = new MultivaluedHashMap<>();
161         queryParameters     = Mockito.spy(new MultivaluedHashMap<>());
162
163         headersMultiMap.add("X-FromAppId", "JUNIT");
164         headersMultiMap.add("X-TransactionId", UUID.randomUUID().toString());
165         headersMultiMap.add("Real-Time", "true");
166         headersMultiMap.add("Accept", "application/json");
167         headersMultiMap.add("aai-request-context", "");
168
169         outputMediaTypes = new ArrayList<>();
170         outputMediaTypes.add(APPLICATION_JSON);
171
172         aaiRequestContextList = new ArrayList<>();
173         aaiRequestContextList.add("");
174
175         when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes);
176         when(httpHeaders.getRequestHeaders()).thenReturn(headersMultiMap);
177         when(httpHeaders.getRequestHeader("X-FromAppId")).thenReturn(Arrays.asList("JUNIT"));
178         when(httpHeaders.getRequestHeader("X-TransactionId")).thenReturn(Arrays.asList("JUNIT"));
179
180         when(httpHeaders.getRequestHeader("aai-request-context")).thenReturn(aaiRequestContextList);
181
182
183         // TODO - Check if this is valid since RemoveDME2QueryParameters seems to be very unreasonable
184         Mockito.doReturn(null).when(queryParameters).remove(anyObject());
185
186         when(httpHeaders.getMediaType()).thenReturn(APPLICATION_JSON);
187
188         if ( !ranOnce ) {
189                 ranOnce = true;
190                 addWidgets();
191                 addNamedQueries();
192         }    
193     }
194
195
196     @Test
197     public void getDHVLogicalLinkByCircuitId_1_0_Test() throws Exception {
198
199                 AAIExtensionMap aaiExtMap = new AAIExtensionMap();                                              
200                 aaiExtMap.setHttpHeaders(httpHeaders);
201                 String queryParameters = PayloadUtil.getNamedQueryPayload("query-payload.DHVLogicalLinkByCircuitId-1.0.json");
202                 String putPayload = PayloadUtil.getNamedQueryPayload("logical-link.DHVLogicalLinkByCircuitId-1.0.json");
203                 
204         String linkName = getJsonValue(putPayload, "link-name");
205         String putUri = addVersionToUri("network/logical-links/logical-link/" + linkName);
206
207         Response response     = httpTestUtil.doPut(putUri, putPayload);
208         assertEquals("Expected the logical-link to be created", 201, response.getStatus());
209         
210         HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
211         Mockito.when(request.getContentType()).thenReturn("application/json");
212        
213                 aaiExtMap.setUri("/search/named-query");
214                 aaiExtMap.setApiVersion(AAIApiVersion.get());
215                 aaiExtMap.setServletRequest(request);
216                 
217                 SearchGraph searchGraph = new SearchGraph();
218                 response = searchGraph.runNamedQuery("JUNIT", "JUNIT", queryParameters, DBConnectionType.REALTIME, aaiExtMap);
219         System.out.println("response was\n" + response.getEntity().toString());
220         assertEquals("Expected success from query", 200, response.getStatus());
221         boolean hasLinkName = response.getEntity().toString().indexOf(linkName) > 0 ? true : false;
222         assertTrue("Response contains linkName", hasLinkName );
223     }
224
225     @Test
226     public void getSvcSubscriberModelInfo_1_0_Test() throws Exception {
227
228                 AAIExtensionMap aaiExtMap = new AAIExtensionMap();                                              
229                 aaiExtMap.setHttpHeaders(httpHeaders);
230                 String queryParameters = PayloadUtil.getNamedQueryPayload("query-payload.SvcSubscriberModelInfo-1.0.json");
231                 
232                 String putPayload = PayloadUtil.getNamedQueryPayload("model.SvcSubscriberModelInfo-1.0.json");
233         String modelInvariantId = getJsonValue(putPayload, "model-invariant-id");
234         String putUri = addVersionToUri("service-design-and-creation/models/model/" + modelInvariantId);
235         Response response     = httpTestUtil.doPut(putUri, putPayload);
236         assertEquals("Expected the model to be created", 201, response.getStatus());
237         
238         putPayload = PayloadUtil.getNamedQueryPayload("customer.SvcSubscriberModelInfo-1.0.json");
239         String globalCustomerId = getJsonValue(putPayload, "global-customer-id");
240         putUri = addVersionToUri("business/customers/customer/" + globalCustomerId);
241         response     = httpTestUtil.doPut(putUri, putPayload);
242         assertEquals("Expected the customer to be created", 201, response.getStatus());
243         
244         HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
245         Mockito.when(request.getContentType()).thenReturn("application/json");
246        
247                 aaiExtMap.setUri("/search/named-query");
248                 aaiExtMap.setApiVersion(AAIApiVersion.get());
249                 aaiExtMap.setServletRequest(request);
250                 
251                 SearchGraph searchGraph = new SearchGraph();
252                 response = searchGraph.runNamedQuery("JUNIT", "JUNIT", queryParameters, DBConnectionType.REALTIME, aaiExtMap);
253         assertEquals("Expected success from query", 200, response.getStatus());
254         boolean hasModelName = response.getEntity().toString().indexOf("junit-model-name") > 0 ? true : false;
255         assertTrue("Response contains modelName from model-ver", hasModelName );
256     }
257   
258     @Test
259     public void getClosedLoopNamedQuery_1_0_Test() throws Exception {
260
261                 AAIExtensionMap aaiExtMap = new AAIExtensionMap();                                              
262                 aaiExtMap.setHttpHeaders(httpHeaders);
263                 String queryParameters = PayloadUtil.getNamedQueryPayload("query-payload.closed-loop-named-query-1.0.json");
264                 
265                 String putPayload = PayloadUtil.getNamedQueryPayload("model.closed-loop-named-query-1.0.json");
266         String modelInvariantId = getJsonValue(putPayload, "model-invariant-id");
267         String putUri = addVersionToUri("service-design-and-creation/models/model/" + modelInvariantId);
268         Response response     = httpTestUtil.doPut(putUri, putPayload);
269         assertEquals("Expected the model to be created", 201, response.getStatus());     
270         
271         putPayload = PayloadUtil.getNamedQueryPayload("cloud-region.closed-loop-named-query-1.0.json");
272         String cloudOwner = getJsonValue(putPayload, "cloud-owner");
273         String cloudRegionId = getJsonValue(putPayload, "cloud-region-id");
274         putUri = addVersionToUri("cloud-infrastructure/cloud-regions/cloud-region/" + cloudOwner + "/" + cloudRegionId);
275         response     = httpTestUtil.doPut(putUri, putPayload);
276         assertEquals("Expected the cloud-region to be created", 201, response.getStatus());
277         
278         putPayload = PayloadUtil.getNamedQueryPayload("cloud-region2.closed-loop-named-query-1.0.json");
279         String cloudOwner2 = getJsonValue(putPayload, "cloud-owner");
280         String cloudRegionId2 = getJsonValue(putPayload, "cloud-region-id");
281         putUri = addVersionToUri("cloud-infrastructure/cloud-regions/cloud-region/" + cloudOwner2 + "/" + cloudRegionId2);
282         response     = httpTestUtil.doPut(putUri, putPayload);
283         assertEquals("Expected the cloud-region2 to be created", 201, response.getStatus()); 
284         
285         putPayload = PayloadUtil.getNamedQueryPayload("tenant.closed-loop-named-query-1.0.json");
286         String tenantId = getJsonValue(putPayload, "tenant-id");
287         putUri = addVersionToUri("cloud-infrastructure/cloud-regions/cloud-region/" + cloudOwner + "/" + cloudRegionId
288                         + "/tenants/tenant/" + tenantId);
289         response     = httpTestUtil.doPut(putUri, putPayload);
290         assertEquals("Expected the tenant to be created", 201, response.getStatus());        
291
292         putPayload = PayloadUtil.getNamedQueryPayload("tenant2.closed-loop-named-query-1.0.json");
293         String tenantId2 = getJsonValue(putPayload, "tenant-id");
294         putUri = addVersionToUri("cloud-infrastructure/cloud-regions/cloud-region/" + cloudOwner2 + "/" + cloudRegionId2
295                         + "/tenants/tenant/" + tenantId2);
296         response     = httpTestUtil.doPut(putUri, putPayload);
297         assertEquals("Expected the tenant2 to be created", 201, response.getStatus());
298         
299         putPayload = PayloadUtil.getNamedQueryPayload("vserver.closed-loop-named-query-1.0.json");
300         String vserverId = getJsonValue(putPayload, "vserver-id");
301         putUri = addVersionToUri("cloud-infrastructure/cloud-regions/cloud-region/" + cloudOwner + "/" + cloudRegionId
302                         + "/tenants/tenant/" + tenantId + "/vservers/vserver/" + vserverId);
303         response     = httpTestUtil.doPut(putUri, putPayload);
304         assertEquals("Expected the vserver to be created", 201, response.getStatus());        
305
306         putPayload = PayloadUtil.getNamedQueryPayload("vserver2.closed-loop-named-query-1.0.json");
307         String vserverId2 = getJsonValue(putPayload, "vserver-id");
308         putUri = addVersionToUri("cloud-infrastructure/cloud-regions/cloud-region/" + cloudOwner2 + "/" + cloudRegionId2
309                         + "/tenants/tenant/" + tenantId2 + "/vservers/vserver/" + vserverId2);
310         response     = httpTestUtil.doPut(putUri, putPayload);
311         assertEquals("Expected the vserver2 to be created", 201, response.getStatus());
312         
313         putPayload = PayloadUtil.getNamedQueryPayload("customer.closed-loop-named-query-1.0.json");
314         String globalCustomerId = getJsonValue(putPayload, "global-customer-id");
315         putUri = addVersionToUri("business/customers/customer/" + globalCustomerId);
316         response     = httpTestUtil.doPut(putUri, putPayload);
317         assertEquals("Expected the customer to be created", 201, response.getStatus());
318
319         putPayload = PayloadUtil.getNamedQueryPayload("generic-vnf.closed-loop-named-query-1.0.json");
320         String vnfId = getJsonValue(putPayload, "vnf-id");
321         putUri = addVersionToUri("network/generic-vnfs/generic-vnf/" + vnfId);
322         response     = httpTestUtil.doPut(putUri, putPayload);
323         assertEquals("Expected the generic-vnf to be created", 201, response.getStatus());
324         
325         HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
326         Mockito.when(request.getContentType()).thenReturn("application/json");
327        
328                 aaiExtMap.setUri("/search/named-query");
329                 aaiExtMap.setApiVersion(AAIApiVersion.get());
330                 aaiExtMap.setServletRequest(request);
331                 
332                 SearchGraph searchGraph = new SearchGraph();
333                 response = searchGraph.runNamedQuery("JUNIT", "JUNIT", queryParameters, DBConnectionType.REALTIME, aaiExtMap);
334         assertEquals("Expected success from query", 200, response.getStatus());
335         boolean hasModelName = response.getEntity().toString().indexOf("example-model-name-val-closed-loop") > 0 ? true : false;
336         assertTrue("Response contains modelName from model-ver", hasModelName );
337     }
338     
339     @Test
340     public void getComponentList_1_2_Test() throws Exception {
341
342                 AAIExtensionMap aaiExtMap = new AAIExtensionMap();                                              
343                 aaiExtMap.setHttpHeaders(httpHeaders);
344                 String queryParameters = PayloadUtil.getNamedQueryPayload("query-payload.ComponentList-1.2.json");
345                 
346                 String putPayload = PayloadUtil.getNamedQueryPayload("model.ComponentList-1.2.json");
347         String modelInvariantId = getJsonValue(putPayload, "model-invariant-id");
348         String putUri = addVersionToUri("service-design-and-creation/models/model/" + modelInvariantId);
349         Response response     = httpTestUtil.doPut(putUri, putPayload);
350         assertEquals("Expected the model to be created", 201, response.getStatus());
351                 
352         putPayload = PayloadUtil.getNamedQueryPayload("customer.ComponentList-1.2.json");
353         String globalCustomerId = getJsonValue(putPayload, "global-customer-id");
354         putUri = addVersionToUri("business/customers/customer/" + globalCustomerId);
355         response     = httpTestUtil.doPut(putUri, putPayload);
356         assertEquals("Expected the customer to be created", 201, response.getStatus());
357         
358         putPayload = PayloadUtil.getNamedQueryPayload("generic-vnf.ComponentList-1.2.json");
359         String vnfId = getJsonValue(putPayload, "vnf-id");
360         putUri = addVersionToUri("network/generic-vnfs/generic-vnf/" + vnfId);
361         response     = httpTestUtil.doPut(putUri, putPayload);
362         assertEquals("Expected the generic-vnf to be created", 201, response.getStatus());        
363
364         putPayload = PayloadUtil.getNamedQueryPayload("vf-module.ComponentList-1.2.json");
365         String vfModuleId = getJsonValue(putPayload, "vf-module-id");
366         putUri = addVersionToUri("network/generic-vnfs/generic-vnf/" + vnfId + "/vf-modules/vf-module/" + vfModuleId);
367         response     = httpTestUtil.doPut(putUri, putPayload);
368         assertEquals("Expected the vf-module to be created", 201, response.getStatus());
369         
370         putPayload = PayloadUtil.getNamedQueryPayload("cloud-region.ComponentList-1.2.json");
371         String cloudOwner = getJsonValue(putPayload, "cloud-owner");
372         String cloudRegionId = getJsonValue(putPayload, "cloud-region-id");
373         putUri = addVersionToUri("cloud-infrastructure/cloud-regions/cloud-region/" + cloudOwner + "/" + cloudRegionId);
374         response     = httpTestUtil.doPut(putUri, putPayload);
375         assertEquals("Expected the cloud-region to be created", 201, response.getStatus());
376         
377         putPayload = PayloadUtil.getNamedQueryPayload("tenant.ComponentList-1.2.json");
378         String tenantId = getJsonValue(putPayload, "tenant-id");
379         putUri = addVersionToUri("cloud-infrastructure/cloud-regions/cloud-region/" + cloudOwner + "/" + cloudRegionId
380                         + "/tenants/tenant/" + tenantId);
381         response     = httpTestUtil.doPut(putUri, putPayload);
382         assertEquals("Expected the tenant to be created", 201, response.getStatus());        
383         
384         putPayload = PayloadUtil.getNamedQueryPayload("vserver.ComponentList-1.2.json");
385         String vserverId = getJsonValue(putPayload, "vserver-id");
386         putUri = addVersionToUri("cloud-infrastructure/cloud-regions/cloud-region/" + cloudOwner + "/" + cloudRegionId
387                         + "/tenants/tenant/" + tenantId + "/vservers/vserver/" + vserverId);
388         response     = httpTestUtil.doPut(putUri, putPayload);
389         assertEquals("Expected the vserver to be created", 201, response.getStatus());        
390         
391         HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
392         Mockito.when(request.getContentType()).thenReturn("application/json");
393        
394                 aaiExtMap.setUri("/search/named-query");
395                 aaiExtMap.setApiVersion(AAIApiVersion.get());
396                 aaiExtMap.setServletRequest(request);
397                 
398                 SearchGraph searchGraph = new SearchGraph();
399                 response = searchGraph.runNamedQuery("JUNIT", "JUNIT", queryParameters, DBConnectionType.REALTIME, aaiExtMap);
400         assertEquals("Expected success from query", 200, response.getStatus());
401         boolean hasModelName = response.getEntity().toString().indexOf("example-model-name-val-component-list") > 0 ? true : false;
402         assertTrue("Response contains modelName from model-ver", hasModelName );
403     }    
404 }