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