Format Java code to ONAP standard
[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 java.io.IOException;
24 import java.util.HashMap;
25 import java.util.Map;
26 import org.json.simple.JSONObject;
27 import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreDataEntity;
28 import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreInterface;
29 import org.onap.aai.sa.searchdbabstraction.elasticsearch.exception.DocumentStoreOperationException;
30 import org.onap.aai.sa.searchdbabstraction.entity.Document;
31 import org.onap.aai.sa.searchdbabstraction.entity.DocumentOperationResult;
32 import org.onap.aai.sa.searchdbabstraction.entity.OperationResult;
33 import org.onap.aai.sa.searchdbabstraction.entity.SearchHit;
34 import org.onap.aai.sa.searchdbabstraction.entity.SearchHits;
35 import org.onap.aai.sa.searchdbabstraction.entity.SearchOperationResult;
36 import org.onap.aai.sa.searchdbabstraction.util.DocumentSchemaUtil;
37
38 /**
39  * This class implements a stubbed version of the document store DAO so that we can run unit tests without trying to
40  * connect to a real 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, DocumentSchema documentSchema) {
62
63         // Just return an OK result, with the parameters that we were passed
64         // bundled in the response string. This allows unit tests to validate
65         // that those parameters match what they expected to be passed.
66         OperationResult opResult = new OperationResult();
67         opResult.setResultCode(200);
68
69         try {
70             opResult.setResult(index + "@" + analysisConfig.getEsIndexSettings() + "@"
71                     + DocumentSchemaUtil.generateDocumentMappings(documentSchema));
72         } catch (IOException e) {
73             e.printStackTrace();
74         }
75
76         return opResult;
77     }
78
79     @Override
80     public OperationResult createDynamicIndex(String index, String dynamicSchema) {
81         OperationResult opResult = new OperationResult();
82         opResult.setResultCode(200);
83         // Directly return the json as this flow should not edit the json in any way
84         opResult.setResult(dynamicSchema);
85         return opResult;
86     }
87
88
89     @Override
90     public OperationResult deleteIndex(String indexName) throws DocumentStoreOperationException {
91
92         OperationResult opResult = new OperationResult();
93
94         if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
95             opResult.setResultCode(404);
96         } else {
97             opResult.setResultCode(200);
98             opResult.setResult(indexName);
99         }
100
101         return opResult;
102     }
103
104     @Override
105     public DocumentOperationResult createDocument(String indexName, DocumentStoreDataEntity document,
106             boolean allowImplicitIndexCreation) throws DocumentStoreOperationException {
107
108         DocumentOperationResult opResult = buildSampleDocumentOperationResult();
109
110         if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
111             opResult.setResultCode(404);
112         } else {
113             opResult.setResultCode(200);
114             String id = "dummy";
115             if (document.getId() != null) {
116                 id = document.getId();
117             }
118             opResult.setResultVersion("1");
119         }
120
121         return opResult;
122     }
123
124     @Override
125     public DocumentOperationResult updateDocument(String indexName, DocumentStoreDataEntity document,
126             boolean allowImplicitIndexCreation) 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, DocumentStoreDataEntity document)
146             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                 opResult.setResultVersion("1");
157             }
158             opResult.setResultCode(200);
159             opResult.setResult(indexName + "@" + document.getId());
160             opResult.setResultVersion("1");
161         }
162
163         return opResult;
164     }
165
166     @Override
167     public DocumentOperationResult getDocument(String indexName, DocumentStoreDataEntity document)
168             throws DocumentStoreOperationException {
169         DocumentOperationResult opResult = buildSampleDocumentOperationResult();
170
171         if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
172             opResult.setResultCode(404);
173             // Adding it to make the tests pass.
174             opResult.setResultVersion("1");
175         } else {
176             opResult.setResultCode(200);
177             // Adding it to make the tests pass.
178             opResult.setResultVersion("1");
179         }
180
181         return opResult;
182     }
183
184     @Override
185     public SearchOperationResult search(String indexName, String queryText) throws DocumentStoreOperationException {
186
187         SearchOperationResult opResult = buildSampleSearchOperationResult();
188
189         if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
190             opResult.setResultCode(404);
191         } else {
192             opResult.setResultCode(200);
193             opResult.setResult(indexName + "@" + queryText);
194         }
195
196         return opResult;
197     }
198
199     @Override
200     public SearchOperationResult searchWithPayload(String indexName, String query)
201             throws DocumentStoreOperationException {
202         SearchOperationResult opResult = buildSampleSearchOperationResult();
203
204         if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
205             opResult.setResultCode(404);
206         } else {
207             opResult.setResultCode(200);
208             opResult.setResult(indexName + "@" + query);
209         }
210
211         return opResult;
212     }
213
214     @Override
215     public SearchOperationResult suggestionQueryWithPayload(String indexName, String query)
216             throws DocumentStoreOperationException {
217         SearchOperationResult opResult = new SearchOperationResult();
218
219         if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
220             opResult.setResultCode(404);
221         } else {
222             opResult.setResultCode(200);
223             opResult.setResult(indexName + "@" + query);
224         }
225
226         return opResult;
227     }
228
229     @Override
230     public OperationResult performBulkOperations(BulkRequest[] requests) throws DocumentStoreOperationException {
231
232         OperationResult opResult = new OperationResult();
233         opResult.setResultCode(200);
234
235         return opResult;
236     }
237
238     private DocumentOperationResult buildSampleDocumentOperationResult() {
239         DocumentOperationResult result = new DocumentOperationResult();
240         Document doc = new Document();
241         doc.setEtag("etag1");
242
243         doc.setContent(new JSONObject());
244         result.setDocument(doc);
245         return result;
246     }
247
248     private SearchOperationResult buildSampleSearchOperationResult() {
249         SearchOperationResult result = new SearchOperationResult();
250
251         SearchHits searchHits = new SearchHits();
252         SearchHit[] searchHitArray = new SearchHit[1];
253         SearchHit searchHit = new SearchHit();
254         Document doc = new Document();
255         doc.setEtag("etag1");
256         Map<String, Object> content = new HashMap<String, Object>();
257         content.put("key1", "value1");
258         doc.setContent(new JSONObject());
259         searchHit.setDocument(doc);
260         searchHitArray[0] = searchHit;
261
262         searchHits.setHits(searchHitArray);
263         searchHits.setTotalHits("1");
264         result.setSearchResult(searchHits);
265
266         return result;
267
268     }
269
270 }