]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5174 Create a method to reindex one rule
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 2 Apr 2014 07:32:51 +0000 (09:32 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 2 Apr 2014 07:37:57 +0000 (09:37 +0200)
sonar-server/src/main/java/org/sonar/server/rule/RuleOperations.java
sonar-server/src/main/java/org/sonar/server/rule/RuleRegistry.java
sonar-server/src/test/java/org/sonar/server/rule/RuleOperationsTest.java
sonar-server/src/test/java/org/sonar/server/rule/RuleRegistryTest.java

index d318fefa8d8336d6b8c17c3ffcdc67342a3ca0fe..afd53b9be2455ee10e98d88ebd646e73a0bb120c 100644 (file)
@@ -363,10 +363,7 @@ public class RuleOperations implements ServerComponent {
   }
 
   private void reindexRule(RuleDto rule, SqlSession session) {
-    Integer subCharacteristicId = rule.getSubCharacteristicId();
-    CharacteristicDto subCharacteristic = subCharacteristicId != null ? characteristicDao.selectById(subCharacteristicId, session) : null;
-    CharacteristicDto characteristic = subCharacteristic != null ? characteristicDao.selectById(subCharacteristic.getParentId(), session) : null;
-    ruleRegistry.save(rule, characteristic, subCharacteristic, ruleDao.selectParametersByRuleId(rule.getId(), session), ruleDao.selectTagsByRuleIds(rule.getId(), session));
+    ruleRegistry.reindex(rule, session);
   }
 
   private void checkPermission(UserSession userSession) {
index b41c5dd49e92b58b3ce18078c8e649acf1737f77..e2eb6cd5fb135ba7dd501b20b82c0c3802cf6547 100644 (file)
@@ -115,6 +115,9 @@ public class RuleRegistry {
     return reindex(ruleDao.selectEnablesAndNonManual(session), session);
   }
 
+  /**
+   * Reindex a given list of rules
+   */
   public String[] reindex(Collection<RuleDto> rules, SqlSession session) {
     Multimap<Integer, RuleParamDto> paramsByRuleId = ArrayListMultimap.create();
     Multimap<Integer, RuleRuleTagDto> tagsByRuleId = ArrayListMultimap.create();
@@ -151,6 +154,144 @@ public class RuleRegistry {
     return bulkIndexRules(rules, characteristicsById, paramsByRuleId, tagsByRuleId);
   }
 
+  /**
+   * Reindex one rule
+   */
+  public void reindex(RuleDto rule) {
+    SqlSession sqlSession = myBatis.openSession();
+    try {
+      reindex(rule, sqlSession);
+    } finally {
+      sqlSession.close();
+    }
+  }
+
+  public void reindex(RuleDto rule, SqlSession session) {
+    try {
+      Integer ruleId = rule.getId();
+      Integer effectiveSubCharacteristicId = rule.getSubCharacteristicId() != null ? rule.getSubCharacteristicId() : rule.getDefaultSubCharacteristicId();
+      CharacteristicDto subCharacteristic = effectiveSubCharacteristicId != null ? characteristicDao.selectById(effectiveSubCharacteristicId, session) : null;
+      CharacteristicDto characteristic = subCharacteristic != null ? characteristicDao.selectById(subCharacteristic.getParentId(), session) : null;
+      searchIndex.putSynchronous(INDEX_RULES, TYPE_RULE, Long.toString(ruleId), ruleDocument(rule,
+        characteristic, subCharacteristic,
+        ruleDao.selectParametersByRuleIds(newArrayList(ruleId), session),
+        ruleDao.selectTagsByRuleIds(newArrayList(ruleId), session)));
+    } catch (IOException ioexception) {
+      throw new IllegalStateException("Unable to index rule with id=" + rule.getId(), ioexception);
+    }
+  }
+
+  private String[] bulkIndexRules(Collection<RuleDto> rules, Map<Integer, CharacteristicDto> characteristicsById, Multimap<Integer, RuleParamDto> paramsByRule,
+                                  Multimap<Integer, RuleRuleTagDto> tagsByRule) {
+    try {
+      String[] ids = new String[rules.size()];
+      BytesStream[] docs = new BytesStream[rules.size()];
+      int index = 0;
+      TimeProfiler profiler = new TimeProfiler();
+      profiler.start("Build rules documents");
+      for (RuleDto rule : rules) {
+        ids[index] = rule.getId().toString();
+        CharacteristicDto effectiveSubCharacteristic =
+          characteristicsById.get(rule.getSubCharacteristicId() != null ? rule.getSubCharacteristicId() : rule.getDefaultSubCharacteristicId());
+        CharacteristicDto effectiveCharacteristic = effectiveSubCharacteristic != null ? characteristicsById.get(effectiveSubCharacteristic.getParentId()) : null;
+        docs[index] = ruleDocument(rule, effectiveCharacteristic, effectiveSubCharacteristic, paramsByRule.get(rule.getId()), tagsByRule.get(rule.getId()));
+        index++;
+      }
+      profiler.stop();
+
+      if (!rules.isEmpty()) {
+        profiler.start("Index rules");
+        searchIndex.bulkIndex(INDEX_RULES, TYPE_RULE, ids, docs);
+        profiler.stop();
+      }
+      return ids;
+    } catch (IOException ioe) {
+      throw new IllegalStateException("Unable to index rules", ioe);
+    }
+  }
+
+  public void removeDeletedRules(String[] ids) {
+    List<String> indexIds = searchIndex.findDocumentIds(SearchQuery.create().index(INDEX_RULES).type(TYPE_RULE));
+    indexIds.removeAll(Arrays.asList(ids));
+    TimeProfiler profiler = new TimeProfiler();
+    if (!indexIds.isEmpty()) {
+      profiler.start("Remove deleted rule documents");
+      searchIndex.bulkDelete(INDEX_RULES, TYPE_RULE, indexIds.toArray(new String[0]));
+      profiler.stop();
+    }
+  }
+
+  private XContentBuilder ruleDocument(RuleDto rule, @Nullable CharacteristicDto characteristicDto, @Nullable CharacteristicDto subCharacteristicDto,
+                                       Collection<RuleParamDto> params, Collection<RuleRuleTagDto> tags) throws IOException {
+    XContentBuilder document = XContentFactory.jsonBuilder()
+      .startObject()
+      .field(RuleDocument.FIELD_ID, rule.getId())
+      .field(RuleDocument.FIELD_KEY, rule.getRuleKey())
+      .field(RuleDocument.FIELD_LANGUAGE, rule.getLanguage())
+      .field(RuleDocument.FIELD_NAME, rule.getName())
+      .field(RuleDocument.FIELD_DESCRIPTION, rule.getDescription())
+      .field(RuleDocument.FIELD_TEMPLATE_ID, rule.getParentId())
+      .field(RuleDocument.FIELD_REPOSITORY_KEY, rule.getRepositoryKey())
+      .field(RuleDocument.FIELD_SEVERITY, rule.getSeverityString())
+      .field(RuleDocument.FIELD_STATUS, rule.getStatus())
+      .field(RuleDocument.FIELD_CARDINALITY, rule.getCardinality())
+      .field(RuleDocument.FIELD_CREATED_AT, rule.getCreatedAt())
+      .field(RuleDocument.FIELD_UPDATED_AT, rule.getUpdatedAt());
+    if (characteristicDto != null && subCharacteristicDto != null) {
+      boolean isFunctionOverridden = rule.getRemediationFunction() != null;
+      document
+        .field(RuleDocument.FIELD_CHARACTERISTIC_ID, characteristicDto.getId())
+        .field(RuleDocument.FIELD_CHARACTERISTIC_KEY, characteristicDto.getKey())
+        .field(RuleDocument.FIELD_CHARACTERISTIC_NAME, characteristicDto.getName())
+        .field(RuleDocument.FIELD_SUB_CHARACTERISTIC_ID, subCharacteristicDto.getId())
+        .field(RuleDocument.FIELD_SUB_CHARACTERISTIC_KEY, subCharacteristicDto.getKey())
+        .field(RuleDocument.FIELD_SUB_CHARACTERISTIC_NAME, subCharacteristicDto.getName())
+        .field(RuleDocument.FIELD_REMEDIATION_FUNCTION, isFunctionOverridden ? rule.getRemediationFunction() : rule.getDefaultRemediationFunction())
+        .field(RuleDocument.FIELD_REMEDIATION_COEFFICIENT, isFunctionOverridden ? rule.getRemediationCoefficient() : rule.getDefaultRemediationCoefficient())
+        .field(RuleDocument.FIELD_REMEDIATION_OFFSET, isFunctionOverridden ? rule.getRemediationOffset() : rule.getDefaultRemediationOffset());
+    }
+
+    if (rule.getNoteData() != null || rule.getNoteUserLogin() != null) {
+      document.startObject(RuleDocument.FIELD_NOTE)
+        .field(RuleDocument.FIELD_NOTE_DATA, rule.getNoteData())
+        .field(RuleDocument.FIELD_NOTE_USER_LOGIN, rule.getNoteUserLogin())
+        .field(RuleDocument.FIELD_NOTE_CREATED_AT, rule.getNoteCreatedAt())
+        .field(RuleDocument.FIELD_NOTE_UPDATED_AT, rule.getNoteUpdatedAt())
+        .endObject();
+    }
+    if (!params.isEmpty()) {
+      document.startArray(RuleDocument.FIELD_PARAMS);
+      for (RuleParamDto param : params) {
+        document.startObject()
+          .field(RuleDocument.FIELD_PARAM_KEY, param.getName())
+          .field(RuleDocument.FIELD_PARAM_TYPE, param.getType())
+          .field(RuleDocument.FIELD_PARAM_DEFAULT_VALUE, param.getDefaultValue())
+          .field(RuleDocument.FIELD_PARAM_DESCRIPTION, param.getDescription())
+          .endObject();
+      }
+      document.endArray();
+    }
+    List<String> systemTags = Lists.newArrayList();
+    List<String> adminTags = Lists.newArrayList();
+    for (RuleRuleTagDto tag : tags) {
+      if (tag.getType() == RuleTagType.SYSTEM) {
+        systemTags.add(tag.getTag());
+      } else {
+        adminTags.add(tag.getTag());
+      }
+    }
+    if (!systemTags.isEmpty()) {
+      document.array(RuleDocument.FIELD_SYSTEM_TAGS, systemTags.toArray());
+    }
+    if (!adminTags.isEmpty()) {
+      document.array(RuleDocument.FIELD_ADMIN_TAGS, adminTags.toArray());
+    }
+
+    document.endObject();
+    return document;
+  }
+
+
   /**
    * <p>Find rule IDs matching the given criteria.</p>
    *
@@ -278,15 +419,6 @@ public class RuleRegistry {
     }
   }
 
-  public void save(RuleDto rule, @Nullable CharacteristicDto characteristicDto, @Nullable CharacteristicDto subCharacteristicDto,
-                   Collection<RuleParamDto> params, Collection<RuleRuleTagDto> tags) {
-    try {
-      searchIndex.putSynchronous(INDEX_RULES, TYPE_RULE, Long.toString(rule.getId()), ruleDocument(rule, characteristicDto, subCharacteristicDto, params, tags));
-    } catch (IOException ioexception) {
-      throw new IllegalStateException("Unable to index rule with id=" + rule.getId(), ioexception);
-    }
-  }
-
   @CheckForNull
   public Rule findByKey(RuleKey key) {
     final SearchHits hits = searchIndex.executeRequest(searchIndex.client().prepareSearch(INDEX_RULES).setTypes(TYPE_RULE)
@@ -301,114 +433,4 @@ public class RuleRegistry {
       return RuleDocumentParser.parse(hits.hits()[0].sourceAsMap());
     }
   }
-
-  private String[] bulkIndexRules(Collection<RuleDto> rules, Map<Integer, CharacteristicDto> characteristicsById, Multimap<Integer, RuleParamDto> paramsByRule,
-                                  Multimap<Integer, RuleRuleTagDto> tagsByRule) {
-    try {
-      String[] ids = new String[rules.size()];
-      BytesStream[] docs = new BytesStream[rules.size()];
-      int index = 0;
-      TimeProfiler profiler = new TimeProfiler();
-      profiler.start("Build rules documents");
-      for (RuleDto rule : rules) {
-        ids[index] = rule.getId().toString();
-        CharacteristicDto effectiveSubCharacteristic =
-          characteristicsById.get(rule.getSubCharacteristicId() != null ? rule.getSubCharacteristicId() : rule.getDefaultSubCharacteristicId());
-        CharacteristicDto effectiveCharacteristic = effectiveSubCharacteristic != null ? characteristicsById.get(effectiveSubCharacteristic.getParentId()) : null;
-        docs[index] = ruleDocument(rule, effectiveCharacteristic, effectiveSubCharacteristic, paramsByRule.get(rule.getId()), tagsByRule.get(rule.getId()));
-        index++;
-      }
-      profiler.stop();
-
-      if (!rules.isEmpty()) {
-        profiler.start("Index rules");
-        searchIndex.bulkIndex(INDEX_RULES, TYPE_RULE, ids, docs);
-        profiler.stop();
-      }
-      return ids;
-    } catch (IOException ioe) {
-      throw new IllegalStateException("Unable to index rules", ioe);
-    }
-  }
-
-  public void removeDeletedRules(String[] ids) {
-    List<String> indexIds = searchIndex.findDocumentIds(SearchQuery.create().index(INDEX_RULES).type(TYPE_RULE));
-    indexIds.removeAll(Arrays.asList(ids));
-    TimeProfiler profiler = new TimeProfiler();
-    if (!indexIds.isEmpty()) {
-      profiler.start("Remove deleted rule documents");
-      searchIndex.bulkDelete(INDEX_RULES, TYPE_RULE, indexIds.toArray(new String[0]));
-      profiler.stop();
-    }
-  }
-
-  private XContentBuilder ruleDocument(RuleDto rule, @Nullable CharacteristicDto characteristicDto, @Nullable CharacteristicDto subCharacteristicDto,
-                                       Collection<RuleParamDto> params, Collection<RuleRuleTagDto> tags) throws IOException {
-    XContentBuilder document = XContentFactory.jsonBuilder()
-      .startObject()
-      .field(RuleDocument.FIELD_ID, rule.getId())
-      .field(RuleDocument.FIELD_KEY, rule.getRuleKey())
-      .field(RuleDocument.FIELD_LANGUAGE, rule.getLanguage())
-      .field(RuleDocument.FIELD_NAME, rule.getName())
-      .field(RuleDocument.FIELD_DESCRIPTION, rule.getDescription())
-      .field(RuleDocument.FIELD_TEMPLATE_ID, rule.getParentId())
-      .field(RuleDocument.FIELD_REPOSITORY_KEY, rule.getRepositoryKey())
-      .field(RuleDocument.FIELD_SEVERITY, rule.getSeverityString())
-      .field(RuleDocument.FIELD_STATUS, rule.getStatus())
-      .field(RuleDocument.FIELD_CARDINALITY, rule.getCardinality())
-      .field(RuleDocument.FIELD_CREATED_AT, rule.getCreatedAt())
-      .field(RuleDocument.FIELD_UPDATED_AT, rule.getUpdatedAt());
-    if (characteristicDto != null && subCharacteristicDto != null) {
-      boolean isFunctionOverridden = rule.getRemediationFunction() != null;
-      document
-        .field(RuleDocument.FIELD_CHARACTERISTIC_ID, characteristicDto.getId())
-        .field(RuleDocument.FIELD_CHARACTERISTIC_KEY, characteristicDto.getKey())
-        .field(RuleDocument.FIELD_CHARACTERISTIC_NAME, characteristicDto.getName())
-        .field(RuleDocument.FIELD_SUB_CHARACTERISTIC_ID, subCharacteristicDto.getId())
-        .field(RuleDocument.FIELD_SUB_CHARACTERISTIC_KEY, subCharacteristicDto.getKey())
-        .field(RuleDocument.FIELD_SUB_CHARACTERISTIC_NAME, subCharacteristicDto.getName())
-        .field(RuleDocument.FIELD_REMEDIATION_FUNCTION, isFunctionOverridden ? rule.getRemediationFunction() : rule.getDefaultRemediationFunction())
-        .field(RuleDocument.FIELD_REMEDIATION_COEFFICIENT, isFunctionOverridden ? rule.getRemediationCoefficient() : rule.getDefaultRemediationCoefficient())
-        .field(RuleDocument.FIELD_REMEDIATION_OFFSET, isFunctionOverridden ? rule.getRemediationOffset() : rule.getDefaultRemediationOffset());
-    }
-
-    if (rule.getNoteData() != null || rule.getNoteUserLogin() != null) {
-      document.startObject(RuleDocument.FIELD_NOTE)
-        .field(RuleDocument.FIELD_NOTE_DATA, rule.getNoteData())
-        .field(RuleDocument.FIELD_NOTE_USER_LOGIN, rule.getNoteUserLogin())
-        .field(RuleDocument.FIELD_NOTE_CREATED_AT, rule.getNoteCreatedAt())
-        .field(RuleDocument.FIELD_NOTE_UPDATED_AT, rule.getNoteUpdatedAt())
-        .endObject();
-    }
-    if (!params.isEmpty()) {
-      document.startArray(RuleDocument.FIELD_PARAMS);
-      for (RuleParamDto param : params) {
-        document.startObject()
-          .field(RuleDocument.FIELD_PARAM_KEY, param.getName())
-          .field(RuleDocument.FIELD_PARAM_TYPE, param.getType())
-          .field(RuleDocument.FIELD_PARAM_DEFAULT_VALUE, param.getDefaultValue())
-          .field(RuleDocument.FIELD_PARAM_DESCRIPTION, param.getDescription())
-          .endObject();
-      }
-      document.endArray();
-    }
-    List<String> systemTags = Lists.newArrayList();
-    List<String> adminTags = Lists.newArrayList();
-    for (RuleRuleTagDto tag : tags) {
-      if (tag.getType() == RuleTagType.SYSTEM) {
-        systemTags.add(tag.getTag());
-      } else {
-        adminTags.add(tag.getTag());
-      }
-    }
-    if (!systemTags.isEmpty()) {
-      document.array(RuleDocument.FIELD_SYSTEM_TAGS, systemTags.toArray());
-    }
-    if (!adminTags.isEmpty()) {
-      document.array(RuleDocument.FIELD_ADMIN_TAGS, adminTags.toArray());
-    }
-
-    document.endObject();
-    return document;
-  }
 }
index 249a1d8abe43c052e7b4699bc65e1cb52cdcae2f..5ccf3b58082e26fef13b84bf76f1d2afe69e443f 100644 (file)
@@ -148,7 +148,7 @@ public class RuleOperationsTest {
     assertThat(argumentCaptor.getValue().getNoteUpdatedAt()).isEqualTo(now);
 
     verify(session).commit();
-    verify(ruleRegistry).save(eq(rule), any(CharacteristicDto.class), any(CharacteristicDto.class), eq(ruleParams), eq(ruleTags));
+    verify(ruleRegistry).reindex(eq(rule), eq(session));
   }
 
   @Test
@@ -185,7 +185,7 @@ public class RuleOperationsTest {
     assertThat(argumentCaptor.getValue().getNoteUpdatedAt()).isEqualTo(now);
 
     verify(session).commit();
-    verify(ruleRegistry).save(eq(rule), any(CharacteristicDto.class), any(CharacteristicDto.class), eq(ruleParams), eq(ruleTags));
+    verify(ruleRegistry).reindex(eq(rule), eq(session));
   }
 
   @Test
@@ -211,7 +211,7 @@ public class RuleOperationsTest {
     assertThat(argumentCaptor.getValue().getNoteUpdatedAt()).isNull();
 
     verify(session).commit();
-    verify(ruleRegistry).save(eq(rule), any(CharacteristicDto.class), any(CharacteristicDto.class), eq(ruleParams), eq(ruleTags));
+    verify(ruleRegistry).reindex(eq(rule), eq(session));
   }
 
   @Test
@@ -247,8 +247,7 @@ public class RuleOperationsTest {
     assertThat(ruleTagArgument.getValue().getType()).isEqualTo(RuleTagType.SYSTEM);
 
     verify(session).commit();
-    verify(ruleRegistry).save(eq(ruleArgument.getValue()), isNull(CharacteristicDto.class), isNull(CharacteristicDto.class),
-      anyListOf(RuleParamDto.class), anyListOf(RuleRuleTagDto.class));
+    verify(ruleRegistry).reindex(eq(ruleArgument.getValue()), eq(session));
   }
 
   @Test
@@ -272,8 +271,7 @@ public class RuleOperationsTest {
     assertThat(ruleParamArgument.getValue().getDefaultValue()).isEqualTo("21");
 
     verify(session).commit();
-    verify(ruleRegistry).save(eq(ruleArgument.getValue()), any(CharacteristicDto.class), any(CharacteristicDto.class),
-      eq(newArrayList(ruleParamArgument.getValue())), eq(ruleTags));
+    verify(ruleRegistry).reindex(eq(ruleArgument.getValue()), eq(session));
   }
 
   @Test
@@ -295,7 +293,7 @@ public class RuleOperationsTest {
     assertThat(ruleCaptor.getValue().getStatus()).isEqualTo(Rule.STATUS_REMOVED);
     assertThat(ruleCaptor.getValue().getUpdatedAt()).isEqualTo(now);
 
-    verify(ruleRegistry).save(eq(ruleCaptor.getValue()), any(CharacteristicDto.class), any(CharacteristicDto.class), eq(newArrayList(param)), eq(ruleTags));
+    verify(ruleRegistry).reindex(eq(ruleCaptor.getValue()), eq(session));
     verify(activeRuleDao).deleteParameters(eq(activeRuleId), eq(session));
     verify(activeRuleDao).deleteFromRule(eq(ruleId), eq(session));
     verify(session, times(2)).commit();
@@ -403,7 +401,7 @@ public class RuleOperationsTest {
     assertThat(result.getUpdatedAt()).isEqualTo(now);
 
     verify(session).commit();
-    verify(ruleRegistry).save(eq(result), eq(characteristic), eq(subCharacteristic), anyListOf(RuleParamDto.class), anyListOf(RuleRuleTagDto.class));
+    verify(ruleRegistry).reindex(eq(result), eq(session));
   }
 
   @Test
@@ -425,7 +423,7 @@ public class RuleOperationsTest {
 
     verify(ruleDao, never()).update(any(RuleDto.class), eq(session));
     verify(session, never()).commit();
-    verify(ruleRegistry, never()).save(any(RuleDto.class), any(CharacteristicDto.class), any(CharacteristicDto.class), anyListOf(RuleParamDto.class), anyListOf(RuleRuleTagDto.class));
+    verify(ruleRegistry, never()).reindex(any(RuleDto.class), eq(session));
   }
 
   @Test
@@ -452,7 +450,7 @@ public class RuleOperationsTest {
     assertThat(result.getUpdatedAt()).isEqualTo(now);
 
     verify(session).commit();
-    verify(ruleRegistry).save(eq(result), isNull(CharacteristicDto.class), isNull(CharacteristicDto.class), anyListOf(RuleParamDto.class), anyListOf(RuleRuleTagDto.class));
+    verify(ruleRegistry).reindex(eq(result), eq(session));
   }
 
   @Test
@@ -469,7 +467,7 @@ public class RuleOperationsTest {
 
     verify(ruleDao, never()).update(any(RuleDto.class), eq(session));
     verify(session, never()).commit();
-    verify(ruleRegistry, never()).save(any(RuleDto.class), any(CharacteristicDto.class), any(CharacteristicDto.class), anyListOf(RuleParamDto.class), anyListOf(RuleRuleTagDto.class));
+    verify(ruleRegistry, never()).reindex(any(RuleDto.class), eq(session));
   }
 
   @Test
@@ -490,7 +488,7 @@ public class RuleOperationsTest {
 
     verify(ruleDao, never()).update(any(RuleDto.class), eq(session));
     verify(session, never()).commit();
-    verify(ruleRegistry, never()).save(any(RuleDto.class), any(CharacteristicDto.class), any(CharacteristicDto.class), anyListOf(RuleParamDto.class), anyListOf(RuleRuleTagDto.class));
+    verify(ruleRegistry, never()).reindex(any(RuleDto.class), eq(session));
   }
 
   @Test
@@ -515,7 +513,7 @@ public class RuleOperationsTest {
 
     verify(ruleDao, never()).update(any(RuleDto.class), eq(session));
     verify(session, never()).commit();
-    verify(ruleRegistry, never()).save(any(RuleDto.class), any(CharacteristicDto.class), any(CharacteristicDto.class), anyListOf(RuleParamDto.class), anyListOf(RuleRuleTagDto.class));
+    verify(ruleRegistry, never()).reindex(any(RuleDto.class), eq(session));
   }
 
   @Test
@@ -541,6 +539,6 @@ public class RuleOperationsTest {
 
     verify(ruleDao, never()).update(any(RuleDto.class), eq(session));
     verify(session, never()).commit();
-    verify(ruleRegistry, never()).save(any(RuleDto.class), any(CharacteristicDto.class), any(CharacteristicDto.class), anyListOf(RuleParamDto.class), anyListOf(RuleRuleTagDto.class));
+    verify(ruleRegistry, never()).reindex(any(RuleDto.class), eq(session));
   }
 }
index 9e664fc628fef79ebbf08be6c3a4f6f01e820e18..4e885b282af5e0020b6708998c9c4dae0fc80136 100644 (file)
@@ -116,7 +116,7 @@ public class RuleRegistryTest {
   }
 
   @Test
-  public void index_rules() {
+  public void index_new_rules() {
     when(ruleDao.selectEnablesAndNonManual(session)).thenReturn(newArrayList(
       new RuleDto().setId(3).setRepositoryKey("repo").setRuleKey("key").setSeverity(Severity.MINOR).setNoteData("noteData").setNoteUserLogin("userLogin")
         .setDefaultSubCharacteristicId(11).setDefaultRemediationFunction("LINEAR_OFFSET").setDefaultRemediationCoefficient("1h").setDefaultRemediationOffset("15min")
@@ -161,7 +161,25 @@ public class RuleRegistryTest {
   }
 
   @Test
-  public void index_overridden_characteristic_if_both_default_and_overridden_characteristics_exists() {
+  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 index_overridden_characteristic_if_both_default_and_overridden_characteristics_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
@@ -169,15 +187,18 @@ public class RuleRegistryTest {
         .setSubCharacteristicId(13).setRemediationFunction("LINEAR_OFFSET").setRemediationCoefficient("1h").setRemediationOffset("15min")
     ));
 
-    when(characteristicDao.selectCharacteristicsByIds(newHashSet(11, 13), session)).thenReturn(newArrayList(
-      new CharacteristicDto().setId(11).setKey("MODULARITY").setName("Modularity").setParentId(10),
-      new CharacteristicDto().setId(13).setKey("COMPILER").setName("Compiler").setParentId(12)
-    ));
+    // Characteristics
     when(characteristicDao.selectCharacteristicsByIds(newHashSet(10, 12), session)).thenReturn(newArrayList(
       new CharacteristicDto().setId(10).setKey("REUSABILITY").setName("Reusability"),
       new CharacteristicDto().setId(12).setKey("PORTABILITY").setName("Portability")
     ));
 
+    // Sub-characteristics
+    when(characteristicDao.selectCharacteristicsByIds(newHashSet(11, 13), session)).thenReturn(newArrayList(
+      new CharacteristicDto().setId(11).setKey("MODULARITY").setName("Modularity").setParentId(10),
+      new CharacteristicDto().setId(13).setKey("COMPILER").setName("Compiler").setParentId(12)
+    ));
+
     registry.reindex();
 
     Map<String, Object> ruleDocument = esSetup.client().prepareGet("rules", "rule", Integer.toString(10)).execute().actionGet().getSourceAsMap();
@@ -194,7 +215,7 @@ public class RuleRegistryTest {
   }
 
   @Test
-  public void index_overridden_function_if_both_default_and_overridden_functions_exists() {
+  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
@@ -224,6 +245,112 @@ public class RuleRegistryTest {
     assertThat(ruleDocument.get(RuleDocument.FIELD_REMEDIATION_OFFSET)).isNull();
   }
 
+  @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_CHARACTERISTIC_NAME)).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_SUB_CHARACTERISTIC_NAME)).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");
+  }
+
+  @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 index_overridden_characteristic_if_both_default_and_overridden_characteristics_exists_when_indexing_one_rule() {
+    RuleDto ruleDto = new RuleDto().setId(10).setRepositoryKey("repo").setRuleKey("key1").setSeverity(Severity.MINOR)
+        // default and overridden debt values are set
+        .setDefaultSubCharacteristicId(11).setDefaultRemediationFunction("LINEAR").setDefaultRemediationCoefficient("2h")
+        .setSubCharacteristicId(13).setRemediationFunction("LINEAR_OFFSET").setRemediationCoefficient("1h").setRemediationOffset("15min");
+
+    when(characteristicDao.selectById(12, session)).thenReturn(new CharacteristicDto().setId(12).setKey("REUSABILITY").setName("Reusability"));
+    when(characteristicDao.selectById(13, session)).thenReturn(new CharacteristicDto().setId(13).setKey("MODULARITY").setName("Modularity").setParentId(12));
+
+    registry.reindex(ruleDto);
+
+    Map<String, Object> ruleDocument = esSetup.client().prepareGet("rules", "rule", Integer.toString(10)).execute().actionGet().getSourceAsMap();
+    assertThat(ruleDocument.get(RuleDocument.FIELD_CHARACTERISTIC_ID)).isEqualTo(12);
+    assertThat(ruleDocument.get(RuleDocument.FIELD_CHARACTERISTIC_KEY)).isEqualTo("REUSABILITY");
+    assertThat(ruleDocument.get(RuleDocument.FIELD_CHARACTERISTIC_NAME)).isEqualTo("Reusability");
+    assertThat(ruleDocument.get(RuleDocument.FIELD_SUB_CHARACTERISTIC_ID)).isEqualTo(13);
+    assertThat(ruleDocument.get(RuleDocument.FIELD_SUB_CHARACTERISTIC_KEY)).isEqualTo("MODULARITY");
+    assertThat(ruleDocument.get(RuleDocument.FIELD_SUB_CHARACTERISTIC_NAME)).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");
+  }
+
+  @Test
+  public void index_overridden_function_if_both_default_and_overridden_functions_exists_when_indexing_one_rule() {
+    RuleDto ruleDto = 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.selectById(10, session)).thenReturn(new CharacteristicDto().setId(10).setKey("REUSABILITY").setName("Reusability"));
+    when(characteristicDao.selectById(11, session)).thenReturn(new CharacteristicDto().setId(11).setKey("MODULARITY").setName("Modularity").setParentId(10));
+
+    registry.reindex(ruleDto);
+
+    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_CHARACTERISTIC_NAME)).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_SUB_CHARACTERISTIC_NAME)).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 remove_all_rules_when_ro_rule_registered() {