]> source.dussan.org Git - sonarqube.git/commitdiff
Fix quality flaws
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Tue, 9 Dec 2014 14:44:45 +0000 (15:44 +0100)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Tue, 9 Dec 2014 14:52:55 +0000 (15:52 +0100)
server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QGatesAppAction.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileProjectLookup.java
server/sonar-server/src/main/java/org/sonar/server/search/SearchClient.java
server/sonar-server/src/main/java/org/sonar/server/search/ws/SearchRequestHandler.java
server/sonar-server/src/test/java/org/sonar/server/issue/ws/SearchActionMediumTest.java
server/sonar-server/src/test/java/org/sonar/server/search/SearchClientMediumTest.java
server/sonar-server/src/test/java/org/sonar/server/search/request/ProxyIndexRequestBuilderTest.java [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/issue/ws/SearchActionMediumTest/display_zero_facets.json [new file with mode: 0644]

index 464226f1c0375828efd7d07609d80720cc608d8b..2e9b6b594ff7f3e10fe13c620e940ab8296ae3ad 100644 (file)
@@ -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);
index 088bd5af9f7a1e6055e09aca6dd2aa36bcd0e1a4..995580c36eaa68c8d23ca5199b394b8dfb97cb63 100644 (file)
@@ -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());
         }
       }
 
index 8b1fb8b707c82c50ecd82785b85f18a6396858e2..d18997686021a1ba2c8c6a2a6482f12a9fe35f7e 100644 (file)
@@ -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();
   }
 
index ef0ae5ff8663075bdabdb013767e62a3af5d04d0..18535e465acbd0208a4e74aa70d58bebd42b0bb4 100644 (file)
@@ -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();
index dbe7faa0002009d71c68441106a6678a754be697..5ea03bc325f1680265c2dd392f168d00e7ee6295 100644 (file)
@@ -466,6 +466,28 @@ public class SearchActionMediumTest {
     result.assertJson(this.getClass(), "display_facets.json", false);
   }
 
+  @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)
index a5394be73b9e0f76ac9f2dc1c057354db1c84bd8..d9d2c510931e86d88efe749057f6af496a79d1c8 100644 (file)
@@ -51,30 +51,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 {
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 (file)
index 0000000..61625f9
--- /dev/null
@@ -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 (file)
index 0000000..65c2a54
--- /dev/null
@@ -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
+        }
+      ]
+    }
+  ]}