]> source.dussan.org Git - sonarqube.git/commitdiff
Increase coverage
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 13 Jan 2015 08:08:09 +0000 (09:08 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 13 Jan 2015 08:08:09 +0000 (09:08 +0100)
server/sonar-server/src/main/java/org/sonar/server/exceptions/Message.java
server/sonar-server/src/main/java/org/sonar/server/user/index/UserIndexer.java
server/sonar-server/src/test/java/org/sonar/server/component/ws/ComponentAppActionTest.java
server/sonar-server/src/test/java/org/sonar/server/exceptions/MessageTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/measure/MeasureFilterFactoryTest.java
server/sonar-server/src/test/java/org/sonar/server/user/UserUpdaterTest.java
server/sonar-server/src/test/resources/org/sonar/server/user/UserUpdaterTest/associate_default_groups_when_reactivating_user.xml
server/sonar-server/src/test/resources/org/sonar/server/user/UserUpdaterTest/associate_default_groups_when_updating_user.xml
server/sonar-server/src/test/resources/org/sonar/server/user/UserUpdaterTest/not_associate_default_group_when_updating_user_if_already_existing.xml [new file with mode: 0644]

index 80aa6ac7e6b361ab7cf4cba9e78424d2594278e2..28f9fb2e071826b49c55883f961077e7540c450d 100644 (file)
@@ -22,6 +22,9 @@ package org.sonar.server.exceptions;
 import com.google.common.base.Objects;
 import org.apache.commons.lang.StringUtils;
 
+import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
+
 import java.util.Arrays;
 
 public class Message {
@@ -29,7 +32,7 @@ public class Message {
   private final String key;
   private final Object[] params;
 
-  private Message(String key, Object[] params) {
+  private Message(String key, @Nullable Object[] params) {
     this.key = key;
     this.params = params;
   }
@@ -38,6 +41,7 @@ public class Message {
     return key;
   }
 
+  @CheckForNull
   public Object[] getParams() {
     return params;
   }
index 94bc1e1f317ae6cb5b7a6d4ecc24b6f013a1afe0..611b776a1ee776959f3840739eb676a0a9bd0561 100644 (file)
@@ -21,8 +21,6 @@
 package org.sonar.server.user.index;
 
 import org.apache.commons.dbutils.DbUtils;
-import org.elasticsearch.action.get.GetRequestBuilder;
-import org.elasticsearch.action.get.GetResponse;
 import org.elasticsearch.action.update.UpdateRequest;
 import org.sonar.core.persistence.DbSession;
 import org.sonar.server.db.DbClient;
@@ -30,8 +28,6 @@ import org.sonar.server.es.BaseIndexer;
 import org.sonar.server.es.BulkIndexer;
 import org.sonar.server.es.EsClient;
 
-import javax.annotation.CheckForNull;
-
 import java.sql.Connection;
 import java.util.Iterator;
 
@@ -75,21 +71,6 @@ public class UserIndexer extends BaseIndexer {
     return maxUpdatedAt;
   }
 
-  @CheckForNull
-  public UserDoc getNullableByKey(String login) {
-    GetRequestBuilder request = esClient.prepareGet()
-      .setType(UserIndexDefinition.INDEX)
-      .setIndex(UserIndexDefinition.TYPE_USER)
-      .setId(login)
-      .setFetchSource(true)
-      .setRouting(login);
-    GetResponse response = request.get();
-    if (response.isExists()) {
-      return new UserDoc(response.getSource());
-    }
-    return null;
-  }
-
   private UpdateRequest newUpsertRequest(UserDoc user) {
     return new UpdateRequest(UserIndexDefinition.INDEX, UserIndexDefinition.TYPE_USER, user.login())
       .doc(user.getFields())
index 6060174b54ea0cb678a55f96f3e13a1f2df71fef..bf54f209aaaf235c91499034c4d2363419e1b7f2 100644 (file)
@@ -42,6 +42,7 @@ import org.sonar.core.properties.PropertyQuery;
 import org.sonar.server.component.ComponentTesting;
 import org.sonar.server.component.db.ComponentDao;
 import org.sonar.server.db.DbClient;
+import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.measure.persistence.MeasureDao;
 import org.sonar.server.user.MockUserSession;
 import org.sonar.server.ws.WsTester;
@@ -51,13 +52,12 @@ import java.util.Locale;
 
 import static com.google.common.collect.Lists.newArrayList;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.Assert.fail;
 import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.anyListOf;
 import static org.mockito.Matchers.anyString;
 import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.*;
 
 @RunWith(MockitoJUnitRunner.class)
 public class ComponentAppActionTest {
@@ -91,8 +91,6 @@ public class ComponentAppActionTest {
 
   WsTester tester;
 
-  ComponentDto project;
-
   @Before
   public void setUp() throws Exception {
     DbClient dbClient = mock(DbClient.class);
@@ -101,13 +99,6 @@ public class ComponentAppActionTest {
     when(dbClient.propertiesDao()).thenReturn(propertiesDao);
     when(dbClient.measureDao()).thenReturn(measureDao);
 
-    project = ComponentTesting.newProjectDto()
-      .setId(1L)
-      .setName("SonarQube")
-      .setUuid(PROJECT_UUID)
-      .setLongName("SonarQube")
-      .setKey("org.codehaus.sonar:sonar");
-
     when(measureDao.findByComponentKeyAndMetricKeys(anyString(), anyListOf(String.class), eq(session))).thenReturn(measures);
 
     tester = new WsTester(new ComponentsWs(new ComponentAppAction(dbClient, durations, i18n)));
@@ -116,6 +107,7 @@ public class ComponentAppActionTest {
   @Test
   public void app() throws Exception {
     MockUserSession.set().setLogin("john").addComponentPermission(UserRole.USER, SUB_PROJECT_KEY, COMPONENT_KEY);
+    ComponentDto project = newProject();
 
     ComponentDto file = ComponentTesting.newFileDto(project)
       .setId(10L)
@@ -138,7 +130,8 @@ public class ComponentAppActionTest {
   @Test
   public void app_with_measures() throws Exception {
     MockUserSession.set().addComponentPermission(UserRole.USER, SUB_PROJECT_KEY, COMPONENT_KEY);
-    addComponent();
+    ComponentDto project = newProject();
+    newComponent(project);
 
     addMeasure(CoreMetrics.LINES_KEY, 200);
     addMeasure(CoreMetrics.COVERAGE_KEY, 95.4);
@@ -160,7 +153,8 @@ public class ComponentAppActionTest {
   @Test
   public void app_with_overall_measure() throws Exception {
     MockUserSession.set().addComponentPermission(UserRole.USER, SUB_PROJECT_KEY, COMPONENT_KEY);
-    addComponent();
+    ComponentDto project = newProject();
+    newComponent(project);
 
     addMeasure(CoreMetrics.OVERALL_COVERAGE_KEY, 90.1);
     addMeasure(CoreMetrics.COVERAGE_KEY, 95.4);
@@ -173,7 +167,8 @@ public class ComponentAppActionTest {
   @Test
   public void app_with_ut_measure() throws Exception {
     MockUserSession.set().addComponentPermission(UserRole.USER, SUB_PROJECT_KEY, COMPONENT_KEY);
-    addComponent();
+    ComponentDto project = newProject();
+    newComponent(project);
 
     addMeasure(CoreMetrics.COVERAGE_KEY, 95.4);
     addMeasure(CoreMetrics.IT_COVERAGE_KEY, 85.2);
@@ -185,7 +180,8 @@ public class ComponentAppActionTest {
   @Test
   public void app_with_it_measure() throws Exception {
     MockUserSession.set().addComponentPermission(UserRole.USER, SUB_PROJECT_KEY, COMPONENT_KEY);
-    addComponent();
+    ComponentDto project = newProject();
+    newComponent(project);
 
     addMeasure(CoreMetrics.IT_COVERAGE_KEY, 85.2);
 
@@ -193,7 +189,29 @@ public class ComponentAppActionTest {
     request.execute().assertJson(getClass(), "app_with_it_measure.json");
   }
 
-  private ComponentDto addComponent() {
+  @Test
+  public void fail_on_unknown_component() throws Exception {
+    MockUserSession.set().setLogin("john").addComponentPermission(UserRole.USER, SUB_PROJECT_KEY, COMPONENT_KEY);
+    when(componentDao.getNullableByUuid(session, COMPONENT_UUID)).thenReturn(null);
+
+    try {
+      tester.newGetRequest("api/components", "app").setParam("uuid", COMPONENT_UUID).execute();
+      fail();
+    } catch (Exception e) {
+      assertThat(e).isInstanceOf(NotFoundException.class).hasMessage("Component 'ABCDE' does not exist");
+    }
+  }
+
+  private ComponentDto newProject() {
+    return ComponentTesting.newProjectDto()
+      .setId(1L)
+      .setName("SonarQube")
+      .setUuid(PROJECT_UUID)
+      .setLongName("SonarQube")
+      .setKey("org.codehaus.sonar:sonar");
+  }
+
+  private ComponentDto newComponent(ComponentDto project) {
     ComponentDto file = ComponentTesting.newFileDto(project)
       .setId(10L)
       .setQualifier("FIL")
diff --git a/server/sonar-server/src/test/java/org/sonar/server/exceptions/MessageTest.java b/server/sonar-server/src/test/java/org/sonar/server/exceptions/MessageTest.java
new file mode 100644 (file)
index 0000000..4196a58
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * 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.exceptions;
+
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class MessageTest {
+
+  @Test
+  public void create_message() {
+    Message message = Message.of("key1", "param1");
+    assertThat(message.getKey()).isEqualTo("key1");
+    assertThat(message.getParams()).containsOnly("param1");
+  }
+
+  @Test
+  public void create_message_without_params() {
+    Message message = Message.of("key1");
+    assertThat(message.getKey()).isEqualTo("key1");
+    assertThat(message.getParams()).isEmpty();
+  }
+
+  @Test
+  public void test_equals_and_hashcode() throws Exception {
+    Message message1 = Message.of("key1", "param1");
+    Message message2 = Message.of("key2", "param2");
+    Message sameAsMessage1 = Message.of("key1", "param1");
+
+    assertThat(message1).isEqualTo(message1);
+    assertThat(message1).isNotEqualTo(message2);
+    assertThat(message1).isEqualTo(sameAsMessage1);
+
+    assertThat(message1.hashCode()).isEqualTo(message1.hashCode());
+    assertThat(message1.hashCode()).isNotEqualTo(message2.hashCode());
+    assertThat(message1.hashCode()).isEqualTo(sameAsMessage1.hashCode());
+  }
+
+  @Test
+  public void to_string() throws Exception {
+    assertThat(Message.of("key1", "param1").toString()).isEqualTo("Message{key=key1, params=[param1]}");
+    assertThat(Message.of("key1").toString()).isEqualTo("Message{key=key1, params=[]}");
+  }
+}
index c6bb445317c9e092d5c43c3ebf8d51098d392734..1c3b2727464c6056ad0b6eabb030cd20b2e41a9e 100644 (file)
@@ -132,7 +132,7 @@ public class MeasureFilterFactoryTest {
     Map<String, Object> props = ImmutableMap.<String, Object>of(
       "fromDate", "2012-01-25",
       "toDate", "2012-02-18"
-    );
+      );
     MeasureFilter filter = factory.create(props);
 
     assertThat(DateUtils.formatDate(filter.getFromDate())).isEqualTo("2012-01-25");
@@ -148,7 +148,7 @@ public class MeasureFilterFactoryTest {
     Map<String, Object> props = ImmutableMap.<String, Object>of(
       "ageMaxDays", "3",
       "ageMinDays", "1"
-    );
+      );
     MeasureFilter filter = factory.create(props);
     assertThat(DateUtils.formatDate(filter.getFromDate())).isEqualTo("2013-05-15");
     assertThat(DateUtils.formatDate(filter.getToDate())).isEqualTo("2013-05-17");
@@ -161,7 +161,7 @@ public class MeasureFilterFactoryTest {
       "c1_metric", "complexity",
       "c1_op", "gte",
       "c1_val", "3.14"
-    );
+      );
     MeasureFilter filter = factory.create(props);
 
     List<MeasureFilterCondition> conditions = filter.getMeasureConditions();
@@ -180,7 +180,7 @@ public class MeasureFilterFactoryTest {
       "c1_op", "gte",
       "c1_val", "3.14",
       "c1_period", "3"
-    );
+      );
     MeasureFilter filter = factory.create(props);
 
     List<MeasureFilterCondition> conditions = filter.getMeasureConditions();
@@ -194,10 +194,9 @@ public class MeasureFilterFactoryTest {
   @Test
   public void alert_level_condition() {
     MeasureFilterFactory factory = new MeasureFilterFactory(newMetricFinder(), system);
-    Map<String, Object> props = ImmutableMap.<String, Object>of(
+    MeasureFilter filter = factory.create(ImmutableMap.<String, Object>of(
       "alertLevels", Arrays.asList("error", "warn", "unknown")
-    );
-    MeasureFilter filter = factory.create(props);
+      ));
 
     List<MeasureFilterCondition> conditions = filter.getMeasureConditions();
     assertThat(conditions).hasSize(1);
@@ -208,12 +207,23 @@ public class MeasureFilterFactoryTest {
     assertThat(conditions.get(0).period()).isNull();
   }
 
+  @Test
+  public void alert_level_condition_with_no_alert_status_metric() {
+    MeasureFilterFactory factory = new MeasureFilterFactory(mock(MetricFinder.class), system);
+    MeasureFilter filter = factory.create(ImmutableMap.<String, Object>of(
+      "alertLevels", Arrays.asList("error", "warn", "unknown")
+      ));
+
+    List<MeasureFilterCondition> conditions = filter.getMeasureConditions();
+    assertThat(conditions).isEmpty();
+  }
+
   @Test
   public void name_conditions() {
     MeasureFilterFactory factory = new MeasureFilterFactory(newMetricFinder(), system);
     Map<String, Object> props = ImmutableMap.<String, Object>of(
       "nameSearch", "SonarQube"
-    );
+      );
     MeasureFilter filter = factory.create(props);
 
     assertThat(filter.getResourceName()).isEqualTo("SonarQube");
@@ -223,8 +233,8 @@ public class MeasureFilterFactoryTest {
   public void not_fail_when_name_conditions_contains_array() {
     MeasureFilterFactory factory = new MeasureFilterFactory(newMetricFinder(), system);
     Map<String, Object> props = ImmutableMap.<String, Object>of(
-      "nameSearch", new String[]{"sonar", "qube"}
-    );
+      "nameSearch", new String[] {"sonar", "qube"}
+      );
     MeasureFilter filter = factory.create(props);
 
     assertThat(filter.getResourceName()).isEqualTo("sonar,qube");
@@ -235,7 +245,7 @@ public class MeasureFilterFactoryTest {
     MeasureFilterFactory factory = new MeasureFilterFactory(newMetricFinder(), system);
     Map<String, Object> props = ImmutableMap.<String, Object>of(
       "nameSearch", newArrayList("sonar", "qube")
-    );
+      );
     MeasureFilter filter = factory.create(props);
 
     assertThat(filter.getResourceName()).isEqualTo("sonar,qube");
@@ -247,7 +257,7 @@ public class MeasureFilterFactoryTest {
     Map<String, Object> props = ImmutableMap.<String, Object>of(
       "c1_op", "gte",
       "c1_val", "3.14"
-    );
+      );
     MeasureFilter filter = factory.create(props);
 
     List<MeasureFilterCondition> conditions = filter.getMeasureConditions();
index 8b9bd54d1ef963459f875d0053cb77eebb9a3af1..f58774250c162c2057f73105529740792a43a59d 100644 (file)
@@ -444,7 +444,7 @@ public class UserUpdaterTest {
   }
 
   @Test
-  public void associate_default_groups_when_creating_user() throws Exception {
+  public void associate_default_group_when_creating_user() throws Exception {
     createDefaultGroup();
 
     userUpdater.create(NewUser.create()
@@ -462,7 +462,7 @@ public class UserUpdaterTest {
   }
 
   @Test
-  public void fail_to_associate_default_groups_to_user_if_no_default_group() throws Exception {
+  public void fail_to_associate_default_group_to_user_if_no_default_group() throws Exception {
     settings.setProperty(CoreProperties.CORE_DEFAULT_GROUP, (String) null);
 
     try {
@@ -698,7 +698,7 @@ public class UserUpdaterTest {
   }
 
   @Test
-  public void associate_default_groups_when_updating_user() throws Exception {
+  public void associate_default_group_when_updating_user() throws Exception {
     db.prepareDbUnit(getClass(), "associate_default_groups_when_updating_user.xml");
     createDefaultGroup();
 
@@ -716,6 +716,33 @@ public class UserUpdaterTest {
     assertThat(membership.groups().get(0).isMember()).isTrue();
   }
 
+  @Test
+  public void not_associate_default_group_when_updating_user_if_already_existing() throws Exception {
+    db.prepareDbUnit(getClass(), "not_associate_default_group_when_updating_user_if_already_existing.xml");
+    settings.setProperty(CoreProperties.CORE_DEFAULT_GROUP, "sonar-users");
+    session.commit();
+
+    // User is already associate to the default group
+    GroupMembershipFinder.Membership membership = groupMembershipFinder.find(GroupMembershipQuery.builder().login("marius").groupSearch("sonar-users").build());
+    assertThat(membership.groups()).hasSize(1);
+    assertThat(membership.groups().get(0).name()).isEqualTo("sonar-users");
+    assertThat(membership.groups().get(0).isMember()).isTrue();
+
+    userUpdater.update(UpdateUser.create("marius")
+      .setName("Marius2")
+      .setEmail("marius2@mail.com")
+      .setPassword("password2")
+      .setPasswordConfirmation("password2")
+      .setScmAccounts(newArrayList("ma2")));
+    session.commit();
+
+    // Nothing as changed
+    membership = groupMembershipFinder.find(GroupMembershipQuery.builder().login("marius").groupSearch("sonar-users").build());
+    assertThat(membership.groups()).hasSize(1);
+    assertThat(membership.groups().get(0).name()).isEqualTo("sonar-users");
+    assertThat(membership.groups().get(0).isMember()).isTrue();
+  }
+
   @Test
   public void fail_to_update_unknown_user() throws Exception {
     try {
index d4b47674bdf3f948c265784afe7adbc3c1b1e347..15ee29c168a1ae62cddb3ddf644b9076741378b9 100644 (file)
@@ -5,6 +5,6 @@
 
   <groups id="1" name="sonar-devs" description="Sonar Devs" created_at="2014-09-08" updated_at="2014-09-08"/>
 
-  <groups_users user_id="1" group_id="1"/>
+  <groups_users user_id="101" group_id="1"/>
 
 </dataset>
index d4b47674bdf3f948c265784afe7adbc3c1b1e347..fee1ef143c53e0469aae30e33ae084ea3b34c1be 100644 (file)
@@ -1,10 +1,10 @@
 <dataset>
 
-  <users id="101" login="marius" name="Marius" email="marius@lesbronzes.fr" active="[false]" scm_accounts="ma,marius33" created_at="1418215735482" updated_at="1418215735485"
+  <users id="101" login="marius" name="Marius" email="marius@lesbronzes.fr" active="[true]" scm_accounts="ma,marius33" created_at="1418215735482" updated_at="1418215735485"
          salt="79bd6a8e79fb8c76ac8b121cc7e8e11ad1af8365" crypted_password="650d2261c98361e2f67f90ce5c65a95e7d8ea2fg"/>
 
   <groups id="1" name="sonar-devs" description="Sonar Devs" created_at="2014-09-08" updated_at="2014-09-08"/>
 
-  <groups_users user_id="1" group_id="1"/>
+  <groups_users user_id="101" group_id="1"/>
 
 </dataset>
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/user/UserUpdaterTest/not_associate_default_group_when_updating_user_if_already_existing.xml b/server/sonar-server/src/test/resources/org/sonar/server/user/UserUpdaterTest/not_associate_default_group_when_updating_user_if_already_existing.xml
new file mode 100644 (file)
index 0000000..8bbd3da
--- /dev/null
@@ -0,0 +1,10 @@
+<dataset>
+
+  <users id="101" login="marius" name="Marius" email="marius@lesbronzes.fr" active="[true]" scm_accounts="ma,marius33" created_at="1418215735482" updated_at="1418215735485"
+         salt="79bd6a8e79fb8c76ac8b121cc7e8e11ad1af8365" crypted_password="650d2261c98361e2f67f90ce5c65a95e7d8ea2fg"/>
+
+  <groups id="1" name="sonar-users" description="Sonar Users" created_at="2014-09-08" updated_at="2014-09-08"/>
+
+  <groups_users user_id="101" group_id="1"/>
+
+</dataset>