Adding pass through shema creation
[aai/search-data-service.git] / src / test / java / org / onap / aai / sa / rest / StubEsController.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.aai.sa.rest;
22
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;
33
34 import java.util.HashMap;
35 import java.util.Map;
36
37 /**
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
40  * document store.
41  */
42 public class StubEsController implements DocumentStoreInterface {
43
44   public static final String DOES_NOT_EXIST_INDEX = "index-does-not-exist";
45
46   private AnalysisConfiguration analysisConfig = null;
47
48   /**
49    *
50    */
51   //private IndexAPIHarness indexAPIHarness;
52
53   StubEsController() {
54     analysisConfig = new AnalysisConfiguration();
55     analysisConfig.init("src/test/resources/json/filter-config.json",
56         "src/test/resources/json/analysis-config.json");
57   }
58
59
60   @Override
61   public OperationResult createIndex(String         index, 
62                                      DocumentSchema documentSchema) {
63
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);
69
70     opResult.setResult(index + "@" + analysisConfig.getEsIndexSettings() + "@"
71         + DocumentSchemaUtil.generateDocumentMappings(documentSchema));
72
73     return opResult;
74   }
75
76   @Override
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);
82     return opResult;
83   }
84
85   @Override
86   public OperationResult deleteIndex(String indexName) throws DocumentStoreOperationException {
87
88     OperationResult opResult = new OperationResult();
89
90     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
91       opResult.setResultCode(404);
92     } else {
93       opResult.setResultCode(200);
94       opResult.setResult(indexName);
95     }
96
97     return opResult;
98   }
99
100   @Override
101   public DocumentOperationResult createDocument(String                  indexName,
102                                                 DocumentStoreDataEntity document,
103                                                 boolean                 allowImplicitIndexCreation) 
104     throws DocumentStoreOperationException {
105     
106     DocumentOperationResult opResult = buildSampleDocumentOperationResult();
107
108     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
109       opResult.setResultCode(404);
110     } else {
111       opResult.setResultCode(200);
112       String id = "dummy";
113       if (document.getId() != null) {
114         id = document.getId();
115       }
116       opResult.setResultVersion("1");
117     }
118
119     return opResult;
120   }
121
122   @Override
123   public DocumentOperationResult updateDocument(String                  indexName,
124                                                 DocumentStoreDataEntity document,
125                                                 boolean                 allowImplicitIndexCreation) 
126     throws DocumentStoreOperationException {
127     
128     DocumentOperationResult opResult = buildSampleDocumentOperationResult();
129
130     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
131       opResult.setResultCode(404);
132     } else {
133       opResult.setResultCode(200);
134       String version = "1";
135       if (document.getVersion() != null) {
136         version = String.valueOf(Integer.parseInt(document.getVersion()) + 1);
137       }
138       opResult.setResultVersion(version);
139     }
140
141     return opResult;
142   }
143
144   @Override
145   public DocumentOperationResult deleteDocument(String indexName,
146                                                 DocumentStoreDataEntity document) throws DocumentStoreOperationException {
147     DocumentOperationResult opResult = buildSampleDocumentOperationResult();
148
149
150     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
151       opResult.setResultCode(404);
152     } else {
153       if (opResult.getDocument() != null) {
154         opResult.getDocument().setEtag(null);
155         opResult.getDocument().setUrl(null);
156       }
157       opResult.setResultCode(200);
158       opResult.setResult(indexName + "@" + document.getId());
159     }
160
161     return opResult;
162   }
163
164   @Override
165   public DocumentOperationResult getDocument(String indexName,
166                                              DocumentStoreDataEntity document) throws DocumentStoreOperationException {
167     DocumentOperationResult opResult = buildSampleDocumentOperationResult();
168
169     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
170       opResult.setResultCode(404);
171     } else {
172       opResult.setResultCode(200);
173     }
174
175     return opResult;
176   }
177
178   @Override
179   public SearchOperationResult search(String indexName,
180                                       String queryText) throws DocumentStoreOperationException {
181
182     SearchOperationResult opResult = buildSampleSearchOperationResult();
183
184     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
185       opResult.setResultCode(404);
186     } else {
187       opResult.setResultCode(200);
188       opResult.setResult(indexName + "@" + queryText);
189     }
190
191     return opResult;
192   }
193
194   @Override
195   public SearchOperationResult searchWithPayload(String indexName,
196                                                  String query) throws DocumentStoreOperationException {
197     SearchOperationResult opResult = buildSampleSearchOperationResult();
198
199     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
200       opResult.setResultCode(404);
201     } else {
202       opResult.setResultCode(200);
203       opResult.setResult(indexName + "@" + query);
204     }
205
206     return opResult;
207   }
208
209   @Override
210   public OperationResult performBulkOperations(BulkRequest[] requests) throws DocumentStoreOperationException {
211
212     OperationResult opResult = new OperationResult();
213     opResult.setResultCode(200);
214
215     return opResult;
216   }
217
218   private DocumentOperationResult buildSampleDocumentOperationResult() {
219     DocumentOperationResult result = new DocumentOperationResult();
220     Document doc = new Document();
221     doc.setEtag("etag1");
222
223     doc.setContent(new JSONObject());
224     result.setDocument(doc);
225     return result;
226   }
227
228   private SearchOperationResult buildSampleSearchOperationResult() {
229     SearchOperationResult result = new SearchOperationResult();
230
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;
241
242     searchHits.setHits(searchHitArray);
243     searchHits.setTotalHits("1");
244     result.setSearchResult(searchHits);
245
246     return result;
247
248   }
249
250 }