*/
package org.sonar.core.user;
+import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.user.UserQuery;
+import org.sonar.api.utils.DateUtils;
import org.sonar.core.persistence.AbstractDaoTestCase;
+import org.sonar.core.persistence.DbSession;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
+import java.util.*;
import static org.fest.assertions.Assertions.assertThat;
public class UserDaoTest extends AbstractDaoTestCase {
- private UserDao dao;
+ UserDao dao;
+
+ DbSession session;
@Before
public void setUp() {
+ session = getMyBatis().openSession(false);
dao = new UserDao(getMyBatis());
}
+ @After
+ public void tearDown() throws Exception {
+ session.close();
+ }
+
@Test
public void selectUserByLogin_ignore_inactive() {
setupData("selectActiveUserByLogin");
assertThat(group).isNull();
}
+ @Test
+ public void insert_user() {
+ Date date = DateUtils.parseDate("2014-06-20");
+
+ UserDto userDto = new UserDto().setId(1L).setLogin("john").setName("John").setEmail("jo@hn.com").setCreatedAt(date).setUpdatedAt(date);
+ dao.insert(session, userDto);
+
+ checkTables("insert");
+ }
+
@Test
public void deactivate_user() {
setupData("deactivate_user");
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
+
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
profileActivity.ruleName(ruleDto != null ? ruleDto.getName() : null);
UserDto user = db.userDao().selectActiveUserByLogin(profileActivity.login(), session);
- if (user != null) {
- profileActivity.authorName(user.getName());
- } else {
- profileActivity.authorName(null);
- }
+ profileActivity.authorName(user != null ? user.getName() : null);
results.add(profileActivity);
}
return results;
import org.sonar.core.persistence.DbSession;
import org.sonar.core.qualityprofile.db.ActiveRuleKey;
import org.sonar.core.rule.RuleDto;
+import org.sonar.core.user.UserDto;
import org.sonar.server.activity.ActivityService;
import org.sonar.server.db.DbClient;
import org.sonar.server.qualityprofile.index.ActiveRuleNormalizer;
import org.sonar.server.tester.ServerTester;
import org.sonar.server.user.MockUserSession;
+import java.util.Date;
import java.util.List;
import java.util.Map;
@Test
public void search_qprofile_activity() throws InterruptedException {
- MockUserSession.set().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN).setLogin("me");
+ MockUserSession.set().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN).setLogin("david");
+
+ UserDto user = new UserDto().setLogin("david").setName("David").setEmail("dav@id.com").setCreatedAt(new Date()).setUpdatedAt(new Date());
+ db.userDao().insert(dbSession, user);
// We need an actual rule in DB to test RuleName in Activity
RuleDto rule = db.ruleDao().getByKey(dbSession, RuleTesting.XOO_X1);
assertThat(activity.profileKey()).isEqualTo(XOO_P1_KEY);
assertThat(activity.severity()).isEqualTo(Severity.MAJOR);
assertThat(activity.ruleName()).isEqualTo(rule.getName());
- assertThat(activity.login()).isEqualTo("me");
- assertThat(activity.authorName()).isNull();
+ assertThat(activity.login()).isEqualTo("david");
+ assertThat(activity.authorName()).isEqualTo("David");
assertThat(activity.parameters()).hasSize(1);
assertThat(activity.parameters().get("max")).isEqualTo("10");
assertThat(activity.parameters().get("max")).isEqualTo("10");
}
+ @Test
+ public void search_qprofile_activity_with_user_not_found() throws InterruptedException {
+ MockUserSession.set().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN).setLogin("david");
+
+ // We need an actual rule in DB to test RuleName in Activity
+ db.ruleDao().getByKey(dbSession, RuleTesting.XOO_X1);
+
+ tester.get(ActivityService.class).write(dbSession, Activity.Type.QPROFILE,
+ ActiveRuleChange.createFor(ActiveRuleChange.Type.ACTIVATED, ActiveRuleKey.of(XOO_P1_KEY, RuleTesting.XOO_X1))
+ .setSeverity(Severity.MAJOR)
+ .setParameter("max", "10")
+ );
+ dbSession.commit();
+
+ List<QProfileActivity> activities = service.findActivities(new QProfileActivityQuery(), new QueryOptions());
+ assertThat(activities).hasSize(1);
+
+ QProfileActivity activity = activities.get(0);
+ assertThat(activity.login()).isEqualTo("david");
+ assertThat(activity.authorName()).isNull();
+ }
+
@Test
public void search_qprofile_activity_with_rule_not_found() throws InterruptedException {
MockUserSession.set().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN).setLogin("me");