Reject doc create requests if index does not exist
[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, 
64                                      DocumentSchema documentSchema) {
65
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);
71
72     opResult.setResult(index + "@" + analysisConfig.getEsIndexSettings() + "@"
73         + DocumentSchemaUtil.generateDocumentMappings(documentSchema));
74
75     return opResult;
76   }
77
78
79   @Override
80   public OperationResult deleteIndex(String indexName) throws DocumentStoreOperationException {
81
82     OperationResult opResult = new OperationResult();
83
84     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
85       opResult.setResultCode(404);
86     } else {
87       opResult.setResultCode(200);
88       opResult.setResult(indexName);
89     }
90
91     return opResult;
92   }
93
94   @Override
95   public DocumentOperationResult createDocument(String                  indexName,
96                                                 DocumentStoreDataEntity document,
97                                                 boolean                 allowImplicitIndexCreation) 
98     throws DocumentStoreOperationException {
99     
100     DocumentOperationResult opResult = buildSampleDocumentOperationResult();
101
102     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
103       opResult.setResultCode(404);
104     } else {
105       opResult.setResultCode(200);
106       String id = "dummy";
107       if (document.getId() != null) {
108         id = document.getId();
109       }
110       opResult.setResultVersion("1");
111     }
112
113     return opResult;
114   }
115
116   @Override
117   public DocumentOperationResult updateDocument(String                  indexName,
118                                                 DocumentStoreDataEntity document,
119                                                 boolean                 allowImplicitIndexCreation) 
120     throws DocumentStoreOperationException {
121     
122     DocumentOperationResult opResult = buildSampleDocumentOperationResult();
123
124     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
125       opResult.setResultCode(404);
126     } else {
127       opResult.setResultCode(200);
128       String version = "1";
129       if (document.getVersion() != null) {
130         version = String.valueOf(Integer.parseInt(document.getVersion()) + 1);
131       }
132       opResult.setResultVersion(version);
133     }
134
135     return opResult;
136   }
137
138   @Override
139   public DocumentOperationResult deleteDocument(String indexName,
140                                                 DocumentStoreDataEntity document) throws DocumentStoreOperationException {
141     DocumentOperationResult opResult = buildSampleDocumentOperationResult();
142
143
144     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
145       opResult.setResultCode(404);
146     } else {
147       if (opResult.getDocument() != null) {
148         opResult.getDocument().setEtag(null);
149         opResult.getDocument().setUrl(null);
150       }
151       opResult.setResultCode(200);
152       opResult.setResult(indexName + "@" + document.getId());
153     }
154
155     return opResult;
156   }
157
158   @Override
159   public DocumentOperationResult getDocument(String indexName,
160                                              DocumentStoreDataEntity document) throws DocumentStoreOperationException {
161     DocumentOperationResult opResult = buildSampleDocumentOperationResult();
162
163     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
164       opResult.setResultCode(404);
165     } else {
166       opResult.setResultCode(200);
167     }
168
169     return opResult;
170   }
171
172   @Override
173   public SearchOperationResult search(String indexName,
174                                       String queryText) throws DocumentStoreOperationException {
175
176     SearchOperationResult opResult = buildSampleSearchOperationResult();
177
178     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
179       opResult.setResultCode(404);
180     } else {
181       opResult.setResultCode(200);
182       opResult.setResult(indexName + "@" + queryText);
183     }
184
185     return opResult;
186   }
187
188   @Override
189   public SearchOperationResult searchWithPayload(String indexName,
190                                                  String query) throws DocumentStoreOperationException {
191     SearchOperationResult opResult = buildSampleSearchOperationResult();
192
193     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
194       opResult.setResultCode(404);
195     } else {
196       opResult.setResultCode(200);
197       opResult.setResult(indexName + "@" + query);
198     }
199
200     return opResult;
201   }
202
203   @Override
204   public OperationResult performBulkOperations(BulkRequest[] requests) throws DocumentStoreOperationException {
205
206     OperationResult opResult = new OperationResult();
207     opResult.setResultCode(200);
208
209     return opResult;
210   }
211
212   private DocumentOperationResult buildSampleDocumentOperationResult() {
213     DocumentOperationResult result = new DocumentOperationResult();
214     Document doc = new Document();
215     doc.setEtag("etag1");
216
217     doc.setContent(new JSONObject());
218     result.setDocument(doc);
219     return result;
220   }
221
222   private SearchOperationResult buildSampleSearchOperationResult() {
223     SearchOperationResult result = new SearchOperationResult();
224
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;
235
236     searchHits.setHits(searchHitArray);
237     searchHits.setTotalHits("1");
238     result.setSearchResult(searchHits);
239
240     return result;
241
242   }
243
244 }