2 * ============LICENSE_START=======================================================
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
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.sa.searchdbabstraction.elasticsearch.dao;
25 import org.json.JSONException;
26 import org.json.JSONObject;
27 import org.junit.Before;
28 import org.junit.Ignore;
29 import org.junit.Test;
30 import org.openecomp.sa.searchdbabstraction.elasticsearch.config.ElasticSearchConfig;
31 import org.openecomp.sa.searchdbabstraction.entity.OperationResult;
33 import java.util.Properties;
35 @Ignore("All tests in this classes require an Elasticsearch instance to run locally")
36 public class ElasticSearchHttpControllerTest {
38 private static ElasticSearchHttpController elasticSearch;
39 private static AAIEntityTestObject testDocument;
41 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}";
42 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}";
45 public void setUp() throws Exception {
46 Properties properties = new Properties();
47 properties.put(ElasticSearchConfig.ES_IP_ADDRESS, "127.0.0.1");
48 properties.put(ElasticSearchConfig.ES_HTTP_PORT, "9200");
49 ElasticSearchConfig config = new ElasticSearchConfig(properties);
50 elasticSearch = new ElasticSearchHttpController(config);
52 testDocument = new AAIEntityTestObject();
53 testDocument.setId("test123");
54 testDocument.setEntityType("service-instance");
55 testDocument.setEdgeTagQueryEntityFieldName("service-instance.service-instance-id");
56 testDocument.setEdgeTagQueryEntityFieldValue("123456");
57 testDocument.setSearchTagIDs("0");
58 testDocument.setSearchTags("service-instance-id");
63 public void testCreateTable() throws Exception {
64 OperationResult result = elasticSearch.createTable("test", "aai-entities", indexSettings, indexMappings);
65 System.out.println(result);
69 public void testCreateDocument() throws Exception {
70 OperationResult result = elasticSearch.createDocument("test", testDocument, false);
71 System.out.println(result);
73 DocumentStoreDataEntityImpl ds = new DocumentStoreDataEntityImpl();
74 ds.setId(testDocument.getId());
76 result = elasticSearch.getDocument("test", ds);
77 System.out.println(result);
81 public void testUpdateDocument() throws Exception {
82 testDocument.setEdgeTagQueryEntityFieldValue("567890");
84 OperationResult result = elasticSearch.updateDocument("test", testDocument, false);
85 System.out.println(result);
87 result = elasticSearch.getDocument("test", testDocument);
88 System.out.println(result);
92 public void testDeleteDocument() throws Exception {
93 OperationResult result = elasticSearch.deleteDocument("test", testDocument);
94 System.out.println(result);
96 result = elasticSearch.getDocument("test", testDocument);
97 System.out.println(result);
101 public void testBulkCreateDocuments() throws Exception {
102 for (int i = 0; i < 10; i++) {
103 AAIEntityTestObject doc = new AAIEntityTestObject();
104 doc.setId("test-" + i);
105 doc.setEntityType("service-instance");
106 doc.setEdgeTagQueryEntityFieldName("service-instance.service-instance-id");
107 doc.setEdgeTagQueryEntityFieldValue("123456" + i);
108 doc.setSearchTagIDs("" + i);
109 doc.setSearchTags("service-instance-id");
111 OperationResult result = elasticSearch.createDocument("test", doc, false);
112 System.out.println(result);
117 public void serchByEntityType() throws Exception {
118 OperationResult result = elasticSearch.search("test", "q=instance");
119 System.out.println(result);
123 public void serchByTagIDs() throws Exception {
124 OperationResult result = elasticSearch.search("test", "q=9");
125 System.out.println(result);
129 public void serchByTags() throws Exception {
130 OperationResult result = elasticSearch.search("test", "q=service");
131 System.out.println(result);
135 public void testCreateDocumentWithoutId() throws Exception {
136 AAIEntityTestObject doc = new AAIEntityTestObject();
137 doc.setEntityType("service-instance");
138 doc.setEdgeTagQueryEntityFieldName("service-instance.service-instance-id");
139 doc.setEdgeTagQueryEntityFieldValue("1111111");
140 doc.setSearchTagIDs("321");
141 doc.setSearchTags("service-instance-id");
143 OperationResult result = elasticSearch.createDocument("test", doc, false);
144 System.out.println(result);
148 public void testDeleteIndex() throws Exception {
149 OperationResult result = elasticSearch.deleteIndex("test");
150 System.out.println(result);
153 class AAIEntityTestObject implements DocumentStoreDataEntity {
155 private String entityType;
156 private String edgeTagQueryEntityFieldName;
157 private String edgeTagQueryEntityFieldValue;
158 private String searchTagIDs;
159 private String searchTags;
161 public void setId(String id) {
166 public String getId() {
170 public String getEntityType() {
174 public void setEntityType(String entityType) {
175 this.entityType = entityType;
178 public String getEdgeTagQueryEntityFieldName() {
179 return edgeTagQueryEntityFieldName;
182 public void setEdgeTagQueryEntityFieldName(String edgeTagQueryEntityFieldName) {
183 this.edgeTagQueryEntityFieldName = edgeTagQueryEntityFieldName;
186 public String getEdgeTagQueryEntityFieldValue() {
187 return edgeTagQueryEntityFieldValue;
190 public void setEdgeTagQueryEntityFieldValue(String edgeTagQueryEntityFieldValue) {
191 this.edgeTagQueryEntityFieldValue = edgeTagQueryEntityFieldValue;
194 public String getSearchTagIDs() {
198 public void setSearchTagIDs(String searchTagIDs) {
199 this.searchTagIDs = searchTagIDs;
202 public String getSearchTags() {
206 public void setSearchTags(String searchTags) {
207 this.searchTags = searchTags;
211 public String getVersion() {
216 public String getContentInJson() {
218 return new JSONObject()
219 .put("entityType", entityType)
220 .put("edgeTagQueryEntityFieldName", edgeTagQueryEntityFieldName)
221 .put("edgeTagQueryEntityFieldValue", edgeTagQueryEntityFieldValue)
222 .put("searchTagIDs", searchTagIDs)
223 .put("searchTags", searchTags).toString();
224 } catch (JSONException e) {
225 // TODO Auto-generated catch block