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