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
40 * that we can run unit tests without trying to connect to a real
43 public class StubEsController implements DocumentStoreInterface {
45 public static final String DOES_NOT_EXIST_INDEX = "index-does-not-exist";
47 private AnalysisConfiguration analysisConfig = null;
52 //private IndexAPIHarness indexAPIHarness;
55 analysisConfig = new AnalysisConfiguration();
56 analysisConfig.init("src/test/resources/json/filter-config.json",
57 "src/test/resources/json/analysis-config.json");
62 public OperationResult createIndex(String index,
63 DocumentSchema documentSchema) {
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);
72 opResult.setResult(index + "@" + analysisConfig.getEsIndexSettings() + "@"
73 + DocumentSchemaUtil.generateDocumentMappings(documentSchema));
74 } catch (IOException e) {
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);
92 public OperationResult deleteIndex(String indexName) throws DocumentStoreOperationException {
94 OperationResult opResult = new OperationResult();
96 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
97 opResult.setResultCode(404);
99 opResult.setResultCode(200);
100 opResult.setResult(indexName);
107 public DocumentOperationResult createDocument(String indexName,
108 DocumentStoreDataEntity document,
109 boolean allowImplicitIndexCreation)
110 throws DocumentStoreOperationException {
112 DocumentOperationResult opResult = buildSampleDocumentOperationResult();
114 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
115 opResult.setResultCode(404);
117 opResult.setResultCode(200);
119 if (document.getId() != null) {
120 id = document.getId();
122 opResult.setResultVersion("1");
129 public DocumentOperationResult updateDocument(String indexName,
130 DocumentStoreDataEntity document,
131 boolean allowImplicitIndexCreation)
132 throws DocumentStoreOperationException {
134 DocumentOperationResult opResult = buildSampleDocumentOperationResult();
136 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
137 opResult.setResultCode(404);
139 opResult.setResultCode(200);
140 String version = "1";
141 if (document.getVersion() != null) {
142 version = String.valueOf(Integer.parseInt(document.getVersion()) + 1);
144 opResult.setResultVersion(version);
151 public DocumentOperationResult deleteDocument(String indexName,
152 DocumentStoreDataEntity document) throws DocumentStoreOperationException {
153 DocumentOperationResult opResult = buildSampleDocumentOperationResult();
156 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
157 opResult.setResultCode(404);
159 if (opResult.getDocument() != null) {
160 opResult.getDocument().setEtag(null);
161 opResult.getDocument().setUrl(null);
162 opResult.setResultVersion("1");
164 opResult.setResultCode(200);
165 opResult.setResult(indexName + "@" + document.getId());
166 opResult.setResultVersion("1");
173 public DocumentOperationResult getDocument(String indexName,
174 DocumentStoreDataEntity document) throws DocumentStoreOperationException {
175 DocumentOperationResult opResult = buildSampleDocumentOperationResult();
177 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
178 opResult.setResultCode(404);
179 // Adding it to make the tests pass.
180 opResult.setResultVersion ("1");
182 opResult.setResultCode(200);
183 // Adding it to make the tests pass.
184 opResult.setResultVersion ("1");
191 public SearchOperationResult search(String indexName,
192 String queryText) throws DocumentStoreOperationException {
194 SearchOperationResult opResult = buildSampleSearchOperationResult();
196 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
197 opResult.setResultCode(404);
199 opResult.setResultCode(200);
200 opResult.setResult(indexName + "@" + queryText);
207 public SearchOperationResult searchWithPayload(String indexName,
208 String query) throws DocumentStoreOperationException {
209 SearchOperationResult opResult = buildSampleSearchOperationResult();
211 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
212 opResult.setResultCode(404);
214 opResult.setResultCode(200);
215 opResult.setResult(indexName + "@" + query);
222 public SearchOperationResult suggestionQueryWithPayload(String indexName, String query)
223 throws DocumentStoreOperationException {
224 SearchOperationResult opResult = new SearchOperationResult();
226 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
227 opResult.setResultCode(404);
229 opResult.setResultCode(200);
230 opResult.setResult(indexName + "@" + query);
237 public OperationResult performBulkOperations(BulkRequest[] requests) throws DocumentStoreOperationException {
239 OperationResult opResult = new OperationResult();
240 opResult.setResultCode(200);
245 private DocumentOperationResult buildSampleDocumentOperationResult() {
246 DocumentOperationResult result = new DocumentOperationResult();
247 Document doc = new Document();
248 doc.setEtag("etag1");
250 doc.setContent(new JSONObject());
251 result.setDocument(doc);
255 private SearchOperationResult buildSampleSearchOperationResult() {
256 SearchOperationResult result = new SearchOperationResult();
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;
269 searchHits.setHits(searchHitArray);
270 searchHits.setTotalHits("1");
271 result.setSearchResult(searchHits);