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";
44 String queryJson = "{" + "\"queries\": [" + "{\"may\": {\"parsed-query\": {" + "\"field\": \"" + field + "\","
45 + "\"query-string\": \"" + queryString + "\"}}}" + "]" + "}" + "}";
47 String queryES = "{" + "\"version\": true," + "\"query\": {" + "\"bool\": {" + "\"must\": [], "
48 + "\"should\": [" + "{\"query_string\": {\"default_field\": \"searchTags\", \"query\": \"aai3255\"}}"
49 + "]," + "\"must_not\": []}" + "}" + "}";
51 // Marshal our simple query JSON to a SearchStatement object.
52 ObjectMapper mapper = new ObjectMapper();
53 SearchStatement ss = mapper.readValue(queryJson, SearchStatement.class);
55 // We expect to have a search statement with one query.
56 assertEquals("Unexpected number of queries in marshalled result", 1, ss.getQueries().length);
58 // Validate that the query is of the expected type and contains the
60 QueryStatement query = ss.getQueries()[0].getQueryStatement();
61 assertNotNull("Expected marshalled statement to contain a 'parsed query'", query.getParsedQuery());
62 assertTrue("Unexpected field name in marshalled query. Expected: " + field + " Actual: "
63 + query.getParsedQuery().getField(), field.equals(query.getParsedQuery().getField()));
65 "Unexpected query string in marshalled query. Expected: " + queryString + " Actual: "
66 + query.getParsedQuery().getQueryString(),
67 queryString.equals(query.getParsedQuery().getQueryString()));
69 // Validate that we are able to produce the expected ElasticSearch
70 // query syntax from the search statement.
71 assertTrue("Unexpected ElasticSearch syntax. Expected: " + queryES + " Actual: " + ss.toElasticSearch(),
72 queryES.equals(ss.toElasticSearch()));
77 public void simpleSortedQueryTest() throws JsonParseException, JsonMappingException, IOException {
79 String field = "searchTags";
80 String queryString = "aai3255";
81 String queryJson = "{" + "\"queries\": [" + "{\"may\": {\"parsed-query\": {" + "\"field\": \"" + field + "\","
82 + "\"query-string\": \"" + queryString + "\"}}}" + "],"
83 + "\"sort\": { \"field\": \"date\", \"order\": \"ascending\" }" + "}";
86 String queryES = "{" + "\"version\": true," + "\"query\": {" + "\"bool\": {" + "\"must\": [], "
87 + "\"should\": [" + "{\"query_string\": {\"default_field\": \"searchTags\", \"query\": \"aai3255\"}}"
88 + "]," + "\"must_not\": []" + "}" + "}, " + "\"sort\": { \"date\": { \"order\": \"asc\"}}" + "}";
90 // Marshal our simple query JSON to a SearchStatement object.
91 ObjectMapper mapper = new ObjectMapper();
92 SearchStatement ss = mapper.readValue(queryJson, SearchStatement.class);
94 // We expect to have a search statement with one query.
95 assertEquals("Unexpected number of queries in marshalled result", 1, ss.getQueries().length);
97 // Validate that the query is of the expected type and contains the
99 QueryStatement query = ss.getQueries()[0].getQueryStatement();
100 assertNotNull("Expected marshalled statement to contain a 'parsed query'", query.getParsedQuery());
101 assertTrue("Unexpected field name in marshalled query. Expected: " + field + " Actual: "
102 + query.getParsedQuery().getField(), field.equals(query.getParsedQuery().getField()));
104 "Unexpected query string in marshalled query. Expected: " + queryString + " Actual: "
105 + query.getParsedQuery().getQueryString(),
106 queryString.equals(query.getParsedQuery().getQueryString()));
107 System.out.println("GDF: ES = " + ss.toElasticSearch());
108 // Validate that we are able to produce the expected ElasticSearch
109 // query syntax from the search statement.
110 assertTrue("Unexpected ElasticSearch syntax. Expected: " + queryES + " Actual: " + ss.toElasticSearch(),
111 queryES.equals(ss.toElasticSearch()));
112 assertNull(ss.getAggregations());
116 public void filteredQueryTest() throws JsonParseException, JsonMappingException, IOException {
118 String filterField1 = "field1";
119 String filterField2 = "field2";
120 String filterField3 = "field3";
121 String filterValue1 = "a";
122 String filterValue2 = "b";
123 String filterValue3 = "string";
124 String filterJson = "{ \"any\": [ " + "{\"match\": {\"field\": \"" + filterField1 + "\", \"value\": \""
125 + filterValue1 + "\"}}," + "{\"match\": {\"field\": \"" + filterField2 + "\", \"value\": \""
126 + filterValue2 + "\"}}" + "]," + "\"all\": [" + "{\"parsed-query\": {\"field\": \"" + filterField3
127 + "\", \"query-string\": \"" + filterValue3 + "\"}}" + "]" + "}";
129 String filterStanzaJson = "\"filter\": " + filterJson;
131 String queryStanzaJson = "\"queries\": [ "
132 + "{\"may\": {\"match\": {\"field\": \"searchTags\", \"value\": \"a\"}}},"
133 + "{\"may\": {\"match\": {\"field\": \"searchTags\", \"value\": \"b\"}}},"
134 + "{\"may\": {\"parsed-query\": {\"field\": \"fieldname\", \"query-string\": \"string\"}}}" + "]";
136 String queryES = "{" + "\"version\": true," + "\"query\": {" + "\"bool\": {" + "\"must\": [], "
137 + "\"should\": [" + "{\"term\": {\"searchTags\" : \"a\"}}, " + "{\"term\": {\"searchTags\" : \"b\"}}, "
138 + "{\"query_string\": {\"default_field\": \"fieldname\", \"query\": \"string\"}}" + "],"
139 + "\"must_not\": [], " + "\"filter\": {" + "\"bool\": {" + "\"must\": ["
140 + "{\"query_string\": {\"default_field\": \"field3\", \"query\": \"string\"}}" + "],"
141 + "\"must_not\": []," + "\"should\": [" + "{\"term\": {\"field1\" : \"a\"}}, "
142 + "{\"term\": {\"field2\" : \"b\"}}" + "]," + "\"must_not\": []" + "}" + "}" + "}" + "}" + "}";
144 StringBuilder sb = new StringBuilder();
146 sb.append(filterStanzaJson).append(", ");
147 sb.append(queryStanzaJson);
150 ObjectMapper mapper = new ObjectMapper();
151 SearchStatement ss = mapper.readValue(sb.toString(), SearchStatement.class);
153 assertEquals("Unexpected number of queries in the 'any' list for this statement's filter", 2,
154 ss.getFilter().getAny().length);
155 assertEquals("Unexpected number of queries in the 'all' list for this statement's filter", 1,
156 ss.getFilter().getAll().length);
158 assertTrue("Unexpected ElasticSearch syntax. Expected: " + queryES + " Actual: " + ss.toElasticSearch(),
159 queryES.equals(ss.toElasticSearch()));
161 assertNull(ss.getAggregations());
165 public void aggregationTest() {
167 "{\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}";
169 ObjectMapper mapper = new ObjectMapper();
171 SearchStatement ss = mapper.readValue(input, SearchStatement.class);
172 Aggregation[] aggs = ss.getAggregations();
174 assertEquals("Unexpected number aggregations", 2, aggs.length);
175 assertEquals("byDate", aggs[0].getName());
176 assertNotNull(aggs[0].getStatement().getDateRange());
177 assertEquals("mydate", aggs[0].getStatement().getDateRange().getField());
178 assertNotNull(aggs[0].getStatement().getSubAggregations());
179 assertEquals(2, aggs[0].getStatement().getSubAggregations().length);
180 assertEquals("byTerm", aggs[0].getStatement().getSubAggregations()[0].getName());
181 assertEquals("byDate", aggs[0].getStatement().getSubAggregations()[1].getName());
182 assertNull(aggs[0].getStatement().getGroupBy());
183 assertEquals("2nd", aggs[1].getName());
184 assertNotNull(aggs[1].getStatement().getGroupBy());
185 assertEquals("anotherTerm", aggs[1].getStatement().getGroupBy().getField());
186 assertNull(aggs[1].getStatement().getDateRange());
187 assertNull(aggs[1].getStatement().getSubAggregations());
189 } catch (Exception e) {
190 fail("Encountered exception: " + e.getMessage());
195 public void resultSetRangeTest() throws IOException {
197 // Simple query with a result set subrange specified.
198 File queryWithSubrangeFile = new File("src/test/resources/json/queries/query-with-subrange.json");
199 String queryWithSubrangeStr = TestUtils.readFileToString(queryWithSubrangeFile);
200 String queryWithSubrangeExpectedESString =
201 "{\"version\": true,\"from\": 0, \"size\": 10, \"query\": {\"bool\": {\"must\": [{\"term\": {\"field1\" : \"Bob\"}}], \"should\": [],\"must_not\": []}}}";
203 ObjectMapper mapper = new ObjectMapper();
204 SearchStatement ss = mapper.readValue(queryWithSubrangeStr, SearchStatement.class);
206 assertEquals("Unexpected index for result set start", ss.getFrom(), (Integer) 0);
207 assertEquals("Unexpected value for result set size", ss.getSize(), (Integer) 10);
208 assertTrue("Unexpected elastic search query generated from search statement",
209 ss.toElasticSearch().equals(queryWithSubrangeExpectedESString));