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