2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.sa.rest;
23 import java.io.IOException;
24 import java.util.HashMap;
26 import org.json.simple.JSONObject;
27 import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreDataEntity;
28 import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreInterface;
29 import org.onap.aai.sa.searchdbabstraction.elasticsearch.exception.DocumentStoreOperationException;
30 import org.onap.aai.sa.searchdbabstraction.entity.Document;
31 import org.onap.aai.sa.searchdbabstraction.entity.DocumentOperationResult;
32 import org.onap.aai.sa.searchdbabstraction.entity.OperationResult;
33 import org.onap.aai.sa.searchdbabstraction.entity.SearchHit;
34 import org.onap.aai.sa.searchdbabstraction.entity.SearchHits;
35 import org.onap.aai.sa.searchdbabstraction.entity.SearchOperationResult;
36 import org.onap.aai.sa.searchdbabstraction.util.DocumentSchemaUtil;
39 * This class implements a stubbed version of the document store DAO so that we can run unit tests without trying to
40 * connect to a real document store.
42 public class StubEsController implements DocumentStoreInterface {
44 public static final String DOES_NOT_EXIST_INDEX = "index-does-not-exist";
46 private AnalysisConfiguration analysisConfig = null;
51 // private IndexAPIHarness indexAPIHarness;
54 analysisConfig = new AnalysisConfiguration();
55 analysisConfig.init("src/test/resources/json/filter-config.json",
56 "src/test/resources/json/analysis-config.json");
61 public OperationResult createIndex(String index, DocumentSchema documentSchema) {
63 // Just return an OK result, with the parameters that we were passed
64 // bundled in the response string. This allows unit tests to validate
65 // that those parameters match what they expected to be passed.
66 OperationResult opResult = new OperationResult();
67 opResult.setResultCode(200);
70 opResult.setResult(index + "@" + analysisConfig.getEsIndexSettings() + "@"
71 + DocumentSchemaUtil.generateDocumentMappings(documentSchema));
72 } catch (IOException e) {
80 public OperationResult createDynamicIndex(String index, String dynamicSchema) {
81 OperationResult opResult = new OperationResult();
82 opResult.setResultCode(200);
83 // Directly return the json as this flow should not edit the json in any way
84 opResult.setResult(dynamicSchema);
90 public OperationResult deleteIndex(String indexName) throws DocumentStoreOperationException {
92 OperationResult opResult = new OperationResult();
94 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
95 opResult.setResultCode(404);
97 opResult.setResultCode(200);
98 opResult.setResult(indexName);
105 public DocumentOperationResult createDocument(String indexName, DocumentStoreDataEntity document,
106 boolean allowImplicitIndexCreation) throws DocumentStoreOperationException {
108 DocumentOperationResult opResult = buildSampleDocumentOperationResult();
110 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
111 opResult.setResultCode(404);
113 opResult.setResultCode(200);
114 opResult.setResultVersion("1");
121 public DocumentOperationResult updateDocument(String indexName, DocumentStoreDataEntity document,
122 boolean allowImplicitIndexCreation) throws DocumentStoreOperationException {
124 DocumentOperationResult opResult = buildSampleDocumentOperationResult();
126 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
127 opResult.setResultCode(404);
129 opResult.setResultCode(200);
130 String version = "1";
131 if (document.getVersion() != null) {
132 version = String.valueOf(Integer.parseInt(document.getVersion()) + 1);
134 opResult.setResultVersion(version);
141 public DocumentOperationResult deleteDocument(String indexName, DocumentStoreDataEntity document)
142 throws DocumentStoreOperationException {
143 DocumentOperationResult opResult = buildSampleDocumentOperationResult();
146 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
147 opResult.setResultCode(404);
149 if (opResult.getDocument() != null) {
150 opResult.getDocument().setEtag(null);
151 opResult.getDocument().setUrl(null);
152 opResult.setResultVersion("1");
154 opResult.setResultCode(200);
155 opResult.setResult(indexName + "@" + document.getId());
156 opResult.setResultVersion("1");
163 public DocumentOperationResult getDocument(String indexName, DocumentStoreDataEntity document)
164 throws DocumentStoreOperationException {
165 DocumentOperationResult opResult = buildSampleDocumentOperationResult();
167 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
168 opResult.setResultCode(404);
169 // Adding it to make the tests pass.
170 opResult.setResultVersion("1");
172 opResult.setResultCode(200);
173 // Adding it to make the tests pass.
174 opResult.setResultVersion("1");
181 public SearchOperationResult search(String indexName, String queryText) throws DocumentStoreOperationException {
183 SearchOperationResult opResult = buildSampleSearchOperationResult();
185 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
186 opResult.setResultCode(404);
188 opResult.setResultCode(200);
189 opResult.setResult(indexName + "@" + queryText);
196 public SearchOperationResult searchWithPayload(String indexName, String query)
197 throws DocumentStoreOperationException {
198 SearchOperationResult opResult = buildSampleSearchOperationResult();
200 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
201 opResult.setResultCode(404);
203 opResult.setResultCode(200);
204 opResult.setResult(indexName + "@" + query);
211 public SearchOperationResult suggestionQueryWithPayload(String indexName, String query)
212 throws DocumentStoreOperationException {
213 SearchOperationResult opResult = new SearchOperationResult();
215 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
216 opResult.setResultCode(404);
218 opResult.setResultCode(200);
219 opResult.setResult(indexName + "@" + query);
226 public OperationResult performBulkOperations(BulkRequest[] requests) throws DocumentStoreOperationException {
228 OperationResult opResult = new OperationResult();
229 opResult.setResultCode(200);
234 private DocumentOperationResult buildSampleDocumentOperationResult() {
235 DocumentOperationResult result = new DocumentOperationResult();
236 Document doc = new Document();
237 doc.setEtag("etag1");
239 doc.setContent(new JSONObject());
240 result.setDocument(doc);
244 private SearchOperationResult buildSampleSearchOperationResult() {
245 SearchOperationResult result = new SearchOperationResult();
247 SearchHits searchHits = new SearchHits();
248 SearchHit[] searchHitArray = new SearchHit[1];
249 SearchHit searchHit = new SearchHit();
250 Document doc = new Document();
251 doc.setEtag("etag1");
252 Map<String, Object> content = new HashMap<>();
253 content.put("key1", "value1");
254 doc.setContent(new JSONObject());
255 searchHit.setDocument(doc);
256 searchHitArray[0] = searchHit;
258 searchHits.setHits(searchHitArray);
259 searchHits.setTotalHits("1");
260 result.setSearchResult(searchHits);