61e64f5a2ae81e25ffeafb876c7ddc867455f15c
[aai/search-data-service.git] / src / test / java / org / onap / aai / sa / searchdbabstraction / elasticsearch / dao / AggregationResponseParsingTest.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.elasticsearch.dao;
22
23 import com.fasterxml.jackson.annotation.JsonInclude.Include;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import org.json.simple.JSONObject;
26 import org.json.simple.parser.JSONParser;
27 import org.junit.Test;
28 import org.onap.aai.sa.searchdbabstraction.entity.AggregationResult;
29 import org.onap.aai.sa.searchdbabstraction.entity.AggregationResults;
30 import org.onap.aai.sa.searchdbabstraction.util.AggregationParsingUtil;
31
32 public class AggregationResponseParsingTest {
33
34   @Test
35   public void testParseAggregationResponse() {
36     JSONParser parser = new JSONParser();
37     JSONObject root;
38
39     String input =
40         "{\r\n  \"aggregations\": {\r\n    \"violations\": {\r\n      \"doc_count\": 2,\r\n      \"by_Timestamp\": {\r\n        \"doc_count_error_upper_bound\": 0,\r\n        \"sum_other_doc_count\": 0,\r\n        \"buckets\": [\r\n          {\r\n            \"key\": 7199992,\r\n            \"key_as_string\": \"Jan 1 1970 01:59:59\",\r\n            \"doc_count\": 2\r\n          }\r\n        ]\r\n      }\r\n    }\r\n  }\r\n}";
41
42     try {
43       root = (JSONObject) parser.parse(input);
44       JSONObject aggregations = (JSONObject) root.get("aggregations");
45       AggregationResult[] results = AggregationParsingUtil.parseAggregationResults(aggregations);
46       AggregationResults aggs = new AggregationResults();
47       ObjectMapper mapper = new ObjectMapper();
48       aggs.setAggregations(results);
49       System.out.println(mapper.setSerializationInclusion(Include.NON_NULL)
50           .writerWithDefaultPrettyPrinter().writeValueAsString(aggs));
51     } catch (Exception e) {
52       e.printStackTrace();
53     }
54   }
55
56   @Test
57   public void testParseAggregationResponse2() {
58     JSONParser parser = new JSONParser();
59     JSONObject root;
60
61     String input =
62         "{\r\n  \"aggregations\": {\r\n    \"entityType\": {\r\n      \"doc_count_error_upper_bound\": 0,\r\n      \"sum_other_doc_count\": 0,\r\n      \"buckets\": [\r\n        {\r\n          \"key\": \"entity1\",\r\n          \"doc_count\": 5,\r\n          \"byVersion\": {\r\n            \"doc_count_error_upper_bound\": 0,\r\n            \"sum_other_doc_count\": 0,\r\n            \"buckets\": [\r\n              {\r\n                \"key\": \"0\",\r\n                \"doc_count\": 5\r\n              }\r\n            ]\r\n          }\r\n        }\r\n      ]\r\n    }\r\n  }\r\n}";
63
64     try {
65       root = (JSONObject) parser.parse(input);
66       JSONObject aggregations = (JSONObject) root.get("aggregations");
67       AggregationResult[] results = AggregationParsingUtil.parseAggregationResults(aggregations);
68       AggregationResults aggs = new AggregationResults();
69       ObjectMapper mapper = new ObjectMapper();
70       aggs.setAggregations(results);
71       System.out.println(mapper.setSerializationInclusion(Include.NON_NULL)
72           .writerWithDefaultPrettyPrinter().writeValueAsString(aggs));
73     } catch (Exception e) {
74       e.printStackTrace();
75     }
76   }
77
78   @Test
79   public void testParseAggregationResponse3() {
80     JSONParser parser = new JSONParser();
81     JSONObject root;
82
83     String input =
84         "{\r\n  \"aggregations\": {\r\n    \"validateTimes\": {\r\n      \"buckets\": [\r\n        {\r\n          \"key\": \"Jan 10 2017 21:6:6-Jan 24 2017 13:43:5\",\r\n          \"from\": 1484082366000,\r\n          \"from_as_string\": \"Jan 10 2017 21:6:6\",\r\n          \"to\": 1485265385000,\r\n          \"to_as_string\": \"Jan 24 2017 13:43:5\",\r\n          \"doc_count\": 95\r\n        },\r\n        {\r\n          \"key\": \"Feb 3 2017 18:27:39-*\",\r\n          \"from\": 1486146459000,\r\n          \"from_as_string\": \"Feb 3 2017 18:27:39\",\r\n          \"doc_count\": 2\r\n        }\r\n      ]\r\n    }\r\n  }\r\n}";
85
86     try {
87       root = (JSONObject) parser.parse(input);
88       JSONObject aggregations = (JSONObject) root.get("aggregations");
89       AggregationResult[] results = AggregationParsingUtil.parseAggregationResults(aggregations);
90       AggregationResults aggs = new AggregationResults();
91       ObjectMapper mapper = new ObjectMapper();
92       aggs.setAggregations(results);
93       System.out.println(mapper.setSerializationInclusion(Include.NON_NULL)
94           .writerWithDefaultPrettyPrinter().writeValueAsString(aggs));
95     } catch (Exception e) {
96       e.printStackTrace();
97     }
98   }
99 }