Updating Search service to be ES 6.1.2 compliant
[aai/search-data-service.git] / src / test / java / org / onap / aai / sa / rest / StubEsController.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.json.simple.JSONObject;
24 import org.onap.aai.sa.rest.AnalysisConfiguration;
25 import org.onap.aai.sa.rest.BulkRequest;
26 import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreDataEntity;
27 import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreInterface;
28 import org.onap.aai.sa.searchdbabstraction.elasticsearch.exception.DocumentStoreOperationException;
29 import org.onap.aai.sa.searchdbabstraction.entity.*;
30 import org.onap.aai.sa.searchdbabstraction.util.DocumentSchemaUtil;
31 import org.onap.aai.sa.searchdbabstraction.entity.Document;
32 import org.onap.aai.sa.rest.DocumentSchema;
33
34 import java.io.IOException;
35 import java.util.HashMap;
36 import java.util.Map;
37
38 /**
39  * This class implements a stubbed version of the document store DAO so
40  * that we can run unit tests without trying to connect to a real
41  * document store.
42  */
43 public class StubEsController implements DocumentStoreInterface {
44
45   public static final String DOES_NOT_EXIST_INDEX = "index-does-not-exist";
46
47   private AnalysisConfiguration analysisConfig = null;
48
49   /**
50    *
51    */
52   //private IndexAPIHarness indexAPIHarness;
53
54   StubEsController() {
55     analysisConfig = new AnalysisConfiguration();
56     analysisConfig.init("src/test/resources/json/filter-config.json",
57         "src/test/resources/json/analysis-config.json");
58   }
59
60
61   @Override
62   public OperationResult createIndex(String         index, 
63                                      DocumentSchema documentSchema) {
64
65     // Just return an OK result, with the parameters that we were passed
66     // bundled in the response string. This allows unit tests to validate
67     // that those parameters match what they expected to be passed.
68     OperationResult opResult = new OperationResult();
69     opResult.setResultCode(200);
70
71     try {
72                 opResult.setResult(index + "@" + analysisConfig.getEsIndexSettings() + "@"
73                     + DocumentSchemaUtil.generateDocumentMappings(documentSchema));
74         } catch (IOException e) {
75                 e.printStackTrace();
76         }
77
78     return opResult;
79   }
80
81   @Override
82   public OperationResult createDynamicIndex(String index, String dynamicSchema) {
83     OperationResult opResult = new OperationResult();
84     opResult.setResultCode(200);
85     // Directly return the json as this flow should not edit the json in any way
86     opResult.setResult(dynamicSchema);
87     return opResult;
88   }
89
90
91   @Override
92   public OperationResult deleteIndex(String indexName) throws DocumentStoreOperationException {
93
94     OperationResult opResult = new OperationResult();
95
96     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
97       opResult.setResultCode(404);
98     } else {
99       opResult.setResultCode(200);
100       opResult.setResult(indexName);
101     }
102
103     return opResult;
104   }
105
106   @Override
107   public DocumentOperationResult createDocument(String                  indexName,
108                                                 DocumentStoreDataEntity document,
109                                                 boolean                 allowImplicitIndexCreation) 
110     throws DocumentStoreOperationException {
111     
112     DocumentOperationResult opResult = buildSampleDocumentOperationResult();
113
114     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
115       opResult.setResultCode(404);
116     } else {
117       opResult.setResultCode(200);
118       String id = "dummy";
119       if (document.getId() != null) {
120         id = document.getId();
121       }
122       opResult.setResultVersion("1");
123     }
124
125     return opResult;
126   }
127
128   @Override
129   public DocumentOperationResult updateDocument(String                  indexName,
130                                                 DocumentStoreDataEntity document,
131                                                 boolean                 allowImplicitIndexCreation) 
132     throws DocumentStoreOperationException {
133     
134     DocumentOperationResult opResult = buildSampleDocumentOperationResult();
135
136     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
137       opResult.setResultCode(404);
138     } else {
139       opResult.setResultCode(200);
140       String version = "1";
141       if (document.getVersion() != null) {
142         version = String.valueOf(Integer.parseInt(document.getVersion()) + 1);
143       }
144       opResult.setResultVersion(version);
145     }
146
147     return opResult;
148   }
149
150   @Override
151   public DocumentOperationResult deleteDocument(String indexName,
152                                                 DocumentStoreDataEntity document) throws DocumentStoreOperationException {
153     DocumentOperationResult opResult = buildSampleDocumentOperationResult();
154
155
156     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
157       opResult.setResultCode(404);
158     } else {
159       if (opResult.getDocument() != null) {
160         opResult.getDocument().setEtag(null);
161         opResult.getDocument().setUrl(null);
162         opResult.setResultVersion("1");
163       }
164       opResult.setResultCode(200);
165       opResult.setResult(indexName + "@" + document.getId());
166       opResult.setResultVersion("1");
167     }
168
169     return opResult;
170   }
171
172   @Override
173   public DocumentOperationResult getDocument(String indexName,
174                                              DocumentStoreDataEntity document) throws DocumentStoreOperationException {
175     DocumentOperationResult opResult = buildSampleDocumentOperationResult();
176
177     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
178       opResult.setResultCode(404);
179       // Adding it to make the tests pass.
180       opResult.setResultVersion ("1");
181     } else {
182       opResult.setResultCode(200);
183       // Adding it to make the tests pass.
184       opResult.setResultVersion ("1");
185     }
186
187     return opResult;
188   }
189
190   @Override
191   public SearchOperationResult search(String indexName,
192                                       String queryText) throws DocumentStoreOperationException {
193
194     SearchOperationResult opResult = buildSampleSearchOperationResult();
195
196     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
197       opResult.setResultCode(404);
198     } else {
199       opResult.setResultCode(200);
200       opResult.setResult(indexName + "@" + queryText);
201     }
202
203     return opResult;
204   }
205
206   @Override
207   public SearchOperationResult searchWithPayload(String indexName,
208                                                  String query) throws DocumentStoreOperationException {
209     SearchOperationResult opResult = buildSampleSearchOperationResult();
210
211     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
212       opResult.setResultCode(404);
213     } else {
214       opResult.setResultCode(200);
215       opResult.setResult(indexName + "@" + query);
216     }
217
218     return opResult;
219   }
220
221   @Override
222   public SearchOperationResult suggestionQueryWithPayload(String indexName, String query)
223           throws DocumentStoreOperationException {
224     SearchOperationResult opResult = new SearchOperationResult();
225
226     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
227       opResult.setResultCode(404);
228     } else {
229       opResult.setResultCode(200);
230       opResult.setResult(indexName + "@" + query);
231     }
232
233     return opResult;
234   }
235
236   @Override
237   public OperationResult performBulkOperations(BulkRequest[] requests) throws DocumentStoreOperationException {
238
239     OperationResult opResult = new OperationResult();
240     opResult.setResultCode(200);
241
242     return opResult;
243   }
244
245   private DocumentOperationResult buildSampleDocumentOperationResult() {
246     DocumentOperationResult result = new DocumentOperationResult();
247     Document doc = new Document();
248     doc.setEtag("etag1");
249
250     doc.setContent(new JSONObject());
251     result.setDocument(doc);
252     return result;
253   }
254
255   private SearchOperationResult buildSampleSearchOperationResult() {
256     SearchOperationResult result = new SearchOperationResult();
257
258     SearchHits searchHits = new SearchHits();
259     SearchHit[] searchHitArray = new SearchHit[1];
260     SearchHit searchHit = new SearchHit();
261     Document doc = new Document();
262     doc.setEtag("etag1");
263     Map<String, Object> content = new HashMap<String, Object>();
264     content.put("key1", "value1");
265     doc.setContent(new JSONObject());
266     searchHit.setDocument(doc);
267     searchHitArray[0] = searchHit;
268
269     searchHits.setHits(searchHitArray);
270     searchHits.setTotalHits("1");
271     result.setSearchResult(searchHits);
272
273     return result;
274
275   }
276
277 }