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
39 * that we can run unit tests without trying to connect to a real
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,
62 DocumentSchema documentSchema) {
64 // Just return an OK result, with the parameters that we were passed
65 // bundled in the response string. This allows unit tests to validate
66 // that those parameters match what they expected to be passed.
67 OperationResult opResult = new OperationResult();
68 opResult.setResultCode(200);
70 opResult.setResult(index + "@" + analysisConfig.getEsIndexSettings() + "@"
71 + DocumentSchemaUtil.generateDocumentMappings(documentSchema));
77 public OperationResult createDynamicIndex(String index, String dynamicSchema) {
78 OperationResult opResult = new OperationResult();
79 opResult.setResultCode(200);
80 // Directly return the json as this flow should not edit the json in any way
81 opResult.setResult(dynamicSchema);
87 public OperationResult deleteIndex(String indexName) throws DocumentStoreOperationException {
89 OperationResult opResult = new OperationResult();
91 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
92 opResult.setResultCode(404);
94 opResult.setResultCode(200);
95 opResult.setResult(indexName);
102 public DocumentOperationResult createDocument(String indexName,
103 DocumentStoreDataEntity document,
104 boolean allowImplicitIndexCreation)
105 throws DocumentStoreOperationException {
107 DocumentOperationResult opResult = buildSampleDocumentOperationResult();
109 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
110 opResult.setResultCode(404);
112 opResult.setResultCode(200);
114 if (document.getId() != null) {
115 id = document.getId();
117 opResult.setResultVersion("1");
124 public DocumentOperationResult updateDocument(String indexName,
125 DocumentStoreDataEntity document,
126 boolean allowImplicitIndexCreation)
127 throws DocumentStoreOperationException {
129 DocumentOperationResult opResult = buildSampleDocumentOperationResult();
131 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
132 opResult.setResultCode(404);
134 opResult.setResultCode(200);
135 String version = "1";
136 if (document.getVersion() != null) {
137 version = String.valueOf(Integer.parseInt(document.getVersion()) + 1);
139 opResult.setResultVersion(version);
146 public DocumentOperationResult deleteDocument(String indexName,
147 DocumentStoreDataEntity document) throws DocumentStoreOperationException {
148 DocumentOperationResult opResult = buildSampleDocumentOperationResult();
151 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
152 opResult.setResultCode(404);
154 if (opResult.getDocument() != null) {
155 opResult.getDocument().setEtag(null);
156 opResult.getDocument().setUrl(null);
157 opResult.setResultVersion("1");
159 opResult.setResultCode(200);
160 opResult.setResult(indexName + "@" + document.getId());
161 opResult.setResultVersion("1");
168 public DocumentOperationResult getDocument(String indexName,
169 DocumentStoreDataEntity document) throws DocumentStoreOperationException {
170 DocumentOperationResult opResult = buildSampleDocumentOperationResult();
172 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
173 opResult.setResultCode(404);
174 // Adding it to make the tests pass.
175 opResult.setResultVersion ("1");
177 opResult.setResultCode(200);
178 // Adding it to make the tests pass.
179 opResult.setResultVersion ("1");
186 public SearchOperationResult search(String indexName,
187 String queryText) throws DocumentStoreOperationException {
189 SearchOperationResult opResult = buildSampleSearchOperationResult();
191 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
192 opResult.setResultCode(404);
194 opResult.setResultCode(200);
195 opResult.setResult(indexName + "@" + queryText);
202 public SearchOperationResult searchWithPayload(String indexName,
203 String query) throws DocumentStoreOperationException {
204 SearchOperationResult opResult = buildSampleSearchOperationResult();
206 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
207 opResult.setResultCode(404);
209 opResult.setResultCode(200);
210 opResult.setResult(indexName + "@" + query);
217 public SearchOperationResult suggestionQueryWithPayload(String indexName, String query)
218 throws DocumentStoreOperationException {
219 SearchOperationResult opResult = new SearchOperationResult();
221 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
222 opResult.setResultCode(404);
224 opResult.setResultCode(200);
225 opResult.setResult(indexName + "@" + query);
232 public OperationResult performBulkOperations(BulkRequest[] requests) throws DocumentStoreOperationException {
234 OperationResult opResult = new OperationResult();
235 opResult.setResultCode(200);
240 private DocumentOperationResult buildSampleDocumentOperationResult() {
241 DocumentOperationResult result = new DocumentOperationResult();
242 Document doc = new Document();
243 doc.setEtag("etag1");
245 doc.setContent(new JSONObject());
246 result.setDocument(doc);
250 private SearchOperationResult buildSampleSearchOperationResult() {
251 SearchOperationResult result = new SearchOperationResult();
253 SearchHits searchHits = new SearchHits();
254 SearchHit[] searchHitArray = new SearchHit[1];
255 SearchHit searchHit = new SearchHit();
256 Document doc = new Document();
257 doc.setEtag("etag1");
258 Map<String, Object> content = new HashMap<String, Object>();
259 content.put("key1", "value1");
260 doc.setContent(new JSONObject());
261 searchHit.setDocument(doc);
262 searchHitArray[0] = searchHit;
264 searchHits.setHits(searchHitArray);
265 searchHits.setTotalHits("1");
266 result.setSearchResult(searchHits);