Merge "Added docs directory and index file for traversal"
[aai/traversal.git] / aai-traversal / src / test / java / org / openecomp / aai / rest / search / SearchProviderTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
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
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
21 package org.openecomp.aai.rest.search;
22
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;
38
39 import javax.ws.rs.core.*;
40 import java.util.*;
41
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;
47
48 public class SearchProviderTest {
49
50     protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json");
51
52     private static final Set<Integer> VALID_HTTP_STATUS_CODES = new HashSet<>();
53
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;
58
59     private Loader loader;
60     private TransactionalGraphEngine dbEngine;
61
62     static {
63         VALID_HTTP_STATUS_CODES.add(200);
64         VALID_HTTP_STATUS_CODES.add(201);
65         VALID_HTTP_STATUS_CODES.add(204);
66     }
67
68     private SearchProvider searchProvider;
69
70     private HttpHeaders httpHeaders;
71
72     private UriInfo uriInfo;
73
74     private MultivaluedMap<String, String> headersMultiMap;
75     private MultivaluedMap<String, String> queryParameters;
76
77     private List<String> aaiRequestContextList;
78
79     private List<MediaType> outputMediaTypes;
80
81     private static final EELFLogger logger = EELFManager.getInstance().getLogger(SearchProviderTest.class.getName());
82
83     @Before
84     public void setup(){
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");
88
89         searchProvider      = new SearchProvider();
90         httpHeaders         = mock(HttpHeaders.class);
91         uriInfo             = mock(UriInfo.class);
92
93         headersMultiMap     = new MultivaluedHashMap<>();
94         queryParameters     = Mockito.spy(new MultivaluedHashMap<>());
95
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", "");
101
102         outputMediaTypes = new ArrayList<>();
103         outputMediaTypes.add(APPLICATION_JSON);
104
105         aaiRequestContextList = new ArrayList<>();
106         aaiRequestContextList.add("");
107
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"));
112
113         when(httpHeaders.getRequestHeader("aai-request-context")).thenReturn(aaiRequestContextList);
114
115
116         when(uriInfo.getQueryParameters()).thenReturn(queryParameters);
117         when(uriInfo.getQueryParameters(false)).thenReturn(queryParameters);
118
119         // TODO - Check if this is valid since RemoveDME2QueryParameters seems to be very unreasonable
120         Mockito.doReturn(null).when(queryParameters).remove(anyObject());
121
122         when(httpHeaders.getMediaType()).thenReturn(APPLICATION_JSON);
123         loader = LoaderFactory.createLoaderForVersion(introspectorFactoryType, version);
124         dbEngine = new TitanDBEngine(
125                 queryStyle,
126                 type,
127                 loader);
128     }
129
130     @Test
131     public void testNodesQueryInvalidData() throws Exception {
132
133         List<String> keys = new ArrayList<>();
134         keys.add("cloud-region.cloud-owner:test-aic");
135
136         List<String> includeStrings = new ArrayList<>();
137         includeStrings.add("cloud-region");
138
139         DBSerializer serializer = new DBSerializer(version, dbEngine, introspectorFactoryType, "JUNIT");
140         UrlBuilder urlBuilder = new UrlBuilder(version, serializer);
141
142         Response response = searchProvider.getNodesQueryResponse(
143                 httpHeaders,
144                 null,
145                 "cloud-region",
146                 keys,
147                 includeStrings,
148                 version.toString()
149         );
150
151         assertNotNull(response);
152         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
153
154         response = searchProvider.getNodesQueryResponse(
155                 httpHeaders,
156                 null,
157                 "cloud-region",
158                 keys,
159                 includeStrings,
160                 "latest"
161         );
162
163         assertNotNull(response);
164         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
165     }
166
167     @Test
168     public void testNodesQueryInvalidHeaders() throws Exception {
169
170         List<String> keys = new ArrayList<>();
171         keys.add("cloud-region.cloud-owner:test-aic");
172
173         List<String> includeStrings = new ArrayList<>();
174         includeStrings.add("cloud-region");
175
176         httpHeaders = mock(HttpHeaders.class);
177
178         when(httpHeaders.getRequestHeader("X-FromAppId")).thenThrow(IllegalArgumentException.class);
179         when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes);
180
181         DBSerializer serializer = new DBSerializer(version, dbEngine, introspectorFactoryType, "JUNIT");
182         UrlBuilder urlBuilder = new UrlBuilder(version, serializer);
183
184         Response response = searchProvider.getNodesQueryResponse(
185                 httpHeaders,
186                 null,
187                 "cloud-region",
188                 keys,
189                 includeStrings,
190                 version.toString()
191         );
192
193         assertNotNull(response);
194         assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
195     }
196
197     @Test
198     public void testGenericQueryInvalidData() throws Exception {
199
200         List<String> keys = new ArrayList<>();
201         keys.add("cloud-region.cloud-owner:test-aic");
202
203         List<String> includeStrings = new ArrayList<>();
204         includeStrings.add("cloud-region");
205
206         DBSerializer serializer = new DBSerializer(version, dbEngine, introspectorFactoryType, "JUNIT");
207         UrlBuilder urlBuilder = new UrlBuilder(version, serializer);
208
209         Response response = searchProvider.getGenericQueryResponse(
210                 httpHeaders,
211                 null,
212                 "cloud-region",
213                 keys,
214                 includeStrings,
215                 0,
216                 version.toString()
217         );
218
219         assertNotNull(response);
220         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
221
222         response = searchProvider.getNodesQueryResponse(
223                 httpHeaders,
224                 null,
225                 "cloud-region",
226                 keys,
227                 includeStrings,
228                 "latest"
229         );
230
231         assertNotNull(response);
232         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
233     }
234
235     @Test
236     public void testGenericQueryInvalidHeaders() throws Exception {
237
238         List<String> keys = new ArrayList<>();
239         keys.add("cloud-region.cloud-owner:test-aic");
240
241         List<String> includeStrings = new ArrayList<>();
242         includeStrings.add("cloud-region");
243
244         httpHeaders = mock(HttpHeaders.class);
245
246         when(httpHeaders.getRequestHeader("X-FromAppId")).thenThrow(IllegalArgumentException.class);
247         when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes);
248
249         DBSerializer serializer = new DBSerializer(version, dbEngine, introspectorFactoryType, "JUNIT");
250         UrlBuilder urlBuilder = new UrlBuilder(version, serializer);
251
252         Response response = searchProvider.getGenericQueryResponse(
253                 httpHeaders,
254                 null,
255                 "cloud-region",
256                 keys,
257                 includeStrings,
258                 0,
259                 version.toString()
260         );
261
262         assertNotNull(response);
263         assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
264     }
265
266 }