Add missing distributionManagement section to poms
[aai/search-data-service.git] / search-data-service-app / src / test / java / org / onap / aai / sa / searchdbabstraction / searchapi / QueryTest.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.searchapi;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25
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;
31
32 public class QueryTest {
33
34     /**
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.
37      *
38      * @throws JsonParseException
39      * @throws JsonMappingException
40      * @throws IOException
41      */
42     @Test
43     public void termQueryTest() throws JsonParseException, JsonMappingException, IOException {
44
45         Integer intValue = 1;
46         String field = "searchTags";
47         String termQueryWithIntegerValueJson = "{\"field\": \"" + field + "\", \"value\": " + intValue + "}";
48         String termQueryWithIntegerValueExpectedES = "{\"term\": {\"" + field + "\" : " + intValue + "}}";
49
50         Double doubleValue = 5.7;
51         String termQueryWithDoubleValueJson = "{\"field\": \"" + field + "\", \"value\": " + doubleValue + "}";
52         String termQueryWithDoubleValueExpectedES = "{\"term\": {\"" + field + "\" : " + doubleValue + "}}";
53
54         String stringValue = "theValue";
55         String termQueryWithStringValueJson = "{\"field\": \"" + field + "\", \"value\": \"" + stringValue + "\"}";
56         String termQueryWithStringValueExpectedES = "{\"term\": {\"" + field + "\" : \"" + stringValue + "\"}}";
57
58         ObjectMapper mapper = new ObjectMapper();
59
60
61         // Validate that we can marshal a term query where the supplied value
62         // is an Integer.
63         TermQuery integerTermQuery = mapper.readValue(termQueryWithIntegerValueJson, TermQuery.class);
64         assertTrue(
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());
69
70         assertTrue("ElasticSearch term query translation does not match the expected result",
71                 termQueryWithIntegerValueExpectedES.equals(integerTermQuery.toElasticSearch()));
72
73         // Validate that we can marshal a term query where the supplied value
74         // is a Double.
75         TermQuery doubleTermQuery = mapper.readValue(termQueryWithDoubleValueJson, TermQuery.class);
76         assertTrue(
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()));
82
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);
86         assertTrue(
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()));
92
93
94     }
95
96
97     /**
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.
100      *
101      * @throws JsonParseException
102      * @throws JsonMappingException
103      * @throws IOException
104      */
105     @Test
106     public void parsedQueryTest() throws JsonParseException, JsonMappingException, IOException {
107
108         String field = "fieldname";
109         String queryString = "The query string";
110
111         String queryJson = "{\"field\": \"" + field + "\", \"query-string\": \"" + queryString + "\"}";
112         String queryExpectedES =
113                 "{\"query_string\": {\"default_field\": \"" + field + "\", \"query\": \"" + queryString + "\"}}";
114
115         ObjectMapper mapper = new ObjectMapper();
116         ParsedQuery pq = mapper.readValue(queryJson, ParsedQuery.class);
117
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()));
122         assertTrue(
123                 "Unexpected ElasticSearch syntax.  Expected: " + queryExpectedES + " Actual: " + pq.toElasticSearch(),
124                 queryExpectedES.equals(pq.toElasticSearch()));
125     }
126
127
128     /**
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.
131      *
132      * @throws JsonParseException
133      * @throws IOException
134      */
135     @Test
136     public void rangeQueryConflictingBoundsTest() throws JsonParseException, IOException {
137
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\"}";
143
144         ObjectMapper mapper = new ObjectMapper();
145
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;
149         try {
150             mapper.readValue(invalidGTAndGTE, RangeQuery.class);
151         } catch (JsonMappingException e) {
152             gotExpectedException = true;
153         }
154         assertTrue("Attempting to set both a 'gt' and 'gte' value on the same query should not have been allowed",
155                 gotExpectedException);
156
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;
160         try {
161             mapper.readValue(invalidLTAndLTE, RangeQuery.class);
162         } catch (JsonMappingException e) {
163             gotExpectedException = true;
164         }
165         assertTrue("Attempting to set both a 'lt' and 'lte' value on the same query should not have been allowed",
166                 gotExpectedException);
167
168         // Attempt to parse a query where we are mixing numeric and date values
169         // in the same query.
170         gotExpectedException = false;
171         try {
172             mapper.readValue(invalidTypes, RangeQuery.class);
173         } catch (JsonMappingException e) {
174             gotExpectedException = true;
175         }
176         assertTrue("Attempting to mix numeric and date values in the same query should not have been allowed",
177                 gotExpectedException);
178
179
180     }
181
182
183     /**
184      * This test validates that date range queries can be marshalled to a Java POJO and unmarshalled to ElasticSearch
185      * syntax.
186      *
187      * @throws JsonParseException
188      * @throws JsonMappingException
189      * @throws IOException
190      */
191     @Test
192     public void dateRangeQueryTest() throws JsonParseException, JsonMappingException, IOException {
193
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";
197
198         ObjectMapper mapper = new ObjectMapper();
199
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\"}}}";
206
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);
210
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()));
221         assertTrue(
222                 "Unexpected ElasticSearch syntax.  Expected: " + dateRangeExpectedES + " Actual: "
223                         + 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 = "{ \"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\"}}}";
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: "
239                 + dateRangeQuery.getField(), field.equals(dateRangeQuery.getField()));
240
241         assertTrue("Unexpected type for 'gt' value.  Expected: String  Actual: "
242                 + dateRangeQuery.getGt().getClass().getName(), dateRangeQuery.getGt() instanceof String);
243
244         assertTrue("Unexpected type for 'lte' value.  Expected: String  Actual: "
245                 + dateRangeQuery.getLte().getClass().getName(), dateRangeQuery.getLte() instanceof String);
246
247         assertTrue("Unexpected marshalled value for 'gt'.  Expected: " + greaterThanDate + " Actual: "
248                 + dateRangeQuery.getGt(), greaterThanDate.equals(dateRangeQuery.getGt()));
249
250         assertTrue("Unexpected marshalled value for 'lte'.  Expected: " + lessThanDate + " Actual: "
251                 + dateRangeQuery.getLte(), lessThanDate.equals(dateRangeQuery.getLte()));
252
253         assertTrue(
254                 "Unexpected ElasticSearch syntax.  Expected: " + dateRangeExpectedES + " Actual: "
255                         + dateRangeQuery.toElasticSearch(),
256                 dateRangeExpectedES.equals(dateRangeQuery.toElasticSearch()));
257
258
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\"}}}";
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: "
268                 + dateRangeQuery.getField(), field.equals(dateRangeQuery.getField()));
269
270         assertTrue("Unexpected type for 'gt' value.  Expected: String  Actual: "
271                 + dateRangeQuery.getGt().getClass().getName(), dateRangeQuery.getGt() instanceof String);
272
273         assertTrue("Unexpected marshalled value for 'gt'.  Expected: " + greaterThanDate + " Actual: "
274                 + dateRangeQuery.getGt(), greaterThanDate.equals(dateRangeQuery.getGt()));
275
276         assertTrue(
277                 "Unexpected ElasticSearch syntax.  Expected: " + dateRangeExpectedES + " Actual: "
278                         + dateRangeQuery.toElasticSearch(),
279                 dateRangeExpectedES.equals(dateRangeQuery.toElasticSearch()));
280
281     }
282
283     /**
284      * This test validates that numeric range queries can be marshalled to a Java POJO and unmarshalled to ElasticSearch
285      * syntax.
286      *
287      * @throws JsonParseException
288      * @throws JsonMappingException
289      * @throws IOException
290      */
291     @Test
292     public void numericRangeQueryTest() throws JsonParseException, JsonMappingException, IOException {
293
294         String field = "version";
295         Integer greaterThanInt = 5;
296         Integer lessThanInt = 100;
297
298         ObjectMapper mapper = new ObjectMapper();
299
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 + "}}}";
306
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);
310
311         assertTrue("Unexpected marshalled value for 'field'.  Expected: " + field + " Actual: "
312                 + numericRangeQuery.getField(), field.equals(numericRangeQuery.getField()));
313         assertTrue(
314                 "Unexpected type for 'gte' value.  Expected: Integer  Actual: "
315                         + numericRangeQuery.getGte().getClass().getName(),
316                 numericRangeQuery.getGte() instanceof Integer);
317         assertTrue(
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());
325         assertTrue(
326                 "Unexpected ElasticSearch syntax.  Expected: " + numericRangeExpectedES + " Actual: "
327                         + numericRangeQuery.toElasticSearch(),
328                 numericRangeExpectedES.equals(numericRangeQuery.toElasticSearch()));
329
330
331         Double greaterThanDouble = 5.0;
332         Double lessThanDouble = 100.0;
333
334         // Generate a date range query using 'greater than' and 'less than or
335         // equal' operations.
336         numericRangeJson =
337                 "{ \"field\": \"" + field + "\", \"gt\": " + greaterThanDouble + ", \"lte\": " + lessThanDouble + "}";
338         numericRangeExpectedES = "{\"range\": {\"" + field + "\": {\"gt\": " + greaterThanDouble + ", \"lte\": "
339                 + lessThanDouble + "}}}";
340
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);
344
345         assertTrue("Unexpected marshalled value for 'field'.  Expected: " + field + " Actual: "
346                 + numericRangeQuery.getField(), field.equals(numericRangeQuery.getField()));
347
348         assertTrue("Unexpected type for 'gt' value.  Expected: Double  Actual: "
349                 + numericRangeQuery.getGt().getClass().getName(), numericRangeQuery.getGt() instanceof Double);
350
351         assertTrue(
352                 "Unexpected type for 'lte' value.  Expected: Double  Actual: "
353                         + numericRangeQuery.getLte().getClass().getName(),
354                 numericRangeQuery.getLte() instanceof Double);
355
356         assertEquals("Unexpected marshalled value for 'gt'.  Expected: " + greaterThanDouble + " Actual: "
357                 + numericRangeQuery.getGt(), greaterThanDouble, numericRangeQuery.getGt());
358
359         assertEquals("Unexpected marshalled value for 'lte'.  Expected: " + lessThanDouble + " Actual: "
360                 + numericRangeQuery.getLte(), lessThanDouble, numericRangeQuery.getLte());
361
362         assertTrue(
363                 "Unexpected ElasticSearch syntax.  Expected: " + numericRangeExpectedES + " Actual: "
364                         + numericRangeQuery.toElasticSearch(),
365                 numericRangeExpectedES.equals(numericRangeQuery.toElasticSearch()));
366     }
367
368 }