2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License ati
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.openecomp.sa.searchdbabstraction.elasticsearch.dao;
27 import org.json.JSONException;
28 import org.json.JSONObject;
29 import org.junit.Before;
30 import org.junit.Ignore;
31 import org.junit.Test;
32 import org.openecomp.sa.searchdbabstraction.elasticsearch.config.ElasticSearchConfig;
33 import org.openecomp.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 = "{\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}";
44 private static final String indexSettings = "{\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}";
47 public void setUp() throws Exception {
48 Properties properties = new Properties();
49 properties.put(ElasticSearchConfig.ES_IP_ADDRESS, "127.0.0.1");
50 properties.put(ElasticSearchConfig.ES_HTTP_PORT, "9200");
51 ElasticSearchConfig config = new ElasticSearchConfig(properties);
52 elasticSearch = new ElasticSearchHttpController(config);
54 testDocument = new AAIEntityTestObject();
55 testDocument.setId("test123");
56 testDocument.setEntityType("service-instance");
57 testDocument.setEdgeTagQueryEntityFieldName("service-instance.service-instance-id");
58 testDocument.setEdgeTagQueryEntityFieldValue("123456");
59 testDocument.setSearchTagIDs("0");
60 testDocument.setSearchTags("service-instance-id");
65 public void testCreateTable() throws Exception {
66 OperationResult result = elasticSearch.createTable("test", "aai-entities", indexSettings, indexMappings);
67 System.out.println(result);
71 public void testCreateDocument() throws Exception {
72 OperationResult result = elasticSearch.createDocument("test", testDocument);
73 System.out.println(result);
75 DocumentStoreDataEntityImpl ds = new DocumentStoreDataEntityImpl();
76 ds.setId(testDocument.getId());
78 result = elasticSearch.getDocument("test", ds);
79 System.out.println(result);
83 public void testUpdateDocument() throws Exception {
84 testDocument.setEdgeTagQueryEntityFieldValue("567890");
86 OperationResult result = elasticSearch.updateDocument("test", testDocument);
87 System.out.println(result);
89 result = elasticSearch.getDocument("test", testDocument);
90 System.out.println(result);
94 public void testDeleteDocument() throws Exception {
95 OperationResult result = elasticSearch.deleteDocument("test", testDocument);
96 System.out.println(result);
98 result = elasticSearch.getDocument("test", testDocument);
99 System.out.println(result);
103 public void testBulkCreateDocuments() throws Exception {
104 for (int i = 0; i < 10; i++) {
105 AAIEntityTestObject doc = new AAIEntityTestObject();
106 doc.setId("test-" + i);
107 doc.setEntityType("service-instance");
108 doc.setEdgeTagQueryEntityFieldName("service-instance.service-instance-id");
109 doc.setEdgeTagQueryEntityFieldValue("123456" + i);
110 doc.setSearchTagIDs("" + i);
111 doc.setSearchTags("service-instance-id");
113 OperationResult result = elasticSearch.createDocument("test", doc);
114 System.out.println(result);
119 public void serchByEntityType() throws Exception {
120 OperationResult result = elasticSearch.search("test", "q=instance");
121 System.out.println(result);
125 public void serchByTagIDs() throws Exception {
126 OperationResult result = elasticSearch.search("test", "q=9");
127 System.out.println(result);
131 public void serchByTags() throws Exception {
132 OperationResult result = elasticSearch.search("test", "q=service");
133 System.out.println(result);
137 public void testCreateDocumentWithoutId() throws Exception {
138 AAIEntityTestObject doc = new AAIEntityTestObject();
139 doc.setEntityType("service-instance");
140 doc.setEdgeTagQueryEntityFieldName("service-instance.service-instance-id");
141 doc.setEdgeTagQueryEntityFieldValue("1111111");
142 doc.setSearchTagIDs("321");
143 doc.setSearchTags("service-instance-id");
145 OperationResult result = elasticSearch.createDocument("test", doc);
146 System.out.println(result);
150 public void testDeleteIndex() throws Exception {
151 OperationResult result = elasticSearch.deleteIndex("test");
152 System.out.println(result);
155 class AAIEntityTestObject implements DocumentStoreDataEntity {
157 private String entityType;
158 private String edgeTagQueryEntityFieldName;
159 private String edgeTagQueryEntityFieldValue;
160 private String searchTagIDs;
161 private String searchTags;
163 public void setId(String id) {
168 public String getId() {
172 public String getEntityType() {
176 public void setEntityType(String entityType) {
177 this.entityType = entityType;
180 public String getEdgeTagQueryEntityFieldName() {
181 return edgeTagQueryEntityFieldName;
184 public void setEdgeTagQueryEntityFieldName(String edgeTagQueryEntityFieldName) {
185 this.edgeTagQueryEntityFieldName = edgeTagQueryEntityFieldName;
188 public String getEdgeTagQueryEntityFieldValue() {
189 return edgeTagQueryEntityFieldValue;
192 public void setEdgeTagQueryEntityFieldValue(String edgeTagQueryEntityFieldValue) {
193 this.edgeTagQueryEntityFieldValue = edgeTagQueryEntityFieldValue;
196 public String getSearchTagIDs() {
200 public void setSearchTagIDs(String searchTagIDs) {
201 this.searchTagIDs = searchTagIDs;
204 public String getSearchTags() {
208 public void setSearchTags(String searchTags) {
209 this.searchTags = searchTags;
213 public String getVersion() {
218 public String getContentInJson() {
220 return new JSONObject()
221 .put("entityType", entityType)
222 .put("edgeTagQueryEntityFieldName", edgeTagQueryEntityFieldName)
223 .put("edgeTagQueryEntityFieldValue", edgeTagQueryEntityFieldValue)
224 .put("searchTagIDs", searchTagIDs)
225 .put("searchTags", searchTags).toString();
226 } catch (JSONException e) {
227 // TODO Auto-generated catch block