2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.openecomp.sa.searchdbabstraction.searchapi;
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;
32 import java.io.IOException;
34 import static org.junit.Assert.assertEquals;
35 import static org.junit.Assert.assertTrue;
38 public class QueryTest {
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.
45 * @throws JsonParseException
46 * @throws JsonMappingException
50 public void termQueryTest() throws JsonParseException, JsonMappingException, IOException {
53 String field = "searchTags";
54 String termQueryWithIntegerValueJson = "{\"field\": \"" + field + "\", \"value\": " + intValue + "}";
55 String termQueryWithIntegerValueExpectedES = "{\"term\": {\"" + field + "\" : " + intValue + "}}";
57 Double doubleValue = 5.7;
58 String termQueryWithDoubleValueJson = "{\"field\": \"" + field + "\", \"value\": " + doubleValue + "}";
59 String termQueryWithDoubleValueExpectedES = "{\"term\": {\"" + field + "\" : " + doubleValue + "}}";
61 String stringValue = "theValue";
62 String termQueryWithStringValueJson = "{\"field\": \"" + field + "\", \"value\": \"" + stringValue + "\"}";
63 String termQueryWithStringValueExpectedES = "{\"term\": {\"" + field + "\" : \"" + stringValue + "\"}}";
65 ObjectMapper mapper = new ObjectMapper();
68 // Validate that we can marshal a term query where the supplied value
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());
75 assertTrue("ElasticSearch term query translation does not match the expected result",
76 termQueryWithIntegerValueExpectedES.equals(integerTermQuery.toElasticSearch()));
78 // Validate that we can marshal a term query where the supplied value
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()));
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()));
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.
105 * @throws JsonParseException
106 * @throws JsonMappingException
107 * @throws IOException
110 public void parsedQueryTest() throws JsonParseException, JsonMappingException, IOException {
112 String field = "fieldname";
113 String queryString = "The query string";
115 String queryJson = "{\"field\": \"" + field + "\", \"query-string\": \"" + queryString + "\"}";
116 String queryExpectedES = "{\"query_string\": {\"default_field\": \"" + field + "\", \"query\": \"" + queryString + "\"}}";
118 ObjectMapper mapper = new ObjectMapper();
119 ParsedQuery pq = mapper.readValue(queryJson, ParsedQuery.class);
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()));
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
136 * @throws JsonParseException
137 * @throws IOException
140 public void rangeQueryConflictingBoundsTest() throws JsonParseException, IOException {
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\"}";
146 ObjectMapper mapper = new ObjectMapper();
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;
152 RangeQuery badRangeQuery = mapper.readValue(invalidGTAndGTE, RangeQuery.class);
153 } catch (JsonMappingException e) {
154 gotExpectedException = true;
156 assertTrue("Attempting to set both a 'gt' and 'gte' value on the same query should not have been allowed",
157 gotExpectedException);
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;
163 RangeQuery badRangeQuery = mapper.readValue(invalidLTAndLTE, RangeQuery.class);
164 } catch (JsonMappingException e) {
165 gotExpectedException = true;
167 assertTrue("Attempting to set both a 'lt' and 'lte' value on the same query should not have been allowed",
168 gotExpectedException);
170 // Attempt to parse a query where we are mixing numeric and date values
171 // in the same query.
172 gotExpectedException = false;
174 RangeQuery badRangeQuery = mapper.readValue(invalidTypes, RangeQuery.class);
175 } catch (JsonMappingException e) {
176 gotExpectedException = true;
178 assertTrue("Attempting to mix numeric and date values in the same query should not have been allowed",
179 gotExpectedException);
186 * This test validates that date range queries can be marshalled to a Java
187 * POJO and unmarshalled to ElasticSearch syntax.
189 * @throws JsonParseException
190 * @throws JsonMappingException
191 * @throws IOException
194 public void dateRangeQueryTest() throws JsonParseException, JsonMappingException, IOException {
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";
200 ObjectMapper mapper = new ObjectMapper();
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\"}}}";
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);
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()));
227 // Generate a date range query using 'greater than' and 'less than or
228 // equal' operations.
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\"}}}";
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);
238 assertTrue("Unexpected marshalled value for 'field'. Expected: " + field + " Actual: " + dateRangeQuery.getField(),
239 field.equals(dateRangeQuery.getField()));
241 assertTrue("Unexpected type for 'gt' value. Expected: String Actual: " + dateRangeQuery.getGt().getClass().getName(),
242 dateRangeQuery.getGt() instanceof String);
244 assertTrue("Unexpected type for 'lte' value. Expected: String Actual: " + dateRangeQuery.getLte().getClass().getName(),
245 dateRangeQuery.getLte() instanceof String);
247 assertTrue("Unexpected marshalled value for 'gt'. Expected: " + greaterThanDate + " Actual: " + dateRangeQuery.getGt(),
248 greaterThanDate.equals(dateRangeQuery.getGt()));
250 assertTrue("Unexpected marshalled value for 'lte'. Expected: " + lessThanDate + " Actual: " + dateRangeQuery.getLte(),
251 lessThanDate.equals(dateRangeQuery.getLte()));
253 assertTrue("Unexpected ElasticSearch syntax. Expected: " + dateRangeExpectedES + " Actual: " + dateRangeQuery.toElasticSearch(),
254 dateRangeExpectedES.equals(dateRangeQuery.toElasticSearch()));
257 // Generate a date range query using only a 'greater than' operation.
259 "{ \"field\": \"" + field + "\", \"gt\": \"" + greaterThanDate + "\"}";
260 dateRangeExpectedES =
261 "{\"range\": {\"timestamp\": {\"gt\": \"2016-10-06T00:00:00.558+03:00\"}}}";
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);
267 assertTrue("Unexpected marshalled value for 'field'. Expected: " + field + " Actual: " + dateRangeQuery.getField(),
268 field.equals(dateRangeQuery.getField()));
270 assertTrue("Unexpected type for 'gt' value. Expected: String Actual: " + dateRangeQuery.getGt().getClass().getName(),
271 dateRangeQuery.getGt() instanceof String);
273 assertTrue("Unexpected marshalled value for 'gt'. Expected: " + greaterThanDate + " Actual: " + dateRangeQuery.getGt(),
274 greaterThanDate.equals(dateRangeQuery.getGt()));
276 assertTrue("Unexpected ElasticSearch syntax. Expected: " + dateRangeExpectedES + " Actual: " + dateRangeQuery.toElasticSearch(),
277 dateRangeExpectedES.equals(dateRangeQuery.toElasticSearch()));
282 * This test validates that numeric range queries can be marshalled to a Java
283 * POJO and unmarshalled to ElasticSearch syntax.
285 * @throws JsonParseException
286 * @throws JsonMappingException
287 * @throws IOException
290 public void numericRangeQueryTest() throws JsonParseException, JsonMappingException, IOException {
292 String field = "version";
293 Integer greaterThanInt = 5;
294 Integer lessThanInt = 100;
296 ObjectMapper mapper = new ObjectMapper();
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 + "}}}";
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);
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()));
323 Double greaterThanDouble = 5.0;
324 Double lessThanDouble = 100.0;
326 // Generate a date range query using 'greater than' and 'less than or
327 // equal' operations.
329 "{ \"field\": \"" + field + "\", \"gt\": " + greaterThanDouble + ", \"lte\": " + lessThanDouble + "}";
330 numericRangeExpectedES =
331 "{\"range\": {\"" + field + "\": {\"gt\": " + greaterThanDouble + ", \"lte\": " + lessThanDouble + "}}}";
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);
337 assertTrue("Unexpected marshalled value for 'field'. Expected: " + field + " Actual: " + numericRangeQuery.getField(),
338 field.equals(numericRangeQuery.getField()));
340 assertTrue("Unexpected type for 'gt' value. Expected: Double Actual: " + numericRangeQuery.getGt().getClass().getName(),
341 numericRangeQuery.getGt() instanceof Double);
343 assertTrue("Unexpected type for 'lte' value. Expected: Double Actual: " + numericRangeQuery.getLte().getClass().getName(),
344 numericRangeQuery.getLte() instanceof Double);
346 assertEquals("Unexpected marshalled value for 'gt'. Expected: " + greaterThanDouble + " Actual: " + numericRangeQuery.getGt(),
347 greaterThanDouble, numericRangeQuery.getGt());
349 assertEquals("Unexpected marshalled value for 'lte'. Expected: " + lessThanDouble + " Actual: " + numericRangeQuery.getLte(),
350 lessThanDouble, numericRangeQuery.getLte());
352 assertTrue("Unexpected ElasticSearch syntax. Expected: " + numericRangeExpectedES + " Actual: " + numericRangeQuery.toElasticSearch(),
353 numericRangeExpectedES.equals(numericRangeQuery.toElasticSearch()));