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.eclipse.jetty.util.security.Password;
33 import org.json.JSONObject;
34 import org.junit.Before;
35 import org.junit.Ignore;
36 import org.junit.Test;
37 import org.onap.aai.sa.rest.DocumentSchema;
38 import org.onap.aai.sa.searchdbabstraction.elasticsearch.config.ElasticSearchConfig;
39 import org.onap.aai.sa.searchdbabstraction.entity.OperationResult;
41 @Ignore("All tests in this classes require an Elasticsearch instance to run locally")
42 public class ElasticSearchHttpControllerTest {
45 // Set the location of the payload translation JSON file.
46 System.setProperty("CONFIG_HOME", "src/test/resources/json");
49 private static ElasticSearchHttpController elasticSearch;
50 private static AAIEntityTestObject testDocument;
52 private static final String TEST_INDEX_NAME = "test";
54 private static final String indexMappings =
55 "{\r\n \"properties\": {\r\n \"entityType\": {\r\n \"type\": \"text\"\r\n },\r\n"
56 + " \"edgeTagQueryEntityFieldName\": {\r\n \"type\": \"text\",\r\n \"index\": \"false\"\r\n },\r\n"
57 + " \"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}";
58 private static final String indexSettings =
59 "{\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}";
62 public void setUp() throws Exception {
63 Properties properties = new Properties();
64 properties.put(ElasticSearchConfig.ES_IP_ADDRESS, "127.0.0.1");
65 properties.put(ElasticSearchConfig.ES_HTTP_PORT, "9200");
66 properties.put(ElasticSearchConfig.ES_URI_SCHEME, "http");
67 properties.put(ElasticSearchConfig.ES_AUTH_USER, "your_user_here");
68 properties.put(ElasticSearchConfig.ES_AUTH_ENC, Password.obfuscate("your_password_here"));
69 elasticSearch = new ElasticSearchHttpController(new ElasticSearchConfig(properties));
71 testDocument = new AAIEntityTestObject();
72 testDocument.setId("test123");
73 testDocument.setEntityType("service-instance");
74 testDocument.setEdgeTagQueryEntityFieldName("service-instance.service-instance-id");
75 testDocument.setEdgeTagQueryEntityFieldValue("123456");
76 testDocument.setSearchTagIDs("0");
77 testDocument.setSearchTags("service-instance-id");
81 public void testGetInstance() throws Exception {
82 ElasticSearchHttpController.getInstance();
86 public void testCreateIndex() throws Exception {
88 OperationResult result = elasticSearch.createIndex(TEST_INDEX_NAME, new DocumentSchema());
89 assertThat(result.getResult(), containsString("/search/indexes/test"));
90 assertThat(result.getResultCode(), is(201));
92 result = elasticSearch.createIndex(TEST_INDEX_NAME, new DocumentSchema());
93 assertThat(result.getResult(), containsString("already exists"));
94 assertThat(result.getResultCode(), is(400));
97 @Test(expected = IllegalArgumentException.class)
98 public void testCreateDynamicIndexEmptySchema() throws Exception {
99 elasticSearch.createDynamicIndex(TEST_INDEX_NAME, "");
103 public void testCreateDynamicIndex() throws Exception {
104 String indexName = "test_dynamic";
105 elasticSearch.deleteIndex(indexName);
107 OperationResult result = elasticSearch.createDynamicIndex(indexName,
108 "{\"mappings\":{\"_doc\":{\"dynamic_templates\":[{\"strings_as_text\":{\"match_mapping_type\":\"string\",\"mapping\":{\"type\":\"text\"}}}]}}}");
109 assertThat(result.getResult(), containsString("/search/indexes/test"));
110 assertThat(result.getResultCode(), is(201));
112 elasticSearch.deleteIndex(indexName);
116 public void testCreateTable() throws Exception {
117 OperationResult result =
118 elasticSearch.createTable(TEST_INDEX_NAME, "aai-entities", indexSettings, indexMappings);
119 assertThat(result.getResult(), containsString("\"index\":\"test\"}"));
120 assertThat(result.getResultCode(), either(is(200)).or(is(400)));
124 public void testCreateDocument() throws Exception {
125 OperationResult result = elasticSearch.createDocument(TEST_INDEX_NAME, testDocument, false);
126 assertThat(result.getResult(), not(equalTo("")));
128 DocumentStoreDataEntityImpl ds = new DocumentStoreDataEntityImpl();
129 ds.setId(testDocument.getId());
131 result = elasticSearch.getDocument(TEST_INDEX_NAME, ds);
132 assertThat(result.getResult(), not(equalTo("")));
136 public void testCreateDocumentInvalidIndex() throws Exception {
137 OperationResult result = elasticSearch.createDocument("index_does_not_exist", testDocument, false);
138 assertThat(result.getResultCode(), is(404));
139 assertThat(result.getResult(), not(equalTo("")));
143 public void testUpdateDocument() throws Exception {
144 testDocument.setEdgeTagQueryEntityFieldValue("567890");
146 OperationResult result = elasticSearch.getDocument(TEST_INDEX_NAME, testDocument);
147 if (result.getResultCode() == 404) {
148 testCreateDocument();
150 // assertThat(result.getResultCode(), anyOf(equalTo(200), equalTo(412)));
151 // assertThat(result.getResult(), containsString("\"found\":true"));
153 result = elasticSearch.updateDocument(TEST_INDEX_NAME, testDocument, false);
154 assertThat(result.getResultCode(), anyOf(equalTo(200), equalTo(412)));
156 result = elasticSearch.getDocument(TEST_INDEX_NAME, testDocument);
157 assertThat(result.getResult(), containsString("test123"));
161 public void testDeleteDocument() throws Exception {
162 OperationResult result = elasticSearch.getDocument(TEST_INDEX_NAME, testDocument);
163 if (result.getResultCode() == 404) {
164 testCreateDocument();
167 result = elasticSearch.deleteDocument(TEST_INDEX_NAME, testDocument);
168 assertThat(result.getResult(), containsString(TEST_INDEX_NAME));
170 result = elasticSearch.getDocument(TEST_INDEX_NAME, testDocument);
171 assertThat(result.getResult(), containsString("test123"));
175 public void testBulkCreateDocuments() throws Exception {
176 for (int i = 0; i < 10; i++) {
177 AAIEntityTestObject doc = new AAIEntityTestObject();
178 doc.setId("test-" + i);
179 doc.setEntityType("service-instance");
180 doc.setEdgeTagQueryEntityFieldName("service-instance.service-instance-id");
181 doc.setEdgeTagQueryEntityFieldValue("123456" + i);
182 doc.setSearchTagIDs("" + i);
183 doc.setSearchTags("service-instance-id");
185 OperationResult result = elasticSearch.createDocument(TEST_INDEX_NAME, doc, false);
186 assertThat(result.getResultCode(), anyOf(equalTo(201), equalTo(400)));
191 public void serchByEntityType() throws Exception {
192 OperationResult result = elasticSearch.search(TEST_INDEX_NAME, "q=instance");
193 assertThat(result.getResult(), not(equalTo("")));
197 public void serchByTagIDs() throws Exception {
198 OperationResult result = elasticSearch.search(TEST_INDEX_NAME, "q=9");
199 assertThat(result.getResult(), not(equalTo("")));
203 public void serchByTags() throws Exception {
204 OperationResult result = elasticSearch.search(TEST_INDEX_NAME, "q=service");
205 assertThat(result.getResult(), not(equalTo("")));
209 public void searchWithPayload() throws Exception {
211 OperationResult result =
212 elasticSearch.searchWithPayload(TEST_INDEX_NAME, "{\"query\":{\"term\":{\"user\":\"fred\"}}}");
213 assertThat(result.getResult(), containsString("successful"));
214 assertThat(result.getResultCode(), is(equalTo(200)));
218 * The _suggest endpoint appears to be deprecated in ES 5.x and above.
221 public void suggestionQueryWithPayload() throws Exception {
223 OperationResult result = elasticSearch.suggestionQueryWithPayload(TEST_INDEX_NAME,
224 "{\"my-suggestion\":{\"text\":\"fred\",\"term\":{\"field\":\"body\"}}}");
225 assertThat(result.getResult(), containsString("error"));
226 assertThat(result.getResultCode(), is(equalTo(400)));
230 public void testCreateDocumentWithoutId() throws Exception {
231 AAIEntityTestObject doc = new AAIEntityTestObject();
232 doc.setEntityType("service-instance");
233 doc.setEdgeTagQueryEntityFieldName("service-instance.service-instance-id");
234 doc.setEdgeTagQueryEntityFieldValue("1111111");
235 doc.setSearchTagIDs("321");
236 doc.setSearchTags("service-instance-id");
238 OperationResult result = elasticSearch.createDocument(TEST_INDEX_NAME, doc, false);
239 assertThat(result.getResult(), not(equalTo("")));
243 public void testDeleteIndex() throws Exception {
244 OperationResult result = elasticSearch.deleteIndex(TEST_INDEX_NAME);
245 assertThat(result.getResultCode(), anyOf(equalTo(200), equalTo(404)));
246 assertThat(result.getResult(), not(equalTo("")));
249 class AAIEntityTestObject implements DocumentStoreDataEntity {
251 private String entityType;
252 private String edgeTagQueryEntityFieldName;
253 private String edgeTagQueryEntityFieldValue;
254 private String searchTagIDs;
255 private String searchTags;
257 public void setId(String id) {
262 public String getId() {
266 public String getEntityType() {
270 public void setEntityType(String entityType) {
271 this.entityType = entityType;
274 public String getEdgeTagQueryEntityFieldName() {
275 return edgeTagQueryEntityFieldName;
278 public void setEdgeTagQueryEntityFieldName(String edgeTagQueryEntityFieldName) {
279 this.edgeTagQueryEntityFieldName = edgeTagQueryEntityFieldName;
282 public String getEdgeTagQueryEntityFieldValue() {
283 return edgeTagQueryEntityFieldValue;
286 public void setEdgeTagQueryEntityFieldValue(String edgeTagQueryEntityFieldValue) {
287 this.edgeTagQueryEntityFieldValue = edgeTagQueryEntityFieldValue;
290 public String getSearchTagIDs() {
294 public void setSearchTagIDs(String searchTagIDs) {
295 this.searchTagIDs = searchTagIDs;
298 public String getSearchTags() {
302 public void setSearchTags(String searchTags) {
303 this.searchTags = searchTags;
307 public String getVersion() {
312 public String getContentInJson() {
313 return new JSONObject(). //
314 put("entityType", entityType) //
315 .put("edgeTagQueryEntityFieldName", edgeTagQueryEntityFieldName)
316 .put("edgeTagQueryEntityFieldValue", edgeTagQueryEntityFieldValue) //
317 .put("searchTagIDs", searchTagIDs) //
318 .put("searchTags", searchTags) //