Cleanup project's name in Sonar
[aai/search-data-service.git] / src / test / java / org / onap / aai / sa / searchdbabstraction / searchapi / AggregationStatementTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.sa.searchdbabstraction.searchapi;
24
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import org.junit.Test;
27 import org.onap.aai.sa.searchdbabstraction.searchapi.AggregationStatement;
28
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.fail;
31
32 public class AggregationStatementTest {
33
34   private static ObjectMapper mapper = new ObjectMapper();
35
36   @Test
37   public void testGroupBy() {
38     String input = "{\r\n    \"group-by\": {\r\n      \"field\": \"entityType\"\r\n    }\r\n  }";
39
40     String expected = "{\"terms\": {\"field\": \"entityType\"}}";
41
42     AggregationStatement actual;
43     try {
44       actual = mapper.readValue(input, AggregationStatement.class);
45       assertEquals(expected, actual.toElasticSearch());
46     } catch (Exception e) {
47       fail("Exception occurred: " + e.getMessage());
48     }
49
50   }
51
52   @Test
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}";
55
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}}";
57
58     AggregationStatement actual;
59     try {
60       actual = mapper.readValue(input, AggregationStatement.class);
61       assertEquals(expected, actual.toElasticSearch());
62     } catch (Exception e) {
63       fail("Exception occurred: " + e.getMessage());
64     }
65
66   }
67
68   @Test
69   public void testDateHistogram() {
70     String input = "{\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 = "{\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\"}}}}";
88
89     AggregationStatement actual;
90     try {
91       actual = mapper.readValue(input, AggregationStatement.class);
92       assertEquals(expected, actual.toElasticSearch());
93     } catch (Exception e) {
94       fail("Exception occurred: " + e.getMessage());
95     }
96
97   }
98
99   @Test
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\"}}}}";
103
104     AggregationStatement actual;
105     try {
106       actual = mapper.readValue(input, AggregationStatement.class);
107       assertEquals(expected, actual.toElasticSearch());
108     } catch (Exception e) {
109       fail("Exception occurred: " + e.getMessage());
110     }
111
112   }
113
114
115   @Test
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\"}}}}";
119
120     AggregationStatement actual;
121     try {
122       actual = mapper.readValue(input, AggregationStatement.class);
123       assertEquals(expected, actual.toElasticSearch());
124     } catch (Exception e) {
125       fail("Exception occurred: " + e.getMessage());
126     }
127
128   }
129
130   @Test
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\"}}}}";
134
135     AggregationStatement actual;
136     try {
137       actual = mapper.readValue(input, AggregationStatement.class);
138       assertEquals(expected, actual.toElasticSearch());
139     } catch (Exception e) {
140       fail("Exception occurred: " + e.getMessage());
141     }
142
143   }
144
145
146 }