@Test public void delete_active_rules_from_profile() throws Exception { esSetup .client() .prepareBulk() // On profile 1 .add( Requests.indexRequest() .index("rules") .type("active_rule") .parent("1") .source(testFileAsString("delete_from_profile/active_rule25.json"))) .add( Requests.indexRequest() .index("rules") .type("active_rule") .parent("3") .source(testFileAsString("delete_from_profile/active_rule2702.json"))) // On profile 2 .add( Requests.indexRequest() .index("rules") .type("active_rule") .parent("2") .source(testFileAsString("delete_from_profile/active_rule523.json"))) .setRefresh(true) .execute() .actionGet(); esActiveRule.deleteActiveRulesFromProfile(1); assertThat(!esSetup.exists("rules", "active_rule", "25")); assertThat(!esSetup.exists("rules", "active_rule", "2702")); assertThat(esSetup.exists("rules", "active_rule", "523")); }
@Test public void update_existing_rules_and_forget_deleted_rules() { when(ruleDao.selectEnablesAndNonManual(session)) .thenReturn( newArrayList( new RuleDto() .setId(1) .setRepositoryKey("xoo") .setRuleKey("key1") .setSeverity(Severity.MINOR), new RuleDto() .setId(2) .setRepositoryKey("xoo") .setRuleKey("key2") .setSeverity(Severity.MINOR))); assertThat(esSetup.exists("rules", "rule", "3")).isTrue(); String[] ids = registry.reindex(session); registry.removeDeletedRules(ids); assertThat(registry.findIds(ImmutableMap.of("repositoryKey", "xoo"))) .hasSize(2) .containsOnly(1, 2); assertThat(esSetup.exists("rules", "rule", "3")).isFalse(); }
@Test public void reindex_existing_rule() { // Update severity to MAJOR RuleDto ruleDto = new RuleDto() .setId(3) .setRepositoryKey("repo") .setRuleKey("key") .setSeverity(Severity.MAJOR); assertThat(esSetup.exists("rules", "rule", "3")).isTrue(); registry.reindex(ruleDto); Map<String, Object> ruleDocument = esSetup .client() .prepareGet("rules", "rule", Integer.toString(3)) .execute() .actionGet() .getSourceAsMap(); assertThat(ruleDocument.get(RuleDocument.FIELD_ID)).isEqualTo(3); assertThat(ruleDocument.get(RuleDocument.FIELD_REPOSITORY_KEY)).isEqualTo("repo"); assertThat(ruleDocument.get(RuleDocument.FIELD_KEY)).isEqualTo("key"); assertThat(ruleDocument.get(RuleDocument.FIELD_SEVERITY)).isEqualTo("MAJOR"); }
@Test public void reindex_existing_rules() { assertThat(esSetup.exists("rules", "rule", "3")).isTrue(); // Update severity to MAJOR when(ruleDao.selectEnablesAndNonManual(session)) .thenReturn( newArrayList( new RuleDto() .setId(3) .setRepositoryKey("repo") .setRuleKey("key") .setSeverity(Severity.MAJOR))); registry.reindex(); Map<String, Object> ruleDocument = esSetup .client() .prepareGet("rules", "rule", Integer.toString(3)) .execute() .actionGet() .getSourceAsMap(); assertThat(ruleDocument.get(RuleDocument.FIELD_ID)).isEqualTo(3); assertThat(ruleDocument.get(RuleDocument.FIELD_REPOSITORY_KEY)).isEqualTo("repo"); assertThat(ruleDocument.get(RuleDocument.FIELD_KEY)).isEqualTo("key"); assertThat(ruleDocument.get(RuleDocument.FIELD_SEVERITY)).isEqualTo("MAJOR"); }
@Test public void bulk_index_active_rules_from_ids() throws IOException { when(myBatis.openSession(false)).thenReturn(session); List<Integer> ids = newArrayList(1); when(activeRuleDao.selectByIds(ids, session)) .thenReturn( newArrayList( new ActiveRuleDto() .setId(1) .setProfileId(10) .setRuleId(1) .setSeverity(Severity.MAJOR) .setParentId(5))); when(activeRuleDao.selectParamsByActiveRuleIds(ids, session)) .thenReturn( newArrayList( new ActiveRuleParamDto() .setId(1) .setActiveRuleId(1) .setRulesParameterId(1) .setKey("key") .setValue("RuleWithParameters"))); esActiveRule.bulkIndexActiveRules(ids); assertThat(esSetup.exists("rules", "active_rule", "1")); SearchHit[] parentHit = esSetup .client() .prepareSearch("rules") .setPostFilter(hasChildFilter("active_rule", termFilter("profileId", 10))) .execute() .actionGet() .getHits() .getHits(); assertThat(parentHit).hasSize(1); assertThat(parentHit[0].getId()).isEqualTo("1"); SearchHit[] childHit = esSetup .client() .prepareSearch("rules") .setPostFilter(hasParentFilter("rule", termFilter("key", "RuleWithParameters"))) .execute() .actionGet() .getHits() .getHits(); assertThat(childHit).hasSize(1); assertThat(childHit[0].getId()).isEqualTo("1"); }
@Test public void register_mapping_at_startup() { assertThat(esSetup.exists("rules")).isTrue(); assertThat( esSetup .client() .admin() .indices() .prepareTypesExists("rules") .setTypes("rule") .execute() .actionGet() .isExists()) .isTrue(); }
@Test public void bulk_index_active_rules() throws IOException { List<ActiveRuleDto> activeRules = newArrayList( new ActiveRuleDto() .setId(1) .setProfileId(10) .setRuleId(1) .setSeverity(Severity.MAJOR) .setParentId(5)); Multimap<Integer, ActiveRuleParamDto> paramsByActiveRule = ArrayListMultimap.create(); paramsByActiveRule.putAll( 1, newArrayList( new ActiveRuleParamDto() .setId(1) .setActiveRuleId(1) .setRulesParameterId(1) .setKey("key") .setValue("RuleWithParameters"))); esActiveRule.bulkIndexActiveRules(activeRules, paramsByActiveRule); assertThat(esSetup.exists("rules", "active_rule", "1")); SearchHit[] parentHit = esSetup .client() .prepareSearch("rules") .setPostFilter(hasChildFilter("active_rule", termFilter("profileId", 10))) .execute() .actionGet() .getHits() .getHits(); assertThat(parentHit).hasSize(1); assertThat(parentHit[0].getId()).isEqualTo("1"); SearchHit[] childHit = esSetup .client() .prepareSearch("rules") .setPostFilter(hasParentFilter("rule", termFilter("key", "RuleWithParameters"))) .execute() .actionGet() .getHits() .getHits(); assertThat(childHit).hasSize(1); assertThat(childHit[0].getId()).isEqualTo("1"); }
@Test public void bulk_index_active_rules_checking_into_db() throws IOException { List<ActiveRuleDto> activeRules = newArrayList( new ActiveRuleDto() .setId(1) .setProfileId(10) .setRuleId(1) .setSeverity(Severity.MAJOR) .setParentId(5) .setNoteData("polop") .setNoteCreatedAt(new Date()) .setNoteUserLogin("godin")); DbSession session = mock(DbSession.class); when(myBatis.openSession(false)).thenReturn(session); when(activeRuleDao.selectAll(session)).thenReturn(activeRules); when(activeRuleDao.selectAllParams(session)) .thenReturn(Lists.<ActiveRuleParamDto>newArrayList()); esActiveRule.bulkRegisterActiveRules(); assertThat(esSetup.exists("rules", "active_rule", "1")); SearchHit[] parentHit = esSetup .client() .prepareSearch("rules") .setPostFilter(hasChildFilter("active_rule", termFilter("profileId", 10))) .execute() .actionGet() .getHits() .getHits(); assertThat(parentHit).hasSize(1); assertThat(parentHit[0].getId()).isEqualTo("1"); SearchHit[] childHit = esSetup .client() .prepareSearch("rules") .setPostFilter(hasParentFilter("rule", termFilter("key", "RuleWithParameters"))) .execute() .actionGet() .getHits() .getHits(); assertThat(childHit).hasSize(1); assertThat(childHit[0].getId()).isEqualTo("1"); }
@Test public void save_active_rule() throws IOException { ActiveRuleDto activeRule = new ActiveRuleDto().setId(1).setProfileId(10).setRuleId(1).setSeverity(Severity.MAJOR); ArrayList<ActiveRuleParamDto> params = newArrayList( new ActiveRuleParamDto() .setId(1) .setActiveRuleId(1) .setRulesParameterId(1) .setKey("key") .setValue("RuleWithParameters")); esActiveRule.save(activeRule, params); assertThat(esSetup.exists("rules", "active_rule", "1")); SearchHit[] parentHit = esSetup .client() .prepareSearch("rules") .setPostFilter(hasChildFilter("active_rule", termFilter("profileId", 10))) .execute() .actionGet() .getHits() .getHits(); assertThat(parentHit).hasSize(1); assertThat(parentHit[0].getId()).isEqualTo("1"); SearchHit[] childHit = esSetup .client() .prepareSearch("rules") .setPostFilter(hasParentFilter("rule", termFilter("key", "RuleWithParameters"))) .execute() .actionGet() .getHits() .getHits(); assertThat(childHit).hasSize(1); assertThat(childHit[0].getId()).isEqualTo("1"); }