9ab7bc574349d6806f8aafbb130054431b802294
[aai/traversal.git] / aai-traversal / src / test / java / org / onap / aai / rest / search / ModelAndNamedQueryRestProviderTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  *
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.rest.search;
23
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;
40
41 import javax.servlet.http.HttpServletRequest;
42 import javax.ws.rs.core.*;
43 import java.io.IOException;
44 import java.io.InputStream;
45 import java.util.*;
46
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;
52
53 public class ModelAndNamedQueryRestProviderTest {
54
55     protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json");
56
57     private static final Set<Integer> VALID_HTTP_STATUS_CODES = new HashSet<>();
58
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;
63
64     private Loader loader;
65     private TransactionalGraphEngine dbEngine;
66
67     static {
68         VALID_HTTP_STATUS_CODES.add(200);
69         VALID_HTTP_STATUS_CODES.add(201);
70         VALID_HTTP_STATUS_CODES.add(204);
71     }
72
73     private ModelAndNamedQueryRestProvider modelAndNamedQueryRestProvider;
74
75     private HttpHeaders httpHeaders;
76
77     private UriInfo uriInfo;
78
79     private MultivaluedMap<String, String> headersMultiMap;
80     private MultivaluedMap<String, String> queryParameters;
81
82     private List<String> aaiRequestContextList;
83
84     private List<MediaType> outputMediaTypes;
85
86     private static final EELFLogger logger = EELFManager.getInstance().getLogger(ModelAndNamedQueryRestProviderTest.class.getName());
87
88     @Before
89     public void setup(){
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");
93
94         modelAndNamedQueryRestProvider      = new ModelAndNamedQueryRestProvider();
95         httpHeaders         = mock(HttpHeaders.class);
96         uriInfo             = mock(UriInfo.class);
97
98         when(uriInfo.getPath()).thenReturn("JUNITURI");
99
100         headersMultiMap     = new MultivaluedHashMap<>();
101         queryParameters     = Mockito.spy(new MultivaluedHashMap<>());
102
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", "");
108
109         outputMediaTypes = new ArrayList<>();
110         outputMediaTypes.add(APPLICATION_JSON);
111
112         aaiRequestContextList = new ArrayList<>();
113         aaiRequestContextList.add("");
114
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"));
119
120         when(httpHeaders.getRequestHeader("aai-request-context")).thenReturn(aaiRequestContextList);
121
122
123         when(uriInfo.getQueryParameters()).thenReturn(queryParameters);
124         when(uriInfo.getQueryParameters(false)).thenReturn(queryParameters);
125
126         // TODO - Check if this is valid since RemoveDME2QueryParameters seems to be very unreasonable
127         Mockito.doReturn(null).when(queryParameters).remove(anyObject());
128
129         when(httpHeaders.getMediaType()).thenReturn(APPLICATION_JSON);
130         loader = LoaderFactory.createLoaderForVersion(introspectorFactoryType, version);
131         dbEngine = new TitanDBEngine(
132                 queryStyle,
133                 type,
134                 loader);
135     }
136
137     @Test
138     public void testNamedQueryWhenNoDataToBeFoundReturnHttpNotFound() throws Exception {
139
140         String queryParameters = getPayload("payloads/named-queries/named-query.json");
141         HttpServletRequest request = mock(HttpServletRequest.class);
142
143         when(request.getContentType()).thenReturn("application/json");
144
145         Response response = modelAndNamedQueryRestProvider.getNamedQueryResponse(
146                 httpHeaders,
147                 request,
148                 queryParameters,
149                 uriInfo
150         );
151
152         assertNotNull(response);
153         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus());
154     }
155
156     @Test
157     public void testNamedQueryInvalidHeaders() throws Exception {
158
159         httpHeaders = mock(HttpHeaders.class);
160
161         when(httpHeaders.getRequestHeader("X-FromAppId")).thenThrow(IllegalArgumentException.class);
162         when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes);
163
164         DBSerializer serializer = new DBSerializer(version, dbEngine, introspectorFactoryType, "JUNIT");
165         UrlBuilder urlBuilder = new UrlBuilder(version, serializer);
166
167         Response response = modelAndNamedQueryRestProvider.getNamedQueryResponse(
168                 httpHeaders,
169                 null,
170                 "cloud-region",
171                 uriInfo
172         );
173
174         assertNotNull(response);
175         assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
176     }
177
178     @Test
179     public void testNamedQueryCallTimeoutThrown() throws Exception {
180
181         String queryParameters = getPayload("payloads/named-queries/named-query.json");
182         HttpServletRequest request = mock(HttpServletRequest.class);
183
184         headersMultiMap.putSingle("X-FromAppId", "JUNITTESTAPP1");
185         when(httpHeaders.getRequestHeaders()).thenReturn(headersMultiMap);
186
187         when(request.getContentType()).thenReturn("application/json");
188
189         Response response = modelAndNamedQueryRestProvider.getNamedQueryResponse(
190                 httpHeaders,
191                 request,
192                 queryParameters,
193                 uriInfo
194         );
195
196         assertNotNull(response);
197         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
198     }
199
200     @Test
201     public void testNamedQueryCallTimeoutBypassed() throws Exception {
202
203         String queryParameters = getPayload("payloads/named-queries/named-query.json");
204         HttpServletRequest request = mock(HttpServletRequest.class);
205
206         headersMultiMap.putSingle("X-FromAppId", "JUNITTESTAPP2");
207         when(httpHeaders.getRequestHeaders()).thenReturn(headersMultiMap);
208
209         when(request.getContentType()).thenReturn("application/json");
210
211         Response response = modelAndNamedQueryRestProvider.getNamedQueryResponse(
212                 httpHeaders,
213                 request,
214                 queryParameters,
215                 uriInfo
216         );
217
218         assertNotNull(response);
219         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus());
220     }
221
222     public String getPayload(String filename) throws IOException {
223
224         InputStream inputStream = getClass()
225                 .getClassLoader()
226                 .getResourceAsStream(filename);
227
228         String message = String.format("Unable to find the %s in src/test/resources", filename);
229         assertNotNull(message, inputStream);
230
231         String resource = IOUtils.toString(inputStream);
232         return resource;
233     }
234 }