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.rest.search;
24 import com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
26 import org.apache.commons.io.IOUtils;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.Mockito;
30 import org.onap.aai.dbmap.DBConnectionType;
31 import org.onap.aai.introspection.Loader;
32 import org.onap.aai.introspection.LoaderFactory;
33 import org.onap.aai.introspection.ModelType;
34 import org.onap.aai.introspection.Version;
35 import org.onap.aai.serialization.db.DBSerializer;
36 import org.onap.aai.serialization.engines.QueryStyle;
37 import org.onap.aai.serialization.engines.TitanDBEngine;
38 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
39 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
41 import javax.servlet.http.HttpServletRequest;
42 import javax.ws.rs.core.*;
43 import java.io.IOException;
44 import java.io.InputStream;
47 import static org.junit.Assert.assertEquals;
48 import static org.junit.Assert.assertNotNull;
49 import static org.mockito.Matchers.anyObject;
50 import static org.mockito.Mockito.mock;
51 import static org.mockito.Mockito.when;
53 public class ModelAndNamedQueryRestProviderTest {
55 protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json");
57 private static final Set<Integer> VALID_HTTP_STATUS_CODES = new HashSet<>();
59 private static final Version version = Version.getLatest();
60 private static final ModelType introspectorFactoryType = ModelType.MOXY;
61 private static final QueryStyle queryStyle = QueryStyle.TRAVERSAL;
62 private static final DBConnectionType type = DBConnectionType.REALTIME;
64 private Loader loader;
65 private TransactionalGraphEngine dbEngine;
68 VALID_HTTP_STATUS_CODES.add(200);
69 VALID_HTTP_STATUS_CODES.add(201);
70 VALID_HTTP_STATUS_CODES.add(204);
73 private ModelAndNamedQueryRestProvider modelAndNamedQueryRestProvider;
75 private HttpHeaders httpHeaders;
77 private UriInfo uriInfo;
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 final EELFLogger logger = EELFManager.getInstance().getLogger(ModelAndNamedQueryRestProviderTest.class.getName());
90 logger.info("Starting the setup for the integration tests of Rest Endpoints");
91 System.setProperty("AJSC_HOME", ".");
92 System.setProperty("BUNDLECONFIG_DIR", "bundleconfig-local");
94 modelAndNamedQueryRestProvider = new ModelAndNamedQueryRestProvider();
95 httpHeaders = mock(HttpHeaders.class);
96 uriInfo = mock(UriInfo.class);
98 when(uriInfo.getPath()).thenReturn("JUNITURI");
100 headersMultiMap = new MultivaluedHashMap<>();
101 queryParameters = Mockito.spy(new MultivaluedHashMap<>());
103 headersMultiMap.add("X-FromAppId", "JUNIT");
104 headersMultiMap.add("X-TransactionId", UUID.randomUUID().toString());
105 headersMultiMap.add("Real-Time", "true");
106 headersMultiMap.add("Accept", "application/json");
107 headersMultiMap.add("aai-request-context", "");
109 outputMediaTypes = new ArrayList<>();
110 outputMediaTypes.add(APPLICATION_JSON);
112 aaiRequestContextList = new ArrayList<>();
113 aaiRequestContextList.add("");
115 when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes);
116 when(httpHeaders.getRequestHeaders()).thenReturn(headersMultiMap);
117 when(httpHeaders.getRequestHeader("X-FromAppId")).thenReturn(Arrays.asList("JUNIT"));
118 when(httpHeaders.getRequestHeader("X-TransactionId")).thenReturn(Arrays.asList("JUNIT"));
120 when(httpHeaders.getRequestHeader("aai-request-context")).thenReturn(aaiRequestContextList);
123 when(uriInfo.getQueryParameters()).thenReturn(queryParameters);
124 when(uriInfo.getQueryParameters(false)).thenReturn(queryParameters);
126 // TODO - Check if this is valid since RemoveDME2QueryParameters seems to be very unreasonable
127 Mockito.doReturn(null).when(queryParameters).remove(anyObject());
129 when(httpHeaders.getMediaType()).thenReturn(APPLICATION_JSON);
130 loader = LoaderFactory.createLoaderForVersion(introspectorFactoryType, version);
131 dbEngine = new TitanDBEngine(
138 public void testNamedQueryWhenNoDataToBeFoundReturnHttpNotFound() throws Exception {
140 String queryParameters = getPayload("payloads/named-queries/named-query.json");
141 HttpServletRequest request = mock(HttpServletRequest.class);
143 when(request.getContentType()).thenReturn("application/json");
145 Response response = modelAndNamedQueryRestProvider.getNamedQueryResponse(
152 assertNotNull(response);
153 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus());
157 public void testNamedQueryInvalidHeaders() throws Exception {
159 httpHeaders = mock(HttpHeaders.class);
161 when(httpHeaders.getRequestHeader("X-FromAppId")).thenThrow(IllegalArgumentException.class);
162 when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes);
164 DBSerializer serializer = new DBSerializer(version, dbEngine, introspectorFactoryType, "JUNIT");
165 UrlBuilder urlBuilder = new UrlBuilder(version, serializer);
167 Response response = modelAndNamedQueryRestProvider.getNamedQueryResponse(
174 assertNotNull(response);
175 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
179 public void testNamedQueryCallTimeoutThrown() throws Exception {
181 String queryParameters = getPayload("payloads/named-queries/named-query.json");
182 HttpServletRequest request = mock(HttpServletRequest.class);
184 headersMultiMap.putSingle("X-FromAppId", "JUNITTESTAPP1");
185 when(httpHeaders.getRequestHeaders()).thenReturn(headersMultiMap);
187 when(request.getContentType()).thenReturn("application/json");
189 Response response = modelAndNamedQueryRestProvider.getNamedQueryResponse(
196 assertNotNull(response);
197 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
201 public void testNamedQueryCallTimeoutBypassed() throws Exception {
203 String queryParameters = getPayload("payloads/named-queries/named-query.json");
204 HttpServletRequest request = mock(HttpServletRequest.class);
206 headersMultiMap.putSingle("X-FromAppId", "JUNITTESTAPP2");
207 when(httpHeaders.getRequestHeaders()).thenReturn(headersMultiMap);
209 when(request.getContentType()).thenReturn("application/json");
211 Response response = modelAndNamedQueryRestProvider.getNamedQueryResponse(
218 assertNotNull(response);
219 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus());
222 public String getPayload(String filename) throws IOException {
224 InputStream inputStream = getClass()
226 .getResourceAsStream(filename);
228 String message = String.format("Unable to find the %s in src/test/resources", filename);
229 assertNotNull(message, inputStream);
231 String resource = IOUtils.toString(inputStream);