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 {
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;
}
return key;
}
+ @CheckForNull
public Object[] getParams() {
return params;
}
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;
import org.sonar.server.es.BulkIndexer;
import org.sonar.server.es.EsClient;
-import javax.annotation.CheckForNull;
-
import java.sql.Connection;
import java.util.Iterator;
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())
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;
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 {
WsTester tester;
- ComponentDto project;
-
@Before
public void setUp() throws Exception {
DbClient dbClient = mock(DbClient.class);
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)));
@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)
@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);
@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);
@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);
@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);
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")
--- /dev/null
+/*
+ * 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=[]}");
+ }
+}
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");
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");
"c1_metric", "complexity",
"c1_op", "gte",
"c1_val", "3.14"
- );
+ );
MeasureFilter filter = factory.create(props);
List<MeasureFilterCondition> conditions = filter.getMeasureConditions();
"c1_op", "gte",
"c1_val", "3.14",
"c1_period", "3"
- );
+ );
MeasureFilter filter = factory.create(props);
List<MeasureFilterCondition> conditions = filter.getMeasureConditions();
@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);
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");
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");
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");
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();
}
@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()
}
@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 {
}
@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();
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 {
<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>
<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>
--- /dev/null
+<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>