2 * ============LICENSE_START=======================================================
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
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=========================================================
20 package org.onap.aai.dbgraphmap;
22 import com.google.gson.JsonObject;
23 import com.google.gson.JsonParser;
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;
39 import javax.servlet.http.HttpServletRequest;
40 import javax.ws.rs.core.*;
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;
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.*;
56 public class SearchGraphNamedQueryTest {
58 private SearchGraph searchGraph;
60 protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json");
62 private static final Set<Integer> VALID_HTTP_STATUS_CODES = new HashSet<>();
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;
70 VALID_HTTP_STATUS_CODES.add(200);
71 VALID_HTTP_STATUS_CODES.add(201);
72 VALID_HTTP_STATUS_CODES.add(204);
75 private HttpHeaders httpHeaders;
77 private MultivaluedMap<String, String> headersMultiMap;
78 private MultivaluedMap<String, String> queryParameters;
80 private List<String> aaiRequestContextList;
82 private List<MediaType> outputMediaTypes;
84 private static boolean ranOnce = false;
86 private HttpTestUtil httpTestUtil;
89 private String getJsonValue(String json, String key ) {
90 JsonObject jsonObj = new JsonParser().parse(json).getAsJsonObject();
92 if ( jsonObj.isJsonObject()) {
93 strValue = jsonObj.get(key).getAsString();
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";
102 File dir = new File(widgetPath);
103 File[] files = dir.listFiles();
104 for ( File file : files) {
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
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";
124 File dir = new File(namedQueryPath);
125 File[] files = dir.listFiles();
126 for ( File file : files) {
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;
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
143 private String addVersionToUri(String uri ) {
144 return "/aai/" + Version.getLatest() + "/" + uri;
150 httpTestUtil = new HttpTestUtil();
152 System.setProperty("AJSC_HOME", ".");
153 System.setProperty("BUNDLECONFIG_DIR", "src/main/resources");
156 searchGraph = new SearchGraph();
158 httpHeaders = mock(HttpHeaders.class);
160 headersMultiMap = new MultivaluedHashMap<>();
161 queryParameters = Mockito.spy(new MultivaluedHashMap<>());
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", "");
169 outputMediaTypes = new ArrayList<>();
170 outputMediaTypes.add(APPLICATION_JSON);
172 aaiRequestContextList = new ArrayList<>();
173 aaiRequestContextList.add("");
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"));
180 when(httpHeaders.getRequestHeader("aai-request-context")).thenReturn(aaiRequestContextList);
183 // TODO - Check if this is valid since RemoveDME2QueryParameters seems to be very unreasonable
184 Mockito.doReturn(null).when(queryParameters).remove(anyObject());
186 when(httpHeaders.getMediaType()).thenReturn(APPLICATION_JSON);
197 public void getDHVLogicalLinkByCircuitId_1_0_Test() throws Exception {
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");
204 String linkName = getJsonValue(putPayload, "link-name");
205 String putUri = addVersionToUri("network/logical-links/logical-link/" + linkName);
207 Response response = httpTestUtil.doPut(putUri, putPayload);
208 assertEquals("Expected the logical-link to be created", 201, response.getStatus());
210 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
211 Mockito.when(request.getContentType()).thenReturn("application/json");
213 aaiExtMap.setUri("/search/named-query");
214 aaiExtMap.setApiVersion(AAIApiVersion.get());
215 aaiExtMap.setServletRequest(request);
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 );
226 public void getSvcSubscriberModelInfo_1_0_Test() throws Exception {
228 AAIExtensionMap aaiExtMap = new AAIExtensionMap();
229 aaiExtMap.setHttpHeaders(httpHeaders);
230 String queryParameters = PayloadUtil.getNamedQueryPayload("query-payload.SvcSubscriberModelInfo-1.0.json");
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());
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());
244 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
245 Mockito.when(request.getContentType()).thenReturn("application/json");
247 aaiExtMap.setUri("/search/named-query");
248 aaiExtMap.setApiVersion(AAIApiVersion.get());
249 aaiExtMap.setServletRequest(request);
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 );
259 public void getClosedLoopNamedQuery_1_0_Test() throws Exception {
261 AAIExtensionMap aaiExtMap = new AAIExtensionMap();
262 aaiExtMap.setHttpHeaders(httpHeaders);
263 String queryParameters = PayloadUtil.getNamedQueryPayload("query-payload.closed-loop-named-query-1.0.json");
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());
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());
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());
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());
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());
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());
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());
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());
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());
325 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
326 Mockito.when(request.getContentType()).thenReturn("application/json");
328 aaiExtMap.setUri("/search/named-query");
329 aaiExtMap.setApiVersion(AAIApiVersion.get());
330 aaiExtMap.setServletRequest(request);
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 );
340 public void getComponentList_1_2_Test() throws Exception {
342 AAIExtensionMap aaiExtMap = new AAIExtensionMap();
343 aaiExtMap.setHttpHeaders(httpHeaders);
344 String queryParameters = PayloadUtil.getNamedQueryPayload("query-payload.ComponentList-1.2.json");
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());
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());
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());
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());
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());
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());
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());
391 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
392 Mockito.when(request.getContentType()).thenReturn("application/json");
394 aaiExtMap.setUri("/search/named-query");
395 aaiExtMap.setApiVersion(AAIApiVersion.get());
396 aaiExtMap.setServletRequest(request);
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 );