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