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.assertNotNull;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
29 import com.fasterxml.jackson.core.JsonParseException;
30 import com.fasterxml.jackson.databind.JsonMappingException;
31 import com.fasterxml.jackson.databind.ObjectMapper;
33 import java.io.IOException;
34 import org.junit.Test;
35 import org.onap.aai.sa.rest.TestUtils;
37 public class SearchStatementTest {
40 public void simpleQueryTest() throws JsonParseException, JsonMappingException, IOException {
42 String field = "searchTags";
43 String queryString = "aai3255";
47 + "{\"may\": {\"parsed-query\": {"
48 + "\"field\": \"" + field + "\","
49 + "\"query-string\": \"" + queryString + "\"}}}"
56 + "\"version\": true,"
61 + "{\"query_string\": {\"default_field\": \"searchTags\", \"query\": \"aai3255\"}}"
67 // Marshal our simple query JSON to a SearchStatement object.
68 ObjectMapper mapper = new ObjectMapper();
69 SearchStatement ss = mapper.readValue(queryJson, SearchStatement.class);
71 // We expect to have a search statement with one query.
72 assertEquals("Unexpected number of queries in marshalled result",
73 1, ss.getQueries().length);
75 // Validate that the query is of the expected type and contains the
77 QueryStatement query = ss.getQueries()[0].getQueryStatement();
78 assertNotNull("Expected marshalled statement to contain a 'parsed query'",
79 query.getParsedQuery());
80 assertTrue("Unexpected field name in marshalled query. Expected: " + field + " Actual: " + query.getParsedQuery().getField(),
81 field.equals(query.getParsedQuery().getField()));
82 assertTrue("Unexpected query string in marshalled query. Expected: " + queryString + " Actual: " + query.getParsedQuery().getQueryString(),
83 queryString.equals(query.getParsedQuery().getQueryString()));
85 // Validate that we are able to produce the expected ElasticSearch
86 // query syntax from the search statement.
87 assertTrue("Unexpected ElasticSearch syntax. Expected: " + queryES + " Actual: " + ss.toElasticSearch(),
88 queryES.equals(ss.toElasticSearch()));
93 public void simpleSortedQueryTest() throws JsonParseException, JsonMappingException, IOException {
95 String field = "searchTags";
96 String queryString = "aai3255";
100 + "{\"may\": {\"parsed-query\": {"
101 + "\"field\": \"" + field + "\","
102 + "\"query-string\": \"" + queryString + "\"}}}"
104 + "\"sort\": { \"field\": \"date\", \"order\": \"ascending\" }"
110 + "\"version\": true,"
115 + "{\"query_string\": {\"default_field\": \"searchTags\", \"query\": \"aai3255\"}}"
120 + "\"sort\": { \"date\": { \"order\": \"asc\"}}"
123 // Marshal our simple query JSON to a SearchStatement object.
124 ObjectMapper mapper = new ObjectMapper();
125 SearchStatement ss = mapper.readValue(queryJson, SearchStatement.class);
127 // We expect to have a search statement with one query.
128 assertEquals("Unexpected number of queries in marshalled result",
129 1, ss.getQueries().length);
131 // Validate that the query is of the expected type and contains the
133 QueryStatement query = ss.getQueries()[0].getQueryStatement();
134 assertNotNull("Expected marshalled statement to contain a 'parsed query'",
135 query.getParsedQuery());
136 assertTrue("Unexpected field name in marshalled query. Expected: " + field + " Actual: " + query.getParsedQuery().getField(),
137 field.equals(query.getParsedQuery().getField()));
138 assertTrue("Unexpected query string in marshalled query. Expected: " + queryString + " Actual: " + query.getParsedQuery().getQueryString(),
139 queryString.equals(query.getParsedQuery().getQueryString()));
140 System.out.println("GDF: ES = " + ss.toElasticSearch());
141 // Validate that we are able to produce the expected ElasticSearch
142 // query syntax from the search statement.
143 assertTrue("Unexpected ElasticSearch syntax. Expected: " + queryES + " Actual: " + ss.toElasticSearch(),
144 queryES.equals(ss.toElasticSearch()));
145 assertNull(ss.getAggregations());
149 public void filteredQueryTest() throws JsonParseException, JsonMappingException, IOException {
151 String filterField1 = "field1";
152 String filterField2 = "field2";
153 String filterField3 = "field3";
154 String filterValue1 = "a";
155 String filterValue2 = "b";
156 String filterValue3 = "string";
157 String filterJson = "{ \"any\": [ "
158 + "{\"match\": {\"field\": \"" + filterField1 + "\", \"value\": \"" + filterValue1 + "\"}},"
159 + "{\"match\": {\"field\": \"" + filterField2 + "\", \"value\": \"" + filterValue2 + "\"}}"
162 + "{\"parsed-query\": {\"field\": \"" + filterField3 + "\", \"query-string\": \"" + filterValue3 + "\"}}"
166 String filterStanzaJson = "\"filter\": " + filterJson;
168 String queryStanzaJson = "\"queries\": [ "
169 + "{\"may\": {\"match\": {\"field\": \"searchTags\", \"value\": \"a\"}}},"
170 + "{\"may\": {\"match\": {\"field\": \"searchTags\", \"value\": \"b\"}}},"
171 + "{\"may\": {\"parsed-query\": {\"field\": \"fieldname\", \"query-string\": \"string\"}}}"
176 + "\"version\": true,"
181 + "{\"term\": {\"searchTags\" : \"a\"}}, "
182 + "{\"term\": {\"searchTags\" : \"b\"}}, "
183 + "{\"query_string\": {\"default_field\": \"fieldname\", \"query\": \"string\"}}"
185 + "\"must_not\": [], "
189 + "{\"query_string\": {\"default_field\": \"field3\", \"query\": \"string\"}}"
191 + "\"must_not\": [],"
193 + "{\"term\": {\"field1\" : \"a\"}}, "
194 + "{\"term\": {\"field2\" : \"b\"}}"
203 StringBuilder sb = new StringBuilder();
205 sb.append(filterStanzaJson).append(", ");
206 sb.append(queryStanzaJson);
209 ObjectMapper mapper = new ObjectMapper();
210 SearchStatement ss = mapper.readValue(sb.toString(), SearchStatement.class);
212 assertEquals("Unexpected number of queries in the 'any' list for this statement's filter",
213 2, ss.getFilter().getAny().length);
214 assertEquals("Unexpected number of queries in the 'all' list for this statement's filter",
215 1, ss.getFilter().getAll().length);
217 assertTrue("Unexpected ElasticSearch syntax. Expected: " + queryES + " Actual: " + ss.toElasticSearch(),
218 queryES.equals(ss.toElasticSearch()));
220 assertNull(ss.getAggregations());
224 public void aggregationTest() {
225 String input = "{\r\n \"queries\": [\r\n {\r\n \"must\": {\r\n \"match\": {\r\n \"field\": \"searchTags\",\r\n \"value\": \"a\"\r\n }\r\n }\r\n }\r\n ],\r\n \"aggregations\": [\r\n {\r\n \"name\": \"byDate\",\r\n \"aggregation\": {\r\n \"date-range\": {\r\n \"field\": \"mydate\",\r\n \"ranges\": [\r\n {\r\n \"from\": \"2016-12-19T00:00:00.738-05:00\",\r\n \"to\": \"2016-12-23T23:59:59.738-05:00\"\r\n }\r\n ]\r\n },\r\n \"sub-aggregations\": [\r\n {\r\n \"name\": \"byTerm\",\r\n \"aggregation\": {\r\n \"group-by\": {\r\n \"field\": \"myterm\"\r\n }\r\n }\r\n },\r\n {\r\n \"name\": \"byDate\",\r\n \"aggregation\": {\r\n \"date-histogram\": {\r\n \"field\": \"myDate\",\r\n \"interval\": \"myInterval\"\r\n }\r\n }\r\n }\r\n ]\r\n }\r\n },\r\n {\r\n \"name\": \"2nd\",\r\n \"aggregation\": {\r\n \"group-by\": {\r\n \"field\": \"anotherTerm\"\r\n }\r\n }\r\n }\r\n ]\r\n}";
227 ObjectMapper mapper = new ObjectMapper();
229 SearchStatement ss = mapper.readValue(input, SearchStatement.class);
230 Aggregation[] aggs = ss.getAggregations();
232 assertEquals("Unexpected number aggregations", 2, aggs.length);
233 assertEquals("byDate", aggs[0].getName());
234 assertNotNull(aggs[0].getStatement().getDateRange());
235 assertEquals("mydate", aggs[0].getStatement().getDateRange().getField());
236 assertNotNull(aggs[0].getStatement().getSubAggregations());
237 assertEquals(2, aggs[0].getStatement().getSubAggregations().length);
238 assertEquals("byTerm", aggs[0].getStatement().getSubAggregations()[0].getName());
239 assertEquals("byDate", aggs[0].getStatement().getSubAggregations()[1].getName());
240 assertNull(aggs[0].getStatement().getGroupBy());
241 assertEquals("2nd", aggs[1].getName());
242 assertNotNull(aggs[1].getStatement().getGroupBy());
243 assertEquals("anotherTerm", aggs[1].getStatement().getGroupBy().getField());
244 assertNull(aggs[1].getStatement().getDateRange());
245 assertNull(aggs[1].getStatement().getSubAggregations());
247 } catch (Exception e) {
248 fail("Encountered exception: " + e.getMessage());
253 public void resultSetRangeTest() throws IOException {
255 // Simple query with a result set subrange specified.
256 File queryWithSubrangeFile = new File("src/test/resources/json/queries/query-with-subrange.json");
257 String queryWithSubrangeStr = TestUtils.readFileToString(queryWithSubrangeFile);
258 String queryWithSubrangeExpectedESString =
259 "{\"version\": true,\"from\": 0, \"size\": 10, \"query\": {\"bool\": {\"must\": [{\"term\": {\"field1\" : \"Bob\"}}], \"should\": [],\"must_not\": []}}}";
261 ObjectMapper mapper = new ObjectMapper();
262 SearchStatement ss = mapper.readValue(queryWithSubrangeStr, SearchStatement.class);
264 assertEquals("Unexpected index for result set start", ss.getFrom(), (Integer) 0);
265 assertEquals("Unexpected value for result set size", ss.getSize(), (Integer) 10);
266 assertTrue("Unexpected elastic search query generated from search statement",
267 ss.toElasticSearch().equals(queryWithSubrangeExpectedESString));