Reject doc create requests if index does not exist
[aai/search-data-service.git] / src / test / java / org / openecomp / sa / searchdbabstraction / elasticsearch / dao / ElasticSearchHttpControllerTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * Search Data Service
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License ati
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.openecomp.sa.searchdbabstraction.elasticsearch.dao;
26
27 import org.json.JSONException;
28 import org.json.JSONObject;
29 import org.junit.Before;
30 import org.junit.Ignore;
31 import org.junit.Test;
32 import org.openecomp.sa.searchdbabstraction.elasticsearch.config.ElasticSearchConfig;
33 import org.openecomp.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 = "{\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}";
44   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}";
45
46   @Before
47   public void setUp() throws Exception {
48     Properties properties = new Properties();
49     properties.put(ElasticSearchConfig.ES_IP_ADDRESS, "127.0.0.1");
50     properties.put(ElasticSearchConfig.ES_HTTP_PORT, "9200");
51     ElasticSearchConfig config = new ElasticSearchConfig(properties);
52     elasticSearch = new ElasticSearchHttpController(config);
53
54     testDocument = new AAIEntityTestObject();
55     testDocument.setId("test123");
56     testDocument.setEntityType("service-instance");
57     testDocument.setEdgeTagQueryEntityFieldName("service-instance.service-instance-id");
58     testDocument.setEdgeTagQueryEntityFieldValue("123456");
59     testDocument.setSearchTagIDs("0");
60     testDocument.setSearchTags("service-instance-id");
61
62   }
63
64   @Test
65   public void testCreateTable() throws Exception {
66     OperationResult result = elasticSearch.createTable("test", "aai-entities", indexSettings, indexMappings);
67     System.out.println(result);
68   }
69
70   @Test
71   public void testCreateDocument() throws Exception {
72     OperationResult result = elasticSearch.createDocument("test", testDocument, false);
73     System.out.println(result);
74
75     DocumentStoreDataEntityImpl ds = new DocumentStoreDataEntityImpl();
76     ds.setId(testDocument.getId());
77
78     result = elasticSearch.getDocument("test", ds);
79     System.out.println(result);
80   }
81
82   @Test
83   public void testUpdateDocument() throws Exception {
84     testDocument.setEdgeTagQueryEntityFieldValue("567890");
85
86     OperationResult result = elasticSearch.updateDocument("test", testDocument, false);
87     System.out.println(result);
88
89     result = elasticSearch.getDocument("test", testDocument);
90     System.out.println(result);
91   }
92
93   @Test
94   public void testDeleteDocument() throws Exception {
95     OperationResult result = elasticSearch.deleteDocument("test", testDocument);
96     System.out.println(result);
97
98     result = elasticSearch.getDocument("test", testDocument);
99     System.out.println(result);
100   }
101
102   @Test
103   public void testBulkCreateDocuments() throws Exception {
104     for (int i = 0; i < 10; i++) {
105       AAIEntityTestObject doc = new AAIEntityTestObject();
106       doc.setId("test-" + i);
107       doc.setEntityType("service-instance");
108       doc.setEdgeTagQueryEntityFieldName("service-instance.service-instance-id");
109       doc.setEdgeTagQueryEntityFieldValue("123456" + i);
110       doc.setSearchTagIDs("" + i);
111       doc.setSearchTags("service-instance-id");
112
113       OperationResult result = elasticSearch.createDocument("test", doc, false);
114       System.out.println(result);
115     }
116   }
117
118   @Test
119   public void serchByEntityType() throws Exception {
120     OperationResult result = elasticSearch.search("test", "q=instance");
121     System.out.println(result);
122   }
123
124   @Test
125   public void serchByTagIDs() throws Exception {
126     OperationResult result = elasticSearch.search("test", "q=9");
127     System.out.println(result);
128   }
129
130   @Test
131   public void serchByTags() throws Exception {
132     OperationResult result = elasticSearch.search("test", "q=service");
133     System.out.println(result);
134   }
135
136   @Test
137   public void testCreateDocumentWithoutId() throws Exception {
138     AAIEntityTestObject doc = new AAIEntityTestObject();
139     doc.setEntityType("service-instance");
140     doc.setEdgeTagQueryEntityFieldName("service-instance.service-instance-id");
141     doc.setEdgeTagQueryEntityFieldValue("1111111");
142     doc.setSearchTagIDs("321");
143     doc.setSearchTags("service-instance-id");
144
145     OperationResult result = elasticSearch.createDocument("test", doc, false);
146     System.out.println(result);
147   }
148
149   @Test
150   public void testDeleteIndex() throws Exception {
151     OperationResult result = elasticSearch.deleteIndex("test");
152     System.out.println(result);
153   }
154
155   class AAIEntityTestObject implements DocumentStoreDataEntity {
156     private String id;
157     private String entityType;
158     private String edgeTagQueryEntityFieldName;
159     private String edgeTagQueryEntityFieldValue;
160     private String searchTagIDs;
161     private String searchTags;
162
163     public void setId(String id) {
164       this.id = id;
165     }
166
167     @Override
168     public String getId() {
169       return this.id;
170     }
171
172     public String getEntityType() {
173       return entityType;
174     }
175
176     public void setEntityType(String entityType) {
177       this.entityType = entityType;
178     }
179
180     public String getEdgeTagQueryEntityFieldName() {
181       return edgeTagQueryEntityFieldName;
182     }
183
184     public void setEdgeTagQueryEntityFieldName(String edgeTagQueryEntityFieldName) {
185       this.edgeTagQueryEntityFieldName = edgeTagQueryEntityFieldName;
186     }
187
188     public String getEdgeTagQueryEntityFieldValue() {
189       return edgeTagQueryEntityFieldValue;
190     }
191
192     public void setEdgeTagQueryEntityFieldValue(String edgeTagQueryEntityFieldValue) {
193       this.edgeTagQueryEntityFieldValue = edgeTagQueryEntityFieldValue;
194     }
195
196     public String getSearchTagIDs() {
197       return searchTagIDs;
198     }
199
200     public void setSearchTagIDs(String searchTagIDs) {
201       this.searchTagIDs = searchTagIDs;
202     }
203
204     public String getSearchTags() {
205       return searchTags;
206     }
207
208     public void setSearchTags(String searchTags) {
209       this.searchTags = searchTags;
210     }
211
212     @Override
213     public String getVersion() {
214       return "1";
215     }
216
217     @Override
218     public String getContentInJson() {
219       try {
220         return new JSONObject()
221             .put("entityType", entityType)
222             .put("edgeTagQueryEntityFieldName", edgeTagQueryEntityFieldName)
223             .put("edgeTagQueryEntityFieldValue", edgeTagQueryEntityFieldValue)
224             .put("searchTagIDs", searchTagIDs)
225             .put("searchTags", searchTags).toString();
226       } catch (JSONException e) {
227         // TODO Auto-generated catch block
228         e.printStackTrace();
229         return null;
230       }
231     }
232
233   }
234
235 }