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 org.json.JSONException;
24 import org.json.JSONObject;
25 import org.junit.Assert;
26 import org.junit.Before;
27 import org.junit.Ignore;
28 import org.junit.Test;
29 import org.onap.aai.sa.searchdbabstraction.elasticsearch.config.ElasticSearchConfig;
30 import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreDataEntity;
31 import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreDataEntityImpl;
32 import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.ElasticSearchHttpController;
33 import org.onap.aai.sa.searchdbabstraction.entity.OperationResult;
35 import java.util.Properties;
37 @Ignore("All tests in this classes require an Elasticsearch instance to run locally")
38 public class ElasticSearchHttpControllerTest {
40 private static ElasticSearchHttpController elasticSearch;
41 private static AAIEntityTestObject testDocument;
43 private static final String indexMappings =
44 "{\r\n \"properties\": {\r\n \"entityType\": {\r\n \"type\": \"string\"\r\n },\r\n \"edgeTagQueryEntityFieldName\": {\r\n \"type\": \"string\",\r\n \"index\": \"no\"\r\n },\r\n \"edgeTagQueryEntityFieldValue\": {\r\n \"type\": \"string\",\r\n \"index\": \"no\"\r\n },\r\n \"searchTagIDs\" : {\r\n \"type\" : \"string\"\r\n },\r\n \"searchTags\": {\r\n \"type\": \"string\",\r\n \"analyzer\": \"nGram_analyzer\",\r\n \"search_analyzer\": \"whitespace_analyzer\"\r\n }\r\n }\r\n}";
45 private static final String indexSettings =
46 "{\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}";
49 public void setUp() throws Exception {
50 Properties properties = new Properties();
51 properties.put(ElasticSearchConfig.ES_IP_ADDRESS, "127.0.0.1");
52 properties.put(ElasticSearchConfig.ES_HTTP_PORT, "9200");
53 ElasticSearchConfig config = new ElasticSearchConfig(properties);
54 elasticSearch = new ElasticSearchHttpController(config);
56 testDocument = new AAIEntityTestObject();
57 testDocument.setId("test123");
58 testDocument.setEntityType("service-instance");
59 testDocument.setEdgeTagQueryEntityFieldName("service-instance.service-instance-id");
60 testDocument.setEdgeTagQueryEntityFieldValue("123456");
61 testDocument.setSearchTagIDs("0");
62 testDocument.setSearchTags("service-instance-id");
67 public void testCreateTable() throws Exception {
68 OperationResult result =
69 elasticSearch.createTable("test", "aai-entities", indexSettings, indexMappings);
70 System.out.println(result);
74 public void testCreateDocument() throws Exception {
75 OperationResult result = elasticSearch.createDocument("test", testDocument, false);
76 System.out.println(result);
78 DocumentStoreDataEntityImpl ds = new DocumentStoreDataEntityImpl();
79 ds.setId(testDocument.getId());
81 result = elasticSearch.getDocument("test", ds);
82 System.out.println(result);
86 public void testUpdateDocument() throws Exception {
87 testDocument.setEdgeTagQueryEntityFieldValue("567890");
89 OperationResult result = elasticSearch.updateDocument("test", testDocument, false);
90 System.out.println(result);
92 result = elasticSearch.getDocument("test", testDocument);
93 System.out.println(result);
97 public void testDeleteDocument() throws Exception {
98 OperationResult result = elasticSearch.deleteDocument("test", testDocument);
99 System.out.println(result);
101 result = elasticSearch.getDocument("test", testDocument);
102 System.out.println(result);
106 public void testBulkCreateDocuments() throws Exception {
107 for (int i = 0; i < 10; i++) {
108 AAIEntityTestObject doc = new AAIEntityTestObject();
109 doc.setId("test-" + i);
110 doc.setEntityType("service-instance");
111 doc.setEdgeTagQueryEntityFieldName("service-instance.service-instance-id");
112 doc.setEdgeTagQueryEntityFieldValue("123456" + i);
113 doc.setSearchTagIDs("" + i);
114 doc.setSearchTags("service-instance-id");
116 OperationResult result = elasticSearch.createDocument("test", doc, false);
117 System.out.println(result);
122 public void serchByEntityType() throws Exception {
123 OperationResult result = elasticSearch.search("test", "q=instance");
124 System.out.println(result);
128 public void serchByTagIDs() throws Exception {
129 OperationResult result = elasticSearch.search("test", "q=9");
130 System.out.println(result);
134 public void serchByTags() throws Exception {
135 OperationResult result = elasticSearch.search("test", "q=service");
136 System.out.println(result);
140 public void testCreateDocumentWithoutId() throws Exception {
141 AAIEntityTestObject doc = new AAIEntityTestObject();
142 doc.setEntityType("service-instance");
143 doc.setEdgeTagQueryEntityFieldName("service-instance.service-instance-id");
144 doc.setEdgeTagQueryEntityFieldValue("1111111");
145 doc.setSearchTagIDs("321");
146 doc.setSearchTags("service-instance-id");
148 OperationResult result = elasticSearch.createDocument("test", doc, false);
149 System.out.println(result);
153 public void testsuggestionQueryWithPayload() throws Exception {
155 Assert.assertNotNull(elasticSearch.suggestionQueryWithPayload("autoSuggest", "suggest-index"));
160 public void testDeleteIndex() throws Exception {
161 OperationResult result = elasticSearch.deleteIndex("test");
162 System.out.println(result);
165 class AAIEntityTestObject implements DocumentStoreDataEntity {
167 private String entityType;
168 private String edgeTagQueryEntityFieldName;
169 private String edgeTagQueryEntityFieldValue;
170 private String searchTagIDs;
171 private String searchTags;
173 public void setId(String id) {
178 public String getId() {
182 public String getEntityType() {
186 public void setEntityType(String entityType) {
187 this.entityType = entityType;
190 public String getEdgeTagQueryEntityFieldName() {
191 return edgeTagQueryEntityFieldName;
194 public void setEdgeTagQueryEntityFieldName(String edgeTagQueryEntityFieldName) {
195 this.edgeTagQueryEntityFieldName = edgeTagQueryEntityFieldName;
198 public String getEdgeTagQueryEntityFieldValue() {
199 return edgeTagQueryEntityFieldValue;
202 public void setEdgeTagQueryEntityFieldValue(String edgeTagQueryEntityFieldValue) {
203 this.edgeTagQueryEntityFieldValue = edgeTagQueryEntityFieldValue;
206 public String getSearchTagIDs() {
210 public void setSearchTagIDs(String searchTagIDs) {
211 this.searchTagIDs = searchTagIDs;
214 public String getSearchTags() {
218 public void setSearchTags(String searchTags) {
219 this.searchTags = searchTags;
223 public String getVersion() {
228 public String getContentInJson() {
230 return new JSONObject().put("entityType", entityType)
231 .put("edgeTagQueryEntityFieldName", edgeTagQueryEntityFieldName)
232 .put("edgeTagQueryEntityFieldValue", edgeTagQueryEntityFieldValue)
233 .put("searchTagIDs", searchTagIDs).put("searchTags", searchTags).toString();
234 } catch (JSONException e) {
235 // TODO Auto-generated catch block