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