2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 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=========================================================
21 package org.openecomp.aai.rest.search;
23 import com.att.eelf.configuration.EELFLogger;
24 import com.att.eelf.configuration.EELFManager;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.Mockito;
28 import org.openecomp.aai.dbmap.DBConnectionType;
29 import org.openecomp.aai.introspection.Loader;
30 import org.openecomp.aai.introspection.LoaderFactory;
31 import org.openecomp.aai.introspection.ModelType;
32 import org.openecomp.aai.introspection.Version;
33 import org.openecomp.aai.serialization.db.DBSerializer;
34 import org.openecomp.aai.serialization.engines.QueryStyle;
35 import org.openecomp.aai.serialization.engines.TitanDBEngine;
36 import org.openecomp.aai.serialization.engines.TransactionalGraphEngine;
37 import org.openecomp.aai.serialization.queryformats.utils.UrlBuilder;
39 import javax.ws.rs.core.*;
42 import static org.junit.Assert.assertEquals;
43 import static org.junit.Assert.assertNotNull;
44 import static org.mockito.Matchers.anyObject;
45 import static org.mockito.Mockito.mock;
46 import static org.mockito.Mockito.when;
48 public class SearchProviderTest {
50 protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json");
52 private static final Set<Integer> VALID_HTTP_STATUS_CODES = new HashSet<>();
54 private static final Version version = Version.getLatest();
55 private static final ModelType introspectorFactoryType = ModelType.MOXY;
56 private static final QueryStyle queryStyle = QueryStyle.TRAVERSAL;
57 private static final DBConnectionType type = DBConnectionType.REALTIME;
59 private Loader loader;
60 private TransactionalGraphEngine dbEngine;
63 VALID_HTTP_STATUS_CODES.add(200);
64 VALID_HTTP_STATUS_CODES.add(201);
65 VALID_HTTP_STATUS_CODES.add(204);
68 private SearchProvider searchProvider;
70 private HttpHeaders httpHeaders;
72 private UriInfo uriInfo;
74 private MultivaluedMap<String, String> headersMultiMap;
75 private MultivaluedMap<String, String> queryParameters;
77 private List<String> aaiRequestContextList;
79 private List<MediaType> outputMediaTypes;
81 private static final EELFLogger logger = EELFManager.getInstance().getLogger(SearchProviderTest.class.getName());
85 logger.info("Starting the setup for the integration tests of Rest Endpoints");
86 System.setProperty("AJSC_HOME", ".");
87 System.setProperty("BUNDLECONFIG_DIR", "bundleconfig-local");
89 searchProvider = new SearchProvider();
90 httpHeaders = mock(HttpHeaders.class);
91 uriInfo = mock(UriInfo.class);
93 headersMultiMap = new MultivaluedHashMap<>();
94 queryParameters = Mockito.spy(new MultivaluedHashMap<>());
96 headersMultiMap.add("X-FromAppId", "JUNIT");
97 headersMultiMap.add("X-TransactionId", UUID.randomUUID().toString());
98 headersMultiMap.add("Real-Time", "true");
99 headersMultiMap.add("Accept", "application/json");
100 headersMultiMap.add("aai-request-context", "");
102 outputMediaTypes = new ArrayList<>();
103 outputMediaTypes.add(APPLICATION_JSON);
105 aaiRequestContextList = new ArrayList<>();
106 aaiRequestContextList.add("");
108 when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes);
109 when(httpHeaders.getRequestHeaders()).thenReturn(headersMultiMap);
110 when(httpHeaders.getRequestHeader("X-FromAppId")).thenReturn(Arrays.asList("JUNIT"));
111 when(httpHeaders.getRequestHeader("X-TransactionId")).thenReturn(Arrays.asList("JUNIT"));
113 when(httpHeaders.getRequestHeader("aai-request-context")).thenReturn(aaiRequestContextList);
116 when(uriInfo.getQueryParameters()).thenReturn(queryParameters);
117 when(uriInfo.getQueryParameters(false)).thenReturn(queryParameters);
119 // TODO - Check if this is valid since RemoveDME2QueryParameters seems to be very unreasonable
120 Mockito.doReturn(null).when(queryParameters).remove(anyObject());
122 when(httpHeaders.getMediaType()).thenReturn(APPLICATION_JSON);
123 loader = LoaderFactory.createLoaderForVersion(introspectorFactoryType, version);
124 dbEngine = new TitanDBEngine(
131 public void testNodesQueryInvalidData() throws Exception {
133 List<String> keys = new ArrayList<>();
134 keys.add("cloud-region.cloud-owner:test-aic");
136 List<String> includeStrings = new ArrayList<>();
137 includeStrings.add("cloud-region");
139 DBSerializer serializer = new DBSerializer(version, dbEngine, introspectorFactoryType, "JUNIT");
140 UrlBuilder urlBuilder = new UrlBuilder(version, serializer);
142 Response response = searchProvider.getNodesQueryResponse(
151 assertNotNull(response);
152 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
154 response = searchProvider.getNodesQueryResponse(
163 assertNotNull(response);
164 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
168 public void testNodesQueryInvalidHeaders() throws Exception {
170 List<String> keys = new ArrayList<>();
171 keys.add("cloud-region.cloud-owner:test-aic");
173 List<String> includeStrings = new ArrayList<>();
174 includeStrings.add("cloud-region");
176 httpHeaders = mock(HttpHeaders.class);
178 when(httpHeaders.getRequestHeader("X-FromAppId")).thenThrow(IllegalArgumentException.class);
179 when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes);
181 DBSerializer serializer = new DBSerializer(version, dbEngine, introspectorFactoryType, "JUNIT");
182 UrlBuilder urlBuilder = new UrlBuilder(version, serializer);
184 Response response = searchProvider.getNodesQueryResponse(
193 assertNotNull(response);
194 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
198 public void testGenericQueryInvalidData() throws Exception {
200 List<String> keys = new ArrayList<>();
201 keys.add("cloud-region.cloud-owner:test-aic");
203 List<String> includeStrings = new ArrayList<>();
204 includeStrings.add("cloud-region");
206 DBSerializer serializer = new DBSerializer(version, dbEngine, introspectorFactoryType, "JUNIT");
207 UrlBuilder urlBuilder = new UrlBuilder(version, serializer);
209 Response response = searchProvider.getGenericQueryResponse(
219 assertNotNull(response);
220 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
222 response = searchProvider.getNodesQueryResponse(
231 assertNotNull(response);
232 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
236 public void testGenericQueryInvalidHeaders() throws Exception {
238 List<String> keys = new ArrayList<>();
239 keys.add("cloud-region.cloud-owner:test-aic");
241 List<String> includeStrings = new ArrayList<>();
242 includeStrings.add("cloud-region");
244 httpHeaders = mock(HttpHeaders.class);
246 when(httpHeaders.getRequestHeader("X-FromAppId")).thenThrow(IllegalArgumentException.class);
247 when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes);
249 DBSerializer serializer = new DBSerializer(version, dbEngine, introspectorFactoryType, "JUNIT");
250 UrlBuilder urlBuilder = new UrlBuilder(version, serializer);
252 Response response = searchProvider.getGenericQueryResponse(
262 assertNotNull(response);
263 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());