Organise imports to ONAP Java standards
[aai/search-data-service.git] / src / test / java / org / onap / aai / sa / searchdbabstraction / searchapi / SearchStatementTest.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.assertNotNull;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28
29 import com.fasterxml.jackson.core.JsonParseException;
30 import com.fasterxml.jackson.databind.JsonMappingException;
31 import com.fasterxml.jackson.databind.ObjectMapper;
32 import java.io.File;
33 import java.io.IOException;
34 import org.junit.Test;
35 import org.onap.aai.sa.rest.TestUtils;
36
37 public class SearchStatementTest {
38
39   @Test
40   public void simpleQueryTest() throws JsonParseException, JsonMappingException, IOException {
41
42     String field = "searchTags";
43     String queryString = "aai3255";
44     String queryJson =
45         "{"
46             + "\"queries\": ["
47             + "{\"may\": {\"parsed-query\": {"
48             + "\"field\": \"" + field + "\","
49             + "\"query-string\": \"" + queryString + "\"}}}"
50             + "]"
51             + "}"
52             + "}";
53
54     String queryES =
55         "{"
56             + "\"version\": true,"
57             + "\"query\": {"
58             + "\"bool\": {"
59             + "\"must\": [], "
60             + "\"should\": ["
61             + "{\"query_string\": {\"default_field\": \"searchTags\", \"query\": \"aai3255\"}}"
62             + "],"
63             + "\"must_not\": []}"
64             + "}"
65             + "}";
66
67     // Marshal our simple query JSON to a SearchStatement object.
68     ObjectMapper mapper = new ObjectMapper();
69     SearchStatement ss = mapper.readValue(queryJson, SearchStatement.class);
70
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);
74
75     // Validate that the query is of the expected type and contains the
76     // expected values.
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()));
84
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()));
89   }
90
91
92   @Test
93   public void simpleSortedQueryTest() throws JsonParseException, JsonMappingException, IOException {
94
95     String field = "searchTags";
96     String queryString = "aai3255";
97     String queryJson =
98         "{"
99             + "\"queries\": ["
100             + "{\"may\": {\"parsed-query\": {"
101             + "\"field\": \"" + field + "\","
102             + "\"query-string\": \"" + queryString + "\"}}}"
103             + "],"
104             + "\"sort\": { \"field\": \"date\", \"order\": \"ascending\" }"
105             + "}";
106
107
108     String queryES =
109         "{"
110             + "\"version\": true,"
111             + "\"query\": {"
112             + "\"bool\": {"
113             + "\"must\": [], "
114             + "\"should\": ["
115             + "{\"query_string\": {\"default_field\": \"searchTags\", \"query\": \"aai3255\"}}"
116             + "],"
117             + "\"must_not\": []"
118             + "}"
119             + "}, "
120             + "\"sort\": { \"date\": { \"order\": \"asc\"}}"
121             + "}";
122
123     // Marshal our simple query JSON to a SearchStatement object.
124     ObjectMapper mapper = new ObjectMapper();
125     SearchStatement ss = mapper.readValue(queryJson, SearchStatement.class);
126
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);
130
131     // Validate that the query is of the expected type and contains the
132     // expected values.
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());
146   }
147
148   @Test
149   public void filteredQueryTest() throws JsonParseException, JsonMappingException, IOException {
150
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 + "\"}}"
160         + "],"
161         + "\"all\": ["
162         + "{\"parsed-query\": {\"field\": \"" + filterField3 + "\", \"query-string\": \"" + filterValue3 + "\"}}"
163         + "]"
164         + "}";
165
166     String filterStanzaJson = "\"filter\": " + filterJson;
167
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\"}}}"
172         + "]";
173
174     String queryES =
175         "{"
176             + "\"version\": true,"
177             + "\"query\": {"
178             + "\"bool\": {"
179             + "\"must\": [], "
180             + "\"should\": ["
181             + "{\"term\": {\"searchTags\" : \"a\"}}, "
182             + "{\"term\": {\"searchTags\" : \"b\"}}, "
183             + "{\"query_string\": {\"default_field\": \"fieldname\", \"query\": \"string\"}}"
184             + "],"
185             + "\"must_not\": [], "
186             + "\"filter\": {"
187             + "\"bool\": {"
188             + "\"must\": ["
189             + "{\"query_string\": {\"default_field\": \"field3\", \"query\": \"string\"}}"
190             + "],"
191             + "\"must_not\": [],"
192             + "\"should\": ["
193             + "{\"term\": {\"field1\" : \"a\"}}, "
194             + "{\"term\": {\"field2\" : \"b\"}}"
195             + "],"
196             + "\"must_not\": []"
197             + "}"
198             + "}"
199             + "}"
200             + "}"
201             + "}";
202
203     StringBuilder sb = new StringBuilder();
204     sb.append("{");
205     sb.append(filterStanzaJson).append(", ");
206     sb.append(queryStanzaJson);
207     sb.append("}");
208
209     ObjectMapper mapper = new ObjectMapper();
210     SearchStatement ss = mapper.readValue(sb.toString(), SearchStatement.class);
211
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);
216
217     assertTrue("Unexpected ElasticSearch syntax.  Expected: " + queryES + " Actual: " + ss.toElasticSearch(),
218         queryES.equals(ss.toElasticSearch()));
219
220     assertNull(ss.getAggregations());
221   }
222
223   @Test
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}";
226
227     ObjectMapper mapper = new ObjectMapper();
228     try {
229       SearchStatement ss = mapper.readValue(input, SearchStatement.class);
230       Aggregation[] aggs = ss.getAggregations();
231       assertNotNull(aggs);
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());
246
247     } catch (Exception e) {
248       fail("Encountered exception: " + e.getMessage());
249     }
250   }
251
252   @Test
253   public void resultSetRangeTest() throws IOException {
254
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\": []}}}";
260
261     ObjectMapper mapper = new ObjectMapper();
262     SearchStatement ss = mapper.readValue(queryWithSubrangeStr, SearchStatement.class);
263
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));
268   }
269 }