diff options
author | Jean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com> | 2014-12-09 15:44:45 +0100 |
---|---|---|
committer | Jean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com> | 2014-12-09 15:52:55 +0100 |
commit | 4c60b894f15b5bc3d84f48cd20df26550301d5aa (patch) | |
tree | d9de27706175a4d44b5731dac000507a7ba0a1b8 | |
parent | bc5f94813ac5859510f019b602dbff37eb661339 (diff) | |
download | sonarqube-4c60b894f15b5bc3d84f48cd20df26550301d5aa.tar.gz sonarqube-4c60b894f15b5bc3d84f48cd20df26550301d5aa.zip |
Fix quality flaws
8 files changed, 317 insertions, 39 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QGatesAppAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QGatesAppAction.java index 464226f1c03..2e9b6b594ff 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QGatesAppAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QGatesAppAction.java @@ -70,7 +70,7 @@ public class QGatesAppAction implements RequestHandler { private void addPeriods(JsonWriter writer) { writer.name("periods").beginArray(); for (int i=0; i < 3; i ++) { - writer.beginObject().prop("key", i + 1).prop("text", periods.label(i + 1)).endObject(); + writer.beginObject().prop("key", (long) i + 1).prop("text", periods.label(i + 1)).endObject(); } addProjectPeriod(4, writer); addProjectPeriod(5, writer); diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileProjectLookup.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileProjectLookup.java index 088bd5af9f7..995580c36ea 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileProjectLookup.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileProjectLookup.java @@ -61,9 +61,9 @@ public class QProfileProjectLookup implements ServerComponent { UserSession userSession = UserSession.get(); List<Component> result = Lists.newArrayList(); Collection<String> authorizedProjectKeys = db.authorizationDao().selectAuthorizedRootProjectsKeys(userSession.userId(), UserRole.USER); - for (String key : componentsByKeys.keySet()) { - if (authorizedProjectKeys.contains(key)) { - result.add(componentsByKeys.get(key)); + for (Map.Entry<String, Component> entry : componentsByKeys.entrySet()) { + if (authorizedProjectKeys.contains(entry.getKey())) { + result.add(entry.getValue()); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/search/SearchClient.java b/server/sonar-server/src/main/java/org/sonar/server/search/SearchClient.java index 8b1fb8b707c..d1899768602 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/search/SearchClient.java +++ b/server/sonar-server/src/main/java/org/sonar/server/search/SearchClient.java @@ -20,6 +20,8 @@ package org.sonar.server.search; +import org.sonar.server.search.request.ProxyIndexRequestBuilder; + import com.google.common.annotations.VisibleForTesting; import org.apache.commons.lang.StringUtils; import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequestBuilder; @@ -209,27 +211,27 @@ public class SearchClient extends TransportClient implements Startable { return new ProxyDeleteByQueryRequestBuilder(this, profiling).setIndices(indices); } - // **************************************************************************************************************** - // Not yet implemented methods - // **************************************************************************************************************** - - @Override - public MultiSearchRequestBuilder prepareMultiSearch() { - throw throwNotYetImplemented(); - } - @Override public IndexRequestBuilder prepareIndex() { - throw throwNotYetImplemented(); + return new ProxyIndexRequestBuilder(this, profiling); } @Override public IndexRequestBuilder prepareIndex(String index, String type) { - throw throwNotYetImplemented(); + return new ProxyIndexRequestBuilder(this, profiling).setIndex(index).setType(type); } @Override public IndexRequestBuilder prepareIndex(String index, String type, @Nullable String id) { + return new ProxyIndexRequestBuilder(this, profiling).setIndex(index).setType(type).setId(id); + } + + // **************************************************************************************************************** + // Not yet implemented methods + // **************************************************************************************************************** + + @Override + public MultiSearchRequestBuilder prepareMultiSearch() { throw throwNotYetImplemented(); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/search/ws/SearchRequestHandler.java b/server/sonar-server/src/main/java/org/sonar/server/search/ws/SearchRequestHandler.java index ef0ae5ff866..18535e465ac 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/search/ws/SearchRequestHandler.java +++ b/server/sonar-server/src/main/java/org/sonar/server/search/ws/SearchRequestHandler.java @@ -166,6 +166,8 @@ public abstract class SearchRequestHandler<QUERY, DOMAIN> implements RequestHand private void addZeroFacetsForSelectedItems(Request request, String facetName, Set<String> itemsFromFacets, JsonWriter json) { List<String> requestParams = request.paramAsStrings(facetName); if (requestParams != null) { + System.out.println(requestParams); + System.out.println(itemsFromFacets); for (String param: requestParams) { if (!itemsFromFacets.contains(param)) { json.beginObject(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/ws/SearchActionMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/ws/SearchActionMediumTest.java index dbe7faa0002..5ea03bc325f 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/ws/SearchActionMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/ws/SearchActionMediumTest.java @@ -467,6 +467,28 @@ public class SearchActionMediumTest { } @Test + public void display_zero_valued_facets_for_selected_items() throws Exception { + IssueDto issue = IssueTesting.newDto(rule, file, project) + .setIssueCreationDate(DateUtils.parseDate("2014-09-04")) + .setIssueUpdateDate(DateUtils.parseDate("2017-12-04")) + .setDebt(10L) + .setStatus("OPEN") + .setKee("82fd47d4-b650-4037-80bc-7b112bd4eac2") + .setSeverity("MAJOR"); + db.issueDao().insert(session, issue); + session.commit(); + tester.get(IssueIndexer.class).indexAll(); + + WsTester.Result result = wsTester.newGetRequest(IssuesWs.API_ENDPOINT, SearchAction.SEARCH_ACTION) + .setParam("resolved", "false") + .setParam("severities", "MAJOR,MINOR") + .setParam("languages", "xoo,polop,palap") + .setParam(SearchAction.PARAM_FACETS, "statuses,severities,resolutions,projectUuids,rules,componentUuids,assignees,languages,actionPlans") + .execute(); + result.assertJson(this.getClass(), "display_zero_facets.json", false); + } + + @Test public void hide_rules() throws Exception { IssueDto issue = IssueTesting.newDto(rule, file, project) .setIssueCreationDate(DateUtils.parseDate("2014-09-04")) diff --git a/server/sonar-server/src/test/java/org/sonar/server/search/SearchClientMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/search/SearchClientMediumTest.java index a5394be73b9..d9d2c510931 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/search/SearchClientMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/search/SearchClientMediumTest.java @@ -52,30 +52,6 @@ public class SearchClientMediumTest { } @Test - public void prepare_index_is_not_yet_implemented() throws Exception { - try { - searchClient.prepareIndex(); - fail(); - } catch (Exception e) { - assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); - } - - try { - searchClient.prepareIndex("index", "type"); - fail(); - } catch (Exception e) { - assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); - } - - try { - searchClient.prepareIndex("index", "type", "id"); - fail(); - } catch (Exception e) { - assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); - } - } - - @Test public void prepare_update_is_not_yet_implemented() throws Exception { try { searchClient.prepareUpdate(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/search/request/ProxyIndexRequestBuilderTest.java b/server/sonar-server/src/test/java/org/sonar/server/search/request/ProxyIndexRequestBuilderTest.java new file mode 100644 index 00000000000..61625f9320d --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/search/request/ProxyIndexRequestBuilderTest.java @@ -0,0 +1,124 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.server.search.request; + +import org.elasticsearch.action.index.IndexRequestBuilder; +import org.elasticsearch.common.unit.TimeValue; +import org.junit.After; +import org.junit.Test; +import org.sonar.api.config.Settings; +import org.sonar.core.profiling.Profiling; +import org.sonar.server.search.IndexDefinition; +import org.sonar.server.search.SearchClient; + +import static org.fest.assertions.Assertions.assertThat; +import static org.fest.assertions.Fail.fail; + +public class ProxyIndexRequestBuilderTest { + + Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.NONE.name())); + SearchClient searchClient = new SearchClient(new Settings(), profiling); + + @After + public void tearDown() throws Exception { + searchClient.stop(); + } + + @Test + public void index_with_index_type_and_id() { + try { + searchClient.prepareIndex(IndexDefinition.RULE.getIndexName(), IndexDefinition.RULE.getIndexType(), "ruleKey").get(); + + // expected to fail because elasticsearch is not correctly configured, but that does not matter + fail(); + } catch (IllegalStateException e) { + assertThat(e.getMessage()).isEqualTo("Fail to execute ES index request for key 'ruleKey' on index 'rules' on type 'rule'"); + } + } + + @Test + public void with_profiling_basic() { + Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.BASIC.name())); + SearchClient searchClient = new SearchClient(new Settings(), profiling); + + try { + searchClient.prepareIndex() + .setIndex(IndexDefinition.RULE.getIndexName()) + .setType(IndexDefinition.RULE.getIndexType()) + .setId("ruleKey") + .get(); + + // expected to fail because elasticsearch is not correctly configured, but that does not matter + fail(); + } catch (IllegalStateException e) { + assertThat(e.getMessage()).isEqualTo("Fail to execute ES index request for key 'ruleKey' on index 'rules' on type 'rule'"); + } + + // TODO assert profiling + searchClient.stop(); + } + + @Test + public void fail_to_get_bad_query() throws Exception { + IndexRequestBuilder requestBuilder = searchClient.prepareIndex() + .setIndex("unknown") + .setType("test") + .setId("rule1"); + try { + requestBuilder.get(); + fail(); + } catch (Exception e) { + assertThat(e).isInstanceOf(IllegalStateException.class); + assertThat(e.getMessage()).contains("Fail to execute ES index request for key 'rule1' on index 'unknown' on type 'test'"); + } + } + + @Test + public void get_with_string_timeout_is_not_yet_implemented() throws Exception { + try { + searchClient.prepareIndex().get("1"); + fail(); + } catch (Exception e) { + assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); + } + } + + @Test + public void get_with_time_value_timeout_is_not_yet_implemented() throws Exception { + try { + searchClient.prepareIndex().get(TimeValue.timeValueMinutes(1)); + fail(); + } catch (Exception e) { + assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); + } + } + + @Test + public void execute_should_throw_an_unsupported_operation_exception() throws Exception { + try { + searchClient.prepareIndex().execute(); + fail(); + } catch (Exception e) { + assertThat(e).isInstanceOf(UnsupportedOperationException.class).hasMessage("execute() should not be called as it's used for asynchronous"); + } + } + +} diff --git a/server/sonar-server/src/test/resources/org/sonar/server/issue/ws/SearchActionMediumTest/display_zero_facets.json b/server/sonar-server/src/test/resources/org/sonar/server/issue/ws/SearchActionMediumTest/display_zero_facets.json new file mode 100644 index 00000000000..65c2a5466c1 --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/issue/ws/SearchActionMediumTest/display_zero_facets.json @@ -0,0 +1,152 @@ +{ + "issues": [ + { + "key": "82fd47d4-b650-4037-80bc-7b112bd4eac2", + "component": "MyComponent", + "project": "MyProject", + "rule": "xoo:x1", + "status": "OPEN", + "severity": "MAJOR", + "debt": "10min", + "fUpdateAge": "less than a minute" + } + ], + "facets": [ + { + "property": "statuses", + "values": [ + { + "val": "OPEN", + "count": 1 + }, + { + "val": "CONFIRMED", + "count": 0 + }, + { + "val": "REOPENED", + "count": 0 + }, + { + "val": "RESOLVED", + "count": 0 + }, + { + "val": "CLOSED", + "count": 0 + } + ] + }, + { + "property": "severities", + "values": [ + { + "val": "INFO", + "count": 0 + }, + { + "val": "MINOR", + "count": 0 + }, + { + "val": "MAJOR", + "count": 1 + }, + { + "val": "CRITICAL", + "count": 0 + }, + { + "val": "BLOCKER", + "count": 0 + } + ] + }, + { + "property": "resolutions", + "values": [ + { + "val": "", + "count": 1 + }, + { + "val": "FALSE-POSITIVE", + "count": 0 + }, + { + "val": "FIXED", + "count": 0 + }, + { + "val": "REMOVED", + "count": 0 + } + ] + }, + { + "property": "projectUuids", + "values": [ + { + "val": "ABCD", + "count": 1 + } + ] + }, + { + "property": "rules", + "values": [ + { + "val": "xoo:x1", + "count": 1 + } + ] + }, + { + "property": "componentUuids", + "values": [ + { + "val": "BCDE", + "count": 1 + } + ] + }, + { + "property": "assignees", + "values": [ + { + "val": "", + "count": 1 + }, + { + "val": "john", + "count": 0 + } + ] + }, + { + "property": "languages", + "values": [ + { + "val": "xoo", + "count": 1 + }, + { + "val": "polop", + "count": 0 + }, + { + "val": "palap", + "count": 0 + } + ] + }, + { + "property": "actionPlans", + "values": [ + { + "val": "", + "count": 1 + } + ] + } + ]} |