3990b277677d6715f19d12eacfb6258324eed38d
[aai/search-data-service.git] / src / test / java / org / onap / aai / sa / rest / SearchServiceApiHarness.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.sa.rest;
22
23  import org.springframework.http.HttpHeaders;
24 import org.springframework.http.ResponseEntity;
25 import org.springframework.stereotype.Component;
26 import org.springframework.web.bind.annotation.*;
27
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30
31 @Component
32 @RestController
33 @RequestMapping("/test")
34 public class SearchServiceApiHarness extends SearchServiceApi {
35
36
37   public static final String FAIL_AUTHENTICATION_TRIGGER = "FAIL AUTHENTICATION";
38
39   private boolean authenticationShouldSucceed = true;
40
41
42   /**
43    * Performs all one-time initialization required for the end point.
44    */
45   @Override
46   public void init() {
47
48     // Instantiate our Document Store DAO.
49     documentStore = new StubEsController();
50   }
51
52 //  @PUT
53 //  @Path("/indexes/dynamic/{index}")
54 //  @Consumes({MediaType.APPLICATION_JSON})
55   @Override
56   @RequestMapping (value="/indexes/dynamic/{index}",
57           method = RequestMethod.PUT,
58           consumes = { "application/json"})
59   public ResponseEntity<String> processCreateDynamicIndex(@RequestBody String requestBody,
60                                             HttpServletRequest request,
61                                             @RequestHeader HttpHeaders headers,
62                                             @PathVariable("index") String index) {
63
64     return super.processCreateDynamicIndex(requestBody, request, headers, index);
65   }
66
67
68   @Override
69   @RequestMapping (value="/indexes/{index}",
70           method = RequestMethod.PUT,
71           consumes = { "application/json"})
72   public ResponseEntity<String> processCreateIndex(@RequestBody  String requestBody,
73                                      HttpServletRequest request,
74                                      @RequestHeader HttpHeaders headers,
75                                      @PathVariable("index") String index) {
76
77     return super.processCreateIndex(requestBody, request, headers, index);
78   }
79
80   @Override
81   @RequestMapping (value="/indexes/{index}",
82           method = RequestMethod.DELETE,
83           consumes = { "application/json"})
84   public ResponseEntity<String> processDeleteIndex(@RequestBody String requestBody,
85                                      HttpServletRequest request,
86                                      @RequestHeader HttpHeaders headers,
87                                      @PathVariable("index") String index) {
88
89     return super.processDeleteIndex(requestBody, request, headers, index);
90   }
91
92   @Override
93   @RequestMapping (value="/indexes/{index}/documents/{id}",
94           method = RequestMethod.GET,
95           produces = { "application/json"},
96           consumes = { "application/json", "application/xml" })
97   public ResponseEntity<String> processGetDocument(
98           @RequestBody  String requestBody,
99           HttpServletRequest request,
100           HttpServletResponse httpResponse,
101           @RequestHeader HttpHeaders headers,
102           @PathVariable("index") String index,
103           @PathVariable("id") String id) {
104
105     return super.processGetDocument(requestBody, request, httpResponse, headers, index, id);
106   }
107
108   @Override
109   @RequestMapping (value="/indexes/{index}/documents",
110                    method = RequestMethod.POST,
111                    consumes = { "application/json", "application/xml" })
112   public ResponseEntity<String> processCreateDocWithoutId(@RequestBody String requestBody,
113                                                           HttpServletRequest request,
114                                                           HttpServletResponse httpResponse,
115                                                           @RequestHeader HttpHeaders headers,
116                                                           @PathVariable("index") String index) {
117
118     return super.processCreateDocWithoutId(requestBody, request, httpResponse, headers, index);
119   }
120
121   @Override
122   @RequestMapping (value="/indexes/{index}/documents/{id}",
123           method = RequestMethod.PUT,
124           consumes = { "application/json", "application/xml" })
125   public ResponseEntity<String> processUpsertDoc(@RequestBody  String requestBody,
126                                     HttpServletRequest request,
127                                    HttpServletResponse httpResponse,
128                                    @RequestHeader  HttpHeaders headers,
129                                    @PathVariable("index") String index,
130                                    @PathVariable("id") String id) {
131
132     return super.processUpsertDoc(requestBody, request, httpResponse, headers, index, id);
133   }
134
135   @Override
136   @RequestMapping(value = "/indexes/{index}/documents/{id}",
137           method = RequestMethod.DELETE,
138           consumes = { "application/json"})
139   public ResponseEntity<String> processDeleteDoc(@RequestBody String requestBody,
140                                    HttpServletRequest request,
141                                    HttpServletResponse httpResponse,
142                                    @RequestHeader HttpHeaders headers,
143                                    @PathVariable("index") String index,
144                                    @PathVariable("id") String id) {
145
146     return super.processDeleteDoc(requestBody, request, httpResponse, headers, index, id);
147   }
148
149   @Override
150   @RequestMapping(value = "/indexes/{index}/query/{queryText}",
151           method = RequestMethod.GET,
152           consumes = { "application/json"})
153   public ResponseEntity<String> processInlineQuery(String requestBody,
154                                      HttpServletRequest request,
155                                      @RequestHeader HttpHeaders headers,
156                                      @PathVariable("index") String index,
157                                      @PathVariable("queryText") String queryText) {
158
159     return super.processInlineQuery(requestBody, request, headers, index, queryText);
160   }
161
162   @Override
163   @RequestMapping(value = "/indexes/{index}/query",
164           method = RequestMethod.GET,
165           consumes = { "application/json"})
166   public ResponseEntity<String> processQueryWithGet(@RequestBody String requestBody,
167                                                     HttpServletRequest request,
168                                       @RequestHeader HttpHeaders headers,
169                                       @PathVariable("index") String index) {
170
171     return super.processQueryWithGet(requestBody, request, headers, index);
172   }
173
174   @Override
175   @RequestMapping(value = "/indexes/{index}/query",
176           method = RequestMethod.POST,
177           consumes = { "application/json"})
178   public ResponseEntity<String> processQuery(String requestBody,
179                                              HttpServletRequest request,
180                                              @RequestHeader HttpHeaders headers,
181                                              @PathVariable("index") String index) {
182
183     return super.processQuery(requestBody, request, headers, index);
184   }
185
186   @Override
187   @RequestMapping(value = "/bulk",
188                   method = RequestMethod.POST,
189                   consumes = { "application/json", "application/xml" })
190   public ResponseEntity<String> processBulkRequest(@RequestBody String requestBody,
191                                                    HttpServletRequest request,
192                                      @RequestHeader HttpHeaders headers) {
193
194     // If the operations string contains a special keyword, set the
195     // harness to fail the authentication validation.
196     if (requestBody.contains(FAIL_AUTHENTICATION_TRIGGER)) {
197       authenticationShouldSucceed = false;
198     }
199
200     // Just pass the request up to the parent, since that is the code
201     // that we really want to test.
202     //return super.processPost(operations, request, headers, index);
203     return super.processBulkRequest(requestBody, request, headers);
204   }
205
206   @Override
207   protected boolean validateRequest(HttpHeaders headers,
208                                     HttpServletRequest req,
209                                     ApiUtils.Action action,
210                                     String authPolicyFunctionName) throws Exception {
211
212     return authenticationShouldSucceed;
213   }
214 }