f3e56192e6a0d8eaa30b39ecba208625a4be3c75
[aai/search-data-service.git] / src / test / java / org / openecomp / sa / rest / StubEsController.java
1 /**
2  * ============LICENSE_START=======================================================
3  * Search Data Service
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
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
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.openecomp.sa.rest;
26
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;
35
36 import java.util.HashMap;
37 import java.util.Map;
38
39 /**
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
42  * document store.
43  */
44 public class StubEsController implements DocumentStoreInterface {
45
46   public static final String DOES_NOT_EXIST_INDEX = "index-does-not-exist";
47
48   private AnalysisConfiguration analysisConfig = null;
49
50   /**
51    *
52    */
53   //private IndexAPIHarness indexAPIHarness;
54
55   StubEsController() {
56     analysisConfig = new AnalysisConfiguration();
57     analysisConfig.init("src/test/resources/json/filter-config.json",
58         "src/test/resources/json/analysis-config.json");
59   }
60
61
62   @Override
63   public OperationResult createIndex(String index, DocumentSchema documentSchema) {
64
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);
70
71     opResult.setResult(index + "@" + analysisConfig.getEsIndexSettings() + "@"
72         + DocumentSchemaUtil.generateDocumentMappings(documentSchema));
73
74     return opResult;
75   }
76
77
78   @Override
79   public OperationResult deleteIndex(String indexName) throws DocumentStoreOperationException {
80
81     OperationResult opResult = new OperationResult();
82
83     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
84       opResult.setResultCode(404);
85     } else {
86       opResult.setResultCode(200);
87       opResult.setResult(indexName);
88     }
89
90     return opResult;
91   }
92
93   @Override
94   public DocumentOperationResult createDocument(String indexName,
95                                                 DocumentStoreDataEntity document) throws DocumentStoreOperationException {
96     DocumentOperationResult opResult = buildSampleDocumentOperationResult();
97
98     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
99       opResult.setResultCode(404);
100     } else {
101       opResult.setResultCode(200);
102       String id = "dummy";
103       if (document.getId() != null) {
104         id = document.getId();
105       }
106       opResult.setResultVersion("1");
107     }
108
109     return opResult;
110   }
111
112   @Override
113   public DocumentOperationResult updateDocument(String indexName,
114                                                 DocumentStoreDataEntity document) throws DocumentStoreOperationException {
115     DocumentOperationResult opResult = buildSampleDocumentOperationResult();
116
117     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
118       opResult.setResultCode(404);
119     } else {
120       opResult.setResultCode(200);
121       String version = "1";
122       if (document.getVersion() != null) {
123         version = String.valueOf(Integer.parseInt(document.getVersion()) + 1);
124       }
125       opResult.setResultVersion(version);
126     }
127
128     return opResult;
129   }
130
131   @Override
132   public DocumentOperationResult deleteDocument(String indexName,
133                                                 DocumentStoreDataEntity document) throws DocumentStoreOperationException {
134     DocumentOperationResult opResult = buildSampleDocumentOperationResult();
135
136
137     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
138       opResult.setResultCode(404);
139     } else {
140       if (opResult.getDocument() != null) {
141         opResult.getDocument().setEtag(null);
142         opResult.getDocument().setUrl(null);
143       }
144       opResult.setResultCode(200);
145       opResult.setResult(indexName + "@" + document.getId());
146     }
147
148     return opResult;
149   }
150
151   @Override
152   public DocumentOperationResult getDocument(String indexName,
153                                              DocumentStoreDataEntity document) throws DocumentStoreOperationException {
154     DocumentOperationResult opResult = buildSampleDocumentOperationResult();
155
156     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
157       opResult.setResultCode(404);
158     } else {
159       opResult.setResultCode(200);
160     }
161
162     return opResult;
163   }
164
165   @Override
166   public SearchOperationResult search(String indexName,
167                                       String queryText) throws DocumentStoreOperationException {
168
169     SearchOperationResult opResult = buildSampleSearchOperationResult();
170
171     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
172       opResult.setResultCode(404);
173     } else {
174       opResult.setResultCode(200);
175       opResult.setResult(indexName + "@" + queryText);
176     }
177
178     return opResult;
179   }
180
181   @Override
182   public SearchOperationResult searchWithPayload(String indexName,
183                                                  String query) throws DocumentStoreOperationException {
184     SearchOperationResult opResult = buildSampleSearchOperationResult();
185
186     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
187       opResult.setResultCode(404);
188     } else {
189       opResult.setResultCode(200);
190       opResult.setResult(indexName + "@" + query);
191     }
192
193     return opResult;
194   }
195
196   @Override
197   public OperationResult performBulkOperations(BulkRequest[] requests) throws DocumentStoreOperationException {
198
199     OperationResult opResult = new OperationResult();
200     opResult.setResultCode(200);
201
202     return opResult;
203   }
204
205   private DocumentOperationResult buildSampleDocumentOperationResult() {
206     DocumentOperationResult result = new DocumentOperationResult();
207     Document doc = new Document();
208     doc.setEtag("etag1");
209
210     doc.setContent(new JSONObject());
211     result.setDocument(doc);
212     return result;
213   }
214
215   private SearchOperationResult buildSampleSearchOperationResult() {
216     SearchOperationResult result = new SearchOperationResult();
217
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;
228
229     searchHits.setHits(searchHitArray);
230     searchHits.setTotalHits("1");
231     result.setSearchResult(searchHits);
232
233     return result;
234
235   }
236
237 }