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.Before;
26 import org.junit.Ignore;
27 import org.junit.Test;
28 import org.onap.aai.sa.searchdbabstraction.elasticsearch.config.ElasticSearchConfig;
29 import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreDataEntity;
30 import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreDataEntityImpl;
31 import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.ElasticSearchHttpController;
32 import org.onap.aai.sa.searchdbabstraction.entity.OperationResult;
34 import java.util.Properties;
36 @Ignore("All tests in this classes require an Elasticsearch instance to run locally")
37 public class ElasticSearchHttpControllerTest {
39 private static ElasticSearchHttpController elasticSearch;
40 private static AAIEntityTestObject testDocument;
42 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}";
43 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}";
46 public void setUp() throws Exception {
47 Properties properties = new Properties();
48 properties.put(ElasticSearchConfig.ES_IP_ADDRESS, "127.0.0.1");
49 properties.put(ElasticSearchConfig.ES_HTTP_PORT, "9200");
50 ElasticSearchConfig config = new ElasticSearchConfig(properties);
51 elasticSearch = new ElasticSearchHttpController(config);
53 testDocument = new AAIEntityTestObject();
54 testDocument.setId("test123");
55 testDocument.setEntityType("service-instance");
56 testDocument.setEdgeTagQueryEntityFieldName("service-instance.service-instance-id");
57 testDocument.setEdgeTagQueryEntityFieldValue("123456");
58 testDocument.setSearchTagIDs("0");
59 testDocument.setSearchTags("service-instance-id");
64 public void testCreateTable() throws Exception {
65 OperationResult result = elasticSearch.createTable("test", "aai-entities", indexSettings, indexMappings);
66 System.out.println(result);
70 public void testCreateDocument() throws Exception {
71 OperationResult result = elasticSearch.createDocument("test", testDocument, false);
72 System.out.println(result);
74 DocumentStoreDataEntityImpl ds = new DocumentStoreDataEntityImpl();
75 ds.setId(testDocument.getId());
77 result = elasticSearch.getDocument("test", ds);
78 System.out.println(result);
82 public void testUpdateDocument() throws Exception {
83 testDocument.setEdgeTagQueryEntityFieldValue("567890");
85 OperationResult result = elasticSearch.updateDocument("test", testDocument, false);
86 System.out.println(result);
88 result = elasticSearch.getDocument("test", testDocument);
89 System.out.println(result);
93 public void testDeleteDocument() throws Exception {
94 OperationResult result = elasticSearch.deleteDocument("test", testDocument);
95 System.out.println(result);
97 result = elasticSearch.getDocument("test", testDocument);
98 System.out.println(result);
102 public void testBulkCreateDocuments() throws Exception {
103 for (int i = 0; i < 10; i++) {
104 AAIEntityTestObject doc = new AAIEntityTestObject();
105 doc.setId("test-" + i);
106 doc.setEntityType("service-instance");
107 doc.setEdgeTagQueryEntityFieldName("service-instance.service-instance-id");
108 doc.setEdgeTagQueryEntityFieldValue("123456" + i);
109 doc.setSearchTagIDs("" + i);
110 doc.setSearchTags("service-instance-id");
112 OperationResult result = elasticSearch.createDocument("test", doc, false);
113 System.out.println(result);
118 public void serchByEntityType() throws Exception {
119 OperationResult result = elasticSearch.search("test", "q=instance");
120 System.out.println(result);
124 public void serchByTagIDs() throws Exception {
125 OperationResult result = elasticSearch.search("test", "q=9");
126 System.out.println(result);
130 public void serchByTags() throws Exception {
131 OperationResult result = elasticSearch.search("test", "q=service");
132 System.out.println(result);
136 public void testCreateDocumentWithoutId() throws Exception {
137 AAIEntityTestObject doc = new AAIEntityTestObject();
138 doc.setEntityType("service-instance");
139 doc.setEdgeTagQueryEntityFieldName("service-instance.service-instance-id");
140 doc.setEdgeTagQueryEntityFieldValue("1111111");
141 doc.setSearchTagIDs("321");
142 doc.setSearchTags("service-instance-id");
144 OperationResult result = elasticSearch.createDocument("test", doc, false);
145 System.out.println(result);
149 public void testDeleteIndex() throws Exception {
150 OperationResult result = elasticSearch.deleteIndex("test");
151 System.out.println(result);
154 class AAIEntityTestObject implements DocumentStoreDataEntity {
156 private String entityType;
157 private String edgeTagQueryEntityFieldName;
158 private String edgeTagQueryEntityFieldValue;
159 private String searchTagIDs;
160 private String searchTags;
162 public void setId(String id) {
167 public String getId() {
171 public String getEntityType() {
175 public void setEntityType(String entityType) {
176 this.entityType = entityType;
179 public String getEdgeTagQueryEntityFieldName() {
180 return edgeTagQueryEntityFieldName;
183 public void setEdgeTagQueryEntityFieldName(String edgeTagQueryEntityFieldName) {
184 this.edgeTagQueryEntityFieldName = edgeTagQueryEntityFieldName;
187 public String getEdgeTagQueryEntityFieldValue() {
188 return edgeTagQueryEntityFieldValue;
191 public void setEdgeTagQueryEntityFieldValue(String edgeTagQueryEntityFieldValue) {
192 this.edgeTagQueryEntityFieldValue = edgeTagQueryEntityFieldValue;
195 public String getSearchTagIDs() {
199 public void setSearchTagIDs(String searchTagIDs) {
200 this.searchTagIDs = searchTagIDs;
203 public String getSearchTags() {
207 public void setSearchTags(String searchTags) {
208 this.searchTags = searchTags;
212 public String getVersion() {
217 public String getContentInJson() {
219 return new JSONObject()
220 .put("entityType", entityType)
221 .put("edgeTagQueryEntityFieldName", edgeTagQueryEntityFieldName)
222 .put("edgeTagQueryEntityFieldValue", edgeTagQueryEntityFieldValue)
223 .put("searchTagIDs", searchTagIDs)
224 .put("searchTags", searchTags).toString();
225 } catch (JSONException e) {
226 // TODO Auto-generated catch block