Cleanup project's name in Sonar
[aai/search-data-service.git] / src / test / java / org / openecomp / sa / rest / StubEsController.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.openecomp.sa.rest;
24
25 import org.json.simple.JSONObject;
26 import org.openecomp.sa.rest.DocumentSchema;
27 import org.openecomp.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreDataEntity;
28 import org.openecomp.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreInterface;
29 import org.openecomp.sa.searchdbabstraction.elasticsearch.exception.DocumentStoreOperationException;
30 import org.openecomp.sa.searchdbabstraction.entity.Document;
31 import org.openecomp.sa.searchdbabstraction.entity.*;
32 import org.openecomp.sa.searchdbabstraction.util.DocumentSchemaUtil;
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
77   @Override
78   public OperationResult deleteIndex(String indexName) throws DocumentStoreOperationException {
79
80     OperationResult opResult = new OperationResult();
81
82     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
83       opResult.setResultCode(404);
84     } else {
85       opResult.setResultCode(200);
86       opResult.setResult(indexName);
87     }
88
89     return opResult;
90   }
91
92   @Override
93   public DocumentOperationResult createDocument(String                  indexName,
94                                                 DocumentStoreDataEntity document,
95                                                 boolean                 allowImplicitIndexCreation) 
96     throws DocumentStoreOperationException {
97     
98     DocumentOperationResult opResult = buildSampleDocumentOperationResult();
99
100     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
101       opResult.setResultCode(404);
102     } else {
103       opResult.setResultCode(200);
104       String id = "dummy";
105       if (document.getId() != null) {
106         id = document.getId();
107       }
108       opResult.setResultVersion("1");
109     }
110
111     return opResult;
112   }
113
114   @Override
115   public DocumentOperationResult updateDocument(String                  indexName,
116                                                 DocumentStoreDataEntity document,
117                                                 boolean                 allowImplicitIndexCreation) 
118     throws DocumentStoreOperationException {
119     
120     DocumentOperationResult opResult = buildSampleDocumentOperationResult();
121
122     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
123       opResult.setResultCode(404);
124     } else {
125       opResult.setResultCode(200);
126       String version = "1";
127       if (document.getVersion() != null) {
128         version = String.valueOf(Integer.parseInt(document.getVersion()) + 1);
129       }
130       opResult.setResultVersion(version);
131     }
132
133     return opResult;
134   }
135
136   @Override
137   public DocumentOperationResult deleteDocument(String indexName,
138                                                 DocumentStoreDataEntity document) throws DocumentStoreOperationException {
139     DocumentOperationResult opResult = buildSampleDocumentOperationResult();
140
141
142     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
143       opResult.setResultCode(404);
144     } else {
145       if (opResult.getDocument() != null) {
146         opResult.getDocument().setEtag(null);
147         opResult.getDocument().setUrl(null);
148       }
149       opResult.setResultCode(200);
150       opResult.setResult(indexName + "@" + document.getId());
151     }
152
153     return opResult;
154   }
155
156   @Override
157   public DocumentOperationResult getDocument(String indexName,
158                                              DocumentStoreDataEntity document) throws DocumentStoreOperationException {
159     DocumentOperationResult opResult = buildSampleDocumentOperationResult();
160
161     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
162       opResult.setResultCode(404);
163     } else {
164       opResult.setResultCode(200);
165     }
166
167     return opResult;
168   }
169
170   @Override
171   public SearchOperationResult search(String indexName,
172                                       String queryText) throws DocumentStoreOperationException {
173
174     SearchOperationResult opResult = buildSampleSearchOperationResult();
175
176     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
177       opResult.setResultCode(404);
178     } else {
179       opResult.setResultCode(200);
180       opResult.setResult(indexName + "@" + queryText);
181     }
182
183     return opResult;
184   }
185
186   @Override
187   public SearchOperationResult searchWithPayload(String indexName,
188                                                  String query) throws DocumentStoreOperationException {
189     SearchOperationResult opResult = buildSampleSearchOperationResult();
190
191     if (indexName.equals(DOES_NOT_EXIST_INDEX)) {
192       opResult.setResultCode(404);
193     } else {
194       opResult.setResultCode(200);
195       opResult.setResult(indexName + "@" + query);
196     }
197
198     return opResult;
199   }
200
201   @Override
202   public OperationResult performBulkOperations(BulkRequest[] requests) throws DocumentStoreOperationException {
203
204     OperationResult opResult = new OperationResult();
205     opResult.setResultCode(200);
206
207     return opResult;
208   }
209
210   private DocumentOperationResult buildSampleDocumentOperationResult() {
211     DocumentOperationResult result = new DocumentOperationResult();
212     Document doc = new Document();
213     doc.setEtag("etag1");
214
215     doc.setContent(new JSONObject());
216     result.setDocument(doc);
217     return result;
218   }
219
220   private SearchOperationResult buildSampleSearchOperationResult() {
221     SearchOperationResult result = new SearchOperationResult();
222
223     SearchHits searchHits = new SearchHits();
224     SearchHit[] searchHitArray = new SearchHit[1];
225     SearchHit searchHit = new SearchHit();
226     Document doc = new Document();
227     doc.setEtag("etag1");
228     Map<String, Object> content = new HashMap<String, Object>();
229     content.put("key1", "value1");
230     doc.setContent(new JSONObject());
231     searchHit.setDocument(doc);
232     searchHitArray[0] = searchHit;
233
234     searchHits.setHits(searchHitArray);
235     searchHits.setTotalHits("1");
236     result.setSearchResult(searchHits);
237
238     return result;
239
240   }
241
242 }