Initial search service commit
[aai/search-data-service.git] / src / test / java / org / openecomp / sa / searchdbabstraction / elasticsearch / dao / AggregationResponseParsingTest.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.elasticsearch.dao;
26
27 import com.fasterxml.jackson.annotation.JsonInclude.Include;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29 import org.json.simple.JSONObject;
30 import org.json.simple.parser.JSONParser;
31 import org.junit.Test;
32 import org.openecomp.sa.searchdbabstraction.entity.AggregationResult;
33 import org.openecomp.sa.searchdbabstraction.entity.AggregationResults;
34 import org.openecomp.sa.searchdbabstraction.util.AggregationParsingUtil;
35
36 public class AggregationResponseParsingTest {
37
38   @Test
39   public void testParseAggregationResponse() {
40     JSONParser parser = new JSONParser();
41     JSONObject root;
42
43     String input =
44         "{\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}";
45
46     try {
47       root = (JSONObject) parser.parse(input);
48       JSONObject aggregations = (JSONObject) root.get("aggregations");
49       AggregationResult[] results = AggregationParsingUtil.parseAggregationResults(aggregations);
50       AggregationResults aggs = new AggregationResults();
51       ObjectMapper mapper = new ObjectMapper();
52       aggs.setAggregations(results);
53       System.out.println(mapper.setSerializationInclusion(Include.NON_NULL)
54           .writerWithDefaultPrettyPrinter().writeValueAsString(aggs));
55     } catch (Exception e) {
56       e.printStackTrace();
57     }
58   }
59
60   @Test
61   public void testParseAggregationResponse2() {
62     JSONParser parser = new JSONParser();
63     JSONObject root;
64
65     String input =
66         "{\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}";
67
68     try {
69       root = (JSONObject) parser.parse(input);
70       JSONObject aggregations = (JSONObject) root.get("aggregations");
71       AggregationResult[] results = AggregationParsingUtil.parseAggregationResults(aggregations);
72       AggregationResults aggs = new AggregationResults();
73       ObjectMapper mapper = new ObjectMapper();
74       aggs.setAggregations(results);
75       System.out.println(mapper.setSerializationInclusion(Include.NON_NULL)
76           .writerWithDefaultPrettyPrinter().writeValueAsString(aggs));
77     } catch (Exception e) {
78       e.printStackTrace();
79     }
80   }
81
82   @Test
83   public void testParseAggregationResponse3() {
84     JSONParser parser = new JSONParser();
85     JSONObject root;
86
87     String input =
88         "{\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}";
89
90     try {
91       root = (JSONObject) parser.parse(input);
92       JSONObject aggregations = (JSONObject) root.get("aggregations");
93       AggregationResult[] results = AggregationParsingUtil.parseAggregationResults(aggregations);
94       AggregationResults aggs = new AggregationResults();
95       ObjectMapper mapper = new ObjectMapper();
96       aggs.setAggregations(results);
97       System.out.println(mapper.setSerializationInclusion(Include.NON_NULL)
98           .writerWithDefaultPrettyPrinter().writeValueAsString(aggs));
99     } catch (Exception e) {
100       e.printStackTrace();
101     }
102   }
103 }