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 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;
34 import java.util.HashMap;
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.
41 public class StubEsController implements DocumentStoreInterface {
43 public static final String DOES_NOT_EXIST_INDEX = "index-does-not-exist";
45 private AnalysisConfiguration analysisConfig = null;
50 // private IndexAPIHarness indexAPIHarness;
53 analysisConfig = new AnalysisConfiguration();
54 analysisConfig.init("src/test/resources/json/filter-config.json",
55 "src/test/resources/json/analysis-config.json");
59 public OperationResult createIndex(String index, DocumentSchema documentSchema) {
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);
67 opResult.setResult(index + "@" + analysisConfig.getEsIndexSettings() + "@"
68 + DocumentSchemaUtil.generateDocumentMappings(documentSchema));
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
79 opResult.setResult(dynamicSchema);
84 public OperationResult deleteIndex(String indexName) throws DocumentStoreOperationException {
86 OperationResult opResult = new OperationResult();
88 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
89 opResult.setResultCode(404);
91 opResult.setResultCode(200);
92 opResult.setResult(indexName);
99 public DocumentOperationResult createDocument(String indexName, DocumentStoreDataEntity document,
100 boolean allowImplicitIndexCreation) throws DocumentStoreOperationException {
102 DocumentOperationResult opResult = buildSampleDocumentOperationResult();
104 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
105 opResult.setResultCode(404);
107 opResult.setResultCode(200);
109 if (document.getId() != null) {
110 id = document.getId();
112 opResult.setResultVersion("1");
119 public DocumentOperationResult updateDocument(String indexName, DocumentStoreDataEntity document,
120 boolean allowImplicitIndexCreation) throws DocumentStoreOperationException {
122 DocumentOperationResult opResult = buildSampleDocumentOperationResult();
124 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
125 opResult.setResultCode(404);
127 opResult.setResultCode(200);
128 String version = "1";
129 if (document.getVersion() != null) {
130 version = String.valueOf(Integer.parseInt(document.getVersion()) + 1);
132 opResult.setResultVersion(version);
139 public DocumentOperationResult deleteDocument(String indexName, DocumentStoreDataEntity document)
140 throws DocumentStoreOperationException {
141 DocumentOperationResult opResult = buildSampleDocumentOperationResult();
143 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
144 opResult.setResultCode(404);
146 if (opResult.getDocument() != null) {
147 opResult.getDocument().setEtag(null);
148 opResult.getDocument().setUrl(null);
150 opResult.setResultCode(200);
151 opResult.setResult(indexName + "@" + document.getId());
158 public DocumentOperationResult getDocument(String indexName, DocumentStoreDataEntity document)
159 throws DocumentStoreOperationException {
160 DocumentOperationResult opResult = buildSampleDocumentOperationResult();
162 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
163 opResult.setResultCode(404);
165 opResult.setResultCode(200);
172 public SearchOperationResult search(String indexName, String queryText)
173 throws DocumentStoreOperationException {
175 SearchOperationResult opResult = buildSampleSearchOperationResult();
177 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
178 opResult.setResultCode(404);
180 opResult.setResultCode(200);
181 opResult.setResult(indexName + "@" + queryText);
188 public SearchOperationResult searchWithPayload(String indexName, String query)
189 throws DocumentStoreOperationException {
190 SearchOperationResult opResult = buildSampleSearchOperationResult();
192 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
193 opResult.setResultCode(404);
195 opResult.setResultCode(200);
196 opResult.setResult(indexName + "@" + query);
203 public SearchOperationResult suggestionQueryWithPayload(String indexName, String query)
204 throws DocumentStoreOperationException {
205 SearchOperationResult opResult = new SearchOperationResult();
207 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
208 opResult.setResultCode(404);
210 opResult.setResultCode(200);
211 opResult.setResult(indexName + "@" + query);
218 public OperationResult performBulkOperations(BulkRequest[] requests)
219 throws DocumentStoreOperationException {
221 OperationResult opResult = new OperationResult();
222 opResult.setResultCode(200);
227 private DocumentOperationResult buildSampleDocumentOperationResult() {
228 DocumentOperationResult result = new DocumentOperationResult();
229 Document doc = new Document();
230 doc.setEtag("etag1");
232 doc.setContent(new JSONObject());
233 result.setDocument(doc);
237 private SearchOperationResult buildSampleSearchOperationResult() {
238 SearchOperationResult result = new SearchOperationResult();
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;
251 searchHits.setHits(searchHitArray);
252 searchHits.setTotalHits("1");
253 result.setSearchResult(searchHits);