Fix file formatting issues
[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 static org.hamcrest.CoreMatchers.anyOf;
24 import static org.hamcrest.CoreMatchers.containsString;
25 import static org.hamcrest.CoreMatchers.either;
26 import static org.hamcrest.CoreMatchers.equalTo;
27 import static org.hamcrest.CoreMatchers.is;
28 import static org.hamcrest.CoreMatchers.not;
29 import static org.junit.Assert.assertThat;
30
31 import java.util.Properties;
32 import org.eclipse.jetty.util.security.Password;
33 import org.json.JSONObject;
34 import org.junit.Before;
35 import org.junit.Ignore;
36 import org.junit.Test;
37 import org.onap.aai.sa.rest.DocumentSchema;
38 import org.onap.aai.sa.searchdbabstraction.elasticsearch.config.ElasticSearchConfig;
39 import org.onap.aai.sa.searchdbabstraction.entity.OperationResult;
40
41 @Ignore("All tests in this classes require an Elasticsearch instance to run locally")
42 public class ElasticSearchHttpControllerTest {
43
44     static {
45         // Set the location of the payload translation JSON file.
46         System.setProperty("CONFIG_HOME", "src/test/resources/json");
47     }
48
49     private static ElasticSearchHttpController elasticSearch;
50     private static AAIEntityTestObject testDocument;
51
52     private static final String TEST_INDEX_NAME = "test";
53
54     private static final String indexMappings =
55             "{\r\n    \"properties\": {\r\n        \"entityType\": {\r\n            \"type\": \"text\"\r\n        },\r\n"
56                     + "        \"edgeTagQueryEntityFieldName\": {\r\n            \"type\": \"text\",\r\n            \"index\": \"false\"\r\n        },\r\n"
57                     + "        \"edgeTagQueryEntityFieldValue\": {\r\n            \"type\": \"text\",\r\n            \"index\": \"false\"\r\n        },\r\n        \"searchTagIDs\" : {\r\n            \"type\" : \"text\"\r\n          },\r\n        \"searchTags\": {\r\n            \"type\": \"text\",\r\n            \"analyzer\": \"nGram_analyzer\",\r\n            \"search_analyzer\": \"whitespace_analyzer\"\r\n        }\r\n    }\r\n}";
58     private static final String indexSettings =
59             "{\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}";
60
61     @Before
62     public void setUp() throws Exception {
63         Properties properties = new Properties();
64         properties.put(ElasticSearchConfig.ES_IP_ADDRESS, "127.0.0.1");
65         properties.put(ElasticSearchConfig.ES_HTTP_PORT, "9200");
66         properties.put(ElasticSearchConfig.ES_URI_SCHEME, "http");
67         properties.put(ElasticSearchConfig.ES_AUTH_USER, "your_user_here");
68         properties.put(ElasticSearchConfig.ES_AUTH_ENC, Password.obfuscate("your_password_here"));
69         elasticSearch = new ElasticSearchHttpController(new ElasticSearchConfig(properties));
70
71         testDocument = new AAIEntityTestObject();
72         testDocument.setId("test123");
73         testDocument.setEntityType("service-instance");
74         testDocument.setEdgeTagQueryEntityFieldName("service-instance.service-instance-id");
75         testDocument.setEdgeTagQueryEntityFieldValue("123456");
76         testDocument.setSearchTagIDs("0");
77         testDocument.setSearchTags("service-instance-id");
78     }
79
80     @Test
81     public void testGetInstance() throws Exception {
82         ElasticSearchHttpController.getInstance();
83     }
84
85     @Test
86     public void testCreateIndex() throws Exception {
87         testDeleteIndex();
88         OperationResult result = elasticSearch.createIndex(TEST_INDEX_NAME, new DocumentSchema());
89         assertThat(result.getResult(), containsString("/search/indexes/test"));
90         assertThat(result.getResultCode(), is(201));
91
92         result = elasticSearch.createIndex(TEST_INDEX_NAME, new DocumentSchema());
93         assertThat(result.getResult(), containsString("already exists"));
94         assertThat(result.getResultCode(), is(400));
95     }
96
97     @Test(expected = IllegalArgumentException.class)
98     public void testCreateDynamicIndexEmptySchema() throws Exception {
99         elasticSearch.createDynamicIndex(TEST_INDEX_NAME, "");
100     }
101
102     @Test
103     public void testCreateDynamicIndex() throws Exception {
104         String indexName = "test_dynamic";
105         elasticSearch.deleteIndex(indexName);
106
107         OperationResult result = elasticSearch.createDynamicIndex(indexName,
108                 "{\"mappings\":{\"_doc\":{\"dynamic_templates\":[{\"strings_as_text\":{\"match_mapping_type\":\"string\",\"mapping\":{\"type\":\"text\"}}}]}}}");
109         assertThat(result.getResult(), containsString("/search/indexes/test"));
110         assertThat(result.getResultCode(), is(201));
111
112         elasticSearch.deleteIndex(indexName);
113     }
114
115     @Test
116     public void testCreateTable() throws Exception {
117         OperationResult result =
118                 elasticSearch.createTable(TEST_INDEX_NAME, "aai-entities", indexSettings, indexMappings);
119         assertThat(result.getResult(), containsString("\"index\":\"test\"}"));
120         assertThat(result.getResultCode(), either(is(200)).or(is(400)));
121     }
122
123     @Test
124     public void testCreateDocument() throws Exception {
125         OperationResult result = elasticSearch.createDocument(TEST_INDEX_NAME, testDocument, false);
126         assertThat(result.getResult(), not(equalTo("")));
127
128         DocumentStoreDataEntityImpl ds = new DocumentStoreDataEntityImpl();
129         ds.setId(testDocument.getId());
130
131         result = elasticSearch.getDocument(TEST_INDEX_NAME, ds);
132         assertThat(result.getResult(), not(equalTo("")));
133     }
134
135     @Test
136     public void testCreateDocumentInvalidIndex() throws Exception {
137         OperationResult result = elasticSearch.createDocument("index_does_not_exist", testDocument, false);
138         assertThat(result.getResultCode(), is(404));
139         assertThat(result.getResult(), not(equalTo("")));
140     }
141
142     @Test
143     public void testUpdateDocument() throws Exception {
144         testDocument.setEdgeTagQueryEntityFieldValue("567890");
145
146         OperationResult result = elasticSearch.getDocument(TEST_INDEX_NAME, testDocument);
147         if (result.getResultCode() == 404) {
148             testCreateDocument();
149         }
150         // assertThat(result.getResultCode(), anyOf(equalTo(200), equalTo(412)));
151         // assertThat(result.getResult(), containsString("\"found\":true"));
152
153         result = elasticSearch.updateDocument(TEST_INDEX_NAME, testDocument, false);
154         assertThat(result.getResultCode(), anyOf(equalTo(200), equalTo(412)));
155
156         result = elasticSearch.getDocument(TEST_INDEX_NAME, testDocument);
157         assertThat(result.getResult(), containsString("test123"));
158     }
159
160     @Test
161     public void testDeleteDocument() throws Exception {
162         OperationResult result = elasticSearch.getDocument(TEST_INDEX_NAME, testDocument);
163         if (result.getResultCode() == 404) {
164             testCreateDocument();
165         }
166
167         result = elasticSearch.deleteDocument(TEST_INDEX_NAME, testDocument);
168         assertThat(result.getResult(), containsString(TEST_INDEX_NAME));
169
170         result = elasticSearch.getDocument(TEST_INDEX_NAME, testDocument);
171         assertThat(result.getResult(), containsString("test123"));
172     }
173
174     @Test
175     public void testBulkCreateDocuments() throws Exception {
176         for (int i = 0; i < 10; i++) {
177             AAIEntityTestObject doc = new AAIEntityTestObject();
178             doc.setId("test-" + i);
179             doc.setEntityType("service-instance");
180             doc.setEdgeTagQueryEntityFieldName("service-instance.service-instance-id");
181             doc.setEdgeTagQueryEntityFieldValue("123456" + i);
182             doc.setSearchTagIDs("" + i);
183             doc.setSearchTags("service-instance-id");
184
185             OperationResult result = elasticSearch.createDocument(TEST_INDEX_NAME, doc, false);
186             assertThat(result.getResultCode(), anyOf(equalTo(201), equalTo(400)));
187         }
188     }
189
190     @Test
191     public void serchByEntityType() throws Exception {
192         OperationResult result = elasticSearch.search(TEST_INDEX_NAME, "q=instance");
193         assertThat(result.getResult(), not(equalTo("")));
194     }
195
196     @Test
197     public void serchByTagIDs() throws Exception {
198         OperationResult result = elasticSearch.search(TEST_INDEX_NAME, "q=9");
199         assertThat(result.getResult(), not(equalTo("")));
200     }
201
202     @Test
203     public void serchByTags() throws Exception {
204         OperationResult result = elasticSearch.search(TEST_INDEX_NAME, "q=service");
205         assertThat(result.getResult(), not(equalTo("")));
206     }
207
208     @Test
209     public void searchWithPayload() throws Exception {
210         testCreateIndex();
211         OperationResult result =
212                 elasticSearch.searchWithPayload(TEST_INDEX_NAME, "{\"query\":{\"term\":{\"user\":\"fred\"}}}");
213         assertThat(result.getResult(), containsString("successful"));
214         assertThat(result.getResultCode(), is(equalTo(200)));
215     }
216
217     /**
218      * The _suggest endpoint appears to be deprecated in ES 5.x and above.
219      */
220     @Test
221     public void suggestionQueryWithPayload() throws Exception {
222         testCreateIndex();
223         OperationResult result = elasticSearch.suggestionQueryWithPayload(TEST_INDEX_NAME,
224                 "{\"my-suggestion\":{\"text\":\"fred\",\"term\":{\"field\":\"body\"}}}");
225         assertThat(result.getResult(), containsString("error"));
226         assertThat(result.getResultCode(), is(equalTo(400)));
227     }
228
229     @Test
230     public void testCreateDocumentWithoutId() throws Exception {
231         AAIEntityTestObject doc = new AAIEntityTestObject();
232         doc.setEntityType("service-instance");
233         doc.setEdgeTagQueryEntityFieldName("service-instance.service-instance-id");
234         doc.setEdgeTagQueryEntityFieldValue("1111111");
235         doc.setSearchTagIDs("321");
236         doc.setSearchTags("service-instance-id");
237
238         OperationResult result = elasticSearch.createDocument(TEST_INDEX_NAME, doc, false);
239         assertThat(result.getResult(), not(equalTo("")));
240     }
241
242     @Test
243     public void testDeleteIndex() throws Exception {
244         OperationResult result = elasticSearch.deleteIndex(TEST_INDEX_NAME);
245         assertThat(result.getResultCode(), anyOf(equalTo(200), equalTo(404)));
246         assertThat(result.getResult(), not(equalTo("")));
247     }
248
249     class AAIEntityTestObject implements DocumentStoreDataEntity {
250         private String id;
251         private String entityType;
252         private String edgeTagQueryEntityFieldName;
253         private String edgeTagQueryEntityFieldValue;
254         private String searchTagIDs;
255         private String searchTags;
256
257         public void setId(String id) {
258             this.id = id;
259         }
260
261         @Override
262         public String getId() {
263             return this.id;
264         }
265
266         public String getEntityType() {
267             return entityType;
268         }
269
270         public void setEntityType(String entityType) {
271             this.entityType = entityType;
272         }
273
274         public String getEdgeTagQueryEntityFieldName() {
275             return edgeTagQueryEntityFieldName;
276         }
277
278         public void setEdgeTagQueryEntityFieldName(String edgeTagQueryEntityFieldName) {
279             this.edgeTagQueryEntityFieldName = edgeTagQueryEntityFieldName;
280         }
281
282         public String getEdgeTagQueryEntityFieldValue() {
283             return edgeTagQueryEntityFieldValue;
284         }
285
286         public void setEdgeTagQueryEntityFieldValue(String edgeTagQueryEntityFieldValue) {
287             this.edgeTagQueryEntityFieldValue = edgeTagQueryEntityFieldValue;
288         }
289
290         public String getSearchTagIDs() {
291             return searchTagIDs;
292         }
293
294         public void setSearchTagIDs(String searchTagIDs) {
295             this.searchTagIDs = searchTagIDs;
296         }
297
298         public String getSearchTags() {
299             return searchTags;
300         }
301
302         public void setSearchTags(String searchTags) {
303             this.searchTags = searchTags;
304         }
305
306         @Override
307         public String getVersion() {
308             return "1";
309         }
310
311         @Override
312         public String getContentInJson() {
313             return new JSONObject(). //
314                     put("entityType", entityType) //
315                     .put("edgeTagQueryEntityFieldName", edgeTagQueryEntityFieldName)
316                     .put("edgeTagQueryEntityFieldValue", edgeTagQueryEntityFieldValue) //
317                     .put("searchTagIDs", searchTagIDs) //
318                     .put("searchTags", searchTags) //
319                     .toString();
320         }
321     }
322
323 }