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