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