@Test public void getUntaggedIssuesByapplicationFromESTest() throws Exception { JsonArray untaggedIsuueByApp; String response = "{\"took\":1660,\"timed_out\":false,\"_shards\":{\"total\":176,\"successful\":176,\"failed\":0},\"hits\":{\"total\":58839,\"max_score\":0.0,\"hits\":[]},\"aggregations\":{\"apps\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"My-TMO\",\"doc_count\":3062,\"tags\":{\"buckets\":{\"Environment\":{\"doc_count\":154},\"Role\":{\"doc_count\":677},\"Stack\":{\"doc_count\":3052}}}},{\"key\":\"Layer3\",\"doc_count\":1897,\"tags\":{\"buckets\":{\"Environment\":{\"doc_count\":1784},\"Role\":{\"doc_count\":1896},\"Stack\":{\"doc_count\":1263}}}}]}}}"; mockStatic(PacHttpUtils.class); String mandatoryTags = "Application,env"; when(PacHttpUtils.doHttpPost(anyString(), anyString())).thenReturn(response); ReflectionTestUtils.setField(taggingRepositoryImpl, "esUrl", "dummyEsURL"); untaggedIsuueByApp = taggingRepositoryImpl.getUntaggedIssuesByapplicationFromES("aws-all", mandatoryTags, "rebellion", 1, 10); assertTrue(untaggedIsuueByApp.size() > 0); untaggedIsuueByApp = taggingRepositoryImpl.getUntaggedIssuesByapplicationFromES("aws-all", mandatoryTags, null, 1, 10); assertTrue(untaggedIsuueByApp.size() > 0); when(PacHttpUtils.doHttpPost(anyString(), anyString())).thenThrow(new DataException()); assertThatThrownBy( () -> taggingRepositoryImpl.getUntaggedIssuesByapplicationFromES("aws-all", mandatoryTags, null, 1, 10)) .isInstanceOf(DataException.class); }
public JsonArray getUntaggedIssuesByapplicationFromES(String assetGroup, String mandatoryTags, String searchText, int from, int size) throws DataException { List<String> mandatoryTagsList = new ArrayList<>(); if(!com.amazonaws.util.StringUtils.isNullOrEmpty(mandatoryTags)){ mandatoryTagsList = Arrays .asList(mandatoryTags.split(",")); } String responseJson = null; JsonParser jsonParser; JsonObject resultJson; StringBuilder urlToQueryBuffer = new StringBuilder(esUrl).append("/") .append(assetGroup).append("/").append(SEARCH); StringBuilder requestBody = new StringBuilder( "{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"term\":{\"type.keyword\":{\"value\":\"issue\"}}},{\"term\":{\"policyId.keyword\":{\"value\":\"PacMan_TaggingRule_version-1\"}}}"); if (!Strings.isNullOrEmpty(searchText)) { requestBody.append("," + "{\"match_phrase_prefix\":{\"_all\":\"" + searchText + "\"" + "}}"); } requestBody .append("],\"should\":[{\"term\":{\"issueStatus.keyword\":{\"value\":\"open\"}}}],\"minimum_should_match\":1}},\"aggs\":{\"apps\":{\"terms\":{\"field\":\"tags.Application.keyword\",\"size\":" + pageSize + "},\"aggs\":{\"tags\":{\"filters\":{\"filters\":{"); for (String tags : mandatoryTagsList) { if (!"Application".equalsIgnoreCase(tags)) { requestBody.append("\"" + tags + "\"" + ":{\"bool\":{\"must\":[{\"match\":{\"missingTags\":" + "\"" + tags + "\"" + "}}]}},"); } } requestBody.setLength(requestBody.length() - 1); requestBody.append("}}}}}}}"); try{ responseJson = PacHttpUtils.doHttpPost(urlToQueryBuffer.toString(), requestBody.toString()); } catch (Exception e) { throw new DataException(e); } jsonParser = new JsonParser(); resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get( AGGREGATIONS).toString()); return aggsJson.getAsJsonObject("apps").getAsJsonArray(BUCKETS); }
TaggingRepositoryImpl implements TaggingRepository, Constants { public JsonArray getUntaggedIssuesByapplicationFromES(String assetGroup, String mandatoryTags, String searchText, int from, int size) throws DataException { List<String> mandatoryTagsList = new ArrayList<>(); if(!com.amazonaws.util.StringUtils.isNullOrEmpty(mandatoryTags)){ mandatoryTagsList = Arrays .asList(mandatoryTags.split(",")); } String responseJson = null; JsonParser jsonParser; JsonObject resultJson; StringBuilder urlToQueryBuffer = new StringBuilder(esUrl).append("/") .append(assetGroup).append("/").append(SEARCH); StringBuilder requestBody = new StringBuilder( "{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"term\":{\"type.keyword\":{\"value\":\"issue\"}}},{\"term\":{\"policyId.keyword\":{\"value\":\"PacMan_TaggingRule_version-1\"}}}"); if (!Strings.isNullOrEmpty(searchText)) { requestBody.append("," + "{\"match_phrase_prefix\":{\"_all\":\"" + searchText + "\"" + "}}"); } requestBody .append("],\"should\":[{\"term\":{\"issueStatus.keyword\":{\"value\":\"open\"}}}],\"minimum_should_match\":1}},\"aggs\":{\"apps\":{\"terms\":{\"field\":\"tags.Application.keyword\",\"size\":" + pageSize + "},\"aggs\":{\"tags\":{\"filters\":{\"filters\":{"); for (String tags : mandatoryTagsList) { if (!"Application".equalsIgnoreCase(tags)) { requestBody.append("\"" + tags + "\"" + ":{\"bool\":{\"must\":[{\"match\":{\"missingTags\":" + "\"" + tags + "\"" + "}}]}},"); } } requestBody.setLength(requestBody.length() - 1); requestBody.append("}}}}}}}"); try{ responseJson = PacHttpUtils.doHttpPost(urlToQueryBuffer.toString(), requestBody.toString()); } catch (Exception e) { throw new DataException(e); } jsonParser = new JsonParser(); resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get( AGGREGATIONS).toString()); return aggsJson.getAsJsonObject("apps").getAsJsonArray(BUCKETS); } }
TaggingRepositoryImpl implements TaggingRepository, Constants { public JsonArray getUntaggedIssuesByapplicationFromES(String assetGroup, String mandatoryTags, String searchText, int from, int size) throws DataException { List<String> mandatoryTagsList = new ArrayList<>(); if(!com.amazonaws.util.StringUtils.isNullOrEmpty(mandatoryTags)){ mandatoryTagsList = Arrays .asList(mandatoryTags.split(",")); } String responseJson = null; JsonParser jsonParser; JsonObject resultJson; StringBuilder urlToQueryBuffer = new StringBuilder(esUrl).append("/") .append(assetGroup).append("/").append(SEARCH); StringBuilder requestBody = new StringBuilder( "{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"term\":{\"type.keyword\":{\"value\":\"issue\"}}},{\"term\":{\"policyId.keyword\":{\"value\":\"PacMan_TaggingRule_version-1\"}}}"); if (!Strings.isNullOrEmpty(searchText)) { requestBody.append("," + "{\"match_phrase_prefix\":{\"_all\":\"" + searchText + "\"" + "}}"); } requestBody .append("],\"should\":[{\"term\":{\"issueStatus.keyword\":{\"value\":\"open\"}}}],\"minimum_should_match\":1}},\"aggs\":{\"apps\":{\"terms\":{\"field\":\"tags.Application.keyword\",\"size\":" + pageSize + "},\"aggs\":{\"tags\":{\"filters\":{\"filters\":{"); for (String tags : mandatoryTagsList) { if (!"Application".equalsIgnoreCase(tags)) { requestBody.append("\"" + tags + "\"" + ":{\"bool\":{\"must\":[{\"match\":{\"missingTags\":" + "\"" + tags + "\"" + "}}]}},"); } } requestBody.setLength(requestBody.length() - 1); requestBody.append("}}}}}}}"); try{ responseJson = PacHttpUtils.doHttpPost(urlToQueryBuffer.toString(), requestBody.toString()); } catch (Exception e) { throw new DataException(e); } jsonParser = new JsonParser(); resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get( AGGREGATIONS).toString()); return aggsJson.getAsJsonObject("apps").getAsJsonArray(BUCKETS); }  }
TaggingRepositoryImpl implements TaggingRepository, Constants { public JsonArray getUntaggedIssuesByapplicationFromES(String assetGroup, String mandatoryTags, String searchText, int from, int size) throws DataException { List<String> mandatoryTagsList = new ArrayList<>(); if(!com.amazonaws.util.StringUtils.isNullOrEmpty(mandatoryTags)){ mandatoryTagsList = Arrays .asList(mandatoryTags.split(",")); } String responseJson = null; JsonParser jsonParser; JsonObject resultJson; StringBuilder urlToQueryBuffer = new StringBuilder(esUrl).append("/") .append(assetGroup).append("/").append(SEARCH); StringBuilder requestBody = new StringBuilder( "{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"term\":{\"type.keyword\":{\"value\":\"issue\"}}},{\"term\":{\"policyId.keyword\":{\"value\":\"PacMan_TaggingRule_version-1\"}}}"); if (!Strings.isNullOrEmpty(searchText)) { requestBody.append("," + "{\"match_phrase_prefix\":{\"_all\":\"" + searchText + "\"" + "}}"); } requestBody .append("],\"should\":[{\"term\":{\"issueStatus.keyword\":{\"value\":\"open\"}}}],\"minimum_should_match\":1}},\"aggs\":{\"apps\":{\"terms\":{\"field\":\"tags.Application.keyword\",\"size\":" + pageSize + "},\"aggs\":{\"tags\":{\"filters\":{\"filters\":{"); for (String tags : mandatoryTagsList) { if (!"Application".equalsIgnoreCase(tags)) { requestBody.append("\"" + tags + "\"" + ":{\"bool\":{\"must\":[{\"match\":{\"missingTags\":" + "\"" + tags + "\"" + "}}]}},"); } } requestBody.setLength(requestBody.length() - 1); requestBody.append("}}}}}}}"); try{ responseJson = PacHttpUtils.doHttpPost(urlToQueryBuffer.toString(), requestBody.toString()); } catch (Exception e) { throw new DataException(e); } jsonParser = new JsonParser(); resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get( AGGREGATIONS).toString()); return aggsJson.getAsJsonObject("apps").getAsJsonArray(BUCKETS); }  JsonArray getUntaggedIssuesByapplicationFromES(String assetGroup,
            String mandatoryTags, String searchText, int from, int size); List<Map<String, Object>> getRuleParamsFromDbByPolicyId(
            String policyId); Long getUntaggedIssues(String assetGroup, String mandatoryTag); List<Map<String, Object>> getRuleTargetTypesFromDbByPolicyId(
            String policyId); String getUntaggedTargetTypeIssues(
            UntaggedTargetTypeRequest request, List<String> tagsList); @Override String getTaggingByApplication(String ag, String targetType); }
TaggingRepositoryImpl implements TaggingRepository, Constants { public JsonArray getUntaggedIssuesByapplicationFromES(String assetGroup, String mandatoryTags, String searchText, int from, int size) throws DataException { List<String> mandatoryTagsList = new ArrayList<>(); if(!com.amazonaws.util.StringUtils.isNullOrEmpty(mandatoryTags)){ mandatoryTagsList = Arrays .asList(mandatoryTags.split(",")); } String responseJson = null; JsonParser jsonParser; JsonObject resultJson; StringBuilder urlToQueryBuffer = new StringBuilder(esUrl).append("/") .append(assetGroup).append("/").append(SEARCH); StringBuilder requestBody = new StringBuilder( "{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"term\":{\"type.keyword\":{\"value\":\"issue\"}}},{\"term\":{\"policyId.keyword\":{\"value\":\"PacMan_TaggingRule_version-1\"}}}"); if (!Strings.isNullOrEmpty(searchText)) { requestBody.append("," + "{\"match_phrase_prefix\":{\"_all\":\"" + searchText + "\"" + "}}"); } requestBody .append("],\"should\":[{\"term\":{\"issueStatus.keyword\":{\"value\":\"open\"}}}],\"minimum_should_match\":1}},\"aggs\":{\"apps\":{\"terms\":{\"field\":\"tags.Application.keyword\",\"size\":" + pageSize + "},\"aggs\":{\"tags\":{\"filters\":{\"filters\":{"); for (String tags : mandatoryTagsList) { if (!"Application".equalsIgnoreCase(tags)) { requestBody.append("\"" + tags + "\"" + ":{\"bool\":{\"must\":[{\"match\":{\"missingTags\":" + "\"" + tags + "\"" + "}}]}},"); } } requestBody.setLength(requestBody.length() - 1); requestBody.append("}}}}}}}"); try{ responseJson = PacHttpUtils.doHttpPost(urlToQueryBuffer.toString(), requestBody.toString()); } catch (Exception e) { throw new DataException(e); } jsonParser = new JsonParser(); resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get( AGGREGATIONS).toString()); return aggsJson.getAsJsonObject("apps").getAsJsonArray(BUCKETS); }  JsonArray getUntaggedIssuesByapplicationFromES(String assetGroup,
            String mandatoryTags, String searchText, int from, int size); List<Map<String, Object>> getRuleParamsFromDbByPolicyId(
            String policyId); Long getUntaggedIssues(String assetGroup, String mandatoryTag); List<Map<String, Object>> getRuleTargetTypesFromDbByPolicyId(
            String policyId); String getUntaggedTargetTypeIssues(
            UntaggedTargetTypeRequest request, List<String> tagsList); @Override String getTaggingByApplication(String ag, String targetType);  }