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.rest.search;
22 import com.att.eelf.configuration.EELFLogger;
23 import com.att.eelf.configuration.EELFManager;
24 import org.apache.commons.io.IOUtils;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.Mockito;
28 import org.onap.aai.dbmap.DBConnectionType;
29 import org.onap.aai.introspection.Loader;
30 import org.onap.aai.introspection.LoaderFactory;
31 import org.onap.aai.introspection.ModelType;
32 import org.onap.aai.introspection.Version;
33 import org.onap.aai.serialization.db.DBSerializer;
34 import org.onap.aai.serialization.engines.QueryStyle;
35 import org.onap.aai.serialization.engines.TitanDBEngine;
36 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
37 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
39 import javax.servlet.http.HttpServletRequest;
40 import javax.ws.rs.core.*;
41 import java.io.IOException;
42 import java.io.InputStream;
45 import static org.junit.Assert.assertEquals;
46 import static org.junit.Assert.assertNotNull;
47 import static org.mockito.Matchers.anyObject;
48 import static org.mockito.Mockito.mock;
49 import static org.mockito.Mockito.when;
51 public class ModelAndNamedQueryRestProviderTest {
53 protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json");
55 private static final Set<Integer> VALID_HTTP_STATUS_CODES = new HashSet<>();
57 private static final Version version = Version.getLatest();
58 private static final ModelType introspectorFactoryType = ModelType.MOXY;
59 private static final QueryStyle queryStyle = QueryStyle.TRAVERSAL;
60 private static final DBConnectionType type = DBConnectionType.REALTIME;
62 private Loader loader;
63 private TransactionalGraphEngine dbEngine;
66 VALID_HTTP_STATUS_CODES.add(200);
67 VALID_HTTP_STATUS_CODES.add(201);
68 VALID_HTTP_STATUS_CODES.add(204);
71 private ModelAndNamedQueryRestProvider modelAndNamedQueryRestProvider;
73 private HttpHeaders httpHeaders;
75 private UriInfo uriInfo;
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 final EELFLogger logger = EELFManager.getInstance().getLogger(ModelAndNamedQueryRestProviderTest.class.getName());
88 logger.info("Starting the setup for the integration tests of Rest Endpoints");
89 System.setProperty("AJSC_HOME", ".");
90 System.setProperty("BUNDLECONFIG_DIR", "src/main/resources");
92 modelAndNamedQueryRestProvider = new ModelAndNamedQueryRestProvider();
93 httpHeaders = mock(HttpHeaders.class);
94 uriInfo = mock(UriInfo.class);
96 when(uriInfo.getPath()).thenReturn("JUNITURI");
98 headersMultiMap = new MultivaluedHashMap<>();
99 queryParameters = Mockito.spy(new MultivaluedHashMap<>());
101 headersMultiMap.add("X-FromAppId", "JUNIT");
102 headersMultiMap.add("X-TransactionId", UUID.randomUUID().toString());
103 headersMultiMap.add("Real-Time", "true");
104 headersMultiMap.add("Accept", "application/json");
105 headersMultiMap.add("aai-request-context", "");
107 outputMediaTypes = new ArrayList<>();
108 outputMediaTypes.add(APPLICATION_JSON);
110 aaiRequestContextList = new ArrayList<>();
111 aaiRequestContextList.add("");
113 when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes);
114 when(httpHeaders.getRequestHeaders()).thenReturn(headersMultiMap);
115 when(httpHeaders.getRequestHeader("X-FromAppId")).thenReturn(Arrays.asList("JUNIT"));
116 when(httpHeaders.getRequestHeader("X-TransactionId")).thenReturn(Arrays.asList("JUNIT"));
118 when(httpHeaders.getRequestHeader("aai-request-context")).thenReturn(aaiRequestContextList);
121 when(uriInfo.getQueryParameters()).thenReturn(queryParameters);
122 when(uriInfo.getQueryParameters(false)).thenReturn(queryParameters);
124 // TODO - Check if this is valid since RemoveDME2QueryParameters seems to be very unreasonable
125 Mockito.doReturn(null).when(queryParameters).remove(anyObject());
127 when(httpHeaders.getMediaType()).thenReturn(APPLICATION_JSON);
128 loader = LoaderFactory.createLoaderForVersion(introspectorFactoryType, version);
129 dbEngine = new TitanDBEngine(
136 public void testNamedQueryWhenNoDataToBeFoundReturnHttpNotFound() throws Exception {
138 String queryParameters = getPayload("payloads/named-queries/named-query.json");
139 HttpServletRequest request = mock(HttpServletRequest.class);
141 when(request.getContentType()).thenReturn("application/json");
143 Response response = modelAndNamedQueryRestProvider.getNamedQueryResponse(
150 assertNotNull(response);
151 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus());
155 public void testNamedQueryInvalidHeaders() throws Exception {
157 httpHeaders = mock(HttpHeaders.class);
159 when(httpHeaders.getRequestHeader("X-FromAppId")).thenThrow(IllegalArgumentException.class);
160 when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes);
162 DBSerializer serializer = new DBSerializer(version, dbEngine, introspectorFactoryType, "JUNIT");
163 UrlBuilder urlBuilder = new UrlBuilder(version, serializer);
165 Response response = modelAndNamedQueryRestProvider.getNamedQueryResponse(
172 assertNotNull(response);
173 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
177 public void testNamedQueryCallTimeoutThrown() throws Exception {
179 String queryParameters = getPayload("payloads/named-queries/named-query.json");
180 HttpServletRequest request = mock(HttpServletRequest.class);
182 headersMultiMap.putSingle("X-FromAppId", "JUNITTESTAPP1");
183 when(httpHeaders.getRequestHeaders()).thenReturn(headersMultiMap);
185 when(request.getContentType()).thenReturn("application/json");
187 Response response = modelAndNamedQueryRestProvider.getNamedQueryResponse(
194 assertNotNull(response);
195 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
199 public void testNamedQueryCallTimeoutBypassed() throws Exception {
201 String queryParameters = getPayload("payloads/named-queries/named-query.json");
202 HttpServletRequest request = mock(HttpServletRequest.class);
204 headersMultiMap.putSingle("X-FromAppId", "JUNITTESTAPP2");
205 when(httpHeaders.getRequestHeaders()).thenReturn(headersMultiMap);
207 when(request.getContentType()).thenReturn("application/json");
209 Response response = modelAndNamedQueryRestProvider.getNamedQueryResponse(
216 assertNotNull(response);
217 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus());
220 public String getPayload(String filename) throws IOException {
222 InputStream inputStream = getClass()
224 .getResourceAsStream(filename);
226 String message = String.format("Unable to find the %s in src/test/resources", filename);
227 assertNotNull(message, inputStream);
229 String resource = IOUtils.toString(inputStream);