2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License ati
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.openecomp.sa.searchdbabstraction.searchapi;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import org.junit.Test;
30 import static org.junit.Assert.assertEquals;
31 import static org.junit.Assert.fail;
33 public class AggregationStatementTest {
35 private static ObjectMapper mapper = new ObjectMapper();
38 public void testGroupBy() {
39 String input = "{\r\n \"group-by\": {\r\n \"field\": \"entityType\"\r\n }\r\n }";
41 String expected = "{\"terms\": {\"field\": \"entityType\"}}";
43 AggregationStatement actual;
45 actual = mapper.readValue(input, AggregationStatement.class);
46 assertEquals(expected, actual.toElasticSearch());
47 } catch (Exception e) {
48 fail("Exception occurred: " + e.getMessage());
54 public void testDateRange() {
55 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}";
57 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}}";
59 AggregationStatement actual;
61 actual = mapper.readValue(input, AggregationStatement.class);
62 assertEquals(expected, actual.toElasticSearch());
63 } catch (Exception e) {
64 fail("Exception occurred: " + e.getMessage());
70 public void testDateHistogram() {
71 String input = "{\r\n \"date-histogram\": {\r\n \"field\": \"mydate\",\r\n \"interval\": \"day\"\r\n }\r\n}";
73 String expected = "{\"date_histogram\": {\"field\": \"mydate\", \"interval\": \"day\"}}";
75 AggregationStatement actual;
77 actual = mapper.readValue(input, AggregationStatement.class);
78 assertEquals(expected, actual.toElasticSearch());
79 } catch (Exception e) {
80 fail("Exception occurred: " + e.getMessage());
86 public void testSubAggregation1() {
87 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}";
88 String expected = "{\"terms\": {\"field\": \"severity\"}, \"aggs\": {\"byType\": {\"terms\": {\"field\": \"entityType\"}}}}";
90 AggregationStatement actual;
92 actual = mapper.readValue(input, AggregationStatement.class);
93 assertEquals(expected, actual.toElasticSearch());
94 } catch (Exception e) {
95 fail("Exception occurred: " + e.getMessage());
101 public void testSubAggregation2() {
102 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}";
103 String expected = "{\"terms\": {\"field\": \"severity\"}, \"aggs\": {\"byType\": {\"terms\": {\"field\": \"violationType\"}},\"byRule\": {\"terms\": {\"field\": \"validationRule\"}}}}";
105 AggregationStatement actual;
107 actual = mapper.readValue(input, AggregationStatement.class);
108 assertEquals(expected, actual.toElasticSearch());
109 } catch (Exception e) {
110 fail("Exception occurred: " + e.getMessage());
117 public void testNestedAggregation1() {
118 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}";
119 String expected = "{\"nested\": {\"path\": \"violations\"}, \"aggs\": {\"by_severity\": {\"terms\": {\"field\": \"violations.severity\"}}}}";
121 AggregationStatement actual;
123 actual = mapper.readValue(input, AggregationStatement.class);
124 assertEquals(expected, actual.toElasticSearch());
125 } catch (Exception e) {
126 fail("Exception occurred: " + e.getMessage());
132 public void testNestedAggregation2() {
133 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}";
134 String expected = "{\"nested\": {\"path\": \"violations\"}, \"aggs\": {\"by_severity\": {\"terms\": {\"field\": \"violations.severity\"}},\"by_type\": {\"terms\": {\"field\": \"violations.violationType\"}}}}";
136 AggregationStatement actual;
138 actual = mapper.readValue(input, AggregationStatement.class);
139 assertEquals(expected, actual.toElasticSearch());
140 } catch (Exception e) {
141 fail("Exception occurred: " + e.getMessage());