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);
115 if (document.getId() != null) {
116 id = document.getId();
118 opResult.setResultVersion("1");
125 public DocumentOperationResult updateDocument(String indexName, DocumentStoreDataEntity document,
126 boolean allowImplicitIndexCreation) throws DocumentStoreOperationException {
128 DocumentOperationResult opResult = buildSampleDocumentOperationResult();
130 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
131 opResult.setResultCode(404);
133 opResult.setResultCode(200);
134 String version = "1";
135 if (document.getVersion() != null) {
136 version = String.valueOf(Integer.parseInt(document.getVersion()) + 1);
138 opResult.setResultVersion(version);
145 public DocumentOperationResult deleteDocument(String indexName, DocumentStoreDataEntity document)
146 throws DocumentStoreOperationException {
147 DocumentOperationResult opResult = buildSampleDocumentOperationResult();
150 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
151 opResult.setResultCode(404);
153 if (opResult.getDocument() != null) {
154 opResult.getDocument().setEtag(null);
155 opResult.getDocument().setUrl(null);
156 opResult.setResultVersion("1");
158 opResult.setResultCode(200);
159 opResult.setResult(indexName + "@" + document.getId());
160 opResult.setResultVersion("1");
167 public DocumentOperationResult getDocument(String indexName, DocumentStoreDataEntity document)
168 throws DocumentStoreOperationException {
169 DocumentOperationResult opResult = buildSampleDocumentOperationResult();
171 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
172 opResult.setResultCode(404);
173 // Adding it to make the tests pass.
174 opResult.setResultVersion("1");
176 opResult.setResultCode(200);
177 // Adding it to make the tests pass.
178 opResult.setResultVersion("1");
185 public SearchOperationResult search(String indexName, String queryText) throws DocumentStoreOperationException {
187 SearchOperationResult opResult = buildSampleSearchOperationResult();
189 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
190 opResult.setResultCode(404);
192 opResult.setResultCode(200);
193 opResult.setResult(indexName + "@" + queryText);
200 public SearchOperationResult searchWithPayload(String indexName, String query)
201 throws DocumentStoreOperationException {
202 SearchOperationResult opResult = buildSampleSearchOperationResult();
204 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
205 opResult.setResultCode(404);
207 opResult.setResultCode(200);
208 opResult.setResult(indexName + "@" + query);
215 public SearchOperationResult suggestionQueryWithPayload(String indexName, String query)
216 throws DocumentStoreOperationException {
217 SearchOperationResult opResult = new SearchOperationResult();
219 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
220 opResult.setResultCode(404);
222 opResult.setResultCode(200);
223 opResult.setResult(indexName + "@" + query);
230 public OperationResult performBulkOperations(BulkRequest[] requests) throws DocumentStoreOperationException {
232 OperationResult opResult = new OperationResult();
233 opResult.setResultCode(200);
238 private DocumentOperationResult buildSampleDocumentOperationResult() {
239 DocumentOperationResult result = new DocumentOperationResult();
240 Document doc = new Document();
241 doc.setEtag("etag1");
243 doc.setContent(new JSONObject());
244 result.setDocument(doc);
248 private SearchOperationResult buildSampleSearchOperationResult() {
249 SearchOperationResult result = new SearchOperationResult();
251 SearchHits searchHits = new SearchHits();
252 SearchHit[] searchHitArray = new SearchHit[1];
253 SearchHit searchHit = new SearchHit();
254 Document doc = new Document();
255 doc.setEtag("etag1");
256 Map<String, Object> content = new HashMap<String, Object>();
257 content.put("key1", "value1");
258 doc.setContent(new JSONObject());
259 searchHit.setDocument(doc);
260 searchHitArray[0] = searchHit;
262 searchHits.setHits(searchHitArray);
263 searchHits.setTotalHits("1");
264 result.setSearchResult(searchHits);