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