2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License ati
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.openecomp.sa.rest;
27 import org.json.simple.JSONObject;
28 import org.openecomp.sa.rest.DocumentSchema;
29 import org.openecomp.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreDataEntity;
30 import org.openecomp.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreInterface;
31 import org.openecomp.sa.searchdbabstraction.elasticsearch.exception.DocumentStoreOperationException;
32 import org.openecomp.sa.searchdbabstraction.entity.Document;
33 import org.openecomp.sa.searchdbabstraction.entity.*;
34 import org.openecomp.sa.searchdbabstraction.util.DocumentSchemaUtil;
36 import java.util.HashMap;
40 * This class implements a stubbed version of the document store DAO so
41 * that we can run unit tests without trying to connect to a real
44 public class StubEsController implements DocumentStoreInterface {
46 public static final String DOES_NOT_EXIST_INDEX = "index-does-not-exist";
48 private AnalysisConfiguration analysisConfig = null;
53 //private IndexAPIHarness indexAPIHarness;
56 analysisConfig = new AnalysisConfiguration();
57 analysisConfig.init("src/test/resources/json/filter-config.json",
58 "src/test/resources/json/analysis-config.json");
63 public OperationResult createIndex(String index, 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);
71 opResult.setResult(index + "@" + analysisConfig.getEsIndexSettings() + "@"
72 + DocumentSchemaUtil.generateDocumentMappings(documentSchema));
79 public OperationResult deleteIndex(String indexName) throws DocumentStoreOperationException {
81 OperationResult opResult = new OperationResult();
83 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
84 opResult.setResultCode(404);
86 opResult.setResultCode(200);
87 opResult.setResult(indexName);
94 public DocumentOperationResult createDocument(String indexName,
95 DocumentStoreDataEntity document) throws DocumentStoreOperationException {
96 DocumentOperationResult opResult = buildSampleDocumentOperationResult();
98 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
99 opResult.setResultCode(404);
101 opResult.setResultCode(200);
103 if (document.getId() != null) {
104 id = document.getId();
106 opResult.setResultVersion("1");
113 public DocumentOperationResult updateDocument(String indexName,
114 DocumentStoreDataEntity document) throws DocumentStoreOperationException {
115 DocumentOperationResult opResult = buildSampleDocumentOperationResult();
117 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
118 opResult.setResultCode(404);
120 opResult.setResultCode(200);
121 String version = "1";
122 if (document.getVersion() != null) {
123 version = String.valueOf(Integer.parseInt(document.getVersion()) + 1);
125 opResult.setResultVersion(version);
132 public DocumentOperationResult deleteDocument(String indexName,
133 DocumentStoreDataEntity document) throws DocumentStoreOperationException {
134 DocumentOperationResult opResult = buildSampleDocumentOperationResult();
137 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
138 opResult.setResultCode(404);
140 if (opResult.getDocument() != null) {
141 opResult.getDocument().setEtag(null);
142 opResult.getDocument().setUrl(null);
144 opResult.setResultCode(200);
145 opResult.setResult(indexName + "@" + document.getId());
152 public DocumentOperationResult getDocument(String indexName,
153 DocumentStoreDataEntity document) throws DocumentStoreOperationException {
154 DocumentOperationResult opResult = buildSampleDocumentOperationResult();
156 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
157 opResult.setResultCode(404);
159 opResult.setResultCode(200);
166 public SearchOperationResult search(String indexName,
167 String queryText) throws DocumentStoreOperationException {
169 SearchOperationResult opResult = buildSampleSearchOperationResult();
171 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
172 opResult.setResultCode(404);
174 opResult.setResultCode(200);
175 opResult.setResult(indexName + "@" + queryText);
182 public SearchOperationResult searchWithPayload(String indexName,
183 String query) throws DocumentStoreOperationException {
184 SearchOperationResult opResult = buildSampleSearchOperationResult();
186 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
187 opResult.setResultCode(404);
189 opResult.setResultCode(200);
190 opResult.setResult(indexName + "@" + query);
197 public OperationResult performBulkOperations(BulkRequest[] requests) throws DocumentStoreOperationException {
199 OperationResult opResult = new OperationResult();
200 opResult.setResultCode(200);
205 private DocumentOperationResult buildSampleDocumentOperationResult() {
206 DocumentOperationResult result = new DocumentOperationResult();
207 Document doc = new Document();
208 doc.setEtag("etag1");
210 doc.setContent(new JSONObject());
211 result.setDocument(doc);
215 private SearchOperationResult buildSampleSearchOperationResult() {
216 SearchOperationResult result = new SearchOperationResult();
218 SearchHits searchHits = new SearchHits();
219 SearchHit[] searchHitArray = new SearchHit[1];
220 SearchHit searchHit = new SearchHit();
221 Document doc = new Document();
222 doc.setEtag("etag1");
223 Map<String, Object> content = new HashMap<String, Object>();
224 content.put("key1", "value1");
225 doc.setContent(new JSONObject());
226 searchHit.setDocument(doc);
227 searchHitArray[0] = searchHit;
229 searchHits.setHits(searchHitArray);
230 searchHits.setTotalHits("1");
231 result.setSearchResult(searchHits);