Add missing distributionManagement section to poms
[aai/search-data-service.git] / search-data-service / 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             opResult.setResultVersion("1");
115         }
116
117         return opResult;
118     }
119
120     @Override
121     public DocumentOperationResult updateDocument(String indexName, DocumentStoreDataEntity document,
122             boolean allowImplicitIndexCreation) throws DocumentStoreOperationException {
123
124         DocumentOperationResult opResult = buildSampleDocumentOperationResult();
125
126         if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
127             opResult.setResultCode(404);
128         } else {
129             opResult.setResultCode(200);
130             String version = "1";
131             if (document.getVersion() != null) {
132                 version = String.valueOf(Integer.parseInt(document.getVersion()) + 1);
133             }
134             opResult.setResultVersion(version);
135         }
136
137         return opResult;
138     }
139
140     @Override
141     public DocumentOperationResult deleteDocument(String indexName, DocumentStoreDataEntity document)
142             throws DocumentStoreOperationException {
143         DocumentOperationResult opResult = buildSampleDocumentOperationResult();
144
145
146         if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
147             opResult.setResultCode(404);
148         } else {
149             if (opResult.getDocument() != null) {
150                 opResult.getDocument().setEtag(null);
151                 opResult.getDocument().setUrl(null);
152                 opResult.setResultVersion("1");
153             }
154             opResult.setResultCode(200);
155             opResult.setResult(indexName + "@" + document.getId());
156             opResult.setResultVersion("1");
157         }
158
159         return opResult;
160     }
161
162     @Override
163     public DocumentOperationResult getDocument(String indexName, DocumentStoreDataEntity document)
164             throws DocumentStoreOperationException {
165         DocumentOperationResult opResult = buildSampleDocumentOperationResult();
166
167         if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
168             opResult.setResultCode(404);
169             // Adding it to make the tests pass.
170             opResult.setResultVersion("1");
171         } else {
172             opResult.setResultCode(200);
173             // Adding it to make the tests pass.
174             opResult.setResultVersion("1");
175         }
176
177         return opResult;
178     }
179
180     @Override
181     public SearchOperationResult search(String indexName, String queryText) throws DocumentStoreOperationException {
182
183         SearchOperationResult opResult = buildSampleSearchOperationResult();
184
185         if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
186             opResult.setResultCode(404);
187         } else {
188             opResult.setResultCode(200);
189             opResult.setResult(indexName + "@" + queryText);
190         }
191
192         return opResult;
193     }
194
195     @Override
196     public SearchOperationResult searchWithPayload(String indexName, String query)
197             throws DocumentStoreOperationException {
198         SearchOperationResult opResult = buildSampleSearchOperationResult();
199
200         if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
201             opResult.setResultCode(404);
202         } else {
203             opResult.setResultCode(200);
204             opResult.setResult(indexName + "@" + query);
205         }
206
207         return opResult;
208     }
209
210     @Override
211     public SearchOperationResult suggestionQueryWithPayload(String indexName, String query)
212             throws DocumentStoreOperationException {
213         SearchOperationResult opResult = new SearchOperationResult();
214
215         if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
216             opResult.setResultCode(404);
217         } else {
218             opResult.setResultCode(200);
219             opResult.setResult(indexName + "@" + query);
220         }
221
222         return opResult;
223     }
224
225     @Override
226     public OperationResult performBulkOperations(BulkRequest[] requests) throws DocumentStoreOperationException {
227
228         OperationResult opResult = new OperationResult();
229         opResult.setResultCode(200);
230
231         return opResult;
232     }
233
234     private DocumentOperationResult buildSampleDocumentOperationResult() {
235         DocumentOperationResult result = new DocumentOperationResult();
236         Document doc = new Document();
237         doc.setEtag("etag1");
238
239         doc.setContent(new JSONObject());
240         result.setDocument(doc);
241         return result;
242     }
243
244     private SearchOperationResult buildSampleSearchOperationResult() {
245         SearchOperationResult result = new SearchOperationResult();
246
247         SearchHits searchHits = new SearchHits();
248         SearchHit[] searchHitArray = new SearchHit[1];
249         SearchHit searchHit = new SearchHit();
250         Document doc = new Document();
251         doc.setEtag("etag1");
252         Map<String, Object> content = new HashMap<>();
253         content.put("key1", "value1");
254         doc.setContent(new JSONObject());
255         searchHit.setDocument(doc);
256         searchHitArray[0] = searchHit;
257
258         searchHits.setHits(searchHitArray);
259         searchHits.setTotalHits("1");
260         result.setSearchResult(searchHits);
261
262         return result;
263
264     }
265
266 }