2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.sa.searchdbabstraction.searchapi;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import org.junit.Test;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.fail;
31 public class AggregationStatementTest {
33 private static ObjectMapper mapper = new ObjectMapper();
36 public void testGroupBy() {
37 String input = "{\r\n \"group-by\": {\r\n \"field\": \"entityType\"\r\n }\r\n }";
39 String expected = "{\"terms\": {\"field\": \"entityType\"}}";
41 AggregationStatement actual;
43 actual = mapper.readValue(input, AggregationStatement.class);
44 assertEquals(expected, actual.toElasticSearch());
45 } catch (Exception e) {
46 fail("Exception occurred: " + e.getMessage());
52 public void testDateRange() {
53 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}";
55 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}}";
57 AggregationStatement actual;
59 actual = mapper.readValue(input, AggregationStatement.class);
60 assertEquals(expected, actual.toElasticSearch());
61 } catch (Exception e) {
62 fail("Exception occurred: " + e.getMessage());
68 public void testDateHistogram() {
69 String input = "{\r\n \"date-histogram\": {\r\n \"field\": \"mydate\",\r\n \"interval\": \"day\"\r\n }\r\n}";
71 String expected = "{\"date_histogram\": {\"field\": \"mydate\", \"interval\": \"day\"}}";
73 AggregationStatement actual;
75 actual = mapper.readValue(input, AggregationStatement.class);
76 assertEquals(expected, actual.toElasticSearch());
77 } catch (Exception e) {
78 fail("Exception occurred: " + e.getMessage());
84 public void testSubAggregation1() {
85 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}";
86 String expected = "{\"terms\": {\"field\": \"severity\"}, \"aggs\": {\"byType\": {\"terms\": {\"field\": \"entityType\"}}}}";
88 AggregationStatement actual;
90 actual = mapper.readValue(input, AggregationStatement.class);
91 assertEquals(expected, actual.toElasticSearch());
92 } catch (Exception e) {
93 fail("Exception occurred: " + e.getMessage());
99 public void testSubAggregation2() {
100 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}";
101 String expected = "{\"terms\": {\"field\": \"severity\"}, \"aggs\": {\"byType\": {\"terms\": {\"field\": \"violationType\"}},\"byRule\": {\"terms\": {\"field\": \"validationRule\"}}}}";
103 AggregationStatement actual;
105 actual = mapper.readValue(input, AggregationStatement.class);
106 assertEquals(expected, actual.toElasticSearch());
107 } catch (Exception e) {
108 fail("Exception occurred: " + e.getMessage());
115 public void testNestedAggregation1() {
116 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}";
117 String expected = "{\"nested\": {\"path\": \"violations\"}, \"aggs\": {\"by_severity\": {\"terms\": {\"field\": \"violations.severity\"}}}}";
119 AggregationStatement actual;
121 actual = mapper.readValue(input, AggregationStatement.class);
122 assertEquals(expected, actual.toElasticSearch());
123 } catch (Exception e) {
124 fail("Exception occurred: " + e.getMessage());
130 public void testNestedAggregation2() {
131 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}";
132 String expected = "{\"nested\": {\"path\": \"violations\"}, \"aggs\": {\"by_severity\": {\"terms\": {\"field\": \"violations.severity\"}},\"by_type\": {\"terms\": {\"field\": \"violations.violationType\"}}}}";
134 AggregationStatement actual;
136 actual = mapper.readValue(input, AggregationStatement.class);
137 assertEquals(expected, actual.toElasticSearch());
138 } catch (Exception e) {
139 fail("Exception occurred: " + e.getMessage());