2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.sa.rest;
25 import org.json.simple.JSONObject;
26 import org.openecomp.sa.rest.DocumentSchema;
27 import org.openecomp.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreDataEntity;
28 import org.openecomp.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreInterface;
29 import org.openecomp.sa.searchdbabstraction.elasticsearch.exception.DocumentStoreOperationException;
30 import org.openecomp.sa.searchdbabstraction.entity.Document;
31 import org.openecomp.sa.searchdbabstraction.entity.*;
32 import org.openecomp.sa.searchdbabstraction.util.DocumentSchemaUtil;
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));
78 public OperationResult deleteIndex(String indexName) throws DocumentStoreOperationException {
80 OperationResult opResult = new OperationResult();
82 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
83 opResult.setResultCode(404);
85 opResult.setResultCode(200);
86 opResult.setResult(indexName);
93 public DocumentOperationResult createDocument(String indexName,
94 DocumentStoreDataEntity document,
95 boolean allowImplicitIndexCreation)
96 throws DocumentStoreOperationException {
98 DocumentOperationResult opResult = buildSampleDocumentOperationResult();
100 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
101 opResult.setResultCode(404);
103 opResult.setResultCode(200);
105 if (document.getId() != null) {
106 id = document.getId();
108 opResult.setResultVersion("1");
115 public DocumentOperationResult updateDocument(String indexName,
116 DocumentStoreDataEntity document,
117 boolean allowImplicitIndexCreation)
118 throws DocumentStoreOperationException {
120 DocumentOperationResult opResult = buildSampleDocumentOperationResult();
122 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
123 opResult.setResultCode(404);
125 opResult.setResultCode(200);
126 String version = "1";
127 if (document.getVersion() != null) {
128 version = String.valueOf(Integer.parseInt(document.getVersion()) + 1);
130 opResult.setResultVersion(version);
137 public DocumentOperationResult deleteDocument(String indexName,
138 DocumentStoreDataEntity document) throws DocumentStoreOperationException {
139 DocumentOperationResult opResult = buildSampleDocumentOperationResult();
142 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
143 opResult.setResultCode(404);
145 if (opResult.getDocument() != null) {
146 opResult.getDocument().setEtag(null);
147 opResult.getDocument().setUrl(null);
149 opResult.setResultCode(200);
150 opResult.setResult(indexName + "@" + document.getId());
157 public DocumentOperationResult getDocument(String indexName,
158 DocumentStoreDataEntity document) throws DocumentStoreOperationException {
159 DocumentOperationResult opResult = buildSampleDocumentOperationResult();
161 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
162 opResult.setResultCode(404);
164 opResult.setResultCode(200);
171 public SearchOperationResult search(String indexName,
172 String queryText) throws DocumentStoreOperationException {
174 SearchOperationResult opResult = buildSampleSearchOperationResult();
176 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
177 opResult.setResultCode(404);
179 opResult.setResultCode(200);
180 opResult.setResult(indexName + "@" + queryText);
187 public SearchOperationResult searchWithPayload(String indexName,
188 String query) 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 + "@" + query);
202 public OperationResult performBulkOperations(BulkRequest[] requests) throws DocumentStoreOperationException {
204 OperationResult opResult = new OperationResult();
205 opResult.setResultCode(200);
210 private DocumentOperationResult buildSampleDocumentOperationResult() {
211 DocumentOperationResult result = new DocumentOperationResult();
212 Document doc = new Document();
213 doc.setEtag("etag1");
215 doc.setContent(new JSONObject());
216 result.setDocument(doc);
220 private SearchOperationResult buildSampleSearchOperationResult() {
221 SearchOperationResult result = new SearchOperationResult();
223 SearchHits searchHits = new SearchHits();
224 SearchHit[] searchHitArray = new SearchHit[1];
225 SearchHit searchHit = new SearchHit();
226 Document doc = new Document();
227 doc.setEtag("etag1");
228 Map<String, Object> content = new HashMap<String, Object>();
229 content.put("key1", "value1");
230 doc.setContent(new JSONObject());
231 searchHit.setDocument(doc);
232 searchHitArray[0] = searchHit;
234 searchHits.setHits(searchHitArray);
235 searchHits.setTotalHits("1");
236 result.setSearchResult(searchHits);