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