2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.sa.searchdbabstraction.elasticsearch.dao;
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;
32 public class AggregationResponseParsingTest {
35 public void testParseAggregationResponse() {
36 JSONParser parser = new JSONParser();
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}";
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).writerWithDefaultPrettyPrinter()
50 .writeValueAsString(aggs));
51 } catch (Exception e) {
57 public void testParseAggregationResponse2() {
58 JSONParser parser = new JSONParser();
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}";
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).writerWithDefaultPrettyPrinter()
72 .writeValueAsString(aggs));
73 } catch (Exception e) {
79 public void testParseAggregationResponse3() {
80 JSONParser parser = new JSONParser();
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}";
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).writerWithDefaultPrettyPrinter()
94 .writeValueAsString(aggs));
95 } catch (Exception e) {