b921b32b16dc450bd8d84c3af3b24fe97bef44a6
[aai/search-data-service.git] / src / test / java / org / openecomp / sa / searchdbabstraction / elasticsearch / dao / ElasticSearchHttpControllerTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.openecomp.sa.searchdbabstraction.elasticsearch.dao;
24
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;
32
33 import java.util.Properties;
34
35 @Ignore("All tests in this classes require an Elasticsearch instance to run locally")
36 public class ElasticSearchHttpControllerTest {
37
38   private static ElasticSearchHttpController elasticSearch;
39   private static AAIEntityTestObject testDocument;
40
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}";
43
44   @Before
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);
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 testCreateDocument() throws Exception {
70     OperationResult result = elasticSearch.createDocument("test", testDocument, false);
71     System.out.println(result);
72
73     DocumentStoreDataEntityImpl ds = new DocumentStoreDataEntityImpl();
74     ds.setId(testDocument.getId());
75
76     result = elasticSearch.getDocument("test", ds);
77     System.out.println(result);
78   }
79
80   @Test
81   public void testUpdateDocument() throws Exception {
82     testDocument.setEdgeTagQueryEntityFieldValue("567890");
83
84     OperationResult result = elasticSearch.updateDocument("test", testDocument, false);
85     System.out.println(result);
86
87     result = elasticSearch.getDocument("test", testDocument);
88     System.out.println(result);
89   }
90
91   @Test
92   public void testDeleteDocument() throws Exception {
93     OperationResult result = elasticSearch.deleteDocument("test", testDocument);
94     System.out.println(result);
95
96     result = elasticSearch.getDocument("test", testDocument);
97     System.out.println(result);
98   }
99
100   @Test
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");
110
111       OperationResult result = elasticSearch.createDocument("test", doc, false);
112       System.out.println(result);
113     }
114   }
115
116   @Test
117   public void serchByEntityType() throws Exception {
118     OperationResult result = elasticSearch.search("test", "q=instance");
119     System.out.println(result);
120   }
121
122   @Test
123   public void serchByTagIDs() throws Exception {
124     OperationResult result = elasticSearch.search("test", "q=9");
125     System.out.println(result);
126   }
127
128   @Test
129   public void serchByTags() throws Exception {
130     OperationResult result = elasticSearch.search("test", "q=service");
131     System.out.println(result);
132   }
133
134   @Test
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");
142
143     OperationResult result = elasticSearch.createDocument("test", doc, false);
144     System.out.println(result);
145   }
146
147   @Test
148   public void testDeleteIndex() throws Exception {
149     OperationResult result = elasticSearch.deleteIndex("test");
150     System.out.println(result);
151   }
152
153   class AAIEntityTestObject implements DocumentStoreDataEntity {
154     private String id;
155     private String entityType;
156     private String edgeTagQueryEntityFieldName;
157     private String edgeTagQueryEntityFieldValue;
158     private String searchTagIDs;
159     private String searchTags;
160
161     public void setId(String id) {
162       this.id = id;
163     }
164
165     @Override
166     public String getId() {
167       return this.id;
168     }
169
170     public String getEntityType() {
171       return entityType;
172     }
173
174     public void setEntityType(String entityType) {
175       this.entityType = entityType;
176     }
177
178     public String getEdgeTagQueryEntityFieldName() {
179       return edgeTagQueryEntityFieldName;
180     }
181
182     public void setEdgeTagQueryEntityFieldName(String edgeTagQueryEntityFieldName) {
183       this.edgeTagQueryEntityFieldName = edgeTagQueryEntityFieldName;
184     }
185
186     public String getEdgeTagQueryEntityFieldValue() {
187       return edgeTagQueryEntityFieldValue;
188     }
189
190     public void setEdgeTagQueryEntityFieldValue(String edgeTagQueryEntityFieldValue) {
191       this.edgeTagQueryEntityFieldValue = edgeTagQueryEntityFieldValue;
192     }
193
194     public String getSearchTagIDs() {
195       return searchTagIDs;
196     }
197
198     public void setSearchTagIDs(String searchTagIDs) {
199       this.searchTagIDs = searchTagIDs;
200     }
201
202     public String getSearchTags() {
203       return searchTags;
204     }
205
206     public void setSearchTags(String searchTags) {
207       this.searchTags = searchTags;
208     }
209
210     @Override
211     public String getVersion() {
212       return "1";
213     }
214
215     @Override
216     public String getContentInJson() {
217       try {
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
226         e.printStackTrace();
227         return null;
228       }
229     }
230
231   }
232
233 }