protected long getCount(final String index, final String type) { logger.debug("getCount()"); esSetup.client().admin().indices().refresh(new RefreshRequest()).actionGet(); final CountResponse count = esSetup.client().count(new CountRequest(index).types(type)).actionGet(); return count.getCount(); }
@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 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 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"); }
@Before public void setUp() throws Exception { when(myBatis.openSession(false)).thenReturn(session); esSetup = new EsSetup( ImmutableSettings.builder() .loadFromUrl(ESNode.class.getResource("config/elasticsearch.json")) .build()); esSetup.execute(EsSetup.deleteAll()); ESNode node = mock(ESNode.class); when(node.client()).thenReturn(esSetup.client()); Settings settings = new Settings(); settings.setProperty("sonar.log.profilingLevel", "FULL"); Profiling profiling = new Profiling(settings); searchIndex = new ESIndex(node, profiling); searchIndex.start(); RuleRegistry esRule = new RuleRegistry(searchIndex, null, null, null); esRule.start(); esActiveRule = new ESActiveRule(searchIndex, activeRuleDao, myBatis, profiling); esActiveRule.start(); esSetup.execute( EsSetup.index("rules", "rule", "1").withSource(testFileAsString("shared/rule1.json")), EsSetup.index("rules", "rule", "2").withSource(testFileAsString("shared/rule2.json")), EsSetup.index("rules", "rule", "3").withSource(testFileAsString("shared/rule3.json"))); }
@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 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"); }
@Before public void setUp() throws Exception { when(myBatis.openSession(false)).thenReturn(session); esSetup = new EsSetup( ImmutableSettings.builder() .loadFromUrl(ESNode.class.getResource("config/elasticsearch.json")) .build()); esSetup.execute(EsSetup.deleteAll()); ESNode node = mock(ESNode.class); when(node.client()).thenReturn(esSetup.client()); Settings settings = new Settings(); settings.setProperty("sonar.log.profilingLevel", "FULL"); Profiling profiling = new Profiling(settings); searchIndex = new ESIndex(node, profiling); searchIndex.start(); registry = new RuleRegistry(searchIndex, myBatis, ruleDao, characteristicDao); registry.start(); esSetup.execute( EsSetup.index("rules", "rule", "1").withSource(testFileAsString("shared/rule1.json")), EsSetup.index("rules", "rule", "2").withSource(testFileAsString("shared/rule2.json")), EsSetup.index("rules", "rule", "3").withSource(testFileAsString("shared/rule3.json")), EsSetup.index("rules", "rule", "50") .withSource(testFileAsString("shared/removed_rule.json"))); esSetup .client() .admin() .cluster() .prepareHealth(RuleRegistry.INDEX_RULES) .setWaitForGreenStatus() .execute() .actionGet(); }
@Test public void index_overridden_function_if_both_default_and_overridden_functions_exists_when_indexing_rules() { when(ruleDao.selectEnablesAndNonManual(session)) .thenReturn( newArrayList( new RuleDto() .setId(10) .setRepositoryKey("repo") .setRuleKey("key1") .setSeverity(Severity.MINOR) // default and overridden debt values are set .setDefaultSubCharacteristicId(11) .setDefaultRemediationFunction("CONSTANT_ISSUE") .setDefaultRemediationOffset("15min") .setSubCharacteristicId(11) .setRemediationFunction("LINEAR") .setRemediationCoefficient("1h"))); when(characteristicDao.selectCharacteristicsByIds(newHashSet(11), session)) .thenReturn( newArrayList( new CharacteristicDto() .setId(11) .setKey("MODULARITY") .setName("Modularity") .setParentId(10))); when(characteristicDao.selectCharacteristicsByIds(newHashSet(10), session)) .thenReturn( newArrayList( new CharacteristicDto().setId(10).setKey("REUSABILITY").setName("Reusability"))); registry.reindex(); Map<String, Object> ruleDocument = esSetup .client() .prepareGet("rules", "rule", Integer.toString(10)) .execute() .actionGet() .getSourceAsMap(); assertThat(ruleDocument.get(RuleDocument.FIELD_CHARACTERISTIC_ID)).isEqualTo(10); assertThat(ruleDocument.get(RuleDocument.FIELD_CHARACTERISTIC_KEY)).isEqualTo("REUSABILITY"); assertThat(ruleDocument.get(RuleDocument.FIELD_SUB_CHARACTERISTIC_ID)).isEqualTo(11); assertThat(ruleDocument.get(RuleDocument.FIELD_SUB_CHARACTERISTIC_KEY)).isEqualTo("MODULARITY"); assertThat(ruleDocument.get(RuleDocument.FIELD_REMEDIATION_FUNCTION)).isEqualTo("LINEAR"); assertThat(ruleDocument.get(RuleDocument.FIELD_REMEDIATION_COEFFICIENT)).isEqualTo("1h"); assertThat(ruleDocument.get(RuleDocument.FIELD_REMEDIATION_OFFSET)).isNull(); }
@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(); }
protected void registerRiver(final String typename, final String file) throws ElasticsearchException, IOException { final IndexResponse res = esSetup .client() .prepareIndex() .setIndex("_river") .setType(typename) .setId("_meta") .setSource(loadFile(file)) .execute() .actionGet(); if (!res.isCreated()) { throw new IOException("Unable to register river"); } }
@Test public void should_delete_from_integer_ids() throws Exception { esSetup .client() .prepareBulk() .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"))) .setRefresh(true) .execute() .actionGet(); esActiveRule.deleteActiveRules(ImmutableList.of(25, 2702)); }
@Test public void index_one_rule() { RuleDto ruleDto = new RuleDto() .setId(3) .setRepositoryKey("repo") .setRuleKey("key") .setSeverity(Severity.MINOR) .setNoteData("noteData") .setNoteUserLogin("userLogin") .setDefaultSubCharacteristicId(11) .setDefaultRemediationFunction("LINEAR_OFFSET") .setDefaultRemediationCoefficient("1h") .setDefaultRemediationOffset("15min"); when(ruleDao.selectParametersByRuleIds(newArrayList(3), session)) .thenReturn(newArrayList(new RuleParamDto().setRuleId(3).setName("name"))); when(ruleDao.selectTagsByRuleIds(newArrayList(3), session)) .thenReturn( newArrayList( new RuleRuleTagDto().setRuleId(3).setTag("tag1").setType(RuleTagType.SYSTEM), new RuleRuleTagDto().setRuleId(3).setTag("tag2").setType(RuleTagType.SYSTEM), new RuleRuleTagDto().setRuleId(3).setTag("tag").setType(RuleTagType.ADMIN))); when(characteristicDao.selectById(11, session)) .thenReturn( new CharacteristicDto() .setId(11) .setKey("MODULARITY") .setName("Modularity") .setParentId(10)); when(characteristicDao.selectById(10, session)) .thenReturn(new CharacteristicDto().setId(10).setKey("REUSABILITY").setName("Reusability")); 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("MINOR"); assertThat(ruleDocument.get(RuleDocument.FIELD_NOTE)).isNotNull(); assertThat((List<String>) ruleDocument.get(RuleDocument.FIELD_PARAMS)).hasSize(1); assertThat((List<String>) ruleDocument.get(RuleDocument.FIELD_SYSTEM_TAGS)).hasSize(2); assertThat((List<String>) ruleDocument.get(RuleDocument.FIELD_ADMIN_TAGS)).hasSize(1); assertThat(ruleDocument.get(RuleDocument.FIELD_CHARACTERISTIC_ID)).isEqualTo(10); assertThat(ruleDocument.get(RuleDocument.FIELD_CHARACTERISTIC_KEY)).isEqualTo("REUSABILITY"); assertThat(ruleDocument.get(RuleDocument.FIELD_SUB_CHARACTERISTIC_ID)).isEqualTo(11); assertThat(ruleDocument.get(RuleDocument.FIELD_SUB_CHARACTERISTIC_KEY)).isEqualTo("MODULARITY"); assertThat(ruleDocument.get(RuleDocument.FIELD_REMEDIATION_FUNCTION)) .isEqualTo("LINEAR_OFFSET"); assertThat(ruleDocument.get(RuleDocument.FIELD_REMEDIATION_COEFFICIENT)).isEqualTo("1h"); assertThat(ruleDocument.get(RuleDocument.FIELD_REMEDIATION_OFFSET)).isEqualTo("15min"); }