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.onap.aai.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.onap.aai.sa.searchdbabstraction.elasticsearch.config.ElasticSearchConfig;
31 import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreDataEntity;
32 import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreDataEntityImpl;
33 import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.ElasticSearchHttpController;
34 import org.onap.aai.sa.searchdbabstraction.entity.OperationResult;
36 import java.util.Properties;
38 @Ignore("All tests in this classes require an Elasticsearch instance to run locally")
39 public class ElasticSearchHttpControllerTest {
41 private static ElasticSearchHttpController elasticSearch;
42 private static AAIEntityTestObject testDocument;
44 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}";
45 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}";
48 public void setUp() throws Exception {
49 Properties properties = new Properties();
50 properties.put(ElasticSearchConfig.ES_IP_ADDRESS, "127.0.0.1");
51 properties.put(ElasticSearchConfig.ES_HTTP_PORT, "9200");
52 ElasticSearchConfig config = new ElasticSearchConfig(properties);
53 elasticSearch = new ElasticSearchHttpController(config);
55 testDocument = new AAIEntityTestObject();
56 testDocument.setId("test123");
57 testDocument.setEntityType("service-instance");
58 testDocument.setEdgeTagQueryEntityFieldName("service-instance.service-instance-id");
59 testDocument.setEdgeTagQueryEntityFieldValue("123456");
60 testDocument.setSearchTagIDs("0");
61 testDocument.setSearchTags("service-instance-id");
66 public void testCreateTable() throws Exception {
67 OperationResult result = elasticSearch.createTable("test", "aai-entities", indexSettings, indexMappings);
68 System.out.println(result);
72 public void testCreateDocument() throws Exception {
73 OperationResult result = elasticSearch.createDocument("test", testDocument, false);
74 System.out.println(result);
76 DocumentStoreDataEntityImpl ds = new DocumentStoreDataEntityImpl();
77 ds.setId(testDocument.getId());
79 result = elasticSearch.getDocument("test", ds);
80 System.out.println(result);
84 public void testUpdateDocument() throws Exception {
85 testDocument.setEdgeTagQueryEntityFieldValue("567890");
87 OperationResult result = elasticSearch.updateDocument("test", testDocument, false);
88 System.out.println(result);
90 result = elasticSearch.getDocument("test", testDocument);
91 System.out.println(result);
95 public void testDeleteDocument() throws Exception {
96 OperationResult result = elasticSearch.deleteDocument("test", testDocument);
97 System.out.println(result);
99 result = elasticSearch.getDocument("test", testDocument);
100 System.out.println(result);
104 public void testBulkCreateDocuments() throws Exception {
105 for (int i = 0; i < 10; i++) {
106 AAIEntityTestObject doc = new AAIEntityTestObject();
107 doc.setId("test-" + i);
108 doc.setEntityType("service-instance");
109 doc.setEdgeTagQueryEntityFieldName("service-instance.service-instance-id");
110 doc.setEdgeTagQueryEntityFieldValue("123456" + i);
111 doc.setSearchTagIDs("" + i);
112 doc.setSearchTags("service-instance-id");
114 OperationResult result = elasticSearch.createDocument("test", doc, false);
115 System.out.println(result);
120 public void serchByEntityType() throws Exception {
121 OperationResult result = elasticSearch.search("test", "q=instance");
122 System.out.println(result);
126 public void serchByTagIDs() throws Exception {
127 OperationResult result = elasticSearch.search("test", "q=9");
128 System.out.println(result);
132 public void serchByTags() throws Exception {
133 OperationResult result = elasticSearch.search("test", "q=service");
134 System.out.println(result);
138 public void testCreateDocumentWithoutId() throws Exception {
139 AAIEntityTestObject doc = new AAIEntityTestObject();
140 doc.setEntityType("service-instance");
141 doc.setEdgeTagQueryEntityFieldName("service-instance.service-instance-id");
142 doc.setEdgeTagQueryEntityFieldValue("1111111");
143 doc.setSearchTagIDs("321");
144 doc.setSearchTags("service-instance-id");
146 OperationResult result = elasticSearch.createDocument("test", doc, false);
147 System.out.println(result);
151 public void testDeleteIndex() throws Exception {
152 OperationResult result = elasticSearch.deleteIndex("test");
153 System.out.println(result);
156 class AAIEntityTestObject implements DocumentStoreDataEntity {
158 private String entityType;
159 private String edgeTagQueryEntityFieldName;
160 private String edgeTagQueryEntityFieldValue;
161 private String searchTagIDs;
162 private String searchTags;
164 public void setId(String id) {
169 public String getId() {
173 public String getEntityType() {
177 public void setEntityType(String entityType) {
178 this.entityType = entityType;
181 public String getEdgeTagQueryEntityFieldName() {
182 return edgeTagQueryEntityFieldName;
185 public void setEdgeTagQueryEntityFieldName(String edgeTagQueryEntityFieldName) {
186 this.edgeTagQueryEntityFieldName = edgeTagQueryEntityFieldName;
189 public String getEdgeTagQueryEntityFieldValue() {
190 return edgeTagQueryEntityFieldValue;
193 public void setEdgeTagQueryEntityFieldValue(String edgeTagQueryEntityFieldValue) {
194 this.edgeTagQueryEntityFieldValue = edgeTagQueryEntityFieldValue;
197 public String getSearchTagIDs() {
201 public void setSearchTagIDs(String searchTagIDs) {
202 this.searchTagIDs = searchTagIDs;
205 public String getSearchTags() {
209 public void setSearchTags(String searchTags) {
210 this.searchTags = searchTags;
214 public String getVersion() {
219 public String getContentInJson() {
221 return new JSONObject()
222 .put("entityType", entityType)
223 .put("edgeTagQueryEntityFieldName", edgeTagQueryEntityFieldName)
224 .put("edgeTagQueryEntityFieldValue", edgeTagQueryEntityFieldValue)
225 .put("searchTagIDs", searchTagIDs)
226 .put("searchTags", searchTags).toString();
227 } catch (JSONException e) {
228 // TODO Auto-generated catch block