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.fail;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import org.junit.Test;
29 public class AggregationStatementTest {
31 private static ObjectMapper mapper = new ObjectMapper();
34 public void testGroupBy() {
35 String input = "{\r\n \"group-by\": {\r\n \"field\": \"entityType\"\r\n }\r\n }";
37 String expected = "{\"terms\": {\"field\": \"entityType\"}}";
39 AggregationStatement actual;
41 actual = mapper.readValue(input, AggregationStatement.class);
42 assertEquals(expected, actual.toElasticSearch());
43 } catch (Exception e) {
44 fail("Exception occurred: " + e.getMessage());
50 public void testDateRange() {
51 String input = "{\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 \"format\": \"MM-yyy\",\r\n \"size\": \"5\"\r\n }\r\n}";
53 String expected = "{\"date_range\": {\"field\": \"mydate\", \"format\": \"MM-yyy\", \"ranges\": [{\"from\": \"2016-12-19T00:00:00.738-05:00\", \"to\": \"2016-12-23T23:59:59.738-05:00\"}], \"size\": 5}}";
55 AggregationStatement actual;
57 actual = mapper.readValue(input, AggregationStatement.class);
58 assertEquals(expected, actual.toElasticSearch());
59 } catch (Exception e) {
60 fail("Exception occurred: " + e.getMessage());
66 public void testDateHistogram() {
67 String input = "{\r\n \"date-histogram\": {\r\n \"field\": \"mydate\",\r\n \"interval\": \"day\"\r\n }\r\n}";
69 String expected = "{\"date_histogram\": {\"field\": \"mydate\", \"interval\": \"day\"}}";
71 AggregationStatement actual;
73 actual = mapper.readValue(input, AggregationStatement.class);
74 assertEquals(expected, actual.toElasticSearch());
75 } catch (Exception e) {
76 fail("Exception occurred: " + e.getMessage());
82 public void testSubAggregation1() {
83 String input = "{\r\n \"group-by\": {\r\n \"field\": \"severity\"\r\n },\r\n \"sub-aggregations\": [\r\n {\r\n \"name\": \"byType\",\r\n \"aggregation\": {\r\n \"group-by\": {\r\n \"field\": \"entityType\"\r\n }\r\n }\r\n }\r\n ]\r\n}";
84 String expected = "{\"terms\": {\"field\": \"severity\"}, \"aggs\": {\"byType\": {\"terms\": {\"field\": \"entityType\"}}}}";
86 AggregationStatement actual;
88 actual = mapper.readValue(input, AggregationStatement.class);
89 assertEquals(expected, actual.toElasticSearch());
90 } catch (Exception e) {
91 fail("Exception occurred: " + e.getMessage());
97 public void testSubAggregation2() {
98 String input = "{\r\n \"group-by\": {\r\n \"field\": \"severity\"\r\n },\r\n \"sub-aggregations\": [\r\n {\r\n \"name\": \"byType\",\r\n \"aggregation\": {\r\n \"group-by\": {\r\n \"field\": \"violationType\"\r\n }\r\n }\r\n },\r\n {\r\n \"name\": \"byRule\",\r\n \"aggregation\": {\r\n \"group-by\": {\r\n \"field\": \"validationRule\"\r\n }\r\n }\r\n }\r\n ]\r\n}";
99 String expected = "{\"terms\": {\"field\": \"severity\"}, \"aggs\": {\"byType\": {\"terms\": {\"field\": \"violationType\"}},\"byRule\": {\"terms\": {\"field\": \"validationRule\"}}}}";
101 AggregationStatement actual;
103 actual = mapper.readValue(input, AggregationStatement.class);
104 assertEquals(expected, actual.toElasticSearch());
105 } catch (Exception e) {
106 fail("Exception occurred: " + e.getMessage());
113 public void testNestedAggregation1() {
114 String input = "{\r\n \"nested\": [{\r\n \"name\": \"by_severity\",\r\n \"aggregation\": {\r\n \"group-by\": {\r\n \"field\": \"violations.severity\"\r\n }\r\n }\r\n }]\r\n}";
115 String expected = "{\"nested\": {\"path\": \"violations\"}, \"aggs\": {\"by_severity\": {\"terms\": {\"field\": \"violations.severity\"}}}}";
117 AggregationStatement actual;
119 actual = mapper.readValue(input, AggregationStatement.class);
120 assertEquals(expected, actual.toElasticSearch());
121 } catch (Exception e) {
122 fail("Exception occurred: " + e.getMessage());
128 public void testNestedAggregation2() {
129 String input = "{\r\n \"nested\": [\r\n {\r\n \"name\": \"by_severity\",\r\n \"aggregation\": {\r\n \"group-by\": {\r\n \"field\": \"violations.severity\"\r\n }\r\n }\r\n },\r\n {\r\n \"name\": \"by_type\",\r\n \"aggregation\": {\r\n \"group-by\": {\r\n \"field\": \"violations.violationType\"\r\n }\r\n }\r\n }\r\n ]\r\n}";
130 String expected = "{\"nested\": {\"path\": \"violations\"}, \"aggs\": {\"by_severity\": {\"terms\": {\"field\": \"violations.severity\"}},\"by_type\": {\"terms\": {\"field\": \"violations.violationType\"}}}}";
132 AggregationStatement actual;
134 actual = mapper.readValue(input, AggregationStatement.class);
135 assertEquals(expected, actual.toElasticSearch());
136 } catch (Exception e) {
137 fail("Exception occurred: " + e.getMessage());