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