bff0f4fa66bc5e67102362d42b35fa6ad05e3a27
[aai/search-data-service.git] / search-data-service / src / test / java / org / onap / aai / sa / searchdbabstraction / searchapi / AggregationStatementTest.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.fail;
25
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import org.junit.Test;
28
29 public class AggregationStatementTest {
30
31     private static ObjectMapper mapper = new ObjectMapper();
32
33     @Test
34     public void testGroupBy() {
35         String input = "{\r\n    \"group-by\": {\r\n      \"field\": \"entityType\"\r\n    }\r\n  }";
36
37         String expected = "{\"terms\": {\"field\": \"entityType\"}}";
38
39         AggregationStatement actual;
40         try {
41             actual = mapper.readValue(input, AggregationStatement.class);
42             assertEquals(expected, actual.toElasticSearch());
43         } catch (Exception e) {
44             fail("Exception occurred: " + e.getMessage());
45         }
46
47     }
48
49     @Test
50     public void testDateRange() {
51         String input =
52                 "{\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
54         String expected =
55                 "{\"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}}";
56
57         AggregationStatement actual;
58         try {
59             actual = mapper.readValue(input, AggregationStatement.class);
60             assertEquals(expected, actual.toElasticSearch());
61         } catch (Exception e) {
62             fail("Exception occurred: " + e.getMessage());
63         }
64
65     }
66
67     @Test
68     public void testDateHistogram() {
69         String input =
70                 "{\r\n  \"date-histogram\": {\r\n    \"field\": \"mydate\",\r\n    \"interval\": \"day\"\r\n  }\r\n}";
71
72         String expected = "{\"date_histogram\": {\"field\": \"mydate\", \"interval\": \"day\"}}";
73
74         AggregationStatement actual;
75         try {
76             actual = mapper.readValue(input, AggregationStatement.class);
77             assertEquals(expected, actual.toElasticSearch());
78         } catch (Exception e) {
79             fail("Exception occurred: " + e.getMessage());
80         }
81
82     }
83
84     @Test
85     public void testSubAggregation1() {
86         String input =
87                 "{\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 =
89                 "{\"terms\": {\"field\": \"severity\"}, \"aggs\": {\"byType\": {\"terms\": {\"field\": \"entityType\"}}}}";
90
91         AggregationStatement actual;
92         try {
93             actual = mapper.readValue(input, AggregationStatement.class);
94             assertEquals(expected, actual.toElasticSearch());
95         } catch (Exception e) {
96             fail("Exception occurred: " + e.getMessage());
97         }
98
99     }
100
101     @Test
102     public void testSubAggregation2() {
103         String input =
104                 "{\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}";
105         String expected =
106                 "{\"terms\": {\"field\": \"severity\"}, \"aggs\": {\"byType\": {\"terms\": {\"field\": \"violationType\"}},\"byRule\": {\"terms\": {\"field\": \"validationRule\"}}}}";
107
108         AggregationStatement actual;
109         try {
110             actual = mapper.readValue(input, AggregationStatement.class);
111             assertEquals(expected, actual.toElasticSearch());
112         } catch (Exception e) {
113             fail("Exception occurred: " + e.getMessage());
114         }
115
116     }
117
118
119     @Test
120     public void testNestedAggregation1() {
121         String input =
122                 "{\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}";
123         String expected =
124                 "{\"nested\": {\"path\": \"violations\"}, \"aggs\": {\"by_severity\": {\"terms\": {\"field\": \"violations.severity\"}}}}";
125
126         AggregationStatement actual;
127         try {
128             actual = mapper.readValue(input, AggregationStatement.class);
129             assertEquals(expected, actual.toElasticSearch());
130         } catch (Exception e) {
131             fail("Exception occurred: " + e.getMessage());
132         }
133
134     }
135
136     @Test
137     public void testNestedAggregation2() {
138         String input =
139                 "{\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}";
140         String expected =
141                 "{\"nested\": {\"path\": \"violations\"}, \"aggs\": {\"by_severity\": {\"terms\": {\"field\": \"violations.severity\"}},\"by_type\": {\"terms\": {\"field\": \"violations.violationType\"}}}}";
142
143         AggregationStatement actual;
144         try {
145             actual = mapper.readValue(input, AggregationStatement.class);
146             assertEquals(expected, actual.toElasticSearch());
147         } catch (Exception e) {
148             fail("Exception occurred: " + e.getMessage());
149         }
150
151     }
152
153
154 }