b15ccc8fc9801d26e2d2d64df4a6bc242f817577
[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 javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
25 import javax.ws.rs.*;
26 import javax.ws.rs.core.Context;
27 import javax.ws.rs.core.HttpHeaders;
28 import javax.ws.rs.core.MediaType;
29 import javax.ws.rs.core.Response;
30
31 import org.onap.aai.sa.rest.ApiUtils;
32 import org.onap.aai.sa.rest.SearchServiceApi;
33
34 @Path("test/")
35 public class SearchServiceApiHarness extends SearchServiceApi {
36
37
38   public static final String FAIL_AUTHENTICATION_TRIGGER = "FAIL AUTHENTICATION";
39
40   private boolean authenticationShouldSucceed = true;
41
42
43   /**
44    * Performs all one-time initialization required for the end point.
45    */
46   @Override
47   public void init() {
48
49     // Instantiate our Document Store DAO.
50     documentStore = new StubEsController();
51   }
52
53
54   @PUT
55   @Path("/indexes/{index}")
56   @Consumes({MediaType.APPLICATION_JSON})
57   @Override
58   public Response processCreateIndex(String requestBody,
59                                      @Context HttpServletRequest request,
60                                      @Context HttpHeaders headers,
61                                      @PathParam("index") String index) {
62
63     return super.processCreateIndex(requestBody, request, headers, index);
64   }
65
66   @PUT
67   @Path("/indexes/dynamic/{index}")
68   @Consumes({MediaType.APPLICATION_JSON})
69   @Override
70   public Response processCreateDynamicIndex(String requestBody,
71                                      @Context HttpServletRequest request,
72                                      @Context HttpHeaders headers,
73                                      @PathParam("index") String index) {
74
75     return super.processCreateDynamicIndex(requestBody, request, headers, index);
76   }
77
78   @DELETE
79   @Path("/indexes/{index}")
80   @Consumes({MediaType.APPLICATION_JSON})
81   @Override
82   public Response processDeleteIndex(String requestBody,
83                                      @Context HttpServletRequest request,
84                                      @Context HttpHeaders headers,
85                                      @PathParam("index") String index) {
86
87     return super.processDeleteIndex(requestBody, request, headers, index);
88   }
89
90   @GET
91   @Path("/indexes/{index}/documents/{id}")
92   @Consumes({MediaType.APPLICATION_JSON})
93   @Override
94   public Response processGetDocument(String requestBody,
95                                      @Context HttpServletRequest request,
96                                      @Context HttpServletResponse httpResponse,
97                                      @Context HttpHeaders headers,
98                                      @PathParam("index") String index,
99                                      @PathParam("id") String id) {
100
101     return super.processGetDocument(requestBody, request, httpResponse, headers, index, id);
102   }
103
104   @POST
105   @Path("/indexes/{index}/documents")
106   @Consumes({MediaType.APPLICATION_JSON})
107   @Override
108   public Response processCreateDocWithoutId(String requestBody,
109                                             @Context HttpServletRequest request,
110                                             @Context HttpServletResponse httpResponse,
111                                             @Context HttpHeaders headers,
112                                             @PathParam("index") String index) {
113
114     return super.processCreateDocWithoutId(requestBody, request, httpResponse, headers, index);
115   }
116
117   @PUT
118   @Path("/indexes/{index}/documents/{id}")
119   @Consumes({MediaType.APPLICATION_JSON})
120   @Override
121   public Response processUpsertDoc(String requestBody,
122                                    @Context HttpServletRequest request,
123                                    @Context HttpServletResponse httpResponse,
124                                    @Context HttpHeaders headers,
125                                    @PathParam("index") String index,
126                                    @PathParam("id") String id) {
127
128     return super.processUpsertDoc(requestBody, request, httpResponse, headers, index, id);
129   }
130
131   @DELETE
132   @Path("/indexes/{index}/documents/{id}")
133   @Consumes({MediaType.APPLICATION_JSON})
134   @Override
135   public Response processDeleteDoc(String requestBody,
136                                    @Context HttpServletRequest request,
137                                    @Context HttpServletResponse httpResponse,
138                                    @Context HttpHeaders headers,
139                                    @PathParam("index") String index,
140                                    @PathParam("id") String id) {
141
142     return super.processDeleteDoc(requestBody, request, httpResponse, headers, index, id);
143   }
144
145   @GET
146   @Path("/indexes/{index}/query/{queryText}")
147   @Consumes({MediaType.APPLICATION_JSON})
148   @Override
149   public Response processInlineQuery(String requestBody,
150                                      @Context HttpServletRequest request,
151                                      @Context HttpHeaders headers,
152                                      @PathParam("index") String index,
153                                      @PathParam("queryText") String queryText) {
154
155     return super.processInlineQuery(requestBody, request, headers, index, queryText);
156   }
157
158   @GET
159   @Path("/indexes/{index}/query")
160   @Consumes({MediaType.APPLICATION_JSON})
161   @Override
162   public Response processQueryWithGet(String requestBody,
163                                       @Context HttpServletRequest request,
164                                       @Context HttpHeaders headers,
165                                       @PathParam("index") String index) {
166
167     return super.processQueryWithGet(requestBody, request, headers, index);
168   }
169
170   @POST
171   @Path("/indexes/{index}/query")
172   @Consumes({MediaType.APPLICATION_JSON})
173   @Override
174   public Response processQuery(String requestBody,
175                                @Context HttpServletRequest request,
176                                @Context HttpHeaders headers,
177                                @PathParam("index") String index) {
178
179     return super.processQuery(requestBody, request, headers, index);
180   }
181
182   @POST
183   @Path("/bulk")
184   @Consumes({MediaType.APPLICATION_JSON})
185   @Override
186   public Response processBulkRequest(String requestBody,
187                                      @Context HttpServletRequest request,
188                                      @Context HttpHeaders headers,
189                                      @PathParam("index") String index) {
190
191     // If the operations string contains a special keyword, set the
192     // harness to fail the authentication validation.
193     if (requestBody.contains(FAIL_AUTHENTICATION_TRIGGER)) {
194       authenticationShouldSucceed = false;
195     }
196
197     // Just pass the request up to the parent, since that is the code
198     // that we really want to test.
199     //return super.processPost(operations, request, headers, index);
200     return super.processBulkRequest(requestBody, request, headers, index);
201   }
202
203   @Override
204   protected boolean validateRequest(HttpHeaders headers,
205                                     HttpServletRequest req,
206                                     ApiUtils.Action action,
207                                     String authPolicyFunctionName) throws Exception {
208
209     return authenticationShouldSucceed;
210   }
211 }