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.onap.aai.sa.searchdbabstraction.searchapi;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import org.junit.Test;
27 import org.onap.aai.sa.searchdbabstraction.searchapi.AggregationStatement;
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.fail;
32 public class AggregationStatementTest {
34 private static ObjectMapper mapper = new ObjectMapper();
37 public void testGroupBy() {
38 String input = "{\r\n \"group-by\": {\r\n \"field\": \"entityType\"\r\n }\r\n }";
40 String expected = "{\"terms\": {\"field\": \"entityType\"}}";
42 AggregationStatement actual;
44 actual = mapper.readValue(input, AggregationStatement.class);
45 assertEquals(expected, actual.toElasticSearch());
46 } catch (Exception e) {
47 fail("Exception occurred: " + e.getMessage());
53 public void testDateRange() {
54 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}";
56 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}}";
58 AggregationStatement actual;
60 actual = mapper.readValue(input, AggregationStatement.class);
61 assertEquals(expected, actual.toElasticSearch());
62 } catch (Exception e) {
63 fail("Exception occurred: " + e.getMessage());
69 public void testDateHistogram() {
70 String input = "{\r\n \"date-histogram\": {\r\n \"field\": \"mydate\",\r\n \"interval\": \"day\"\r\n }\r\n}";
72 String expected = "{\"date_histogram\": {\"field\": \"mydate\", \"interval\": \"day\"}}";
74 AggregationStatement actual;
76 actual = mapper.readValue(input, AggregationStatement.class);
77 assertEquals(expected, actual.toElasticSearch());
78 } catch (Exception e) {
79 fail("Exception occurred: " + e.getMessage());
85 public void testSubAggregation1() {
86 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}";
87 String expected = "{\"terms\": {\"field\": \"severity\"}, \"aggs\": {\"byType\": {\"terms\": {\"field\": \"entityType\"}}}}";
89 AggregationStatement actual;
91 actual = mapper.readValue(input, AggregationStatement.class);
92 assertEquals(expected, actual.toElasticSearch());
93 } catch (Exception e) {
94 fail("Exception occurred: " + e.getMessage());
100 public void testSubAggregation2() {
101 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}";
102 String expected = "{\"terms\": {\"field\": \"severity\"}, \"aggs\": {\"byType\": {\"terms\": {\"field\": \"violationType\"}},\"byRule\": {\"terms\": {\"field\": \"validationRule\"}}}}";
104 AggregationStatement actual;
106 actual = mapper.readValue(input, AggregationStatement.class);
107 assertEquals(expected, actual.toElasticSearch());
108 } catch (Exception e) {
109 fail("Exception occurred: " + e.getMessage());
116 public void testNestedAggregation1() {
117 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}";
118 String expected = "{\"nested\": {\"path\": \"violations\"}, \"aggs\": {\"by_severity\": {\"terms\": {\"field\": \"violations.severity\"}}}}";
120 AggregationStatement actual;
122 actual = mapper.readValue(input, AggregationStatement.class);
123 assertEquals(expected, actual.toElasticSearch());
124 } catch (Exception e) {
125 fail("Exception occurred: " + e.getMessage());
131 public void testNestedAggregation2() {
132 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}";
133 String expected = "{\"nested\": {\"path\": \"violations\"}, \"aggs\": {\"by_severity\": {\"terms\": {\"field\": \"violations.severity\"}},\"by_type\": {\"terms\": {\"field\": \"violations.violationType\"}}}}";
135 AggregationStatement actual;
137 actual = mapper.readValue(input, AggregationStatement.class);
138 assertEquals(expected, actual.toElasticSearch());
139 } catch (Exception e) {
140 fail("Exception occurred: " + e.getMessage());