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