10e7d2c51ceb9ef61de47888ab7619215e074348
[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.elasticsearch.dao.DocumentStoreDataEntity;
31 import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreDataEntityImpl;
32 import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.ElasticSearchHttpController;
33 import org.onap.aai.sa.searchdbabstraction.entity.OperationResult;
34
35 import java.util.Properties;
36
37 @Ignore("All tests in this classes require an Elasticsearch instance to run locally")
38 public class ElasticSearchHttpControllerTest {
39
40   private static ElasticSearchHttpController elasticSearch;
41   private static AAIEntityTestObject testDocument;
42
43   private static final String indexMappings =
44       "{\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 =
46       "{\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
48   @Before
49   public void setUp() throws Exception {
50     Properties properties = new Properties();
51     properties.put(ElasticSearchConfig.ES_IP_ADDRESS, "127.0.0.1");
52     properties.put(ElasticSearchConfig.ES_HTTP_PORT, "9200");
53     ElasticSearchConfig config = new ElasticSearchConfig(properties);
54     elasticSearch = new ElasticSearchHttpController(config);
55
56     testDocument = new AAIEntityTestObject();
57     testDocument.setId("test123");
58     testDocument.setEntityType("service-instance");
59     testDocument.setEdgeTagQueryEntityFieldName("service-instance.service-instance-id");
60     testDocument.setEdgeTagQueryEntityFieldValue("123456");
61     testDocument.setSearchTagIDs("0");
62     testDocument.setSearchTags("service-instance-id");
63
64   }
65
66   @Test
67   public void testCreateTable() throws Exception {
68     OperationResult result =
69         elasticSearch.createTable("test", "aai-entities", indexSettings, indexMappings);
70     System.out.println(result);
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 testsuggestionQueryWithPayload() throws Exception {
154
155     Assert.assertNotNull(elasticSearch.suggestionQueryWithPayload("autoSuggest", "suggest-index"));
156
157   }
158
159   @Test
160   public void testDeleteIndex() throws Exception {
161     OperationResult result = elasticSearch.deleteIndex("test");
162     System.out.println(result);
163   }
164
165   class AAIEntityTestObject implements DocumentStoreDataEntity {
166     private String id;
167     private String entityType;
168     private String edgeTagQueryEntityFieldName;
169     private String edgeTagQueryEntityFieldValue;
170     private String searchTagIDs;
171     private String searchTags;
172
173     public void setId(String id) {
174       this.id = id;
175     }
176
177     @Override
178     public String getId() {
179       return this.id;
180     }
181
182     public String getEntityType() {
183       return entityType;
184     }
185
186     public void setEntityType(String entityType) {
187       this.entityType = entityType;
188     }
189
190     public String getEdgeTagQueryEntityFieldName() {
191       return edgeTagQueryEntityFieldName;
192     }
193
194     public void setEdgeTagQueryEntityFieldName(String edgeTagQueryEntityFieldName) {
195       this.edgeTagQueryEntityFieldName = edgeTagQueryEntityFieldName;
196     }
197
198     public String getEdgeTagQueryEntityFieldValue() {
199       return edgeTagQueryEntityFieldValue;
200     }
201
202     public void setEdgeTagQueryEntityFieldValue(String edgeTagQueryEntityFieldValue) {
203       this.edgeTagQueryEntityFieldValue = edgeTagQueryEntityFieldValue;
204     }
205
206     public String getSearchTagIDs() {
207       return searchTagIDs;
208     }
209
210     public void setSearchTagIDs(String searchTagIDs) {
211       this.searchTagIDs = searchTagIDs;
212     }
213
214     public String getSearchTags() {
215       return searchTags;
216     }
217
218     public void setSearchTags(String searchTags) {
219       this.searchTags = searchTags;
220     }
221
222     @Override
223     public String getVersion() {
224       return "1";
225     }
226
227     @Override
228     public String getContentInJson() {
229       try {
230         return new JSONObject().put("entityType", entityType)
231             .put("edgeTagQueryEntityFieldName", edgeTagQueryEntityFieldName)
232             .put("edgeTagQueryEntityFieldValue", edgeTagQueryEntityFieldValue)
233             .put("searchTagIDs", searchTagIDs).put("searchTags", searchTags).toString();
234       } catch (JSONException e) {
235         // TODO Auto-generated catch block
236         e.printStackTrace();
237         return null;
238       }
239     }
240
241   }
242
243 }