Update license date and text
[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   @DELETE
67   @Path("/indexes/{index}")
68   @Consumes({MediaType.APPLICATION_JSON})
69   @Override
70   public Response processDeleteIndex(String requestBody,
71                                      @Context HttpServletRequest request,
72                                      @Context HttpHeaders headers,
73                                      @PathParam("index") String index) {
74
75     return super.processDeleteIndex(requestBody, request, headers, index);
76   }
77
78   @GET
79   @Path("/indexes/{index}/documents/{id}")
80   @Consumes({MediaType.APPLICATION_JSON})
81   @Override
82   public Response processGetDocument(String requestBody,
83                                      @Context HttpServletRequest request,
84                                      @Context HttpServletResponse httpResponse,
85                                      @Context HttpHeaders headers,
86                                      @PathParam("index") String index,
87                                      @PathParam("id") String id) {
88
89     return super.processGetDocument(requestBody, request, httpResponse, headers, index, id);
90   }
91
92   @POST
93   @Path("/indexes/{index}/documents")
94   @Consumes({MediaType.APPLICATION_JSON})
95   @Override
96   public Response processCreateDocWithoutId(String requestBody,
97                                             @Context HttpServletRequest request,
98                                             @Context HttpServletResponse httpResponse,
99                                             @Context HttpHeaders headers,
100                                             @PathParam("index") String index) {
101
102     return super.processCreateDocWithoutId(requestBody, request, httpResponse, headers, index);
103   }
104
105   @PUT
106   @Path("/indexes/{index}/documents/{id}")
107   @Consumes({MediaType.APPLICATION_JSON})
108   @Override
109   public Response processUpsertDoc(String requestBody,
110                                    @Context HttpServletRequest request,
111                                    @Context HttpServletResponse httpResponse,
112                                    @Context HttpHeaders headers,
113                                    @PathParam("index") String index,
114                                    @PathParam("id") String id) {
115
116     return super.processUpsertDoc(requestBody, request, httpResponse, headers, index, id);
117   }
118
119   @DELETE
120   @Path("/indexes/{index}/documents/{id}")
121   @Consumes({MediaType.APPLICATION_JSON})
122   @Override
123   public Response processDeleteDoc(String requestBody,
124                                    @Context HttpServletRequest request,
125                                    @Context HttpServletResponse httpResponse,
126                                    @Context HttpHeaders headers,
127                                    @PathParam("index") String index,
128                                    @PathParam("id") String id) {
129
130     return super.processDeleteDoc(requestBody, request, httpResponse, headers, index, id);
131   }
132
133   @GET
134   @Path("/indexes/{index}/query/{queryText}")
135   @Consumes({MediaType.APPLICATION_JSON})
136   @Override
137   public Response processInlineQuery(String requestBody,
138                                      @Context HttpServletRequest request,
139                                      @Context HttpHeaders headers,
140                                      @PathParam("index") String index,
141                                      @PathParam("queryText") String queryText) {
142
143     return super.processInlineQuery(requestBody, request, headers, index, queryText);
144   }
145
146   @GET
147   @Path("/indexes/{index}/query")
148   @Consumes({MediaType.APPLICATION_JSON})
149   @Override
150   public Response processQueryWithGet(String requestBody,
151                                       @Context HttpServletRequest request,
152                                       @Context HttpHeaders headers,
153                                       @PathParam("index") String index) {
154
155     return super.processQueryWithGet(requestBody, request, headers, index);
156   }
157
158   @POST
159   @Path("/indexes/{index}/query")
160   @Consumes({MediaType.APPLICATION_JSON})
161   @Override
162   public Response processQuery(String requestBody,
163                                @Context HttpServletRequest request,
164                                @Context HttpHeaders headers,
165                                @PathParam("index") String index) {
166
167     return super.processQuery(requestBody, request, headers, index);
168   }
169
170   @POST
171   @Path("/bulk")
172   @Consumes({MediaType.APPLICATION_JSON})
173   @Override
174   public Response processBulkRequest(String requestBody,
175                                      @Context HttpServletRequest request,
176                                      @Context HttpHeaders headers,
177                                      @PathParam("index") String index) {
178
179     // If the operations string contains a special keyword, set the
180     // harness to fail the authentication validation.
181     if (requestBody.contains(FAIL_AUTHENTICATION_TRIGGER)) {
182       authenticationShouldSucceed = false;
183     }
184
185     // Just pass the request up to the parent, since that is the code
186     // that we really want to test.
187     //return super.processPost(operations, request, headers, index);
188     return super.processBulkRequest(requestBody, request, headers, index);
189   }
190
191   @Override
192   protected boolean validateRequest(HttpHeaders headers,
193                                     HttpServletRequest req,
194                                     ApiUtils.Action action,
195                                     String authPolicyFunctionName) throws Exception {
196
197     return authenticationShouldSucceed;
198   }
199 }