import org.sonar.api.impl.utils.ScannerUtils;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.utils.DateUtils;
+import org.sonar.api.utils.MessageException;
import org.sonar.scanner.bootstrap.ScannerWsClient;
import org.sonarqube.ws.Rules;
import org.sonarqube.ws.Rules.Active;
List<LoadedActiveRule> loadedRules = new LinkedList<>();
List<Rule> rulesList = response.getRulesList();
- Map<String, ActiveList> actives = response.getActives().getActives();
+ Map<String, ActiveList> actives = response.getActives().getActivesMap();
for (Rule r : rulesList) {
ActiveList activeList = actives.get(r.getKey());
+ if (activeList == null) {
+ throw MessageException.of("Elasticsearch indices have become inconsistent. Consider re-indexing. " +
+ "Check documentation for more information https://docs.sonarqube.org/latest/setup/troubleshooting");
+ }
Active active = activeList.getActiveList(0);
LoadedActiveRule loadedRule = new LoadedActiveRule();
import org.sonar.api.batch.rule.LoadedActiveRule;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.Severity;
+import org.sonar.api.utils.MessageException;
import org.sonar.scanner.WsTestUtil;
import org.sonar.scanner.bootstrap.DefaultScannerWsClient;
import org.sonar.scanner.scan.branch.BranchConfiguration;
import org.sonarqube.ws.Rules.SearchResponse.Builder;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
verifyNoMoreInteractions(wsClient);
}
+ @Test
+ public void exception_thrown_when_elasticsearch_index_inconsistent() {
+ WsTestUtil.mockStream(wsClient, urlOfPage(1), prepareCorruptedResponse());
+ assertThatThrownBy(() -> loader.load("c+-test_c+-values-17445"))
+ .isInstanceOf(MessageException.class)
+ .hasMessage("Elasticsearch indices have become inconsistent. Consider re-indexing. " +
+ "Check documentation for more information https://docs.sonarqube.org/latest/setup/troubleshooting");
+ }
+
private String urlOfPage(int page) {
return "/api/rules/search.protobuf?f=repo,name,severity,lang,internalKey,templateKey,params,actives,createdAt,updatedAt,deprecatedKeys&activation=true"
+ ("") + "&qprofile=c%2B-test_c%2B-values-17445&ps=500&p=" + page + "";
rules.setTotal(total);
return new ByteArrayInputStream(rules.build().toByteArray());
}
+
+ private InputStream prepareCorruptedResponse() {
+ Builder rules = SearchResponse.newBuilder();
+ Actives.Builder actives = Actives.newBuilder();
+
+ IntStream.rangeClosed(1, 3)
+ .mapToObj(i -> RuleKey.of("java", "S" + i))
+ .forEach(key -> {
+
+ Rule.Builder ruleBuilder = Rule.newBuilder();
+ ruleBuilder.setKey(key.toString());
+ rules.addRules(ruleBuilder);
+ });
+
+ rules.setActives(actives);
+ rules.setPs(3);
+ rules.setTotal(3);
+ return new ByteArrayInputStream(rules.build().toByteArray());
+ }
}