Add missing distributionManagement section to poms
[aai/search-data-service.git] / search-data-service-app / src / test / java / org / onap / aai / sa / rest / DocumentTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.sa.rest;
22
23 import com.fasterxml.jackson.core.JsonProcessingException;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26 // import javax.ws.rs.core.HttpHeaders;
27 import javax.ws.rs.core.MultivaluedMap;
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Ignore;
31 import org.junit.Test;
32 import org.mockito.InjectMocks;
33 import org.mockito.Mock;
34 import org.mockito.Mockito;
35 import org.mockito.MockitoAnnotations;
36 import org.mockito.invocation.InvocationOnMock;
37 import org.mockito.stubbing.Answer;
38 import org.onap.aai.sa.searchdbabstraction.elasticsearch.config.ElasticSearchConfig;
39 import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreDataEntity;
40 import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreInterface;
41 import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.ElasticSearchHttpController;
42 import org.onap.aai.sa.searchdbabstraction.elasticsearch.exception.DocumentStoreOperationException;
43 import org.onap.aai.sa.searchdbabstraction.entity.DocumentOperationResult;
44 import org.onap.aai.sa.searchdbabstraction.entity.ErrorResult;
45 import org.onap.aai.sa.searchdbabstraction.entity.SearchHits;
46 import org.onap.aai.sa.searchdbabstraction.entity.SearchOperationResult;
47 import org.springframework.http.HttpHeaders;
48 import org.springframework.http.HttpStatus;
49 import org.springframework.http.ResponseEntity;
50 import java.util.Properties;
51
52
53 public class DocumentTest {
54
55     @Mock
56     SearchServiceApi searchServiceApi;
57
58     @Mock
59     HttpServletRequest request;
60
61     @Mock
62     HttpHeaders headers;
63
64     @Mock
65     HttpServletResponse httpResponse;
66
67     @Mock
68     DocumentStoreInterface documentStore;
69
70     @Mock
71     MultivaluedMap<String, String> multivaluedMap;
72
73     @InjectMocks
74     IndexApi indexApi;
75
76     DocumentApi documentApi;
77
78     @Mock
79     ElasticSearchHttpController httpController;
80
81     @Before
82     public void setUp() {
83         MockitoAnnotations.initMocks(this);
84         documentApi = new DocumentApi(searchServiceApi);
85     }
86
87     @Test
88     public void testDocumentClass_AllMethods() throws JsonProcessingException {
89         Document doc = new Document();
90         doc.setField("name-1", "value-1");
91         Assert.assertTrue(doc.getFields().size() == 1);
92         Assert.assertTrue(doc.toJson().contains("value-1"));
93         Assert.assertNotNull(doc.toString());
94         Assert.assertTrue(doc.toString().contains("name-1"));
95     }
96
97     @Test
98     public void testProcessPost_NullContent() {
99         String transactionId = "transactionId-1";
100         String remoteAddr = "http://127.0.0.1";
101         String content = null;
102         // Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap);
103         Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId);
104         Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr);
105         Mockito.when(request.getMethod()).thenReturn("testMethod");
106         Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1"));
107         Mockito.when(request.getRemoteHost()).thenReturn("localhost");
108         ResponseEntity<String> response =
109                 documentApi.processPost(content, request, headers, httpResponse, "index", documentStore);
110         Assert.assertNotNull(response);
111         Assert.assertTrue(HttpStatus.BAD_REQUEST.value() == response.getStatusCodeValue());
112     }
113
114     @SuppressWarnings("unchecked")
115     @Test
116     public void testProcessPost_NotNullContent() throws Exception {
117         String transactionId = "transactionId-1";
118         String remoteAddr = "http://127.0.0.1";
119         String content = "content";
120         // Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap);
121         Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId);
122         Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr);
123         Mockito.when(request.getMethod()).thenReturn("testMethod");
124         Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1"));
125         Mockito.when(request.getRemoteHost()).thenReturn("localhost");
126         Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class),
127                 Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString()))
128                 .thenThrow(IllegalArgumentException.class);
129         ResponseEntity<?> response =
130                 documentApi.processPost(content, request, headers, httpResponse, "index", documentStore);
131         Assert.assertNotNull(response);
132         Assert.assertTrue(HttpStatus.FORBIDDEN.value() == response.getStatusCodeValue());
133     }
134
135     //
136     @Test
137     public void testProcessPost_ValidRequest() throws Exception {
138         String transactionId = "transactionId-1";
139         String remoteAddr = "http://127.0.0.1";
140         String content = "content";
141         DocumentOperationResult result = new DocumentOperationResult();
142         result.setResultCode(150);
143         result.setError(new ErrorResult("type-1", "reason-1"));
144         result.setFailureCause("test-failure");
145         // Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap);
146         Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId);
147         Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr);
148         Mockito.when(request.getMethod()).thenReturn("testMethod");
149         Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1"));
150         Mockito.when(request.getRemoteHost()).thenReturn("localhost");
151         Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class),
152                 Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString()))
153                 .thenReturn(true);
154         Mockito.when(documentStore.createDocument(Mockito.anyString(), Mockito.any(DocumentStoreDataEntity.class),
155                 Mockito.anyBoolean())).thenReturn(result);
156         Mockito.doNothing().when(httpResponse).setHeader(Mockito.anyString(), Mockito.anyString());
157         ResponseEntity<String> response =
158                 documentApi.processPost(content, request, headers, httpResponse, "index", documentStore);
159         Assert.assertNotNull(response);
160         Assert.assertTrue(HttpStatus.INTERNAL_SERVER_ERROR.value() == response.getStatusCodeValue());
161     }
162
163     //
164     @Test
165     public void testProcessSearchWithGet_Created() throws Exception {
166         String transactionId = "transactionId-1";
167         String remoteAddr = "http://127.0.0.1";
168         String content = "content";
169         SearchOperationResult result = new SearchOperationResult();
170         result.setResultCode(201);
171         SearchHits hits = new SearchHits();
172         hits.setTotalHits("2");
173         result.setSearchResult(hits);
174         // Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap);
175         Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId);
176         Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr);
177         Mockito.when(request.getMethod()).thenReturn("testMethod");
178         Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1"));
179         Mockito.when(request.getRemoteHost()).thenReturn("localhost");
180         Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class),
181                 Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString()))
182                 .thenReturn(true);
183         Mockito.when(documentStore.search(Mockito.anyString(), Mockito.anyString())).thenReturn(result);
184         ResponseEntity<String> response =
185                 documentApi.processSearchWithGet(content, request, headers, "index-1", "query-text", documentStore);
186         Assert.assertNotNull(response);
187         Assert.assertTrue(HttpStatus.CREATED.value() == response.getStatusCodeValue());
188
189     }
190
191     @SuppressWarnings("unchecked")
192     @Test
193     public void testProcessSearchWithGet_ValidateThrowsException() throws Exception {
194         String transactionId = "transactionId-1";
195         String remoteAddr = "http://127.0.0.1";
196         String content = "content";
197         SearchOperationResult result = new SearchOperationResult();
198         result.setResultCode(201);
199         SearchHits hits = new SearchHits();
200         hits.setTotalHits("2");
201         result.setSearchResult(hits);
202         // Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap);
203         Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId);
204         Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr);
205         Mockito.when(request.getMethod()).thenReturn("testMethod");
206         Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1"));
207         Mockito.when(request.getRemoteHost()).thenReturn("localhost");
208         Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class),
209                 Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString()))
210                 .thenThrow(IllegalArgumentException.class);
211         Mockito.when(documentStore.search(Mockito.anyString(), Mockito.anyString())).thenReturn(result);
212         ResponseEntity<String> response =
213                 documentApi.processSearchWithGet(content, request, headers, "index-1", "query-text", documentStore);
214         Assert.assertNotNull(response);
215         Assert.assertTrue(HttpStatus.FORBIDDEN.value() == response.getStatusCodeValue());
216
217     }
218
219     @Test
220     public void testProcessSearchWithGet_ValidateIsFalse() throws Exception {
221         String transactionId = "transactionId-1";
222         String remoteAddr = "http://127.0.0.1";
223         String content = "content";
224         SearchOperationResult result = new SearchOperationResult();
225         result.setResultCode(201);
226         SearchHits hits = new SearchHits();
227         hits.setTotalHits("2");
228         result.setSearchResult(hits);
229         // Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap);
230         Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId);
231         Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr);
232         Mockito.when(request.getMethod()).thenReturn("testMethod");
233         Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1"));
234         Mockito.when(request.getRemoteHost()).thenReturn("localhost");
235         Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class),
236                 Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString()))
237                 .thenReturn(false);
238         Mockito.when(documentStore.search(Mockito.anyString(), Mockito.anyString())).thenReturn(result);
239         ResponseEntity<String> response =
240                 documentApi.processSearchWithGet(content, request, headers, "index-1", "query-text", documentStore);
241         Assert.assertNotNull(response);
242         Assert.assertTrue(HttpStatus.FORBIDDEN.value() == response.getStatusCodeValue());
243
244     }
245
246     @Test
247     public void testProcessSearchWithGet_InvalidResult() throws Exception {
248         String transactionId = "transactionId-1";
249         String remoteAddr = "http://127.0.0.1";
250         String content = "content";
251         SearchOperationResult result = new SearchOperationResult();
252         result.setResultCode(302);
253         SearchHits hits = new SearchHits();
254         hits.setTotalHits("2");
255         result.setSearchResult(hits);
256         // Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap);;
257         Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId);
258         Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr);
259         Mockito.when(request.getMethod()).thenReturn("testMethod");
260         Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1"));
261         Mockito.when(request.getRemoteHost()).thenReturn("localhost");
262         Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class),
263                 Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString()))
264                 .thenReturn(true);
265         Mockito.when(documentStore.search(Mockito.anyString(), Mockito.anyString())).thenReturn(result);
266         ResponseEntity<String> response =
267                 documentApi.processSearchWithGet(content, request, headers, "index-1", "query-text", documentStore);
268         Assert.assertNotNull(response);
269         Assert.assertTrue(HttpStatus.FOUND.value() == response.getStatusCodeValue());
270
271     }
272
273     @Test
274     public void testProcessPut_NullContent() {
275         String transactionId = "transactionId-1";
276         String remoteAddr = "http://127.0.0.1";
277         String content = null;
278         // Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap);;
279         Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId);
280         Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr);
281         Mockito.when(request.getMethod()).thenReturn("testMethod");
282         Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1"));
283         Mockito.when(request.getRemoteHost()).thenReturn("localhost");
284         ResponseEntity<String> response =
285                 documentApi.processPut(content, request, headers, httpResponse, "index", "id-1", documentStore);
286         Assert.assertNotNull(response);
287         Assert.assertTrue(HttpStatus.BAD_REQUEST.value() == response.getStatusCodeValue());
288     }
289
290     @SuppressWarnings("unchecked")
291     @Test
292     public void testProcessPut_RequestThrowsException() throws Exception {
293         String transactionId = "transactionId-1";
294         String remoteAddr = "http://127.0.0.1";
295         String content = "content";
296         // Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap);;
297         Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId);
298         Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr);
299         Mockito.when(request.getMethod()).thenReturn("testMethod");
300         Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1"));
301         Mockito.when(request.getRemoteHost()).thenReturn("localhost");
302         Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class),
303                 Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString()))
304                 .thenThrow(IllegalArgumentException.class);
305         ResponseEntity<String> response =
306                 documentApi.processPut(content, request, headers, httpResponse, "index", "id-1", documentStore);
307         Assert.assertNotNull(response);
308         Assert.assertTrue(HttpStatus.FORBIDDEN.value() == response.getStatusCodeValue());
309     }
310
311     @Test
312     public void testProcessPut_RequestInvalid() throws Exception {
313         String transactionId = "transactionId-1";
314         String remoteAddr = "http://127.0.0.1";
315         String content = "content";
316         // Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap);;
317         Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId);
318         Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr);
319         Mockito.when(request.getMethod()).thenReturn("testMethod");
320         Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1"));
321         Mockito.when(request.getRemoteHost()).thenReturn("localhost");
322         Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class),
323                 Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString()))
324                 .thenReturn(false);
325         ResponseEntity<String> response =
326                 documentApi.processPut(content, request, headers, httpResponse, "index", "id-1", documentStore);
327         Assert.assertNotNull(response);
328         Assert.assertTrue(HttpStatus.FORBIDDEN.value() == response.getStatusCodeValue());
329     }
330
331     @Test
332     public void testProcessPut_ResultInvalid() throws Exception {
333         String transactionId = "transactionId-1";
334         String remoteAddr = "http://127.0.0.1";
335         String content = "content";
336         DocumentOperationResult result = new DocumentOperationResult();
337         result.setResultCode(302);
338         result.setError(new ErrorResult("type-1", "reason-1"));
339         result.setFailureCause("test-failure");
340         // Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap);;
341         Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId);
342         Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr);
343         Mockito.when(request.getMethod()).thenReturn("testMethod");
344         Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1"));
345         Mockito.when(request.getRemoteHost()).thenReturn("localhost");
346         Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class),
347                 Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString()))
348                 .thenReturn(true);
349         Mockito.when(documentStore.createDocument(Mockito.anyString(), Mockito.any(DocumentStoreDataEntity.class),
350                 Mockito.anyBoolean())).thenReturn(result);
351         ResponseEntity<String> response =
352                 documentApi.processPut(content, request, headers, httpResponse, "index", "id-1", documentStore);
353         Assert.assertNotNull(response);
354         Assert.assertTrue(HttpStatus.FOUND.value() == response.getStatusCodeValue());
355     }
356
357     @SuppressWarnings("unchecked")
358     @Test
359     public void testProcessDelete_RequestThrowsException() throws Exception {
360         String transactionId = "transactionId-1";
361         String remoteAddr = "http://127.0.0.1";
362         String content = "content";
363         // Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap);;
364         Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId);
365         Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr);
366         Mockito.when(request.getMethod()).thenReturn("testMethod");
367         Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1"));
368         Mockito.when(request.getRemoteHost()).thenReturn("localhost");
369         Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class),
370                 Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString()))
371                 .thenThrow(IllegalArgumentException.class);
372         ResponseEntity<String> response =
373                 documentApi.processDelete(content, request, headers, httpResponse, "index", "id-1", documentStore);
374         Assert.assertNotNull(response);
375         Assert.assertTrue(HttpStatus.FORBIDDEN.value() == response.getStatusCodeValue());
376     }
377
378     @Test
379     public void testProcessDelete_RequestInvalid() throws Exception {
380         String transactionId = "transactionId-1";
381         String remoteAddr = "http://127.0.0.1";
382         String content = "content";
383         // Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap);;
384         Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId);
385         Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr);
386         Mockito.when(request.getMethod()).thenReturn("testMethod");
387         Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1"));
388         Mockito.when(request.getRemoteHost()).thenReturn("localhost");
389         Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class),
390                 Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString()))
391                 .thenReturn(false);
392         ResponseEntity<String> response =
393                 documentApi.processDelete(content, request, headers, httpResponse, "index", "id-1", documentStore);
394         Assert.assertNotNull(response);
395         Assert.assertTrue(HttpStatus.FORBIDDEN.value() == response.getStatusCodeValue());
396     }
397
398     @Ignore
399     @Test
400     public void testProcessDelete_ResultInvalid() throws Exception {
401         String transactionId = "transactionId-1";
402         String remoteAddr = "http://127.0.0.1";
403         String content = "content";
404         DocumentOperationResult result = new DocumentOperationResult();
405         result.setResultCode(302);
406         result.setError(new ErrorResult("type-1", "reason-1"));
407         result.setFailureCause("test-failure");
408         // Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap);;
409         Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId);
410         Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr);
411         Mockito.when(request.getMethod()).thenReturn("testMethod");
412         Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1"));
413         Mockito.when(request.getRemoteHost()).thenReturn("localhost");
414         Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class),
415                 Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString()))
416                 .thenReturn(true);
417         Mockito.when(documentStore.deleteDocument(Mockito.anyString(), Mockito.any(DocumentStoreDataEntity.class)))
418                 .thenReturn(result);
419         ResponseEntity<String> response =
420                 documentApi.processDelete(content, request, headers, httpResponse, "index", "id-1", documentStore);
421         Assert.assertNotNull(response);
422         Assert.assertTrue(HttpStatus.FOUND.value() == response.getStatusCodeValue());
423     }
424
425     @SuppressWarnings("unchecked")
426     @Test
427     public void testProcessGet_RequestThrowsException() throws Exception {
428         String transactionId = "transactionId-1";
429         String remoteAddr = "http://127.0.0.1";
430         String content = "content";
431         // Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap);;
432         Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId);
433         Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr);
434         Mockito.when(request.getMethod()).thenReturn("testMethod");
435         Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1"));
436         Mockito.when(request.getRemoteHost()).thenReturn("localhost");
437         Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class),
438                 Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString()))
439                 .thenThrow(IllegalArgumentException.class);
440         ResponseEntity<String> response =
441                 documentApi.processGet(content, request, headers, httpResponse, "index", "id-1", documentStore);
442         Assert.assertNotNull(response);
443         Assert.assertTrue(HttpStatus.FORBIDDEN.value() == response.getStatusCodeValue());
444     }
445
446     @Test
447     public void testProcessGet_RequestInvalid() throws Exception {
448         String transactionId = "transactionId-1";
449         String remoteAddr = "http://127.0.0.1";
450         String content = "content";
451         // Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap);;
452         Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId);
453         Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr);
454         Mockito.when(request.getMethod()).thenReturn("testMethod");
455         Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1"));
456         Mockito.when(request.getRemoteHost()).thenReturn("localhost");
457         Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class),
458                 Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString()))
459                 .thenReturn(false);
460         ResponseEntity<String> response =
461                 documentApi.processGet(content, request, headers, httpResponse, "index", "id-1", documentStore);
462         Assert.assertNotNull(response);
463         Assert.assertTrue(HttpStatus.FORBIDDEN.value() == response.getStatusCodeValue());
464     }
465
466     @Test
467     public void testProcessGet_ResultInvalid() throws Exception {
468         String transactionId = "transactionId-1";
469         String remoteAddr = "http://127.0.0.1";
470         String content = "content";
471         DocumentOperationResult result = new DocumentOperationResult();
472         result.setResultCode(302);
473         result.setError(new ErrorResult("type-1", "reason-1"));
474         result.setFailureCause("test-failure");
475         // Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap);;
476         Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId);
477         Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr);
478         Mockito.when(request.getMethod()).thenReturn("testMethod");
479         Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1"));
480         Mockito.when(request.getRemoteHost()).thenReturn("localhost");
481         Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class),
482                 Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString()))
483                 .thenReturn(true);
484         Mockito.when(documentStore.getDocument(Mockito.anyString(), Mockito.any(DocumentStoreDataEntity.class)))
485                 .thenReturn(result);
486         ResponseEntity<String> response =
487                 documentApi.processGet(content, request, headers, httpResponse, "index", "id-1", documentStore);
488         Assert.assertNotNull(response);
489         Assert.assertTrue(HttpStatus.FOUND.value() == response.getStatusCodeValue());
490     }
491
492     @Test
493     public void testQueryWithGetWithPayload_NullContent() {
494         String transactionId = "transactionId-1";
495         String remoteAddr = "http://127.0.0.1";
496         String content = null;
497         // Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap);;
498         Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId);
499         Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr);
500         Mockito.when(request.getMethod()).thenReturn("testMethod");
501         Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1"));
502         Mockito.when(request.getRemoteHost()).thenReturn("localhost");
503         ResponseEntity<String> response =
504                 documentApi.queryWithGetWithPayload(content, request, headers, "index-1", documentStore);
505         Assert.assertNotNull(response);
506         Assert.assertTrue(HttpStatus.BAD_REQUEST.value() == response.getStatusCodeValue());
507     }
508
509     @SuppressWarnings("unchecked")
510     @Test
511     public void testQueryWithGetWithPayload_RequestThrowsException() throws Exception {
512         String transactionId = "transactionId-1";
513         String remoteAddr = "http://127.0.0.1";
514         String content = "content";
515         // Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap);;
516         Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId);
517         Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr);
518         Mockito.when(request.getMethod()).thenReturn("testMethod");
519         Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1"));
520         Mockito.when(request.getRemoteHost()).thenReturn("localhost");
521         Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class),
522                 Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString()))
523                 .thenThrow(IllegalArgumentException.class);
524         ResponseEntity<String> response =
525                 documentApi.queryWithGetWithPayload(content, request, headers, "index-1", documentStore);
526         Assert.assertNotNull(response);
527         Assert.assertTrue(HttpStatus.FORBIDDEN.value() == response.getStatusCodeValue());
528     }
529
530     @Test
531     public void testQueryWithGetWithPayload_RequestInvalid() throws Exception {
532         String transactionId = "transactionId-1";
533         String remoteAddr = "http://127.0.0.1";
534         String content = "content";
535         // Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap);;
536         Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId);
537         Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr);
538         Mockito.when(request.getMethod()).thenReturn("testMethod");
539         Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1"));
540         Mockito.when(request.getRemoteHost()).thenReturn("localhost");
541         Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class),
542                 Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString()))
543                 .thenReturn(false);
544         ResponseEntity<String> response =
545                 documentApi.queryWithGetWithPayload(content, request, headers, "index-1", documentStore);
546         Assert.assertNotNull(response);
547         Assert.assertTrue(HttpStatus.FORBIDDEN.value() == response.getStatusCodeValue());
548     }
549
550     @Test
551     public void testCreateProcessIndex_IndexApi_RequestInvalid() throws Exception {
552         String transactionId = "transactionId-1";
553         String remoteAddr = "http://127.0.0.1";
554         // Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap);;
555         Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId);
556         Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr);
557         Mockito.when(request.getMethod()).thenReturn("testMethod");
558         Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1"));
559         Mockito.when(request.getRemoteHost()).thenReturn("localhost");
560         Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class),
561                 Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString()))
562                 .thenReturn(false);
563         ResponseEntity<String> response =
564                 indexApi.processCreateIndex("document-1", request, headers, "index-1", documentStore);
565         Assert.assertNotNull(response);
566         Assert.assertTrue(HttpStatus.FORBIDDEN.value() == response.getStatusCodeValue());
567     }
568
569     @SuppressWarnings("unchecked")
570     @Test
571     public void testCreateProcessIndex_IndexApi_RequestThrowsException() throws Exception {
572         String transactionId = "transactionId-1";
573         String remoteAddr = "http://127.0.0.1";
574         // Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap);;
575         Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId);
576         Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr);
577         Mockito.when(request.getMethod()).thenReturn("testMethod");
578         Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1"));
579         Mockito.when(request.getRemoteHost()).thenReturn("localhost");
580         Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class),
581                 Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString()))
582                 .thenThrow(IllegalArgumentException.class);
583         ResponseEntity<String> response =
584                 indexApi.processCreateIndex("document-1", request, headers, "index-1", documentStore);
585         Assert.assertNotNull(response);
586         Assert.assertTrue(HttpStatus.FORBIDDEN.value() == response.getStatusCodeValue());
587     }
588
589     @Test
590     public void testCreateProcessIndex_IndexApi_NullDocument() throws Exception {
591         String transactionId = "transactionId-1";
592         String remoteAddr = "http://127.0.0.1";
593         String documentSchema = null;
594         // Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap);;
595         Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId);
596         Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr);
597         Mockito.when(request.getMethod()).thenReturn("testMethod");
598         Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1"));
599         Mockito.when(request.getRemoteHost()).thenReturn("localhost");
600         Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class),
601                 Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString()))
602                 .thenReturn(true);
603         ResponseEntity<String> response =
604                 indexApi.processCreateIndex(documentSchema, request, headers, "index-1", documentStore);
605         Assert.assertNotNull(response);
606         Assert.assertTrue(HttpStatus.INTERNAL_SERVER_ERROR.value() == response.getStatusCodeValue());
607     }
608
609     @Test
610     public void testProcessDelete_IndexApi_RequestInvalid() throws Exception {
611         String transactionId = "transactionId-1";
612         String remoteAddr = "http://127.0.0.1";
613         // Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap);;
614         Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId);
615         Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr);
616         Mockito.when(request.getMethod()).thenReturn("testMethod");
617         Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1"));
618         Mockito.when(request.getRemoteHost()).thenReturn("localhost");
619         Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class),
620                 Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString()))
621                 .thenReturn(false);
622         ResponseEntity<String> response = indexApi.processDelete("document-1", request, headers, documentStore);
623         Assert.assertNotNull(response);
624         Assert.assertTrue(HttpStatus.FORBIDDEN.value() == response.getStatusCodeValue());
625     }
626
627     @SuppressWarnings("unchecked")
628     @Test
629     public void testProcessDelete_IndexApi_RequestThrowsException() throws Exception {
630         String transactionId = "transactionId-1";
631         String remoteAddr = "http://127.0.0.1";
632         // Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap);;
633         Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId);
634         Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr);
635         Mockito.when(request.getMethod()).thenReturn("testMethod");
636         Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1"));
637         Mockito.when(request.getRemoteHost()).thenReturn("localhost");
638         Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class),
639                 Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString()))
640                 .thenThrow(IllegalArgumentException.class);
641         ResponseEntity<String> response = indexApi.processDelete("document-1", request, headers, documentStore);
642         Assert.assertNotNull(response);
643         Assert.assertTrue(HttpStatus.FORBIDDEN.value() == response.getStatusCodeValue());
644     }
645
646     @SuppressWarnings("unchecked")
647     @Test
648     public void testProcessDelete_IndexApi_DeleteIndexException() throws Exception {
649         String transactionId = "transactionId-1";
650         String remoteAddr = "http://127.0.0.1";
651         // Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap);;
652         Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId);
653         Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr);
654         Mockito.when(request.getMethod()).thenReturn("testMethod");
655         Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1"));
656         Mockito.when(request.getRemoteHost()).thenReturn("localhost");
657         Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class),
658                 Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString()))
659                 .thenReturn(true);
660         Mockito.when(documentStore.deleteIndex(Mockito.anyString())).thenThrow(DocumentStoreOperationException.class);
661         ResponseEntity<String> response = indexApi.processDelete("document-1", request, headers, documentStore);
662         Assert.assertNotNull(response);
663         Assert.assertTrue(HttpStatus.INTERNAL_SERVER_ERROR.value() == response.getStatusCodeValue());
664     }
665
666     @Test
667     public void testUserAuthorization() throws Exception {
668         String transactionId = "transactionId-1";
669         String remoteAddr = "http://127.0.0.1";
670         String content = "content";
671         // Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap);;
672         Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId);
673         Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr);
674         Mockito.when(request.getMethod()).thenReturn("testMethod");
675         Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1"));
676         Mockito.when(request.getRemoteHost()).thenReturn("localhost");
677         Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class),
678                 Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString()))
679                 .thenCallRealMethod();
680
681         Mockito.doAnswer(new Answer<ElasticSearchConfig>() {
682             public ElasticSearchConfig answer(InvocationOnMock invocation) {
683                 Properties properties = new Properties();
684                 return new ElasticSearchConfig(properties);
685             }
686         }).when(httpController).getElasticSearchConfig();
687
688         searchServiceApi.documentStore = httpController;
689
690         ResponseEntity<String> response =
691                 documentApi.processPut(content, request, headers, httpResponse, "index", "id-1", documentStore);
692         Assert.assertNotNull(response);
693         Assert.assertTrue(HttpStatus.FORBIDDEN.value() == response.getStatusCodeValue());
694
695         Mockito.doAnswer(new Answer<ElasticSearchConfig>() {
696             public ElasticSearchConfig answer(InvocationOnMock invocation) {
697                 Properties properties = new Properties();
698                 properties.put(ElasticSearchConfig.ES_AUTH_ENABLED, "true");
699                 return new ElasticSearchConfig(properties);
700             }
701         }).when(httpController).getElasticSearchConfig();
702
703
704         response = documentApi.processPut(content, request, headers, httpResponse, "index", "id-1", documentStore);
705         Assert.assertNotNull(response);
706         Assert.assertTrue(HttpStatus.FORBIDDEN.value() == response.getStatusCodeValue());
707
708         Mockito.doAnswer(new Answer<ElasticSearchConfig>() {
709             public ElasticSearchConfig answer(InvocationOnMock invocation) {
710                 Properties properties = new Properties();
711                 properties.put(ElasticSearchConfig.ES_AUTH_ENABLED, "false");
712                 return new ElasticSearchConfig(properties);
713             }
714         }).when(httpController).getElasticSearchConfig();
715
716         DocumentOperationResult result = new DocumentOperationResult();
717         result.setResultCode(302);
718         result.setError(new ErrorResult("type-1", "reason-1"));
719         result.setFailureCause("test-failure");
720         Mockito.when(documentStore.createDocument(Mockito.anyString(), Mockito.any(DocumentStoreDataEntity.class),
721                 Mockito.anyBoolean())).thenReturn(result);
722         response = documentApi.processPut(content, request, headers, httpResponse, "index", "id-1", documentStore);
723         Assert.assertNotNull(response);
724         Assert.assertTrue(HttpStatus.FOUND.value() == response.getStatusCodeValue());
725
726     }
727 }