e73b88217fba4e62eef16f9303f150c75d25cf77
[aai/search-data-service.git] / src / test / java / org / openecomp / sa / searchdbabstraction / searchapi / AggregationStatementTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * Search Data Service
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
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
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.openecomp.sa.searchdbabstraction.searchapi;
26
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import org.junit.Test;
29
30 import static org.junit.Assert.assertEquals;
31 import static org.junit.Assert.fail;
32
33 public class AggregationStatementTest {
34
35   private static ObjectMapper mapper = new ObjectMapper();
36
37   @Test
38   public void testGroupBy() {
39     String input = "{\r\n    \"group-by\": {\r\n      \"field\": \"entityType\"\r\n    }\r\n  }";
40
41     String expected = "{\"terms\": {\"field\": \"entityType\"}}";
42
43     AggregationStatement actual;
44     try {
45       actual = mapper.readValue(input, AggregationStatement.class);
46       assertEquals(expected, actual.toElasticSearch());
47     } catch (Exception e) {
48       fail("Exception occurred: " + e.getMessage());
49     }
50
51   }
52
53   @Test
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}";
56
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}}";
58
59     AggregationStatement actual;
60     try {
61       actual = mapper.readValue(input, AggregationStatement.class);
62       assertEquals(expected, actual.toElasticSearch());
63     } catch (Exception e) {
64       fail("Exception occurred: " + e.getMessage());
65     }
66
67   }
68
69   @Test
70   public void testDateHistogram() {
71     String input = "{\r\n  \"date-histogram\": {\r\n    \"field\": \"mydate\",\r\n    \"interval\": \"day\"\r\n  }\r\n}";
72
73     String expected = "{\"date_histogram\": {\"field\": \"mydate\", \"interval\": \"day\"}}";
74
75     AggregationStatement actual;
76     try {
77       actual = mapper.readValue(input, AggregationStatement.class);
78       assertEquals(expected, actual.toElasticSearch());
79     } catch (Exception e) {
80       fail("Exception occurred: " + e.getMessage());
81     }
82
83   }
84
85   @Test
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\"}}}}";
89
90     AggregationStatement actual;
91     try {
92       actual = mapper.readValue(input, AggregationStatement.class);
93       assertEquals(expected, actual.toElasticSearch());
94     } catch (Exception e) {
95       fail("Exception occurred: " + e.getMessage());
96     }
97
98   }
99
100   @Test
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\"}}}}";
104
105     AggregationStatement actual;
106     try {
107       actual = mapper.readValue(input, AggregationStatement.class);
108       assertEquals(expected, actual.toElasticSearch());
109     } catch (Exception e) {
110       fail("Exception occurred: " + e.getMessage());
111     }
112
113   }
114
115
116   @Test
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\"}}}}";
120
121     AggregationStatement actual;
122     try {
123       actual = mapper.readValue(input, AggregationStatement.class);
124       assertEquals(expected, actual.toElasticSearch());
125     } catch (Exception e) {
126       fail("Exception occurred: " + e.getMessage());
127     }
128
129   }
130
131   @Test
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\"}}}}";
135
136     AggregationStatement actual;
137     try {
138       actual = mapper.readValue(input, AggregationStatement.class);
139       assertEquals(expected, actual.toElasticSearch());
140     } catch (Exception e) {
141       fail("Exception occurred: " + e.getMessage());
142     }
143
144   }
145
146
147 }