e68534c972a114f8824db7be9847b42fc5908533
[aai/search-data-service.git] / src / test / java / org / onap / aai / sa / searchdbabstraction / elasticsearch / dao / ElasticSearchHttpControllerTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.aai.sa.searchdbabstraction.elasticsearch.dao;
22
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.entity.OperationResult;
31
32 import java.util.Properties;
33
34 @Ignore("All tests in this classes require an Elasticsearch instance to run locally")
35 public class ElasticSearchHttpControllerTest {
36
37   private static ElasticSearchHttpController elasticSearch;
38   private static AAIEntityTestObject testDocument;
39
40   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}";
41   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}";
42
43   @Before
44   public void setUp() throws Exception {
45     Properties properties = new Properties();
46     properties.put(ElasticSearchConfig.ES_IP_ADDRESS, "127.0.0.1");
47     properties.put(ElasticSearchConfig.ES_HTTP_PORT, "9200");
48 //    ElasticSearchConfig config = new ElasticSearchConfig(properties);
49     ElasticSearchConfig config = new ElasticSearchConfig(properties);
50     elasticSearch = new ElasticSearchHttpController(config);
51
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");
59
60   }
61 //
62   @Test
63   public void testCreateTable() throws Exception {
64     OperationResult result = elasticSearch.createTable("test", "aai-entities", indexSettings, indexMappings);
65     System.out.println(result);
66   }
67
68   @Test
69   public void testsuggestionQueryWithPayload() throws Exception {
70     Assert.assertNotNull(elasticSearch.suggestionQueryWithPayload("autoSuggest", "suggest-index"));
71   }
72 //
73   @Test
74   public void testCreateDocument() throws Exception {
75     OperationResult result = elasticSearch.createDocument("test", testDocument, false);
76     System.out.println(result);
77
78     DocumentStoreDataEntityImpl ds = new DocumentStoreDataEntityImpl();
79     ds.setId(testDocument.getId());
80
81     result = elasticSearch.getDocument("test", ds);
82     System.out.println(result);
83   }
84
85   @Test
86   public void testUpdateDocument() throws Exception {
87     testDocument.setEdgeTagQueryEntityFieldValue("567890");
88
89     OperationResult result = elasticSearch.updateDocument("test", testDocument, false);
90     System.out.println(result);
91
92     result = elasticSearch.getDocument("test", testDocument);
93     System.out.println(result);
94   }
95
96   @Test
97   public void testDeleteDocument() throws Exception {
98     OperationResult result = elasticSearch.deleteDocument("test", testDocument);
99     System.out.println(result);
100
101     result = elasticSearch.getDocument("test", testDocument);
102     System.out.println(result);
103   }
104
105   @Test
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");
115
116       OperationResult result = elasticSearch.createDocument("test", doc, false);
117       System.out.println(result);
118     }
119   }
120
121   @Test
122   public void serchByEntityType() throws Exception {
123     OperationResult result = elasticSearch.search("test", "q=instance");
124     System.out.println(result);
125   }
126
127   @Test
128   public void serchByTagIDs() throws Exception {
129     OperationResult result = elasticSearch.search("test", "q=9");
130     System.out.println(result);
131   }
132
133   @Test
134   public void serchByTags() throws Exception {
135     OperationResult result = elasticSearch.search("test", "q=service");
136     System.out.println(result);
137   }
138
139   @Test
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");
147
148     OperationResult result = elasticSearch.createDocument("test", doc, false);
149     System.out.println(result);
150   }
151
152   @Test
153   public void testDeleteIndex() throws Exception {
154     OperationResult result = elasticSearch.deleteIndex("test");
155     System.out.println(result);
156   }
157 //
158   class AAIEntityTestObject implements DocumentStoreDataEntity {
159     private String id;
160     private String entityType;
161     private String edgeTagQueryEntityFieldName;
162     private String edgeTagQueryEntityFieldValue;
163     private String searchTagIDs;
164     private String searchTags;
165
166     public void setId(String id) {
167       this.id = id;
168     }
169
170     @Override
171     public String getId() {
172       return this.id;
173     }
174
175     public String getEntityType() {
176       return entityType;
177     }
178
179     public void setEntityType(String entityType) {
180       this.entityType = entityType;
181     }
182
183     public String getEdgeTagQueryEntityFieldName() {
184       return edgeTagQueryEntityFieldName;
185     }
186
187     public void setEdgeTagQueryEntityFieldName(String edgeTagQueryEntityFieldName) {
188       this.edgeTagQueryEntityFieldName = edgeTagQueryEntityFieldName;
189     }
190
191     public String getEdgeTagQueryEntityFieldValue() {
192       return edgeTagQueryEntityFieldValue;
193     }
194
195     public void setEdgeTagQueryEntityFieldValue(String edgeTagQueryEntityFieldValue) {
196       this.edgeTagQueryEntityFieldValue = edgeTagQueryEntityFieldValue;
197     }
198
199     public String getSearchTagIDs() {
200       return searchTagIDs;
201     }
202
203     public void setSearchTagIDs(String searchTagIDs) {
204       this.searchTagIDs = searchTagIDs;
205     }
206
207     public String getSearchTags() {
208       return searchTags;
209     }
210
211     public void setSearchTags(String searchTags) {
212       this.searchTags = searchTags;
213     }
214
215     @Override
216     public String getVersion() {
217       return "1";
218     }
219
220     @Override
221     public String getContentInJson() {
222       try {
223         return new JSONObject()
224             .put("entityType", entityType)
225             .put("edgeTagQueryEntityFieldName", edgeTagQueryEntityFieldName)
226             .put("edgeTagQueryEntityFieldValue", edgeTagQueryEntityFieldValue)
227             .put("searchTagIDs", searchTagIDs)
228             .put("searchTags", searchTags).toString();
229       } catch (JSONException e) {
230         // TODO Auto-generated catch block
231         e.printStackTrace();
232         return null;
233       }
234     }
235
236   }
237
238 }