@Test public void getClosedIssueInfoTest() throws Exception { List<Map<String, Object>> closedIssueInfo = new ArrayList<>(); Map<String, Object> issueinfo = new HashMap<>(); issueinfo.put("resourceid", "i-12344"); issueinfo.put("issueStatus", "closed"); issueinfo.put("reason-to-close", "Kernel version compliant"); closedIssueInfo.add(issueinfo); ReflectionTestUtils.setField(patchingRepositoryImpl, "esUrl", "dummyEsURL"); String response = "{\"took\":2205,\"timed_out\":false,\"_shards\":{\"total\":3,\"successful\":3,\"failed\":0},\"hits\":{\"total\":4151,\"max_score\":3.7548497," + "\"hits\":[{\"_index\":\"aws_ec2\",\"_type\":\"_type\",\"_id\":\"_id\",\"_score\":3.7548497,\"_routing\":\"_routing\",\"_parent\":\"_parent\",\"_source\":{\"severity\":\"high\",\"_resourceid\":\"_resourceid\",\"ruleCategory\":\"security\",\"closeddate\":\"2018-02-15T05:38:34.533Z\",\"_docid\":\"_docid\",\"failedTypes\":\"[spaceandsat, ldap, rhnsystemdetails, ssh]\",\"targetType\":\"ec2\",\"type\":\"issue\",\"accountid\":\"accountid\",\"executionId\":\"executionId\",\"issueStatus\":\"closed\",\"createdDate\":\"2018-02-15T04:38:23.754Z\",\"policyId\":\"PacMan_cloud-kernel-compliance_version-1\",\"accountname\":\"test\",\"pac_ds\":\"aws\",\"modifiedDate\":\"2018-02-15T04:38:23.754Z\",\"reason-to-close\":\"{sourceType=spaceandsat, kernelVersion=kernelVersion, description=Kernel version is compliant}\",\"ruleId\":\"PacMan_cloud-kernel-compliance_version-1_Ec2-Kernel-Compliance-Rule_ec2\",\"region\":\"region\",\"desc\":\"Kernel version not found.validated using:spaceandsat/ldap/rhnsystemdetails/ssh\"}}]}}"; mockStatic(PacHttpUtils.class); when(PacHttpUtils.doHttpPost(anyString(), anyString())).thenReturn(response); patchingRepositoryImpl.getClosedIssueInfo("aws-all", 100); String dummyResponse = "{\"took\":2205,\"timed_out\":false,\"_shards\":{\"total\":3,\"successful\":3,\"failed\":0}}"; when(PacHttpUtils.doHttpPost(anyString(), anyString())).thenReturn(dummyResponse); patchingRepositoryImpl.getClosedIssueInfo("aws-all", 100); String emptyHits = "{\"took\":2205,\"timed_out\":false,\"_shards\":{\"total\":3,\"successful\":3,\"failed\":0},\"hits\":{\"total\":4151,\"max_score\":3.7548497}}"; when(PacHttpUtils.doHttpPost(anyString(), anyString())).thenReturn(emptyHits); patchingRepositoryImpl.getClosedIssueInfo("aws-all", 100); when(PacHttpUtils.doHttpPost(anyString(), anyString())).thenThrow(new RuntimeException()); assertThatThrownBy(() -> patchingRepositoryImpl.getClosedIssueInfo("aws-all", 100)).isInstanceOf( DataException.class); }
public List<Map<String, Object>> getClosedIssueInfo(String assetGroup, int size) throws DataException { String responseJson = null; List<Map<String, Object>> issueDetails = null; List<Map<String, Object>> closedLissues = new ArrayList<>(); Gson serializer = new GsonBuilder().create(); StringBuilder requestBody = new StringBuilder( "{\"size\":\"" + size + "\",\"_source\":[\"_resourceid\",\"reason-to-close\"],\"sort\":[{\"closeddate\":{\"order\":\"desc\"}}],\"query\":{\"bool\":{\"must\":[{\"match\":{\"issueStatus.keyword\":\"closed\"}},{\"match\":{\"ruleId.keyword\":\"" + EC2_KERNEL_COMPLIANCE_RULE + "\"}},{\"exists\":{\"field\":\"reason-to-close\"}}],\"must_not\":[{\"match\":{\"reason-to-close.keyword\":\"Rule evaluation sucessfull\"}},{\"match\":{\"reason-to-close.keyword\":\"resource not found\"}}]}}}"); StringBuilder urlToQueryBuffer = new StringBuilder(esUrl).append("/") .append(assetGroup).append("/").append(SEARCH); try{ responseJson = PacHttpUtils.doHttpPost(urlToQueryBuffer.toString(), requestBody.toString()); } catch(Exception e){ throw new DataException(e); } Map<String, Object> responseMap = (Map<String, Object>) serializer .fromJson(responseJson, Object.class); if (responseMap.containsKey("hits")) { Map<String, Object> hits = (Map<String, Object>) responseMap .get("hits"); if (hits.containsKey("hits")) { issueDetails = (List<Map<String, Object>>) hits.get("hits"); for (Map<String, Object> issueDetail : issueDetails) { Map<String, Object> sourceMap = (Map<String, Object>) issueDetail .get("_source"); closedLissues.add(sourceMap); } } } return closedLissues; }
PatchingRepositoryImpl implements PatchingRepository, Constants { public List<Map<String, Object>> getClosedIssueInfo(String assetGroup, int size) throws DataException { String responseJson = null; List<Map<String, Object>> issueDetails = null; List<Map<String, Object>> closedLissues = new ArrayList<>(); Gson serializer = new GsonBuilder().create(); StringBuilder requestBody = new StringBuilder( "{\"size\":\"" + size + "\",\"_source\":[\"_resourceid\",\"reason-to-close\"],\"sort\":[{\"closeddate\":{\"order\":\"desc\"}}],\"query\":{\"bool\":{\"must\":[{\"match\":{\"issueStatus.keyword\":\"closed\"}},{\"match\":{\"ruleId.keyword\":\"" + EC2_KERNEL_COMPLIANCE_RULE + "\"}},{\"exists\":{\"field\":\"reason-to-close\"}}],\"must_not\":[{\"match\":{\"reason-to-close.keyword\":\"Rule evaluation sucessfull\"}},{\"match\":{\"reason-to-close.keyword\":\"resource not found\"}}]}}}"); StringBuilder urlToQueryBuffer = new StringBuilder(esUrl).append("/") .append(assetGroup).append("/").append(SEARCH); try{ responseJson = PacHttpUtils.doHttpPost(urlToQueryBuffer.toString(), requestBody.toString()); } catch(Exception e){ throw new DataException(e); } Map<String, Object> responseMap = (Map<String, Object>) serializer .fromJson(responseJson, Object.class); if (responseMap.containsKey("hits")) { Map<String, Object> hits = (Map<String, Object>) responseMap .get("hits"); if (hits.containsKey("hits")) { issueDetails = (List<Map<String, Object>>) hits.get("hits"); for (Map<String, Object> issueDetail : issueDetails) { Map<String, Object> sourceMap = (Map<String, Object>) issueDetail .get("_source"); closedLissues.add(sourceMap); } } } return closedLissues; } }
PatchingRepositoryImpl implements PatchingRepository, Constants { public List<Map<String, Object>> getClosedIssueInfo(String assetGroup, int size) throws DataException { String responseJson = null; List<Map<String, Object>> issueDetails = null; List<Map<String, Object>> closedLissues = new ArrayList<>(); Gson serializer = new GsonBuilder().create(); StringBuilder requestBody = new StringBuilder( "{\"size\":\"" + size + "\",\"_source\":[\"_resourceid\",\"reason-to-close\"],\"sort\":[{\"closeddate\":{\"order\":\"desc\"}}],\"query\":{\"bool\":{\"must\":[{\"match\":{\"issueStatus.keyword\":\"closed\"}},{\"match\":{\"ruleId.keyword\":\"" + EC2_KERNEL_COMPLIANCE_RULE + "\"}},{\"exists\":{\"field\":\"reason-to-close\"}}],\"must_not\":[{\"match\":{\"reason-to-close.keyword\":\"Rule evaluation sucessfull\"}},{\"match\":{\"reason-to-close.keyword\":\"resource not found\"}}]}}}"); StringBuilder urlToQueryBuffer = new StringBuilder(esUrl).append("/") .append(assetGroup).append("/").append(SEARCH); try{ responseJson = PacHttpUtils.doHttpPost(urlToQueryBuffer.toString(), requestBody.toString()); } catch(Exception e){ throw new DataException(e); } Map<String, Object> responseMap = (Map<String, Object>) serializer .fromJson(responseJson, Object.class); if (responseMap.containsKey("hits")) { Map<String, Object> hits = (Map<String, Object>) responseMap .get("hits"); if (hits.containsKey("hits")) { issueDetails = (List<Map<String, Object>>) hits.get("hits"); for (Map<String, Object> issueDetail : issueDetails) { Map<String, Object> sourceMap = (Map<String, Object>) issueDetail .get("_source"); closedLissues.add(sourceMap); } } } return closedLissues; }  }
PatchingRepositoryImpl implements PatchingRepository, Constants { public List<Map<String, Object>> getClosedIssueInfo(String assetGroup, int size) throws DataException { String responseJson = null; List<Map<String, Object>> issueDetails = null; List<Map<String, Object>> closedLissues = new ArrayList<>(); Gson serializer = new GsonBuilder().create(); StringBuilder requestBody = new StringBuilder( "{\"size\":\"" + size + "\",\"_source\":[\"_resourceid\",\"reason-to-close\"],\"sort\":[{\"closeddate\":{\"order\":\"desc\"}}],\"query\":{\"bool\":{\"must\":[{\"match\":{\"issueStatus.keyword\":\"closed\"}},{\"match\":{\"ruleId.keyword\":\"" + EC2_KERNEL_COMPLIANCE_RULE + "\"}},{\"exists\":{\"field\":\"reason-to-close\"}}],\"must_not\":[{\"match\":{\"reason-to-close.keyword\":\"Rule evaluation sucessfull\"}},{\"match\":{\"reason-to-close.keyword\":\"resource not found\"}}]}}}"); StringBuilder urlToQueryBuffer = new StringBuilder(esUrl).append("/") .append(assetGroup).append("/").append(SEARCH); try{ responseJson = PacHttpUtils.doHttpPost(urlToQueryBuffer.toString(), requestBody.toString()); } catch(Exception e){ throw new DataException(e); } Map<String, Object> responseMap = (Map<String, Object>) serializer .fromJson(responseJson, Object.class); if (responseMap.containsKey("hits")) { Map<String, Object> hits = (Map<String, Object>) responseMap .get("hits"); if (hits.containsKey("hits")) { issueDetails = (List<Map<String, Object>>) hits.get("hits"); for (Map<String, Object> issueDetail : issueDetails) { Map<String, Object> sourceMap = (Map<String, Object>) issueDetail .get("_source"); closedLissues.add(sourceMap); } } } return closedLissues; }  @Override List<Map<String, Object>> getPatchingProgress(String assetGroup,
            LocalDate startDate, LocalDate endDate); @Override List<Map<String, Object>> getIssueInfo(String assetGroup); List<Map<String, Object>> getClosedIssueInfo(String assetGroup,
            int size); @Override List<Map<String, Object>> getInstanceInfo(String assetGroup,
            Map<String, String> filters); @Override List<Map<String, Object>> getExecAndDirectorInfo(); @Override long getInstanceInfoCount(String assetGroup,
            Map<String, String> filters, String searchText); @Override String getAmilAvailDate(int year, int quarter); @Override int getPatchingWindow(); @Override Map<String, Long> getQuartersWithPatchingData(String assetGroup); Map<String, Object> addParentConditionPatching(String targetType); @Override List<Map<String, Object>> getOnpremIssueInfo(String assetGroup); @Override List<Map<String, Object>> getOnpremResourceInfo(String assetGroup,
            Map<String, String> filters); @Override Map<String, Object> getDirectorsAndExcutiveSponsers(String appTag,
            String appType); @Override Map<String, Long> getNonCompliantNumberForAgAndResourceType(
            String assetGroup, String resourceType); @Override List<Map<String, Object>> getPatchingPercentForDateRange(String assetGroup, LocalDate fromDate,
			LocalDate toDate); }
PatchingRepositoryImpl implements PatchingRepository, Constants { public List<Map<String, Object>> getClosedIssueInfo(String assetGroup, int size) throws DataException { String responseJson = null; List<Map<String, Object>> issueDetails = null; List<Map<String, Object>> closedLissues = new ArrayList<>(); Gson serializer = new GsonBuilder().create(); StringBuilder requestBody = new StringBuilder( "{\"size\":\"" + size + "\",\"_source\":[\"_resourceid\",\"reason-to-close\"],\"sort\":[{\"closeddate\":{\"order\":\"desc\"}}],\"query\":{\"bool\":{\"must\":[{\"match\":{\"issueStatus.keyword\":\"closed\"}},{\"match\":{\"ruleId.keyword\":\"" + EC2_KERNEL_COMPLIANCE_RULE + "\"}},{\"exists\":{\"field\":\"reason-to-close\"}}],\"must_not\":[{\"match\":{\"reason-to-close.keyword\":\"Rule evaluation sucessfull\"}},{\"match\":{\"reason-to-close.keyword\":\"resource not found\"}}]}}}"); StringBuilder urlToQueryBuffer = new StringBuilder(esUrl).append("/") .append(assetGroup).append("/").append(SEARCH); try{ responseJson = PacHttpUtils.doHttpPost(urlToQueryBuffer.toString(), requestBody.toString()); } catch(Exception e){ throw new DataException(e); } Map<String, Object> responseMap = (Map<String, Object>) serializer .fromJson(responseJson, Object.class); if (responseMap.containsKey("hits")) { Map<String, Object> hits = (Map<String, Object>) responseMap .get("hits"); if (hits.containsKey("hits")) { issueDetails = (List<Map<String, Object>>) hits.get("hits"); for (Map<String, Object> issueDetail : issueDetails) { Map<String, Object> sourceMap = (Map<String, Object>) issueDetail .get("_source"); closedLissues.add(sourceMap); } } } return closedLissues; }  @Override List<Map<String, Object>> getPatchingProgress(String assetGroup,
            LocalDate startDate, LocalDate endDate); @Override List<Map<String, Object>> getIssueInfo(String assetGroup); List<Map<String, Object>> getClosedIssueInfo(String assetGroup,
            int size); @Override List<Map<String, Object>> getInstanceInfo(String assetGroup,
            Map<String, String> filters); @Override List<Map<String, Object>> getExecAndDirectorInfo(); @Override long getInstanceInfoCount(String assetGroup,
            Map<String, String> filters, String searchText); @Override String getAmilAvailDate(int year, int quarter); @Override int getPatchingWindow(); @Override Map<String, Long> getQuartersWithPatchingData(String assetGroup); Map<String, Object> addParentConditionPatching(String targetType); @Override List<Map<String, Object>> getOnpremIssueInfo(String assetGroup); @Override List<Map<String, Object>> getOnpremResourceInfo(String assetGroup,
            Map<String, String> filters); @Override Map<String, Object> getDirectorsAndExcutiveSponsers(String appTag,
            String appType); @Override Map<String, Long> getNonCompliantNumberForAgAndResourceType(
            String assetGroup, String resourceType); @Override List<Map<String, Object>> getPatchingPercentForDateRange(String assetGroup, LocalDate fromDate,
			LocalDate toDate);  }