Initial search service commit
[aai/search-data-service.git] / src / test / java / org / openecomp / sa / searchdbabstraction / searchapi / QueryTest.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.searchapi;
26
27 import com.fasterxml.jackson.core.JsonParseException;
28 import com.fasterxml.jackson.databind.JsonMappingException;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import org.junit.Test;
31
32 import java.io.IOException;
33
34 import static org.junit.Assert.assertEquals;
35 import static org.junit.Assert.assertTrue;
36
37
38 public class QueryTest {
39
40   /**
41    * This test validates that we are able to marshal json structures
42    * representing term queries into POJOs and that we can then
43    * unmarshal those POJOs into ElasticSearch syntax.
44    *
45    * @throws JsonParseException
46    * @throws JsonMappingException
47    * @throws IOException
48    */
49   @Test
50   public void termQueryTest() throws JsonParseException, JsonMappingException, IOException {
51
52     Integer intValue = 1;
53     String field = "searchTags";
54     String termQueryWithIntegerValueJson = "{\"field\": \"" + field + "\", \"value\": " + intValue + "}";
55     String termQueryWithIntegerValueExpectedES = "{\"term\": {\"" + field + "\" : " + intValue + "}}";
56
57     Double doubleValue = 5.7;
58     String termQueryWithDoubleValueJson = "{\"field\": \"" + field + "\", \"value\": " + doubleValue + "}";
59     String termQueryWithDoubleValueExpectedES = "{\"term\": {\"" + field + "\" : " + doubleValue + "}}";
60
61     String stringValue = "theValue";
62     String termQueryWithStringValueJson = "{\"field\": \"" + field + "\", \"value\": \"" + stringValue + "\"}";
63     String termQueryWithStringValueExpectedES = "{\"term\": {\"" + field + "\" : \"" + stringValue + "\"}}";
64
65     ObjectMapper mapper = new ObjectMapper();
66
67
68     // Validate that we can marshal a term query where the supplied value
69     // is an Integer.
70     TermQuery integerTermQuery = mapper.readValue(termQueryWithIntegerValueJson, TermQuery.class);
71     assertTrue("Expected value to be of type Integer, but was type " + integerTermQuery.getValue().getClass().getName(),
72         integerTermQuery.getValue() instanceof Integer);
73     assertEquals(intValue, integerTermQuery.getValue());
74
75     assertTrue("ElasticSearch term query translation does not match the expected result",
76         termQueryWithIntegerValueExpectedES.equals(integerTermQuery.toElasticSearch()));
77
78     // Validate that we can marshal a term query where the supplied value
79     // is a Double.
80     TermQuery doubleTermQuery = mapper.readValue(termQueryWithDoubleValueJson, TermQuery.class);
81     assertTrue("Expected value to be of type Double, but was type " + doubleTermQuery.getValue().getClass().getName(),
82         doubleTermQuery.getValue() instanceof Double);
83     assertEquals(doubleValue, doubleTermQuery.getValue());
84     assertTrue("ElasticSearch term query translation does not match the expected result",
85         termQueryWithDoubleValueExpectedES.equals(doubleTermQuery.toElasticSearch()));
86
87     // Validate that we can marshal a term query where the supplied value
88     // is a String literal.
89     TermQuery stringTermQuery = mapper.readValue(termQueryWithStringValueJson, TermQuery.class);
90     assertTrue("Expected value to be of type String, but was type " + stringTermQuery.getValue().getClass().getName(),
91         stringTermQuery.getValue() instanceof String);
92     assertEquals(stringValue, stringTermQuery.getValue());
93     assertTrue("ElasticSearch term query translation does not match the expected result",
94         termQueryWithStringValueExpectedES.equals(stringTermQuery.toElasticSearch()));
95
96
97   }
98
99
100   /**
101    * This test validates that we are able to marshal json structures
102    * representing parsed queries into POJOs and that we can then
103    * unmarshal those POJOs into ElasticSearch syntax.
104    *
105    * @throws JsonParseException
106    * @throws JsonMappingException
107    * @throws IOException
108    */
109   @Test
110   public void parsedQueryTest() throws JsonParseException, JsonMappingException, IOException {
111
112     String field = "fieldname";
113     String queryString = "The query string";
114
115     String queryJson = "{\"field\": \"" + field + "\", \"query-string\": \"" + queryString + "\"}";
116     String queryExpectedES = "{\"query_string\": {\"default_field\": \"" + field + "\", \"query\": \"" + queryString + "\"}}";
117
118     ObjectMapper mapper = new ObjectMapper();
119     ParsedQuery pq = mapper.readValue(queryJson, ParsedQuery.class);
120
121     assertTrue("Unexpected marshalled value for 'field' - expected: " + field + " actual: " + pq.getField(),
122         field.equals(pq.getField()));
123     assertTrue("Unexpected marshalled value for 'query-string' - expected: " + queryString + " actual: " + pq.getQueryString(),
124         queryString.equals(pq.getQueryString()));
125     assertTrue("Unexpected ElasticSearch syntax.  Expected: " + queryExpectedES + " Actual: " + pq.toElasticSearch(),
126         queryExpectedES.equals(pq.toElasticSearch()));
127   }
128
129
130   /**
131    * This test validates that a ranged query cannot be parsed with values
132    * for both the 'gte' and 'gt' fields or the 'lte' and 'lt' fields, and
133    * that we do not allow mixing of numeric and date types in the same
134    * query.
135    *
136    * @throws JsonParseException
137    * @throws IOException
138    */
139   @Test
140   public void rangeQueryConflictingBoundsTest() throws JsonParseException, IOException {
141
142     String invalidGTAndGTE = "{ \"field\": \"timestamp\", \"gte\": \"2016-10-06T00:00:00.558+03:00\", \"gt\": \"2016-10-06T23:59:59.558+03:00\"}";
143     String invalidLTAndLTE = "{ \"field\": \"timestamp\", \"lte\": \"2016-10-06T00:00:00.558+03:00\", \"lt\": \"2016-10-06T23:59:59.558+03:00\"}";
144     String invalidTypes = "{ \"field\": \"timestamp\", \"lte\": 5, \"gte\": \"2016-10-06T23:59:59.558+03:00\"}";
145
146     ObjectMapper mapper = new ObjectMapper();
147
148     // Attempt to parse a query where we are setting values for both the
149     // 'greater than' and 'greater than and equal to' operators.
150     boolean gotExpectedException = false;
151     try {
152       RangeQuery badRangeQuery = mapper.readValue(invalidGTAndGTE, RangeQuery.class);
153     } catch (JsonMappingException e) {
154       gotExpectedException = true;
155     }
156     assertTrue("Attempting to set both a 'gt' and 'gte' value on the same query should not have been allowed",
157         gotExpectedException);
158
159     // Attempt to parse a query where we are setting values for both the
160     // 'less than' and 'less than and equal to' operators.
161     gotExpectedException = false;
162     try {
163       RangeQuery badRangeQuery = mapper.readValue(invalidLTAndLTE, RangeQuery.class);
164     } catch (JsonMappingException e) {
165       gotExpectedException = true;
166     }
167     assertTrue("Attempting to set both a 'lt' and 'lte' value on the same query should not have been allowed",
168         gotExpectedException);
169
170     // Attempt to parse a query where we are mixing numeric and date values
171     // in the same query.
172     gotExpectedException = false;
173     try {
174       RangeQuery badRangeQuery = mapper.readValue(invalidTypes, RangeQuery.class);
175     } catch (JsonMappingException e) {
176       gotExpectedException = true;
177     }
178     assertTrue("Attempting to mix numeric and date values in the same query should not have been allowed",
179         gotExpectedException);
180
181
182   }
183
184
185   /**
186    * This test validates that date range queries can be marshalled to a Java
187    * POJO and unmarshalled to ElasticSearch syntax.
188    *
189    * @throws JsonParseException
190    * @throws JsonMappingException
191    * @throws IOException
192    */
193   @Test
194   public void dateRangeQueryTest() throws JsonParseException, JsonMappingException, IOException {
195
196     String field = "timestamp";
197     String greaterThanDate = "2016-10-06T00:00:00.558+03:00";
198     String lessThanDate = "2016-10-06T23:59:59.558+03:00";
199
200     ObjectMapper mapper = new ObjectMapper();
201
202     // Generate a date range query using 'greater than or equal' and 'less
203     // than or equal' operations.
204     String dateRangeJson =
205         "{ \"field\": \"" + field + "\", \"gte\": \"" + greaterThanDate + "\", \"lte\": \"" + lessThanDate + "\"}";
206     String dateRangeExpectedES =
207         "{\"range\": {\"timestamp\": {\"gte\": \"2016-10-06T00:00:00.558+03:00\", \"lte\": \"2016-10-06T23:59:59.558+03:00\"}}}";
208
209     // Validate that the query is marshalled correctly to the POJO and that
210     // the generated ElasticSearch syntax looks as expected.
211     RangeQuery dateRangeQuery = mapper.readValue(dateRangeJson, RangeQuery.class);
212
213     assertTrue("Unexpected marshalled value for 'field'.  Expected: " + field + " Actual: " + dateRangeQuery.getField(),
214         field.equals(dateRangeQuery.getField()));
215     assertTrue("Unexpected type for 'gte' value.  Expected: String  Actual: " + dateRangeQuery.getGte().getClass().getName(),
216         dateRangeQuery.getGte() instanceof String);
217     assertTrue("Unexpected type for 'lte' value.  Expected: String  Actual: " + dateRangeQuery.getLte().getClass().getName(),
218         dateRangeQuery.getLte() instanceof String);
219     assertTrue("Unexpected marshalled value for 'gte'.  Expected: " + greaterThanDate + " Actual: " + dateRangeQuery.getGte(),
220         greaterThanDate.equals(dateRangeQuery.getGte()));
221     assertTrue("Unexpected marshalled value for 'lte'.  Expected: " + lessThanDate + " Actual: " + dateRangeQuery.getLte(),
222         lessThanDate.equals(dateRangeQuery.getLte()));
223     assertTrue("Unexpected ElasticSearch syntax.  Expected: " + dateRangeExpectedES + " Actual: " + dateRangeQuery.toElasticSearch(),
224         dateRangeExpectedES.equals(dateRangeQuery.toElasticSearch()));
225
226
227     // Generate a date range query using 'greater than' and 'less than or
228     // equal' operations.
229     dateRangeJson =
230         "{ \"field\": \"" + field + "\", \"gt\": \"" + greaterThanDate + "\", \"lte\": \"" + lessThanDate + "\"}";
231     dateRangeExpectedES =
232         "{\"range\": {\"timestamp\": {\"gt\": \"2016-10-06T00:00:00.558+03:00\", \"lte\": \"2016-10-06T23:59:59.558+03:00\"}}}";
233
234     // Validate that the query is marshalled correctly to the POJO and that
235     // the generated ElasticSearch syntax looks as expected.
236     dateRangeQuery = mapper.readValue(dateRangeJson, RangeQuery.class);
237
238     assertTrue("Unexpected marshalled value for 'field'.  Expected: " + field + " Actual: " + dateRangeQuery.getField(),
239         field.equals(dateRangeQuery.getField()));
240
241     assertTrue("Unexpected type for 'gt' value.  Expected: String  Actual: " + dateRangeQuery.getGt().getClass().getName(),
242         dateRangeQuery.getGt() instanceof String);
243
244     assertTrue("Unexpected type for 'lte' value.  Expected: String  Actual: " + dateRangeQuery.getLte().getClass().getName(),
245         dateRangeQuery.getLte() instanceof String);
246
247     assertTrue("Unexpected marshalled value for 'gt'.  Expected: " + greaterThanDate + " Actual: " + dateRangeQuery.getGt(),
248         greaterThanDate.equals(dateRangeQuery.getGt()));
249
250     assertTrue("Unexpected marshalled value for 'lte'.  Expected: " + lessThanDate + " Actual: " + dateRangeQuery.getLte(),
251         lessThanDate.equals(dateRangeQuery.getLte()));
252
253     assertTrue("Unexpected ElasticSearch syntax.  Expected: " + dateRangeExpectedES + " Actual: " + dateRangeQuery.toElasticSearch(),
254         dateRangeExpectedES.equals(dateRangeQuery.toElasticSearch()));
255
256
257     // Generate a date range query using only a 'greater than' operation.
258     dateRangeJson =
259         "{ \"field\": \"" + field + "\", \"gt\": \"" + greaterThanDate + "\"}";
260     dateRangeExpectedES =
261         "{\"range\": {\"timestamp\": {\"gt\": \"2016-10-06T00:00:00.558+03:00\"}}}";
262
263     // Validate that the query is marshalled correctly to the POJO and that
264     // the generated ElasticSearch syntax looks as expected.
265     dateRangeQuery = mapper.readValue(dateRangeJson, RangeQuery.class);
266
267     assertTrue("Unexpected marshalled value for 'field'.  Expected: " + field + " Actual: " + dateRangeQuery.getField(),
268         field.equals(dateRangeQuery.getField()));
269
270     assertTrue("Unexpected type for 'gt' value.  Expected: String  Actual: " + dateRangeQuery.getGt().getClass().getName(),
271         dateRangeQuery.getGt() instanceof String);
272
273     assertTrue("Unexpected marshalled value for 'gt'.  Expected: " + greaterThanDate + " Actual: " + dateRangeQuery.getGt(),
274         greaterThanDate.equals(dateRangeQuery.getGt()));
275
276     assertTrue("Unexpected ElasticSearch syntax.  Expected: " + dateRangeExpectedES + " Actual: " + dateRangeQuery.toElasticSearch(),
277         dateRangeExpectedES.equals(dateRangeQuery.toElasticSearch()));
278
279   }
280
281   /**
282    * This test validates that numeric range queries can be marshalled to a Java
283    * POJO and unmarshalled to ElasticSearch syntax.
284    *
285    * @throws JsonParseException
286    * @throws JsonMappingException
287    * @throws IOException
288    */
289   @Test
290   public void numericRangeQueryTest() throws JsonParseException, JsonMappingException, IOException {
291
292     String field = "version";
293     Integer greaterThanInt = 5;
294     Integer lessThanInt = 100;
295
296     ObjectMapper mapper = new ObjectMapper();
297
298     // Generate a numeric range query using 'greater than or equal' and 'less
299     // than or equal' operations.
300     String numericRangeJson =
301         "{ \"field\": \"" + field + "\", \"gte\": " + greaterThanInt + ", \"lte\": " + lessThanInt + "}";
302     String numericRangeExpectedES =
303         "{\"range\": {\"" + field + "\": {\"gte\": " + greaterThanInt + ", \"lte\": " + lessThanInt + "}}}";
304
305     // Validate that the query is marshalled correctly to the POJO and that
306     // the generated ElasticSearch syntax looks as expected.
307     RangeQuery numericRangeQuery = mapper.readValue(numericRangeJson, RangeQuery.class);
308
309     assertTrue("Unexpected marshalled value for 'field'.  Expected: " + field + " Actual: " + numericRangeQuery.getField(),
310         field.equals(numericRangeQuery.getField()));
311     assertTrue("Unexpected type for 'gte' value.  Expected: Integer  Actual: " + numericRangeQuery.getGte().getClass().getName(),
312         numericRangeQuery.getGte() instanceof Integer);
313     assertTrue("Unexpected type for 'lte' value.  Expected: Integer  Actual: " + numericRangeQuery.getLte().getClass().getName(),
314         numericRangeQuery.getLte() instanceof Integer);
315     assertEquals("Unexpected marshalled value for 'gte'.  Expected: " + greaterThanInt + " Actual: " + numericRangeQuery.getGte(),
316         greaterThanInt, numericRangeQuery.getGte());
317     assertEquals("Unexpected marshalled value for 'lte'.  Expected: " + lessThanInt + " Actual: " + numericRangeQuery.getLte(),
318         lessThanInt, numericRangeQuery.getLte());
319     assertTrue("Unexpected ElasticSearch syntax.  Expected: " + numericRangeExpectedES + " Actual: " + numericRangeQuery.toElasticSearch(),
320         numericRangeExpectedES.equals(numericRangeQuery.toElasticSearch()));
321
322
323     Double greaterThanDouble = 5.0;
324     Double lessThanDouble = 100.0;
325
326     // Generate a date range query using 'greater than' and 'less than or
327     // equal' operations.
328     numericRangeJson =
329         "{ \"field\": \"" + field + "\", \"gt\": " + greaterThanDouble + ", \"lte\": " + lessThanDouble + "}";
330     numericRangeExpectedES =
331         "{\"range\": {\"" + field + "\": {\"gt\": " + greaterThanDouble + ", \"lte\": " + lessThanDouble + "}}}";
332
333     // Validate that the query is marshalled correctly to the POJO and that
334     // the generated ElasticSearch syntax looks as expected.
335     numericRangeQuery = mapper.readValue(numericRangeJson, RangeQuery.class);
336
337     assertTrue("Unexpected marshalled value for 'field'.  Expected: " + field + " Actual: " + numericRangeQuery.getField(),
338         field.equals(numericRangeQuery.getField()));
339
340     assertTrue("Unexpected type for 'gt' value.  Expected: Double  Actual: " + numericRangeQuery.getGt().getClass().getName(),
341         numericRangeQuery.getGt() instanceof Double);
342
343     assertTrue("Unexpected type for 'lte' value.  Expected: Double  Actual: " + numericRangeQuery.getLte().getClass().getName(),
344         numericRangeQuery.getLte() instanceof Double);
345
346     assertEquals("Unexpected marshalled value for 'gt'.  Expected: " + greaterThanDouble + " Actual: " + numericRangeQuery.getGt(),
347         greaterThanDouble, numericRangeQuery.getGt());
348
349     assertEquals("Unexpected marshalled value for 'lte'.  Expected: " + lessThanDouble + " Actual: " + numericRangeQuery.getLte(),
350         lessThanDouble, numericRangeQuery.getLte());
351
352     assertTrue("Unexpected ElasticSearch syntax.  Expected: " + numericRangeExpectedES + " Actual: " + numericRangeQuery.toElasticSearch(),
353         numericRangeExpectedES.equals(numericRangeQuery.toElasticSearch()));
354   }
355
356 }