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