summaryrefslogtreecommitdiffstats
path: root/src/test/java/org/openecomp/sa/searchdbabstraction/searchapi/QueryTest.java
blob: 224a6032795eb423d1df080b781d518fa192cc9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017 Amdocs
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 *
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 */
package org.openecomp.sa.searchdbabstraction.searchapi;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;


public class QueryTest {

  /**
   * This test validates that we are able to marshal json structures
   * representing term queries into POJOs and that we can then
   * unmarshal those POJOs into ElasticSearch syntax.
   *
   * @throws JsonParseException
   * @throws JsonMappingException
   * @throws IOException
   */
  @Test
  public void termQueryTest() throws JsonParseException, JsonMappingException, IOException {

    Integer intValue = 1;
    String field = "searchTags";
    String termQueryWithIntegerValueJson = "{\"field\": \"" + field + "\", \"value\": " + intValue + "}";
    String termQueryWithIntegerValueExpectedES = "{\"term\": {\"" + field + "\" : " + intValue + "}}";

    Double doubleValue = 5.7;
    String termQueryWithDoubleValueJson = "{\"field\": \"" + field + "\", \"value\": " + doubleValue + "}";
    String termQueryWithDoubleValueExpectedES = "{\"term\": {\"" + field + "\" : " + doubleValue + "}}";

    String stringValue = "theValue";
    String termQueryWithStringValueJson = "{\"field\": \"" + field + "\", \"value\": \"" + stringValue + "\"}";
    String termQueryWithStringValueExpectedES = "{\"term\": {\"" + field + "\" : \"" + stringValue + "\"}}";

    ObjectMapper mapper = new ObjectMapper();


    // Validate that we can marshal a term query where the supplied value
    // is an Integer.
    TermQuery integerTermQuery = mapper.readValue(termQueryWithIntegerValueJson, TermQuery.class);
    assertTrue("Expected value to be of type Integer, but was type " + integerTermQuery.getValue().getClass().getName(),
        integerTermQuery.getValue() instanceof Integer);
    assertEquals(intValue, integerTermQuery.getValue());

    assertTrue("ElasticSearch term query translation does not match the expected result",
        termQueryWithIntegerValueExpectedES.equals(integerTermQuery.toElasticSearch()));

    // Validate that we can marshal a term query where the supplied value
    // is a Double.
    TermQuery doubleTermQuery = mapper.readValue(termQueryWithDoubleValueJson, TermQuery.class);
    assertTrue("Expected value to be of type Double, but was type " + doubleTermQuery.getValue().getClass().getName(),
        doubleTermQuery.getValue() instanceof Double);
    assertEquals(doubleValue, doubleTermQuery.getValue());
    assertTrue("ElasticSearch term query translation does not match the expected result",
        termQueryWithDoubleValueExpectedES.equals(doubleTermQuery.toElasticSearch()));

    // Validate that we can marshal a term query where the supplied value
    // is a String literal.
    TermQuery stringTermQuery = mapper.readValue(termQueryWithStringValueJson, TermQuery.class);
    assertTrue("Expected value to be of type String, but was type " + stringTermQuery.getValue().getClass().getName(),
        stringTermQuery.getValue() instanceof String);
    assertEquals(stringValue, stringTermQuery.getValue());
    assertTrue("ElasticSearch term query translation does not match the expected result",
        termQueryWithStringValueExpectedES.equals(stringTermQuery.toElasticSearch()));


  }


  /**
   * This test validates that we are able to marshal json structures
   * representing parsed queries into POJOs and that we can then
   * unmarshal those POJOs into ElasticSearch syntax.
   *
   * @throws JsonParseException
   * @throws JsonMappingException
   * @throws IOException
   */
  @Test
  public void parsedQueryTest() throws JsonParseException, JsonMappingException, IOException {

    String field = "fieldname";
    String queryString = "The query string";

    String queryJson = "{\"field\": \"" + field + "\", \"query-string\": \"" + queryString + "\"}";
    String queryExpectedES = "{\"query_string\": {\"default_field\": \"" + field + "\", \"query\": \"" + queryString + "\"}}";

    ObjectMapper mapper = new ObjectMapper();
    ParsedQuery pq = mapper.readValue(queryJson, ParsedQuery.class);

    assertTrue("Unexpected marshalled value for 'field' - expected: " + field + " actual: " + pq.getField(),
        field.equals(pq.getField()));
    assertTrue("Unexpected marshalled value for 'query-string' - expected: " + queryString + " actual: " + pq.getQueryString(),
        queryString.equals(pq.getQueryString()));
    assertTrue("Unexpected ElasticSearch syntax.  Expected: " + queryExpectedES + " Actual: " + pq.toElasticSearch(),
        queryExpectedES.equals(pq.toElasticSearch()));
  }


  /**
   * This test validates that a ranged query cannot be parsed with values
   * for both the 'gte' and 'gt' fields or the 'lte' and 'lt' fields, and
   * that we do not allow mixing of numeric and date types in the same
   * query.
   *
   * @throws JsonParseException
   * @throws IOException
   */
  @Test
  public void rangeQueryConflictingBoundsTest() throws JsonParseException, IOException {

    String invalidGTAndGTE = "{ \"field\": \"timestamp\", \"gte\": \"2016-10-06T00:00:00.558+03:00\", \"gt\": \"2016-10-06T23:59:59.558+03:00\"}";
    String invalidLTAndLTE = "{ \"field\": \"timestamp\", \"lte\": \"2016-10-06T00:00:00.558+03:00\", \"lt\": \"2016-10-06T23:59:59.558+03:00\"}";
    String invalidTypes = "{ \"field\": \"timestamp\", \"lte\": 5, \"gte\": \"2016-10-06T23:59:59.558+03:00\"}";

    ObjectMapper mapper = new ObjectMapper();

    // Attempt to parse a query where we are setting values for both the
    // 'greater than' and 'greater than and equal to' operators.
    boolean gotExpectedException = false;
    try {
      RangeQuery badRangeQuery = mapper.readValue(invalidGTAndGTE, RangeQuery.class);
    } catch (JsonMappingException e) {
      gotExpectedException = true;
    }
    assertTrue("Attempting to set both a 'gt' and 'gte' value on the same query should not have been allowed",
        gotExpectedException);

    // Attempt to parse a query where we are setting values for both the
    // 'less than' and 'less than and equal to' operators.
    gotExpectedException = false;
    try {
      RangeQuery badRangeQuery = mapper.readValue(invalidLTAndLTE, RangeQuery.class);
    } catch (JsonMappingException e) {
      gotExpectedException = true;
    }
    assertTrue("Attempting to set both a 'lt' and 'lte' value on the same query should not have been allowed",
        gotExpectedException);

    // Attempt to parse a query where we are mixing numeric and date values
    // in the same query.
    gotExpectedException = false;
    try {
      RangeQuery badRangeQuery = mapper.readValue(invalidTypes, RangeQuery.class);
    } catch (JsonMappingException e) {
      gotExpectedException = true;
    }
    assertTrue("Attempting to mix numeric and date values in the same query should not have been allowed",
        gotExpectedException);


  }


  /**
   * This test validates that date range queries can be marshalled to a Java
   * POJO and unmarshalled to ElasticSearch syntax.
   *
   * @throws JsonParseException
   * @throws JsonMappingException
   * @throws IOException
   */
  @Test
  public void dateRangeQueryTest() throws JsonParseException, JsonMappingException, IOException {

    String field = "timestamp";
    String greaterThanDate = "2016-10-06T00:00:00.558+03:00";
    String lessThanDate = "2016-10-06T23:59:59.558+03:00";

    ObjectMapper mapper = new ObjectMapper();

    // Generate a date range query using 'greater than or equal' and 'less
    // than or equal' operations.
    String dateRangeJson =
        "{ \"field\": \"" + field + "\", \"gte\": \"" + greaterThanDate + "\", \"lte\": \"" + lessThanDate + "\"}";
    String dateRangeExpectedES =
        "{\"range\": {\"timestamp\": {\"gte\": \"2016-10-06T00:00:00.558+03:00\", \"lte\": \"2016-10-06T23:59:59.558+03:00\"}}}";

    // Validate that the query is marshalled correctly to the POJO and that
    // the generated ElasticSearch syntax looks as expected.
    RangeQuery dateRangeQuery = mapper.readValue(dateRangeJson, RangeQuery.class);

    assertTrue("Unexpected marshalled value for 'field'.  Expected: " + field + " Actual: " + dateRangeQuery.getField(),
        field.equals(dateRangeQuery.getField()));
    assertTrue("Unexpected type for 'gte' value.  Expected: String  Actual: " + dateRangeQuery.getGte().getClass().getName(),
        dateRangeQuery.getGte() instanceof String);
    assertTrue("Unexpected type for 'lte' value.  Expected: String  Actual: " + dateRangeQuery.getLte().getClass().getName(),
        dateRangeQuery.getLte() instanceof String);
    assertTrue("Unexpected marshalled value for 'gte'.  Expected: " + greaterThanDate + " Actual: " + dateRangeQuery.getGte(),
        greaterThanDate.equals(dateRangeQuery.getGte()));
    assertTrue("Unexpected marshalled value for 'lte'.  Expected: " + lessThanDate + " Actual: " + dateRangeQuery.getLte(),
        lessThanDate.equals(dateRangeQuery.getLte()));
    assertTrue("Unexpected ElasticSearch syntax.  Expected: " + dateRangeExpectedES + " Actual: " + dateRangeQuery.toElasticSearch(),
        dateRangeExpectedES.equals(dateRangeQuery.toElasticSearch()));


    // Generate a date range query using 'greater than' and 'less than or
    // equal' operations.
    dateRangeJson =
        "{ \"field\": \"" + field + "\", \"gt\": \"" + greaterThanDate + "\", \"lte\": \"" + lessThanDate + "\"}";
    dateRangeExpectedES =
        "{\"range\": {\"timestamp\": {\"gt\": \"2016-10-06T00:00:00.558+03:00\", \"lte\": \"2016-10-06T23:59:59.558+03:00\"}}}";

    // Validate that the query is marshalled correctly to the POJO and that
    // the generated ElasticSearch syntax looks as expected.
    dateRangeQuery = mapper.readValue(dateRangeJson, RangeQuery.class);

    assertTrue("Unexpected marshalled value for 'field'.  Expected: " + field + " Actual: " + dateRangeQuery.getField(),
        field.equals(dateRangeQuery.getField()));

    assertTrue("Unexpected type for 'gt' value.  Expected: String  Actual: " + dateRangeQuery.getGt().getClass().getName(),
        dateRangeQuery.getGt() instanceof String);

    assertTrue("Unexpected type for 'lte' value.  Expected: String  Actual: " + dateRangeQuery.getLte().getClass().getName(),
        dateRangeQuery.getLte() instanceof String);

    assertTrue("Unexpected marshalled value for 'gt'.  Expected: " + greaterThanDate + " Actual: " + dateRangeQuery.getGt(),
        greaterThanDate.equals(dateRangeQuery.getGt()));

    assertTrue("Unexpected marshalled value for 'lte'.  Expected: " + lessThanDate + " Actual: " + dateRangeQuery.getLte(),
        lessThanDate.equals(dateRangeQuery.getLte()));

    assertTrue("Unexpected ElasticSearch syntax.  Expected: " + dateRangeExpectedES + " Actual: " + dateRangeQuery.toElasticSearch(),
        dateRangeExpectedES.equals(dateRangeQuery.toElasticSearch()));


    // Generate a date range query using only a 'greater than' operation.
    dateRangeJson =
        "{ \"field\": \"" + field + "\", \"gt\": \"" + greaterThanDate + "\"}";
    dateRangeExpectedES =
        "{\"range\": {\"timestamp\": {\"gt\": \"2016-10-06T00:00:00.558+03:00\"}}}";

    // Validate that the query is marshalled correctly to the POJO and that
    // the generated ElasticSearch syntax looks as expected.
    dateRangeQuery = mapper.readValue(dateRangeJson, RangeQuery.class);

    assertTrue("Unexpected marshalled value for 'field'.  Expected: " + field + " Actual: " + dateRangeQuery.getField(),
        field.equals(dateRangeQuery.getField()));

    assertTrue("Unexpected type for 'gt' value.  Expected: String  Actual: " + dateRangeQuery.getGt().getClass().getName(),
        dateRangeQuery.getGt() instanceof String);

    assertTrue("Unexpected marshalled value for 'gt'.  Expected: " + greaterThanDate + " Actual: " + dateRangeQuery.getGt(),
        greaterThanDate.equals(dateRangeQuery.getGt()));

    assertTrue("Unexpected ElasticSearch syntax.  Expected: " + dateRangeExpectedES + " Actual: " + dateRangeQuery.toElasticSearch(),
        dateRangeExpectedES.equals(dateRangeQuery.toElasticSearch()));

  }

  /**
   * This test validates that numeric range queries can be marshalled to a Java
   * POJO and unmarshalled to ElasticSearch syntax.
   *
   * @throws JsonParseException
   * @throws JsonMappingException
   * @throws IOException
   */
  @Test
  public void numericRangeQueryTest() throws JsonParseException, JsonMappingException, IOException {

    String field = "version";
    Integer greaterThanInt = 5;
    Integer lessThanInt = 100;

    ObjectMapper mapper = new ObjectMapper();

    // Generate a numeric range query using 'greater than or equal' and 'less
    // than or equal' operations.
    String numericRangeJson =
        "{ \"field\": \"" + field + "\", \"gte\": " + greaterThanInt + ", \"lte\": " + lessThanInt + "}";
    String numericRangeExpectedES =
        "{\"range\": {\"" + field + "\": {\"gte\": " + greaterThanInt + ", \"lte\": " + lessThanInt + "}}}";

    // Validate that the query is marshalled correctly to the POJO and that
    // the generated ElasticSearch syntax looks as expected.
    RangeQuery numericRangeQuery = mapper.readValue(numericRangeJson, RangeQuery.class);

    assertTrue("Unexpected marshalled value for 'field'.  Expected: " + field + " Actual: " + numericRangeQuery.getField(),
        field.equals(numericRangeQuery.getField()));
    assertTrue("Unexpected type for 'gte' value.  Expected: Integer  Actual: " + numericRangeQuery.getGte().getClass().getName(),
        numericRangeQuery.getGte() instanceof Integer);
    assertTrue("Unexpected type for 'lte' value.  Expected: Integer  Actual: " + numericRangeQuery.getLte().getClass().getName(),
        numericRangeQuery.getLte() instanceof Integer);
    assertEquals("Unexpected marshalled value for 'gte'.  Expected: " + greaterThanInt + " Actual: " + numericRangeQuery.getGte(),
        greaterThanInt, numericRangeQuery.getGte());
    assertEquals("Unexpected marshalled value for 'lte'.  Expected: " + lessThanInt + " Actual: " + numericRangeQuery.getLte(),
        lessThanInt, numericRangeQuery.getLte());
    assertTrue("Unexpected ElasticSearch syntax.  Expected: " + numericRangeExpectedES + " Actual: " + numericRangeQuery.toElasticSearch(),
        numericRangeExpectedES.equals(numericRangeQuery.toElasticSearch()));


    Double greaterThanDouble = 5.0;
    Double lessThanDouble = 100.0;

    // Generate a date range query using 'greater than' and 'less than or
    // equal' operations.
    numericRangeJson =
        "{ \"field\": \"" + field + "\", \"gt\": " + greaterThanDouble + ", \"lte\": " + lessThanDouble + "}";
    numericRangeExpectedES =
        "{\"range\": {\"" + field + "\": {\"gt\": " + greaterThanDouble + ", \"lte\": " + lessThanDouble + "}}}";

    // Validate that the query is marshalled correctly to the POJO and that
    // the generated ElasticSearch syntax looks as expected.
    numericRangeQuery = mapper.readValue(numericRangeJson, RangeQuery.class);

    assertTrue("Unexpected marshalled value for 'field'.  Expected: " + field + " Actual: " + numericRangeQuery.getField(),
        field.equals(numericRangeQuery.getField()));

    assertTrue("Unexpected type for 'gt' value.  Expected: Double  Actual: " + numericRangeQuery.getGt().getClass().getName(),
        numericRangeQuery.getGt() instanceof Double);

    assertTrue("Unexpected type for 'lte' value.  Expected: Double  Actual: " + numericRangeQuery.getLte().getClass().getName(),
        numericRangeQuery.getLte() instanceof Double);

    assertEquals("Unexpected marshalled value for 'gt'.  Expected: " + greaterThanDouble + " Actual: " + numericRangeQuery.getGt(),
        greaterThanDouble, numericRangeQuery.getGt());

    assertEquals("Unexpected marshalled value for 'lte'.  Expected: " + lessThanDouble + " Actual: " + numericRangeQuery.getLte(),
        lessThanDouble, numericRangeQuery.getLte());

    assertTrue("Unexpected ElasticSearch syntax.  Expected: " + numericRangeExpectedES + " Actual: " + numericRangeQuery.toElasticSearch(),
        numericRangeExpectedES.equals(numericRangeQuery.toElasticSearch()));
  }

}