2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.sa.searchdbabstraction.searchapi;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
26 import com.fasterxml.jackson.core.JsonParseException;
27 import com.fasterxml.jackson.databind.JsonMappingException;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29 import java.io.IOException;
30 import org.junit.Test;
32 public class QueryTest {
35 * This test validates that we are able to marshal json structures representing term queries into POJOs and that we
36 * can then unmarshal those POJOs into ElasticSearch syntax.
38 * @throws JsonParseException
39 * @throws JsonMappingException
43 public void termQueryTest() throws JsonParseException, JsonMappingException, IOException {
46 String field = "searchTags";
47 String termQueryWithIntegerValueJson = "{\"field\": \"" + field + "\", \"value\": " + intValue + "}";
48 String termQueryWithIntegerValueExpectedES = "{\"term\": {\"" + field + "\" : " + intValue + "}}";
50 Double doubleValue = 5.7;
51 String termQueryWithDoubleValueJson = "{\"field\": \"" + field + "\", \"value\": " + doubleValue + "}";
52 String termQueryWithDoubleValueExpectedES = "{\"term\": {\"" + field + "\" : " + doubleValue + "}}";
54 String stringValue = "theValue";
55 String termQueryWithStringValueJson = "{\"field\": \"" + field + "\", \"value\": \"" + stringValue + "\"}";
56 String termQueryWithStringValueExpectedES = "{\"term\": {\"" + field + "\" : \"" + stringValue + "\"}}";
58 ObjectMapper mapper = new ObjectMapper();
61 // Validate that we can marshal a term query where the supplied value
63 TermQuery integerTermQuery = mapper.readValue(termQueryWithIntegerValueJson, TermQuery.class);
65 "Expected value to be of type Integer, but was type "
66 + integerTermQuery.getValue().getClass().getName(),
67 integerTermQuery.getValue() instanceof Integer);
68 assertEquals(intValue, integerTermQuery.getValue());
70 assertTrue("ElasticSearch term query translation does not match the expected result",
71 termQueryWithIntegerValueExpectedES.equals(integerTermQuery.toElasticSearch()));
73 // Validate that we can marshal a term query where the supplied value
75 TermQuery doubleTermQuery = mapper.readValue(termQueryWithDoubleValueJson, TermQuery.class);
77 "Expected value to be of type Double, but was type " + doubleTermQuery.getValue().getClass().getName(),
78 doubleTermQuery.getValue() instanceof Double);
79 assertEquals(doubleValue, doubleTermQuery.getValue());
80 assertTrue("ElasticSearch term query translation does not match the expected result",
81 termQueryWithDoubleValueExpectedES.equals(doubleTermQuery.toElasticSearch()));
83 // Validate that we can marshal a term query where the supplied value
84 // is a String literal.
85 TermQuery stringTermQuery = mapper.readValue(termQueryWithStringValueJson, TermQuery.class);
87 "Expected value to be of type String, but was type " + stringTermQuery.getValue().getClass().getName(),
88 stringTermQuery.getValue() instanceof String);
89 assertEquals(stringValue, stringTermQuery.getValue());
90 assertTrue("ElasticSearch term query translation does not match the expected result",
91 termQueryWithStringValueExpectedES.equals(stringTermQuery.toElasticSearch()));
98 * This test validates that we are able to marshal json structures representing parsed queries into POJOs and that
99 * we can then unmarshal those POJOs into ElasticSearch syntax.
101 * @throws JsonParseException
102 * @throws JsonMappingException
103 * @throws IOException
106 public void parsedQueryTest() throws JsonParseException, JsonMappingException, IOException {
108 String field = "fieldname";
109 String queryString = "The query string";
111 String queryJson = "{\"field\": \"" + field + "\", \"query-string\": \"" + queryString + "\"}";
112 String queryExpectedES =
113 "{\"query_string\": {\"default_field\": \"" + field + "\", \"query\": \"" + queryString + "\"}}";
115 ObjectMapper mapper = new ObjectMapper();
116 ParsedQuery pq = mapper.readValue(queryJson, ParsedQuery.class);
118 assertTrue("Unexpected marshalled value for 'field' - expected: " + field + " actual: " + pq.getField(),
119 field.equals(pq.getField()));
120 assertTrue("Unexpected marshalled value for 'query-string' - expected: " + queryString + " actual: "
121 + pq.getQueryString(), queryString.equals(pq.getQueryString()));
123 "Unexpected ElasticSearch syntax. Expected: " + queryExpectedES + " Actual: " + pq.toElasticSearch(),
124 queryExpectedES.equals(pq.toElasticSearch()));
129 * This test validates that a ranged query cannot be parsed with values for both the 'gte' and 'gt' fields or the
130 * 'lte' and 'lt' fields, and that we do not allow mixing of numeric and date types in the same query.
132 * @throws JsonParseException
133 * @throws IOException
136 public void rangeQueryConflictingBoundsTest() throws JsonParseException, IOException {
138 String invalidGTAndGTE =
139 "{ \"field\": \"timestamp\", \"gte\": \"2016-10-06T00:00:00.558+03:00\", \"gt\": \"2016-10-06T23:59:59.558+03:00\"}";
140 String invalidLTAndLTE =
141 "{ \"field\": \"timestamp\", \"lte\": \"2016-10-06T00:00:00.558+03:00\", \"lt\": \"2016-10-06T23:59:59.558+03:00\"}";
142 String invalidTypes = "{ \"field\": \"timestamp\", \"lte\": 5, \"gte\": \"2016-10-06T23:59:59.558+03:00\"}";
144 ObjectMapper mapper = new ObjectMapper();
146 // Attempt to parse a query where we are setting values for both the
147 // 'greater than' and 'greater than and equal to' operators.
148 boolean gotExpectedException = false;
150 mapper.readValue(invalidGTAndGTE, RangeQuery.class);
151 } catch (JsonMappingException e) {
152 gotExpectedException = true;
154 assertTrue("Attempting to set both a 'gt' and 'gte' value on the same query should not have been allowed",
155 gotExpectedException);
157 // Attempt to parse a query where we are setting values for both the
158 // 'less than' and 'less than and equal to' operators.
159 gotExpectedException = false;
161 mapper.readValue(invalidLTAndLTE, RangeQuery.class);
162 } catch (JsonMappingException e) {
163 gotExpectedException = true;
165 assertTrue("Attempting to set both a 'lt' and 'lte' value on the same query should not have been allowed",
166 gotExpectedException);
168 // Attempt to parse a query where we are mixing numeric and date values
169 // in the same query.
170 gotExpectedException = false;
172 mapper.readValue(invalidTypes, RangeQuery.class);
173 } catch (JsonMappingException e) {
174 gotExpectedException = true;
176 assertTrue("Attempting to mix numeric and date values in the same query should not have been allowed",
177 gotExpectedException);
184 * This test validates that date range queries can be marshalled to a Java POJO and unmarshalled to ElasticSearch
187 * @throws JsonParseException
188 * @throws JsonMappingException
189 * @throws IOException
192 public void dateRangeQueryTest() throws JsonParseException, JsonMappingException, IOException {
194 String field = "timestamp";
195 String greaterThanDate = "2016-10-06T00:00:00.558+03:00";
196 String lessThanDate = "2016-10-06T23:59:59.558+03:00";
198 ObjectMapper mapper = new ObjectMapper();
200 // Generate a date range query using 'greater than or equal' and 'less
201 // than or equal' operations.
202 String dateRangeJson = "{ \"field\": \"" + field + "\", \"gte\": \"" + greaterThanDate + "\", \"lte\": \""
203 + lessThanDate + "\"}";
204 String dateRangeExpectedES =
205 "{\"range\": {\"timestamp\": {\"gte\": \"2016-10-06T00:00:00.558+03:00\", \"lte\": \"2016-10-06T23:59:59.558+03:00\"}}}";
207 // Validate that the query is marshalled correctly to the POJO and that
208 // the generated ElasticSearch syntax looks as expected.
209 RangeQuery dateRangeQuery = mapper.readValue(dateRangeJson, RangeQuery.class);
211 assertTrue("Unexpected marshalled value for 'field'. Expected: " + field + " Actual: "
212 + dateRangeQuery.getField(), field.equals(dateRangeQuery.getField()));
213 assertTrue("Unexpected type for 'gte' value. Expected: String Actual: "
214 + dateRangeQuery.getGte().getClass().getName(), dateRangeQuery.getGte() instanceof String);
215 assertTrue("Unexpected type for 'lte' value. Expected: String Actual: "
216 + dateRangeQuery.getLte().getClass().getName(), dateRangeQuery.getLte() instanceof String);
217 assertTrue("Unexpected marshalled value for 'gte'. Expected: " + greaterThanDate + " Actual: "
218 + dateRangeQuery.getGte(), greaterThanDate.equals(dateRangeQuery.getGte()));
219 assertTrue("Unexpected marshalled value for 'lte'. Expected: " + lessThanDate + " Actual: "
220 + dateRangeQuery.getLte(), lessThanDate.equals(dateRangeQuery.getLte()));
222 "Unexpected ElasticSearch syntax. Expected: " + dateRangeExpectedES + " Actual: "
223 + dateRangeQuery.toElasticSearch(),
224 dateRangeExpectedES.equals(dateRangeQuery.toElasticSearch()));
227 // Generate a date range query using 'greater than' and 'less than or
228 // equal' operations.
229 dateRangeJson = "{ \"field\": \"" + field + "\", \"gt\": \"" + greaterThanDate + "\", \"lte\": \""
230 + 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: "
239 + dateRangeQuery.getField(), field.equals(dateRangeQuery.getField()));
241 assertTrue("Unexpected type for 'gt' value. Expected: String Actual: "
242 + dateRangeQuery.getGt().getClass().getName(), dateRangeQuery.getGt() instanceof String);
244 assertTrue("Unexpected type for 'lte' value. Expected: String Actual: "
245 + dateRangeQuery.getLte().getClass().getName(), dateRangeQuery.getLte() instanceof String);
247 assertTrue("Unexpected marshalled value for 'gt'. Expected: " + greaterThanDate + " Actual: "
248 + dateRangeQuery.getGt(), greaterThanDate.equals(dateRangeQuery.getGt()));
250 assertTrue("Unexpected marshalled value for 'lte'. Expected: " + lessThanDate + " Actual: "
251 + dateRangeQuery.getLte(), lessThanDate.equals(dateRangeQuery.getLte()));
254 "Unexpected ElasticSearch syntax. Expected: " + dateRangeExpectedES + " Actual: "
255 + dateRangeQuery.toElasticSearch(),
256 dateRangeExpectedES.equals(dateRangeQuery.toElasticSearch()));
259 // Generate a date range query using only a 'greater than' operation.
260 dateRangeJson = "{ \"field\": \"" + field + "\", \"gt\": \"" + greaterThanDate + "\"}";
261 dateRangeExpectedES = "{\"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: "
268 + dateRangeQuery.getField(), field.equals(dateRangeQuery.getField()));
270 assertTrue("Unexpected type for 'gt' value. Expected: String Actual: "
271 + dateRangeQuery.getGt().getClass().getName(), dateRangeQuery.getGt() instanceof String);
273 assertTrue("Unexpected marshalled value for 'gt'. Expected: " + greaterThanDate + " Actual: "
274 + dateRangeQuery.getGt(), greaterThanDate.equals(dateRangeQuery.getGt()));
277 "Unexpected ElasticSearch syntax. Expected: " + dateRangeExpectedES + " Actual: "
278 + dateRangeQuery.toElasticSearch(),
279 dateRangeExpectedES.equals(dateRangeQuery.toElasticSearch()));
284 * This test validates that numeric range queries can be marshalled to a Java POJO and unmarshalled to ElasticSearch
287 * @throws JsonParseException
288 * @throws JsonMappingException
289 * @throws IOException
292 public void numericRangeQueryTest() throws JsonParseException, JsonMappingException, IOException {
294 String field = "version";
295 Integer greaterThanInt = 5;
296 Integer lessThanInt = 100;
298 ObjectMapper mapper = new ObjectMapper();
300 // Generate a numeric range query using 'greater than or equal' and 'less
301 // than or equal' operations.
302 String numericRangeJson =
303 "{ \"field\": \"" + field + "\", \"gte\": " + greaterThanInt + ", \"lte\": " + lessThanInt + "}";
304 String numericRangeExpectedES =
305 "{\"range\": {\"" + field + "\": {\"gte\": " + greaterThanInt + ", \"lte\": " + lessThanInt + "}}}";
307 // Validate that the query is marshalled correctly to the POJO and that
308 // the generated ElasticSearch syntax looks as expected.
309 RangeQuery numericRangeQuery = mapper.readValue(numericRangeJson, RangeQuery.class);
311 assertTrue("Unexpected marshalled value for 'field'. Expected: " + field + " Actual: "
312 + numericRangeQuery.getField(), field.equals(numericRangeQuery.getField()));
314 "Unexpected type for 'gte' value. Expected: Integer Actual: "
315 + numericRangeQuery.getGte().getClass().getName(),
316 numericRangeQuery.getGte() instanceof Integer);
318 "Unexpected type for 'lte' value. Expected: Integer Actual: "
319 + numericRangeQuery.getLte().getClass().getName(),
320 numericRangeQuery.getLte() instanceof Integer);
321 assertEquals("Unexpected marshalled value for 'gte'. Expected: " + greaterThanInt + " Actual: "
322 + numericRangeQuery.getGte(), greaterThanInt, numericRangeQuery.getGte());
323 assertEquals("Unexpected marshalled value for 'lte'. Expected: " + lessThanInt + " Actual: "
324 + numericRangeQuery.getLte(), lessThanInt, numericRangeQuery.getLte());
326 "Unexpected ElasticSearch syntax. Expected: " + numericRangeExpectedES + " Actual: "
327 + numericRangeQuery.toElasticSearch(),
328 numericRangeExpectedES.equals(numericRangeQuery.toElasticSearch()));
331 Double greaterThanDouble = 5.0;
332 Double lessThanDouble = 100.0;
334 // Generate a date range query using 'greater than' and 'less than or
335 // equal' operations.
337 "{ \"field\": \"" + field + "\", \"gt\": " + greaterThanDouble + ", \"lte\": " + lessThanDouble + "}";
338 numericRangeExpectedES = "{\"range\": {\"" + field + "\": {\"gt\": " + greaterThanDouble + ", \"lte\": "
339 + lessThanDouble + "}}}";
341 // Validate that the query is marshalled correctly to the POJO and that
342 // the generated ElasticSearch syntax looks as expected.
343 numericRangeQuery = mapper.readValue(numericRangeJson, RangeQuery.class);
345 assertTrue("Unexpected marshalled value for 'field'. Expected: " + field + " Actual: "
346 + numericRangeQuery.getField(), field.equals(numericRangeQuery.getField()));
348 assertTrue("Unexpected type for 'gt' value. Expected: Double Actual: "
349 + numericRangeQuery.getGt().getClass().getName(), numericRangeQuery.getGt() instanceof Double);
352 "Unexpected type for 'lte' value. Expected: Double Actual: "
353 + numericRangeQuery.getLte().getClass().getName(),
354 numericRangeQuery.getLte() instanceof Double);
356 assertEquals("Unexpected marshalled value for 'gt'. Expected: " + greaterThanDouble + " Actual: "
357 + numericRangeQuery.getGt(), greaterThanDouble, numericRangeQuery.getGt());
359 assertEquals("Unexpected marshalled value for 'lte'. Expected: " + lessThanDouble + " Actual: "
360 + numericRangeQuery.getLte(), lessThanDouble, numericRangeQuery.getLte());
363 "Unexpected ElasticSearch syntax. Expected: " + numericRangeExpectedES + " Actual: "
364 + numericRangeQuery.toElasticSearch(),
365 numericRangeExpectedES.equals(numericRangeQuery.toElasticSearch()));