Reputation: 678
I have this json data:
{"took":2,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":48,"max_score":null,"hits":[{"_index":"tests_report","_type":"summary","_id":"NNNjom4Bp2jL58IyS58A","_score":null,"_source":{"test_name":"CurrentPageTest","start_timestamp":"2019-11-25T11:33:00.585Z","end_timestamp":"2019-11-25T11:47:23.132Z","duration_sec":862,"status":"FAILURE","jira_test_id":62056,"jira_test_key":"AT-202","job_build_number":"10394","indeni_version":"0.0.0.develop","os_version":"NONE","jenkins_node":"py3_172.16.5.205","test_created":"2019-01-20T14:09:21.964Z","indeni_server":"172.16.0.134","doc_index_timestamp":"2019-11-25T11:47:24.481Z","test_case":"Pre-Post Config","first_failure":"java.lang.NullPointerException","link_issue":"FRONT-2889, AT-515"},"sort":[1574682444481]},{"_index":"tests_report","_type":"summary","_id":"oNONn24Bp2jL58IyYZ6N","_score":null,"_source":{"test_name":"EnvSetupTest","start_timestamp":"2019-11-24T22:30:48.924Z","end_timestamp":"2019-11-24T22:34:32.173Z","duration_sec":223,"status":"FAILURE","jira_test_id":61376,"jira_test_key":"AT-126","job_build_number":"10384","indeni_version":"0.0.0.develop","os_version":"NONE","jenkins_node":"py3_172.16.5.205","test_created":"2018-11-30T14:37:00.092Z","indeni_server":"172.16.7.218","doc_index_timestamp":"2019-11-24T22:34:32.254Z","test_case":"Pre-Post Config","first_failure":"java.lang.AssertionError: down services: [knowledge-catalog] expected [UP] but found [DOWN]"},"sort":[1574634872254]},{"_index":"tests_report","_type":"summary","_id":"kdOMn24Bp2jL58IyiJ4p","_score":null,"_source":{"test_name":"EnvSetupTest","start_timestamp":"2019-11-24T22:30:57.309Z","end_timestamp":"2019-11-24T22:33:36.428Z","duration_sec":159,"status":"FAILURE","jira_test_id":61376,"jira_test_key":"AT-126","job_build_number":"10385","indeni_version":"0.0.0.develop","os_version":"NONE","jenkins_node":"py3_172.16.0.55","test_created":"2018-11-30T14:37:00.092Z","indeni_server":"172.16.0.233","doc_index_timestamp":"2019-11-24T22:33:36.507Z","test_case":"Pre-Post Config","first_failure":"java.lang.AssertionError: down services: [backup] expected [UP] but found [DOWN]"},"sort":[1574634816507]},{"_index":"tests_report","_type":"summary","_id":"jdNGn24Bp2jL58Iykp7F","_score":null,"_source":{"test_name":"InsightVerificationTest","start_timestamp":"2019-11-24T20:56:47.832Z","end_timestamp":"2019-11-24T21:17:09.861Z","duration_sec":1222,"status":"FAILURE","jira_test_id":62357,"jira_test_key":"AT-229","job_build_number":"120","indeni_version":"7.0.2.3","os_version":"NONE","jenkins_node":"py3_172.16.2.120","test_created":"2019-02-12T06:02:15.456Z","indeni_server":"10.10.10.90","doc_index_timestamp":"2019-11-24T21:17:10.339Z","test_case":"verifying files content uploaded successfully to RDS","first_failure":"java.lang.AssertionError: RDS does not contain all data came from the CSV files expected [true] but found [false]"},"sort":[1574630230339]},{"_index":"tests_report","_type":"summary","_id":"i9M9n24Bp2jL58Iyz541","_score":null,"_source":{"test_name":"CurrentPageTest","start_timestamp":"2019-11-24T20:56:47.817Z","end_timestamp":"2019-11-24T21:07:35.406Z","duration_sec":647,"status":"FAILURE","jira_test_id":62056,"jira_test_key":"AT-202","job_build_number":"120","indeni_version":"7.0.2.3","os_version":"NONE","jenkins_node":"py3_172.16.2.120","test_created":"2019-01-20T14:09:21.964Z","indeni_server":"10.10.10.90","doc_index_timestamp":"2019-11-24T21:07:36.161Z","test_case":"filter alerts by labels","first_failure":"org.openqa.selenium.ElementClickInterceptedException: failed to click on bth locator=[By.cssSelector: [qa-id='qa-filter-labels-reset']]\nBuild info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T14:04:26.12Z'\nSystem info: host: 'ip-172-16-2-120', ip: '172.16.2.120', os.name: 'Linux', os.a","link_issue":"FRONT-2889, AT-515"},"sort":[1574629656161]},{"_index":"tests_report","_type":"summary","_id":"htM5n24Bp2jL58Iyq56U","_score":null,"_source":{"test_name":"AutomationServiceTest","start_timestamp":"2019-11-24T20:56:47.823Z","end_timestamp":"2019-11-24T21:03:04.553Z","duration_sec":376,"status":"FAILURE","jira_test_id":64877,"jira_test_key":"AT-520","job_build_number":"120","indeni_version":"7.0.2.3","os_version":"NONE","jenkins_node":"py3_172.16.2.120","test_created":"2019-07-18T09:37:52.212Z","indeni_server":"10.10.10.90","doc_index_timestamp":"2019-11-24T21:03:04.885Z","test_case":"alerts remediation steps appear in UI","first_failure":"org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.cssSelector: div[qa-id='qa-more-info-btn'] (tried for 20 second(s) with 500 milliseconds interval)","link_issue":"IS-4652, FRONT-2889, IS-4768"},"sort":[1574629384885]},{"_index":"tests_report","_type":"summary","_id":"cNNEnm4Bp2jL58IyWJ5R","_score":null,"_source":{"test_name":"EnvSetupTest","start_timestamp":"2019-11-24T16:31:33.234Z","end_timestamp":"2019-11-24T16:35:08.304Z","duration_sec":215,"status":"FAILURE","jira_test_id":61376,"jira_test_key":"AT-126","job_build_number":"10378","indeni_version":"0.0.0.develop","os_version":"NONE","jenkins_node":"py3_172.16.5.205","test_created":"2018-11-30T14:37:00.092Z","indeni_server":"172.16.5.2","doc_index_timestamp":"2019-11-24T16:35:08.386Z","test_case":"Pre-Post Config","first_failure":"java.lang.AssertionError: down services: [knowledge-catalog] expected [UP] but found [DOWN]"},"sort":[1574613308386]},{"_index":"tests_report","_type":"summary","_id":"YdNDnm4Bp2jL58IykZ7u","_score":null,"_source":{"test_name":"EnvSetupTest","start_timestamp":"2019-11-24T16:31:38.150Z","end_timestamp":"2019-11-24T16:34:17.676Z","duration_sec":159,"status":"FAILURE","jira_test_id":61376,"jira_test_key":"AT-126","job_build_number":"10379","indeni_version":"0.0.0.develop","os_version":"NONE","jenkins_node":"py3_172.16.0.55","test_created":"2018-11-30T14:37:00.092Z","indeni_server":"172.16.6.147","doc_index_timestamp":"2019-11-24T16:34:17.753Z","test_case":"Pre-Post Config","first_failure":"java.lang.AssertionError: down services: [backup] expected [UP] but found [DOWN]"},"sort":[1574613257753]},{"_index":"tests_report","_type":"summary","_id":"TNP8nG4Bp2jL58IyAJ6h","_score":null,"_source":{"test_name":"EnvSetupTest","start_timestamp":"2019-11-24T10:30:49.285Z","end_timestamp":"2019-11-24T10:36:30.131Z","duration_sec":340,"status":"FAILURE","jira_test_id":61376,"jira_test_key":"AT-126","job_build_number":"10355","indeni_version":"0.0.0.develop","os_version":"NONE","jenkins_node":"py3_172.16.5.205","test_created":"2018-11-30T14:37:00.092Z","indeni_server":"172.16.5.124","doc_index_timestamp":"2019-11-24T10:36:30.225Z","test_case":"Pre-Post Config","first_failure":"java.lang.NullPointerException"},"sort":[1574591790225]},{"_index":"tests_report","_type":"summary","_id":"PdP5nG4Bp2jL58IySJ4k","_score":null,"_source":{"test_name":"EnvSetupTest","start_timestamp":"2019-11-24T10:30:54.274Z","end_timestamp":"2019-11-24T10:33:31.810Z","duration_sec":157,"status":"FAILURE","jira_test_id":61376,"jira_test_key":"AT-126","job_build_number":"10356","indeni_version":"0.0.0.develop","os_version":"NONE","jenkins_node":"py3_172.16.0.55","test_created":"2018-11-30T14:37:00.092Z","indeni_server":"172.16.1.120","doc_index_timestamp":"2019-11-24T10:33:31.892Z","test_case":"Pre-Post Config","first_failure":"java.lang.AssertionError: down services: [backup] expected [UP] but found [DOWN]"},"sort":[1574591611892]},{"_index":"tests_report","_type":"summary","_id":"x9Nnmm4Bp2jL58IyTJ1G","_score":null,"_source":{"test_name":"EnvSetupTest","start_timestamp":"2019-11-23T22:31:37.577Z","end_timestamp":"2019-11-23T22:34:50.297Z","duration_sec":192,"status":"FAILURE","jira_test_id":61376,"jira_test_key":"AT-126","job_build_number":"10346","indeni_version":"0.0.0.develop","os_version":"NONE","jenkins_node":"py3_172.16.5.205","test_created":"2018-11-30T14:37:00.092Z","indeni_server":"172.16.6.241","doc_index_timestamp":"2019-11-23T22:34:50.365Z","test_case":"Pre-Post Config","first_failure":"java.lang.AssertionError: expected to find [Not running\nStarting indeni-knowledge-catalog] inside [Stopped]"},"sort":[1574548490365]},{"_index":"tests_report","_type":"summary","_id":"uNNmmm4Bp2jL58Iy850C","_score":null,"_source":{"test_name":"EnvSetupTest","start_timestamp":"2019-11-23T22:31:41.168Z","end_timestamp":"2019-11-23T22:34:27.306Z","duration_sec":166,"status":"FAILURE","jira_test_id":61376,"jira_test_key":"AT-126","job_build_number":"10347","indeni_version":"0.0.0.develop","os_version":"NONE","jenkins_node":"py3_172.16.0.55","test_created":"2018-11-30T14:37:00.092Z","indeni_server":"172.16.4.62","doc_index_timestamp":"2019-11-23T22:34:27.390Z","test_case":"Pre-Post Config","first_failure":"java.lang.AssertionError: service didn't running expected [true] but found [false]"},"sort":[1574548467390]},{"_index":"tests_report","_type":"summary","_id":"ttMhmm4Bp2jL58IyOJ2Q","_score":null,"_source":{"test_name":"InsightVerificationTest","start_timestamp":"2019-11-23T20:57:46.808Z","end_timestamp":"2019-11-23T21:18:16.458Z","duration_sec":1229,"status":"FAILURE","jira_test_id":62357,"jira_test_key":"AT-229","job_build_number":"119","indeni_version":"7.0.2.3","os_version":"NONE","jenkins_node":"py3_172.16.2.120","test_created":"2019-02-12T06:02:15.456Z","indeni_server":"10.10.10.90","doc_index_timestamp":"2019-11-23T21:18:16.950Z","test_case":"verifying files content uploaded successfully to RDS","first_failure":"java.lang.AssertionError: RDS does not contain all data came from the CSV files expected [true] but found [false]"},"sort":[1574543896950]},{"_index":"tests_report","_type":"summary","_id":"stMXmm4Bp2jL58Iyg51W","_score":null,"_source":{"test_name":"CurrentPageTest","start_timestamp":"2019-11-23T20:57:46.805Z","end_timestamp":"2019-11-23T21:07:39.472Z","duration_sec":592,"status":"FAILURE","jira_test_id":62056,"jira_test_key":"AT-202","job_build_number":"119","indeni_version":"7.0.2.3","os_version":"NONE","jenkins_node":"py3_172.16.2.120","test_created":"2019-01-20T14:09:21.964Z","indeni_server":"10.10.10.90","doc_index_timestamp":"2019-11-23T21:07:40.185Z","test_case":"filter alerts by labels","first_failure":"org.openqa.selenium.ElementClickInterceptedException: failed to click on bth locator=[By.cssSelector: [qa-id='qa-filter-labels-reset']]\nBuild info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T14:04:26.12Z'\nSystem info: host: 'ip-172-16-2-120', ip: '172.16.2.120', os.name: 'Linux', os.a","link_issue":"FRONT-2889, AT-515"},"sort":[1574543260185]},{"_index":"tests_report","_type":"summary","_id":"l9MdmW4Bp2jL58IyKJ3J","_score":null,"_source":{"test_name":"EnvSetupTest","start_timestamp":"2019-11-23T16:31:00.266Z","end_timestamp":"2019-11-23T16:34:14.262Z","duration_sec":193,"status":"FAILURE","jira_test_id":61376,"jira_test_key":"AT-126","job_build_number":"10343","indeni_version":"0.0.0.develop","os_version":"NONE","jenkins_node":"py3_172.16.0.55","test_created":"2018-11-30T14:37:00.092Z","indeni_server":"172.16.0.162","doc_index_timestamp":"2019-11-23T16:34:14.351Z","test_case":"Pre-Post Config","first_failure":"java.lang.AssertionError: service didn't running expected [true] but found [false]"},"sort":[1574526854351]},{"_index":"tests_report","_type":"summary","_id":"iNMcmW4Bp2jL58Iy9J3o","_score":null,"_source":{"test_name":"EnvSetupTest","start_timestamp":"2019-11-23T16:30:55.881Z","end_timestamp":"2019-11-23T16:34:01.029Z","duration_sec":185,"status":"FAILURE","jira_test_id":61376,"jira_test_key":"AT-126","job_build_number":"10342","indeni_version":"0.0.0.develop","os_version":"NONE","jenkins_node":"py3_172.16.5.205","test_created":"2018-11-30T14:37:00.092Z","indeni_server":"172.16.5.146","doc_index_timestamp":"2019-11-23T16:34:01.125Z","test_case":"Pre-Post Config","first_failure":"java.lang.AssertionError: expected to find [Not running\nStarting indeni-knowledge-catalog] inside [Stopped]"},"sort":[1574526841125]},{"_index":"tests_report","_type":"summary","_id":"c9PTl24Bp2jL58IyUJ2R","_score":null,"_source":{"test_name":"EnvSetupTest","start_timestamp":"2019-11-23T10:30:39.361Z","end_timestamp":"2019-11-23T10:33:57.580Z","duration_sec":198,"status":"FAILURE","jira_test_id":61376,"jira_test_key":"AT-126","job_build_number":"10341","indeni_version":"0.0.0.develop","os_version":"NONE","jenkins_node":"py3_172.16.0.55","test_created":"2018-11-30T14:37:00.092Z","indeni_server":"172.16.5.199","doc_index_timestamp":"2019-11-23T10:33:57.655Z","test_case":"Pre-Post Config","first_failure":"java.lang.AssertionError: service didn't running expected [true] but found [false]"},"sort":[1574505237655]},{"_index":"tests_report","_type":"summary","_id":"ZNPTl24Bp2jL58IyGJ1g","_score":null,"_source":{"test_name":"EnvSetupTest","start_timestamp":"2019-11-23T10:30:37.655Z","end_timestamp":"2019-11-23T10:33:43.000Z","duration_sec":185,"status":"FAILURE","jira_test_id":61376,"jira_test_key":"AT-126","job_build_number":"10340","indeni_version":"0.0.0.develop","os_version":"NONE","jenkins_node":"py3_172.16.5.205","test_created":"2018-11-30T14:37:00.092Z","indeni_server":"172.16.3.200","doc_index_timestamp":"2019-11-23T10:33:43.077Z","test_case":"Pre-Post Config","first_failure":"java.lang.AssertionError: expected to find [Not running\nStarting indeni-knowledge-catalog] inside [Stopped]"},"sort":[1574505223077]},{"_index":"tests_report","_type":"summary","_id":"7tM_lW4Bp2jL58Iy7Zy1","_score":null,"_source":{"test_name":"EnvSetupTest","start_timestamp":"2019-11-22T22:30:39.317Z","end_timestamp":"2019-11-22T22:33:44.104Z","duration_sec":184,"status":"FAILURE","jira_test_id":61376,"jira_test_key":"AT-126","job_build_number":"10335","indeni_version":"0.0.0.develop","os_version":"NONE","jenkins_node":"py3_172.16.5.205","test_created":"2018-11-30T14:37:00.092Z","indeni_server":"172.16.0.90","doc_index_timestamp":"2019-11-22T22:33:44.186Z","test_case":"Pre-Post Config","first_failure":"java.lang.AssertionError: expected to find [Not running\nStarting indeni-knowledge-catalog] inside [Stopped]"},"sort":[1574462024186]},{"_index":"tests_report","_type":"summary","_id":"39M_lW4Bp2jL58IyvZxy","_score":null,"_source":{"test_name":"EnvSetupTest","start_timestamp":"2019-11-22T22:30:38.308Z","end_timestamp":"2019-11-22T22:33:31.566Z","duration_sec":173,"status":"FAILURE","jira_test_id":61376,"jira_test_key":"AT-126","job_build_number":"10336","indeni_version":"0.0.0.develop","os_version":"NONE","jenkins_node":"py3_172.16.0.55","test_created":"2018-11-30T14:37:00.092Z","indeni_server":"172.16.7.12","doc_index_timestamp":"2019-11-22T22:33:31.643Z","test_case":"Pre-Post Config","first_failure":"java.lang.AssertionError: service didn't running expected [true] but found [false]"},"sort":[1574462011643]},{"_index":"tests_report","_type":"summary","_id":"3NP7lG4Bp2jL58IyxZy7","_score":null,"_source":{"test_name":"InsightVerificationTest","start_timestamp":"2019-11-22T20:59:56.448Z","end_timestamp":"2019-11-22T21:19:15.878Z","duration_sec":1159,"status":"FAILURE","jira_test_id":62357,"jira_test_key":"AT-229","job_build_number":"118","indeni_version":"7.0.2.3","os_version":"NONE","jenkins_node":"py3_172.16.2.120","test_created":"2019-02-12T06:02:15.456Z","indeni_server":"10.10.10.90","doc_index_timestamp":"2019-11-22T21:19:16.283Z","test_case":"verifying files content uploaded successfully to RDS","first_failure":"java.lang.AssertionError: RDS does not contain all data came from the CSV files expected [true] but found [false]"},"sort":[1574457556283]},{"_index":"tests_report","_type":"summary","_id":"vtP2k24Bp2jL58IygZzu","_score":null,"_source":{"test_name":"EnvSetupTest","start_timestamp":"2019-11-22T16:30:48.264Z","end_timestamp":"2019-11-22T16:33:55.039Z","duration_sec":186,"status":"FAILURE","jira_test_id":61376,"jira_test_key":"AT-126","job_build_number":"10333","indeni_version":"0.0.0.develop","os_version":"NONE","jenkins_node":"py3_172.16.5.205","test_created":"2018-11-30T14:37:00.092Z","indeni_server":"172.16.3.124","doc_index_timestamp":"2019-11-22T16:33:55.119Z","test_case":"Pre-Post Config","first_failure":"java.lang.AssertionError: Service =[knowledge-catalog] is not running expected [true] but found [false]"},"sort":[1574440435119]},{"_index":"tests_report","_type":"summary","_id":"r9P2k24Bp2jL58Iyb5wW","_score":null,"_source":{"test_name":"EnvSetupTest","start_timestamp":"2019-11-22T16:30:53.593Z","end_timestamp":"2019-11-22T16:33:50.157Z","duration_sec":176,"status":"FAILURE","jira_test_id":61376,"jira_test_key":"AT-126","job_build_number":"10334","indeni_version":"0.0.0.develop","os_version":"NONE","jenkins_node":"py3_172.16.0.55","test_created":"2018-11-30T14:37:00.092Z","indeni_server":"172.16.2.211","doc_index_timestamp":"2019-11-22T16:33:50.224Z","test_case":"Pre-Post Config","first_failure":"java.lang.AssertionError: service didn't running expected [true] but found [false]"},"sort":[1574440430224]},{"_index":"tests_report","_type":"summary","_id":"mtOtkm4Bp2jL58IyX5xn","_score":null,"_source":{"test_name":"EnvSetupTest","start_timestamp":"2019-11-22T10:31:20.679Z","end_timestamp":"2019-11-22T10:34:24.686Z","duration_sec":184,"status":"FAILURE","jira_test_id":61376,"jira_test_key":"AT-126","job_build_number":"10330","indeni_version":"0.0.0.develop","os_version":"NONE","jenkins_node":"py3_172.16.0.55","test_created":"2018-11-30T14:37:00.092Z","indeni_server":"172.16.1.79","doc_index_timestamp":"2019-11-22T10:34:24.768Z","test_case":"Pre-Post Config","first_failure":"java.lang.AssertionError: service didn't running expected [true] but found [false]"},"sort":[1574418864768]},{"_index":"tests_report","_type":"summary","_id":"i9Orkm4Bp2jL58IyrZwI","_score":null,"_source":{"test_name":"EnvSetupTest","start_timestamp":"2019-11-22T10:31:15.793Z","end_timestamp":"2019-11-22T10:32:33.524Z","duration_sec":77,"status":"FAILURE","jira_test_id":61376,"jira_test_key":"AT-126","job_build_number":"10329","indeni_version":"0.0.0.develop","os_version":"NONE","jenkins_node":"py3_172.16.5.205","test_created":"2018-11-30T14:37:00.092Z","indeni_server":"172.16.7.98","doc_index_timestamp":"2019-11-22T10:32:33.610Z","test_case":"Pre-Post Config","first_failure":"java.lang.AssertionError: expected to find [Not running\nStarting indeni-knowledge-catalog] inside [Stopped]"},"sort":[1574418753610]}]}}
Now what I am trying to do is to iterate through JSON data and storing it into HashMap where the key is "indeni_version" (e.g. 0.0.0.develop and it might be 7.0.2.3 for example or any other version).
This is my implmentation for HashMap -> private static HashMap<String, List<ReportGenreratorFields>> reportGeneratorMapper = new HashMap<>();
where ReportGeneratorFields
class looks like this:
package com.indeni.automation;
public class ReportGenreratorFields {
private String testname;
private String jenkinsBuildNumber;
private String jiraLinkedIssue;
public String getTestname() {
return testname;
}
public String getJenkinsBuildNumber() {
return jenkinsBuildNumber;
}
public String getJiraLinkedIssue() {
return jiraLinkedIssue;
}
public void setTestname(String testname) {
this.testname = testname;
}
public void setJenkinsBuildNumber(String jenkinsBuildNumber) {
this.jenkinsBuildNumber = jenkinsBuildNumber;
}
public void setJiraLinkedIssue(String jiraLinkedIssue) {
this.jiraLinkedIssue = jiraLinkedIssue;
}
}
and it will contain the values for the HashMap, now when I dynamically poopulate the hashmap I have noticed that I do populate the exact number of keys exist in JSON data but I overwrite the value each time I bump into "indeni_version" that was already inserted into my hashmap which is not good as I want to add the values to the exist already key.
This is what I tried:
public class ReportGenerator {
private static HashMap<String, List<ReportGenreratorFields>> reportGeneratorMapper = new HashMap<>();
private static ReportGenreratorFields reportGenreratorFields = new ReportGenreratorFields();
private static List<ReportGenreratorFields> reportGeneratorFieldsList;
public static void main(String[] args) throws Exception {
try (ElasticSearchClient client = new ElasticSearchClient()) {
SearchResponse sr = client.dailyReport();
SearchHit[] results = sr.getHits().getHits();
for (SearchHit hit : results) {
reportGeneratorFieldsList = new ArrayList<>();
reportGenreratorFields.setTestname(source.get("test_name").toString());
reportGenreratorFields.setJenkinsBuildNumber(source.get("job_build_number").toString());
if (source.get("link_issue") != null) {
reportGenreratorFields.setJiraLinkedIssue(source.get("link_issue").toString());
} else {
reportGenreratorFields.setJiraLinkedIssue("new_bug");
}
reportGeneratorFieldsList.add(reportGenreratorFields);
reportGeneratorMapper.put(source.get("indeni_version").toString(), reportGeneratorFieldsList);
}catch(IOException e){
LOG.error("failed to fetch documents from elastic search cluster", e);
}
}
}
I forgot to mention that getting json data is from this line:
SearchResponse sr = client.dailyReport();
and hit
is just an item from the json data.
Upvotes: 0
Views: 302
Reputation: 744
Use this updated code:
public class ReportGenerator {
private static HashMap<String, List<ReportGenreratorFields>> reportGeneratorMapper = new HashMap<>();
public static void main(String[] args) throws Exception {
try (ElasticSearchClient client = new ElasticSearchClient()) {
SearchResponse sr = client.dailyReport();
SearchHit[] results = sr.getHits().getHits();
for (SearchHit hit : results) {
ReportGenreratorFields reportGenreratorFields = new ReportGenreratorFields();
reportGenreratorFields.setTestname(source.get("test_name").toString());
reportGenreratorFields.setJenkinsBuildNumber(source.get("job_build_number").toString());
if (source.get("link_issue") != null) {
reportGenreratorFields.setJiraLinkedIssue(source.get("link_issue").toString());
} else {
reportGenreratorFields.setJiraLinkedIssue("new_bug");
}
String indeniVer = source.get("indeni_version").toString();
//map contains that indeniVer then add to existing list
if (reportGeneratorMapper.containsKey(indeniVer)) {
List<ReportGenreratorFields> reportGeneratorFieldsList = reportGeneratorMapper.get(indeniVer);
reportGeneratorFieldsList.add(reportGenreratorFields);
reportGeneratorMapper.put(indeniVer, reportGeneratorFieldsList);
} else { //map does not contain that indeniVer
List<ReportGenreratorFields> reportGeneratorFieldsList = new ArrayList<>();
reportGeneratorFieldsList.add(reportGenreratorFields);
reportGeneratorMapper.put(indeniVer, reportGeneratorFieldsList);
}
}
} catch (IOException e) {
LOG.error("failed to fetch documents from elastic search cluster", e);
}
}
}
Upvotes: 1
Reputation: 331
Fix this line:
reportGeneratorMapper.put(source.get("indeni_version").toString(), reportGeneratorFieldsList);
to:
List<ReportGenreratorFields> existingList = reportGeneratorMapper.get(source.get("indeni_version").toString());
if (existingList == null)
reportGeneratorMapper.put(source.get("indeni_version").toString(), reportGeneratorFieldsList);
else{
existingList.add(reportGenreratorFields);
reportGeneratorMapper.put(source.get("indeni_version").toString(),
existingList);
}
Your code doesn't check for collisions. It blindly puts the new list and associates it with the key. In the code above, you first check if there is a list already associated to the key or not. If there is no one, you create a list and associate it to the key (the code you already wrote). If there is one, you get it, add your object to it and assign it again to your key.
Upvotes: 0