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