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.onap.aai.sa.rest;
25 import org.json.simple.JSONObject;
26 import org.onap.aai.sa.rest.AnalysisConfiguration;
27 import org.onap.aai.sa.rest.BulkRequest;
28 import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreDataEntity;
29 import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreInterface;
30 import org.onap.aai.sa.searchdbabstraction.elasticsearch.exception.DocumentStoreOperationException;
31 import org.onap.aai.sa.searchdbabstraction.entity.*;
32 import org.onap.aai.sa.searchdbabstraction.util.DocumentSchemaUtil;
33 import org.onap.aai.sa.searchdbabstraction.entity.Document;
34 import org.onap.aai.sa.rest.DocumentSchema;
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,
64 DocumentSchema documentSchema) {
66 // Just return an OK result, with the parameters that we were passed
67 // bundled in the response string. This allows unit tests to validate
68 // that those parameters match what they expected to be passed.
69 OperationResult opResult = new OperationResult();
70 opResult.setResultCode(200);
72 opResult.setResult(index + "@" + analysisConfig.getEsIndexSettings() + "@"
73 + DocumentSchemaUtil.generateDocumentMappings(documentSchema));
80 public OperationResult deleteIndex(String indexName) throws DocumentStoreOperationException {
82 OperationResult opResult = new OperationResult();
84 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
85 opResult.setResultCode(404);
87 opResult.setResultCode(200);
88 opResult.setResult(indexName);
95 public DocumentOperationResult createDocument(String indexName,
96 DocumentStoreDataEntity document,
97 boolean allowImplicitIndexCreation)
98 throws DocumentStoreOperationException {
100 DocumentOperationResult opResult = buildSampleDocumentOperationResult();
102 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
103 opResult.setResultCode(404);
105 opResult.setResultCode(200);
107 if (document.getId() != null) {
108 id = document.getId();
110 opResult.setResultVersion("1");
117 public DocumentOperationResult updateDocument(String indexName,
118 DocumentStoreDataEntity document,
119 boolean allowImplicitIndexCreation)
120 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,
140 DocumentStoreDataEntity document) throws DocumentStoreOperationException {
141 DocumentOperationResult opResult = buildSampleDocumentOperationResult();
144 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
145 opResult.setResultCode(404);
147 if (opResult.getDocument() != null) {
148 opResult.getDocument().setEtag(null);
149 opResult.getDocument().setUrl(null);
151 opResult.setResultCode(200);
152 opResult.setResult(indexName + "@" + document.getId());
159 public DocumentOperationResult getDocument(String indexName,
160 DocumentStoreDataEntity document) throws DocumentStoreOperationException {
161 DocumentOperationResult opResult = buildSampleDocumentOperationResult();
163 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
164 opResult.setResultCode(404);
166 opResult.setResultCode(200);
173 public SearchOperationResult search(String indexName,
174 String queryText) throws DocumentStoreOperationException {
176 SearchOperationResult opResult = buildSampleSearchOperationResult();
178 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
179 opResult.setResultCode(404);
181 opResult.setResultCode(200);
182 opResult.setResult(indexName + "@" + queryText);
189 public SearchOperationResult searchWithPayload(String indexName,
190 String query) throws DocumentStoreOperationException {
191 SearchOperationResult opResult = buildSampleSearchOperationResult();
193 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
194 opResult.setResultCode(404);
196 opResult.setResultCode(200);
197 opResult.setResult(indexName + "@" + query);
204 public OperationResult performBulkOperations(BulkRequest[] requests) throws DocumentStoreOperationException {
206 OperationResult opResult = new OperationResult();
207 opResult.setResultCode(200);
212 private DocumentOperationResult buildSampleDocumentOperationResult() {
213 DocumentOperationResult result = new DocumentOperationResult();
214 Document doc = new Document();
215 doc.setEtag("etag1");
217 doc.setContent(new JSONObject());
218 result.setDocument(doc);
222 private SearchOperationResult buildSampleSearchOperationResult() {
223 SearchOperationResult result = new SearchOperationResult();
225 SearchHits searchHits = new SearchHits();
226 SearchHit[] searchHitArray = new SearchHit[1];
227 SearchHit searchHit = new SearchHit();
228 Document doc = new Document();
229 doc.setEtag("etag1");
230 Map<String, Object> content = new HashMap<String, Object>();
231 content.put("key1", "value1");
232 doc.setContent(new JSONObject());
233 searchHit.setDocument(doc);
234 searchHitArray[0] = searchHit;
236 searchHits.setHits(searchHitArray);
237 searchHits.setTotalHits("1");
238 result.setSearchResult(searchHits);