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);
86 public OperationResult deleteIndex(String indexName) throws DocumentStoreOperationException {
88 OperationResult opResult = new OperationResult();
90 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
91 opResult.setResultCode(404);
93 opResult.setResultCode(200);
94 opResult.setResult(indexName);
101 public DocumentOperationResult createDocument(String indexName,
102 DocumentStoreDataEntity document,
103 boolean allowImplicitIndexCreation)
104 throws DocumentStoreOperationException {
106 DocumentOperationResult opResult = buildSampleDocumentOperationResult();
108 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
109 opResult.setResultCode(404);
111 opResult.setResultCode(200);
113 if (document.getId() != null) {
114 id = document.getId();
116 opResult.setResultVersion("1");
123 public DocumentOperationResult updateDocument(String indexName,
124 DocumentStoreDataEntity document,
125 boolean allowImplicitIndexCreation)
126 throws DocumentStoreOperationException {
128 DocumentOperationResult opResult = buildSampleDocumentOperationResult();
130 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
131 opResult.setResultCode(404);
133 opResult.setResultCode(200);
134 String version = "1";
135 if (document.getVersion() != null) {
136 version = String.valueOf(Integer.parseInt(document.getVersion()) + 1);
138 opResult.setResultVersion(version);
145 public DocumentOperationResult deleteDocument(String indexName,
146 DocumentStoreDataEntity document) throws DocumentStoreOperationException {
147 DocumentOperationResult opResult = buildSampleDocumentOperationResult();
150 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
151 opResult.setResultCode(404);
153 if (opResult.getDocument() != null) {
154 opResult.getDocument().setEtag(null);
155 opResult.getDocument().setUrl(null);
157 opResult.setResultCode(200);
158 opResult.setResult(indexName + "@" + document.getId());
165 public DocumentOperationResult getDocument(String indexName,
166 DocumentStoreDataEntity document) throws DocumentStoreOperationException {
167 DocumentOperationResult opResult = buildSampleDocumentOperationResult();
169 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
170 opResult.setResultCode(404);
172 opResult.setResultCode(200);
179 public SearchOperationResult search(String indexName,
180 String queryText) throws DocumentStoreOperationException {
182 SearchOperationResult opResult = buildSampleSearchOperationResult();
184 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
185 opResult.setResultCode(404);
187 opResult.setResultCode(200);
188 opResult.setResult(indexName + "@" + queryText);
195 public SearchOperationResult searchWithPayload(String indexName,
196 String query) throws DocumentStoreOperationException {
197 SearchOperationResult opResult = buildSampleSearchOperationResult();
199 if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
200 opResult.setResultCode(404);
202 opResult.setResultCode(200);
203 opResult.setResult(indexName + "@" + query);
210 public OperationResult performBulkOperations(BulkRequest[] requests) throws DocumentStoreOperationException {
212 OperationResult opResult = new OperationResult();
213 opResult.setResultCode(200);
218 private DocumentOperationResult buildSampleDocumentOperationResult() {
219 DocumentOperationResult result = new DocumentOperationResult();
220 Document doc = new Document();
221 doc.setEtag("etag1");
223 doc.setContent(new JSONObject());
224 result.setDocument(doc);
228 private SearchOperationResult buildSampleSearchOperationResult() {
229 SearchOperationResult result = new SearchOperationResult();
231 SearchHits searchHits = new SearchHits();
232 SearchHit[] searchHitArray = new SearchHit[1];
233 SearchHit searchHit = new SearchHit();
234 Document doc = new Document();
235 doc.setEtag("etag1");
236 Map<String, Object> content = new HashMap<String, Object>();
237 content.put("key1", "value1");
238 doc.setContent(new JSONObject());
239 searchHit.setDocument(doc);
240 searchHitArray[0] = searchHit;
242 searchHits.setHits(searchHitArray);
243 searchHits.setTotalHits("1");
244 result.setSearchResult(searchHits);