2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.sa.searchdbabstraction.elasticsearch.dao;
23 import static org.hamcrest.CoreMatchers.anyOf;
24 import static org.hamcrest.CoreMatchers.containsString;
25 import static org.hamcrest.CoreMatchers.either;
26 import static org.hamcrest.CoreMatchers.equalTo;
27 import static org.hamcrest.CoreMatchers.is;
28 import static org.hamcrest.CoreMatchers.not;
29 import static org.junit.Assert.assertThat;
31 import java.util.Properties;
32 import org.json.JSONObject;
33 import org.junit.Before;
34 import org.junit.Ignore;
35 import org.junit.Test;
36 import org.onap.aai.sa.rest.DocumentSchema;
37 import org.onap.aai.sa.searchdbabstraction.elasticsearch.config.ElasticSearchConfig;
38 import org.onap.aai.sa.searchdbabstraction.entity.OperationResult;
40 @Ignore("All tests in this classes require an Elasticsearch instance to run locally")
41 public class ElasticSearchHttpControllerTest {
44 // Set the location of the payload translation JSON file.
45 System.setProperty("CONFIG_HOME", "src/test/resources/json");
48 private static ElasticSearchHttpController elasticSearch;
49 private static AAIEntityTestObject testDocument;
51 private static final String TEST_INDEX_NAME = "test";
53 private static final String indexMappings =
54 "{\r\n \"properties\": {\r\n \"entityType\": {\r\n \"type\": \"text\"\r\n },\r\n"
55 + " \"edgeTagQueryEntityFieldName\": {\r\n \"type\": \"text\",\r\n \"index\": \"false\"\r\n },\r\n"
56 + " \"edgeTagQueryEntityFieldValue\": {\r\n \"type\": \"text\",\r\n \"index\": \"false\"\r\n },\r\n \"searchTagIDs\" : {\r\n \"type\" : \"text\"\r\n },\r\n \"searchTags\": {\r\n \"type\": \"text\",\r\n \"analyzer\": \"nGram_analyzer\",\r\n \"search_analyzer\": \"whitespace_analyzer\"\r\n }\r\n }\r\n}";
57 private static final String indexSettings =
58 "{\r\n \"analysis\": {\r\n \"filter\": {\r\n \"nGram_filter\": {\r\n \"type\": \"nGram\",\r\n \"min_gram\": 1,\r\n \"max_gram\": 50,\r\n \"token_chars\": [\r\n \"letter\",\r\n \"digit\",\r\n \"punctuation\",\r\n \"symbol\"\r\n ]\r\n }\r\n },\r\n \"analyzer\": {\r\n \"nGram_analyzer\": {\r\n \"type\": \"custom\",\r\n \"tokenizer\": \"whitespace\",\r\n \"filter\": [\r\n \"lowercase\",\r\n \"asciifolding\",\r\n \"nGram_filter\"\r\n ]\r\n },\r\n \"whitespace_analyzer\": {\r\n \"type\": \"custom\",\r\n \"tokenizer\": \"whitespace\",\r\n \"filter\": [\r\n \"lowercase\",\r\n \"asciifolding\"\r\n ]\r\n }\r\n }\r\n }\r\n}";
61 public void setUp() throws Exception {
62 Properties properties = new Properties();
63 properties.put(ElasticSearchConfig.ES_IP_ADDRESS, "127.0.0.1");
64 properties.put(ElasticSearchConfig.ES_HTTP_PORT, "9200");
65 elasticSearch = new ElasticSearchHttpController(new ElasticSearchConfig(properties));
67 testDocument = new AAIEntityTestObject();
68 testDocument.setId("test123");
69 testDocument.setEntityType("service-instance");
70 testDocument.setEdgeTagQueryEntityFieldName("service-instance.service-instance-id");
71 testDocument.setEdgeTagQueryEntityFieldValue("123456");
72 testDocument.setSearchTagIDs("0");
73 testDocument.setSearchTags("service-instance-id");
77 public void testGetInstance() throws Exception {
78 ElasticSearchHttpController.getInstance();
82 public void testCreateIndex() throws Exception {
84 OperationResult result = elasticSearch.createIndex(TEST_INDEX_NAME, new DocumentSchema());
85 assertThat(result.getResult(), containsString("/search/indexes/test"));
86 assertThat(result.getResultCode(), is(201));
88 result = elasticSearch.createIndex(TEST_INDEX_NAME, new DocumentSchema());
89 assertThat(result.getResult(), containsString("already exists"));
90 assertThat(result.getResultCode(), is(400));
93 @Test(expected = IllegalArgumentException.class)
94 public void testCreateDynamicIndexEmptySchema() throws Exception {
95 elasticSearch.createDynamicIndex(TEST_INDEX_NAME, "");
99 public void testCreateDynamicIndex() throws Exception {
100 String indexName = "test_dynamic";
101 elasticSearch.deleteIndex(indexName);
103 OperationResult result = elasticSearch.createDynamicIndex(indexName,
104 "{\"mappings\":{\"_doc\":{\"dynamic_templates\":[{\"strings_as_text\":{\"match_mapping_type\":\"string\",\"mapping\":{\"type\":\"text\"}}}]}}}");
105 assertThat(result.getResult(), containsString("/search/indexes/test"));
106 assertThat(result.getResultCode(), is(201));
108 elasticSearch.deleteIndex(indexName);
112 public void testCreateTable() throws Exception {
113 OperationResult result =
114 elasticSearch.createTable(TEST_INDEX_NAME, "aai-entities", indexSettings, indexMappings);
115 assertThat(result.getResult(), containsString("\"index\":\"test\"}"));
116 assertThat(result.getResultCode(), either(is(200)).or(is(400)));
120 public void testCreateDocument() throws Exception {
121 OperationResult result = elasticSearch.createDocument(TEST_INDEX_NAME, testDocument, false);
122 assertThat(result.getResult(), not(equalTo("")));
124 DocumentStoreDataEntityImpl ds = new DocumentStoreDataEntityImpl();
125 ds.setId(testDocument.getId());
127 result = elasticSearch.getDocument(TEST_INDEX_NAME, ds);
128 assertThat(result.getResult(), not(equalTo("")));
132 public void testCreateDocumentInvalidIndex() throws Exception {
133 OperationResult result = elasticSearch.createDocument("index_does_not_exist", testDocument, false);
134 assertThat(result.getResultCode(), is(404));
135 assertThat(result.getResult(), not(equalTo("")));
139 public void testUpdateDocument() throws Exception {
140 testDocument.setEdgeTagQueryEntityFieldValue("567890");
142 OperationResult result = elasticSearch.getDocument(TEST_INDEX_NAME, testDocument);
143 if (result.getResultCode() == 404) {
144 testCreateDocument();
146 // assertThat(result.getResultCode(), anyOf(equalTo(200), equalTo(412)));
147 // assertThat(result.getResult(), containsString("\"found\":true"));
149 result = elasticSearch.updateDocument(TEST_INDEX_NAME, testDocument, false);
150 assertThat(result.getResultCode(), anyOf(equalTo(200), equalTo(412)));
152 result = elasticSearch.getDocument(TEST_INDEX_NAME, testDocument);
153 assertThat(result.getResult(), containsString("test123"));
157 public void testDeleteDocument() throws Exception {
158 OperationResult result = elasticSearch.deleteDocument(TEST_INDEX_NAME, testDocument);
159 assertThat(result.getResult(), containsString(TEST_INDEX_NAME));
161 result = elasticSearch.getDocument(TEST_INDEX_NAME, testDocument);
162 assertThat(result.getResult(), containsString("test123"));
166 public void testBulkCreateDocuments() throws Exception {
167 for (int i = 0; i < 10; i++) {
168 AAIEntityTestObject doc = new AAIEntityTestObject();
169 doc.setId("test-" + i);
170 doc.setEntityType("service-instance");
171 doc.setEdgeTagQueryEntityFieldName("service-instance.service-instance-id");
172 doc.setEdgeTagQueryEntityFieldValue("123456" + i);
173 doc.setSearchTagIDs("" + i);
174 doc.setSearchTags("service-instance-id");
176 // OperationResult result = elasticSearch.createDocument(TEST_INDEX_NAME, doc, false);
177 // assertThat(result.getResultCode(), anyOf(equalTo(201), equalTo(400)));
182 public void serchByEntityType() throws Exception {
183 OperationResult result = elasticSearch.search(TEST_INDEX_NAME, "q=instance");
184 assertThat(result.getResult(), not(equalTo("")));
188 public void serchByTagIDs() throws Exception {
189 OperationResult result = elasticSearch.search(TEST_INDEX_NAME, "q=9");
190 assertThat(result.getResult(), not(equalTo("")));
194 public void serchByTags() throws Exception {
195 OperationResult result = elasticSearch.search(TEST_INDEX_NAME, "q=service");
196 assertThat(result.getResult(), not(equalTo("")));
200 public void searchWithPayload() throws Exception {
202 OperationResult result =
203 elasticSearch.searchWithPayload(TEST_INDEX_NAME, "{\"query\":{\"term\":{\"user\":\"fred\"}}}");
204 assertThat(result.getResult(), containsString("successful"));
205 assertThat(result.getResultCode(), is(equalTo(200)));
209 * The _suggest endpoint appears to be deprecated in ES 5.x and above.
212 public void suggestionQueryWithPayload() throws Exception {
214 OperationResult result = elasticSearch.suggestionQueryWithPayload(TEST_INDEX_NAME,
215 "{\"my-suggestion\":{\"text\":\"fred\",\"term\":{\"field\":\"body\"}}}");
216 assertThat(result.getResult(), containsString("error"));
217 assertThat(result.getResultCode(), is(equalTo(400)));
221 public void testCreateDocumentWithoutId() throws Exception {
222 AAIEntityTestObject doc = new AAIEntityTestObject();
223 doc.setEntityType("service-instance");
224 doc.setEdgeTagQueryEntityFieldName("service-instance.service-instance-id");
225 doc.setEdgeTagQueryEntityFieldValue("1111111");
226 doc.setSearchTagIDs("321");
227 doc.setSearchTags("service-instance-id");
229 OperationResult result = elasticSearch.createDocument(TEST_INDEX_NAME, doc, false);
230 assertThat(result.getResult(), not(equalTo("")));
234 public void testDeleteIndex() throws Exception {
235 OperationResult result = elasticSearch.deleteIndex(TEST_INDEX_NAME);
236 assertThat(result.getResultCode(), anyOf(equalTo(200), equalTo(404)));
237 assertThat(result.getResult(), not(equalTo("")));
240 class AAIEntityTestObject implements DocumentStoreDataEntity {
242 private String entityType;
243 private String edgeTagQueryEntityFieldName;
244 private String edgeTagQueryEntityFieldValue;
245 private String searchTagIDs;
246 private String searchTags;
248 public void setId(String id) {
253 public String getId() {
257 public String getEntityType() {
261 public void setEntityType(String entityType) {
262 this.entityType = entityType;
265 public String getEdgeTagQueryEntityFieldName() {
266 return edgeTagQueryEntityFieldName;
269 public void setEdgeTagQueryEntityFieldName(String edgeTagQueryEntityFieldName) {
270 this.edgeTagQueryEntityFieldName = edgeTagQueryEntityFieldName;
273 public String getEdgeTagQueryEntityFieldValue() {
274 return edgeTagQueryEntityFieldValue;
277 public void setEdgeTagQueryEntityFieldValue(String edgeTagQueryEntityFieldValue) {
278 this.edgeTagQueryEntityFieldValue = edgeTagQueryEntityFieldValue;
281 public String getSearchTagIDs() {
285 public void setSearchTagIDs(String searchTagIDs) {
286 this.searchTagIDs = searchTagIDs;
289 public String getSearchTags() {
293 public void setSearchTags(String searchTags) {
294 this.searchTags = searchTags;
298 public String getVersion() {
303 public String getContentInJson() {
304 return new JSONObject(). //
305 put("entityType", entityType) //
306 .put("edgeTagQueryEntityFieldName", edgeTagQueryEntityFieldName)
307 .put("edgeTagQueryEntityFieldValue", edgeTagQueryEntityFieldValue) //
308 .put("searchTagIDs", searchTagIDs) //
309 .put("searchTags", searchTags) //